forked from ImKKingshuk/LockKnife
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockKnife.sh
More file actions
2644 lines (1904 loc) · 87.3 KB
/
Copy pathLockKnife.sh
File metadata and controls
2644 lines (1904 loc) · 87.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
DEFAULT_WORDLIST="/usr/share/dict/words"
DEFAULT_OUTPUT_DIR="$HOME/lockknife_output"
DEFAULT_DEBUG_MODE=false
DEFAULT_MAX_RETRIES=3
DEFAULT_SECURE_DELETE=true
DEFAULT_PARALLEL_JOBS="50%"
DEFAULT_PIN_LENGTH=4
DEFAULT_SNAPSHOT_DIRS="/data/data /data/system /sdcard"
DEFAULT_PCAP_FILTER="port not 5555"
DEBUG_MODE=$DEFAULT_DEBUG_MODE
TEMP_DIR=$(mktemp -d /tmp/lockknife.XXXXXX)
LOG_FILE="$TEMP_DIR/lockknife_log.txt"
MAX_RETRIES=$DEFAULT_MAX_RETRIES
WORDLIST=$DEFAULT_WORDLIST
OUTPUT_DIR=$DEFAULT_OUTPUT_DIR
SECURE_DELETE=$DEFAULT_SECURE_DELETE
PARALLEL_JOBS=$DEFAULT_PARALLEL_JOBS
PIN_LENGTH=$DEFAULT_PIN_LENGTH
SNAPSHOT_DIRS=$DEFAULT_SNAPSHOT_DIRS
PCAP_FILTER=$DEFAULT_PCAP_FILTER
CONFIG_PATHS=(
"./lockknife.conf"
"$HOME/.config/lockknife/lockknife.conf"
"/etc/lockknife.conf"
)
load_config() {
local config_loaded=false
for config_path in "${CONFIG_PATHS[@]}"; do
if [ -f "$config_path" ]; then
log "INFO" "Loading configuration from $config_path"
source "$config_path"
config_loaded=true
break
fi
done
if [ "$config_loaded" = false ]; then
log "DEBUG" "No configuration file found, using defaults"
fi
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
chmod 700 "$OUTPUT_DIR"
log "DEBUG" "Created output directory: $OUTPUT_DIR"
fi
}
mkdir -p "$TEMP_DIR"
chmod 700 "$TEMP_DIR"
log() {
local level="$1"
local message="$2"
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
local log_message="[$timestamp] [$level] $message"
if [ "$DEBUG_MODE" = true ]; then
echo "$log_message" >> "$LOG_FILE"
fi
case "$level" in
"DEBUG")
[ "$DEBUG_MODE" = true ] && echo "$log_message"
;;
*)
echo "$log_message"
;;
esac
}
cleanup() {
if [ -d "$TEMP_DIR" ]; then
log "INFO" "Securely removing temporary files..."
find "$TEMP_DIR" -type f -exec shred -uzn 3 {} \; 2>/dev/null
rm -rf "$TEMP_DIR"
fi
}
trap cleanup EXIT INT TERM
create_default_config() {
local config_path="$1"
if [ -f "$config_path" ]; then
log "WARNING" "Configuration file already exists at $config_path"
read -p "Overwrite existing config? (y/n): " overwrite
if [ "$overwrite" != "y" ]; then
log "INFO" "Keeping existing configuration file"
return 0
fi
fi
log "INFO" "Creating default configuration file at $config_path"
local config_dir=$(dirname "$config_path")
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
fi
cat > "$config_path" << EOF
DEBUG_MODE=$DEFAULT_DEBUG_MODE
MAX_RETRIES=$DEFAULT_MAX_RETRIES
OUTPUT_DIR="$DEFAULT_OUTPUT_DIR"
SECURE_DELETE=$DEFAULT_SECURE_DELETE
WORDLIST="$DEFAULT_WORDLIST"
PARALLEL_JOBS="$DEFAULT_PARALLEL_JOBS"
PIN_LENGTH=$DEFAULT_PIN_LENGTH
SNAPSHOT_DIRS="$DEFAULT_SNAPSHOT_DIRS"
PCAP_FILTER="$DEFAULT_PCAP_FILTER"
EOF
chmod 600 "$config_path"
log "INFO" "Default configuration file created"
return 0
}
execute_with_retry() {
local cmd="$1"
local description="$2"
local retry_count=0
local max_retries=${3:-$MAX_RETRIES}
local result=0
while [ $retry_count -lt $max_retries ]; do
log "DEBUG" "Executing: $cmd"
if [ "$DEBUG_MODE" = true ]; then
eval "$cmd"
else
eval "$cmd" 2>/dev/null
fi
result=$?
if [ $result -eq 0 ]; then
[ $retry_count -gt 0 ] && log "INFO" "$description succeeded after $retry_count retries"
return 0
else
retry_count=$((retry_count + 1))
log "WARNING" "$description failed (attempt $retry_count/$max_retries)"
sleep 2
fi
done
log "ERROR" "$description failed after $max_retries attempts"
return 1
}
parse_arguments() {
for arg in "$@"; do
case "$arg" in
--debug)
DEBUG_MODE=true
log "DEBUG" "Debug mode enabled"
;;
--config=*)
local config_file="${arg#*=}"
if [ -f "$config_file" ]; then
source "$config_file"
log "INFO" "Loaded custom config from $config_file"
else
log "ERROR" "Config file not found: $config_file"
exit 1
fi
;;
--create-config=*)
local config_path="${arg#*=}"
create_default_config "$config_path"
exit 0
;;
--output-dir=*)
OUTPUT_DIR="${arg#*=}"
log "DEBUG" "Output directory set to $OUTPUT_DIR"
;;
--wordlist=*)
WORDLIST="${arg#*=}"
log "DEBUG" "Wordlist set to $WORDLIST"
;;
--help)
show_help
exit 0
;;
esac
done
}
show_help() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --debug Enable debug mode with verbose logging"
echo " --config=FILE Use specific configuration file"
echo " --create-config=FILE Create default configuration file at specified path"
echo " --output-dir=DIR Specify custom output directory"
echo " --wordlist=FILE Specify custom wordlist file"
echo " --help Show the help message"
}
echo "LockKnife : The Ultimate Android Security Research Tool is developed for research and educational purposes. It should be used responsibly and in compliance with all applicable laws and regulations. The developer of this tool is not responsible for any misuse or illegal activities conducted with this tool.
Password recovery tools should only be used for legitimate purposes and with proper authorization. Using such tools without proper authorization is illegal and a violation of privacy. Ensure proper authorization before using LockKnife for password recovery or data extraction. Always adhere to ethical hacking practices and comply with all applicable laws and regulations."
print_banner() {
local banner=(
"****************************************************"
"* LockKnife *"
"* The Ultimate Android Security Research Tool *"
"* v2.0.0 *"
"* -------------------------------------- *"
"* by @ImKKingshuk *"
"* Github - https://github.com/ImKKingshuk *"
"****************************************************"
)
local width=$(tput cols)
for line in "${banner[@]}"; do
printf "%*s\n" $(((${#line} + width) / 2)) "$line"
done
echo
}
check_adb() {
if ! command -v adb &>/dev/null; then
echo "Error: ADB (Android Debug Bridge) not found. Please install ADB and make sure it's in your PATH."
echo "You can download ADB from the Android SDK platform-tools. Follow the instructions for your OS:"
echo "macOS / Linux / Windows: https://developer.android.com/tools/releases/platform-tools"
exit 1
fi
}
check_dependencies() {
local dependencies=("adb" "sqlite3" "curl")
local missing=()
echo "[INFO] Checking required dependencies..."
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &>/dev/null; then
missing+=("$dep")
fi
done
if [ ${#missing[@]} -ne 0 ]; then
echo "[ERROR] Missing dependencies: ${missing[*]}"
echo "Attempting to install missing dependencies..."
if command -v apt &>/dev/null; then
sudo apt update && sudo apt install -y "${missing[@]}"
elif command -v brew &>/dev/null; then
brew install "${missing[@]}"
elif command -v dnf &>/dev/null; then
sudo dnf install -y "${missing[@]}"
else
echo "[ERROR] Unsupported package manager. Install dependencies manually."
exit 1
fi
else
echo "[INFO] All dependencies are installed."
fi
}
check_for_updates() {
local current_version=$(cat version.txt 2>/dev/null || echo "unknown")
local latest_version=$(curl -sSL "https://raw.githubusercontent.com/ImKKingshuk/LockKnife/main/version.txt" 2>/dev/null || echo "$current_version")
if [ "$latest_version" != "$current_version" ]; then
echo "A new version ($latest_version) is available. Updating Tool... Please Wait..."
update_tool
else
echo "You are using the latest version ($current_version)."
fi
}
update_tool() {
local repo_url="https://raw.githubusercontent.com/ImKKingshuk/LockKnife/main"
local tmp_script="LockKnife_tmp.sh"
local tmp_version="version_tmp.txt"
curl -sSL "$repo_url/LockKnife.sh" -o "$tmp_script"
curl -sSL "$repo_url/version.txt" -o "$tmp_version"
if [[ -s "$tmp_script" && -s "$tmp_version" ]]; then
mv "$tmp_script" LockKnife.sh
mv "$tmp_version" version.txt
echo "[INFO] Tool has been updated to the latest version."
exec bash LockKnife.sh
else
echo "[ERROR] Update failed. Retaining current version."
rm -f "$tmp_script" "$tmp_version"
fi
}
connect_device() {
local device_serial="$1"
log "INFO" "Attempting to connect to device: $device_serial"
if ! execute_with_retry "adb connect $device_serial" "Device connection"; then
log "ERROR" "Failed to connect to the device with serial number: $device_serial."
log "ERROR" "Ensure the device is reachable and ADB debugging is enabled."
exit 1
else
log "INFO" "Successfully connected to device: $device_serial"
fi
}
generate_gesture_patterns() {
local output_file="$1"
local temp_file="$TEMP_DIR/gesture_patterns.txt"
log "INFO" "Generating common gesture patterns and their hashes..."
cat > "$temp_file" << EOF
0,1,2,5,8,7,6,3,4:L pattern
0,1,2,5,8:L shape
0,3,6,7,8:reverse L
0,4,8:diagonal
2,4,6:diagonal
0,1,2,4,6,7,8:U shape
6,7,8,5,2,1,0:reverse U
0,3,6,7,4,1,2:N shape
0,3,6,7,8,5,2:Z shape
0,1,2,5,8,7,6:C shape
2,5,8,7,6,3,0:reverse C
0,1,2,4,7,6,3:S shape
2,1,0,3,6,7,8:mirror S
0,1,2,3,4,5,6,7,8:full square
0,1,2,3,4,5,6:G shape
0,3,6,4,2,5,8:N shape
0,3,4,5,8:check mark
0,3,4,1,2:r shape
6,3,0,1,4,7,8:question mark
0,3,6,4,2:lightning bolt
EOF
log "DEBUG" "Creating gesture pattern hash table: $output_file"
echo "# Gesture pattern hash table (SHA-1)" > "$output_file"
echo "# Format: hash:pattern:description" >> "$output_file"
while IFS=: read -r pattern description; do
local binary=""
local prev_node=""
IFS=',' read -ra NODES <<< "$pattern"
for node in "${NODES[@]}"; do
if [ -n "$prev_node" ]; then
binary+=$(printf "\\$(printf '%03o' $((prev_node * 16 + node)))")
fi
prev_node=$node
done
local hash=$(echo -n "$binary" | sha1sum | awk '{print $1}')
echo "$hash:$pattern:$description" >> "$output_file"
done < "$temp_file"
secure_delete_file "$temp_file"
log "INFO" "Generated $(wc -l < "$output_file") gesture patterns in $output_file"
}
map_gesture_hash() {
local hash_file="$1"
local patterns_file="$OUTPUT_DIR/gesture_patterns.txt"
if [ ! -f "$patterns_file" ]; then
generate_gesture_patterns "$patterns_file"
fi
local file_hash=$(sha1sum "$hash_file" | awk '{print $1}')
log "DEBUG" "Gesture file hash: $file_hash"
local match=$(grep "^$file_hash:" "$patterns_file" 2>/dev/null)
if [ -n "$match" ]; then
local pattern=$(echo "$match" | cut -d: -f2)
local description=$(echo "$match" | cut -d: -f3)
log "SUCCESS" "Gesture pattern found: $description (nodes: $pattern)"
create_gesture_visualization "$pattern" "$OUTPUT_DIR/gesture_visualization.txt"
return 0
else
log "INFO" "No matching pattern found in the lookup table"
log "INFO" "Consider adding this pattern to the database"
return 1
fi
}
create_gesture_visualization() {
local pattern="$1"
local output_file="$2"
log "DEBUG" "Creating visual representation of the pattern"
cat > "$output_file" << EOF
┌───┬───┬───┐
│ │ │ │
├───┼───┼───┤
│ │ │ │
├───┼───┼───┤
│ │ │ │
└───┴───┴───┘
EOF
local grid=(7 8 9 4 5 6 1 2 3)
local nodes=()
IFS=',' read -ra nodes <<< "$pattern"
for node in "${nodes[@]}"; do
local visual_node=$((node + 1))
case $node in
0) sed -i '2s/ /[1]/' "$output_file" ;;
1) sed -i '2s/ / [2] /2' "$output_file" ;;
2) sed -i '2s/ /[3]/' "$output_file" ;;
3) sed -i '4s/ /[4]/' "$output_file" ;;
4) sed -i '4s/ / [5] /2' "$output_file" ;;
5) sed -i '4s/ /[6]/' "$output_file" ;;
6) sed -i '6s/ /[7]/' "$output_file" ;;
7) sed -i '6s/ / [8] /2' "$output_file" ;;
8) sed -i '6s/ /[9]/' "$output_file" ;;
esac
done
log "INFO" "Gesture visualization saved to $output_file"
}
recover_password() {
local file_path="$1"
local file_type="${2:-unknown}"
local password=""
if [[ ! -f "$file_path" ]]; then
log "ERROR" "File $file_path not found or is not accessible. Exiting."
return 1
fi
log "INFO" "Attempting to decrypt password from file: $file_path"
if [ "$file_type" = "gesture" ]; then
map_gesture_hash "$file_path"
fi
while IFS= read -r -n1 byte; do
if [[ -z "$byte" ]]; then
log "WARNING" "Encountered invalid byte in file. Skipping."
continue
fi
byte_value=$(printf "%d" "'$byte")
decrypted_byte=$((byte_value ^ 0x6A))
password+=$(printf "\\$(printf '%03o' "$decrypted_byte")")
done < "$file_path"
log "INFO" "Recovered password: $password"
secure_delete_file "$file_path"
return 0
}
recover_locksettings_db() {
local db_file="$TEMP_DIR/locksettings.db"
local device_serial="$1"
log "INFO" "Attempting to pull locksettings database..."
execute_with_retry "adb -s $device_serial shell 'su -c \"chmod 644 /data/system/locksettings.db\"'" "Setting permissions" || true
if ! execute_with_retry "adb -s $device_serial pull /data/system/locksettings.db $db_file" "Database transfer"; then
log "ERROR" "Unable to pull locksettings.db. Ensure root permissions are granted."
return 1
fi
if [[ ! -f "$db_file" ]]; then
log "ERROR" "Failed to pull locksettings.db. Check device permissions."
return 1
fi
log "INFO" "Locksettings database file pulled successfully. Analyzing..."
sqlite3 "$db_file" "SELECT name, value FROM locksettings WHERE name LIKE 'lockscreen%' OR name LIKE 'pattern%' OR name LIKE 'password%';" | while read -r row; do
log "INFO" "Recovered setting: $row"
done
secure_delete_file "$db_file"
return 0
}
recover_wifi_passwords() {
local wifi_file="/data/misc/wifi/WifiConfigStore.xml"
local local_wifi_file="$TEMP_DIR/WifiConfigStore.xml"
local device_serial="$1"
log "INFO" "Checking for Wi-Fi configuration file on device..."
if ! execute_with_retry "adb -s $device_serial shell 'test -f $wifi_file && echo exists'" "WiFi config check" | grep -q "exists"; then
log "ERROR" "Wi-Fi configuration file not found on device. Exiting."
return 1
fi
execute_with_retry "adb -s $device_serial shell 'su -c \"chmod 644 $wifi_file\"'" "Setting permissions" || true
if ! execute_with_retry "adb -s $device_serial pull $wifi_file $local_wifi_file" "WiFi config transfer"; then
log "ERROR" "Failed to pull Wi-Fi configuration file. Check device permissions."
return 1
fi
if [[ ! -f "$local_wifi_file" ]]; then
log "ERROR" "Failed to pull Wi-Fi configuration file. Check device permissions."
return 1
fi
log "INFO" "Wi-Fi configuration file pulled successfully. Analyzing..."
grep -oP '(?<=<string name="PreSharedKey">).+?(?=</string>)' "$local_wifi_file" | while read -r line; do
log "INFO" "Recovered Wi-Fi password: $line"
done
secure_delete_file "$local_wifi_file"
return 0
}
dictionary_attack() {
local lock_file="$1"
local wordlist
read -p "Enter the full path to your wordlist file: " wordlist
if [[ ! -f "$wordlist" ]]; then
log "ERROR" "The file '$wordlist' does not exist. Please provide a valid wordlist file."
return 1
fi
if [[ ! -f "$lock_file" ]]; then
log "ERROR" "Lock file '$lock_file' not found. Exiting."
return 1
fi
local total_words=$(wc -l < "$wordlist")
log "INFO" "Starting dictionary attack using '$wordlist' with $total_words words..."
if command -v parallel &>/dev/null; then
log "INFO" "Using parallel processing for dictionary attack"
local success_file="$TEMP_DIR/dict_success"
local result_file="$TEMP_DIR/dict_result"
parallel_dict_attack() {
local word="$1"
local hash=$(echo -n "$word" | sha1sum | awk '{print $1}')
if grep -q "$hash" "$lock_file"; then
echo "$word" > "$success_file"
return 0
fi
return 1
}
export -f parallel_dict_attack
export lock_file
export success_file
cat "$wordlist" | parallel --progress --eta --jobs 50% "parallel_dict_attack {}"
if [[ -f "$success_file" ]]; then
local found_password=$(cat "$success_file")
log "SUCCESS" "Password found: $found_password"
return 0
else
log "INFO" "Dictionary attack failed. No matching password found."
return 1
fi
else
log "INFO" "Parallel not found, using single-threaded attack with progress tracking"
local count=0
while IFS= read -r word; do
((count++))
if [ $((count % 100)) -eq 0 ]; then
local percentage=$((count * 100 / total_words))
printf "\rProgress: %d/%d (%d%%)" "$count" "$total_words" "$percentage"
fi
local hash=$(echo -n "$word" | sha1sum | awk '{print $1}')
if grep -q "$hash" "$lock_file"; then
printf "\n"
log "SUCCESS" "Password found: $word"
return 0
fi
done < "$wordlist"
printf "\n"
log "INFO" "Dictionary attack failed. No matching password found."
return 1
fi
}
brute_force_attack() {
local lock_file="$1"
local pin_length="$2"
if ! [[ "$pin_length" =~ ^[0-9]+$ ]] || [ "$pin_length" -lt 4 ]; then
log "ERROR" "Invalid PIN length. Use a number >= 4."
return 1
fi
local total=$((10 ** pin_length))
if [ "$pin_length" -gt 6 ]; then
log "WARNING" "Brute-forcing PINs longer than 6 digits may take significant time."
read -p "Continue? (y/n): " choice
[ "$choice" != "y" ] && return 1
fi
if [ -f "pin_hashes.txt" ] && [ "$pin_length" -le 6 ]; then
log "INFO" "Using precomputed PIN hashes for faster attack"
log "INFO" "Searching for matches in precomputed hash table..."
local target_hash=$(cat "$lock_file")
grep -q "$target_hash" "pin_hashes.txt" && {
local found_pin=$(grep "$target_hash" "pin_hashes.txt" | cut -d: -f1)
log "SUCCESS" "PIN found: $found_pin"
return 0
}
log "INFO" "PIN not found in precomputed hash table. Falling back to brute force."
fi
if command -v parallel &>/dev/null; then
log "INFO" "Using parallel processing for brute force attack"
local cores=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
local chunk_size=$((total / cores))
[ $chunk_size -lt 1000 ] && chunk_size=1000
local success_file="$TEMP_DIR/pin_success"
parallel_pin_attack() {
local start="$1"
local end="$2"
local length="$3"
local file="$4"
for i in $(seq "$start" "$end"); do
local pin=$(printf "%0${length}d" "$i")
local hash=$(echo -n "$pin" | sha1sum | awk '{print $1}')
if grep -q "$hash" "$file"; then
echo "$pin" > "$success_file"
return 0
fi
done
return 1
}
export -f parallel_pin_attack
export lock_file success_file
local job_list="$TEMP_DIR/job_list.txt"
local start=0
while [ $start -lt $total ]; do
local end=$((start + chunk_size - 1))
[ $end -ge $total ] && end=$((total - 1))
echo "$start $end $pin_length $lock_file" >> "$job_list"
start=$((end + 1))
done
log "INFO" "Starting parallel brute-force attack for $pin_length-digit PINs using $cores cores..."
cat "$job_list" | parallel --progress --eta "parallel_pin_attack {1} {2} {3} {4}"
if [[ -f "$success_file" ]]; then
local found_pin=$(cat "$success_file")
log "SUCCESS" "PIN found: $found_pin"
return 0
else
log "INFO" "Brute-force attack failed. No matching PIN found."
return 1
fi
else
log "INFO" "Starting brute-force attack for $pin_length-digit PINs..."
local count=0
for i in $(seq 0 $((total - 1))); do
local pin=$(printf "%0${pin_length}d" "$i")
((count++))
if [ $((count % 1000)) -eq 0 ]; then
local percentage=$(echo "scale=1; $count*100/$total" | bc)
printf "\rProgress: %d/%d (%.1f%%)" "$count" "$total" "$percentage"
fi
local hash=$(echo -n "$pin" | sha1sum | awk '{print $1}')
if grep -q "$hash" "$lock_file"; then
printf "\n"
log "SUCCESS" "PIN found: $pin"
return 0
fi
done
printf "\n"
log "INFO" "Brute-force attack failed."
return 1
fi
}
check_security() {
local device_serial="$1"
local version=$(adb -s "$device_serial" shell getprop ro.build.version.release)
local patch=$(adb -s "$device_serial" shell getprop ro.build.version.security_patch)
local rooted=$(adb -s "$device_serial" shell "su -c 'id'" | grep -q "uid=0" && echo "Yes" || echo "No")
echo "[INFO] Android Version: $version"
echo "[INFO] Security Patch: $patch"
echo "[INFO] Rooted: $rooted"
}
frp_bypass() {
echo "[WARNING] FRP bypass is a sensitive operation and should only be performed on devices you own or have explicit permission to test."
echo "[INFO] This feature is not implemented in this version of LockKnife."
}
select_device() {
local devices=()
local usb_devices=($(adb devices | grep -w device | awk '{print $1}'))
local tcp_devices=($(adb devices | grep -w "device.*:5555" | awk '{print $1}'))
devices=("${usb_devices[@]}" "${tcp_devices[@]}")
if [ ${#devices[@]} -eq 0 ]; then
log "WARNING" "No devices found connected via USB."
read -p "Would you like to connect to a device via IP? (y/n): " connect_choice
if [ "$connect_choice" = "y" ]; then
read -p "Enter the IP address of the device: " device_ip
if [[ ! "$device_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
log "ERROR" "Invalid IP address format. Please enter a valid IP address."
return 1
fi
log "INFO" "Attempting to connect to device at $device_ip:5555..."
if execute_with_retry "adb connect $device_ip:5555" "Device connection"; then
tcp_devices=($(adb devices | grep -w connected | awk '{print $1}'))
devices=("${usb_devices[@]}" "${tcp_devices[@]}")
else
log "ERROR" "Failed to connect to device at $device_ip:5555. Please check the IP and make sure ADB debugging is enabled."
return 1
fi
else
log "ERROR" "No devices found. Please connect a device and ensure ADB debugging is enabled."
exit 1
fi
fi
if [ ${#devices[@]} -eq 0 ]; then
log "ERROR" "No devices found. Please connect a device and ensure ADB debugging is enabled."
exit 1
fi
if [ ${#devices[@]} -eq 1 ]; then
log "INFO" "Using device: ${devices[0]}"
echo "${devices[0]}"
return 0
fi
log "INFO" "Multiple devices found. Please select one:"
for i in "${!devices[@]}"; do
local device_info=$(adb -s "${devices[$i]}" shell getprop ro.product.model 2>/dev/null || echo "Unknown")
echo "$((i+1)). ${devices[$i]} ($device_info)"
done
local valid_selection=false
local num
while [ "$valid_selection" = false ]; do
read -p "Device number (1-${#devices[@]}): " num
if [[ "$num" =~ ^[0-9]+$ && "$num" -ge 1 && "$num" -le ${#devices[@]} ]]; then
valid_selection=true
else
log "ERROR" "Invalid selection. Please enter a number between 1 and ${#devices[@]}."
fi
done
echo "${devices[$((num-1))]}"
return 0
}
secure_pull_file() {
local device_serial="$1"
local remote_path="$2"
local local_path="$TEMP_DIR/$(basename "$remote_path")"
log "DEBUG" "Attempting to pull $remote_path to $local_path"
if ! execute_with_retry "adb -s $device_serial shell '[ -f $remote_path ] && echo exists'" "File existence check" | grep -q "exists"; then
log "ERROR" "File $remote_path does not exist on device."
return 1
fi
execute_with_retry "adb -s $device_serial shell 'su -c \"chmod 644 $remote_path\"' 2>/dev/null" "File permission change" || true
if ! execute_with_retry "adb -s $device_serial pull $remote_path $local_path" "File transfer"; then
log "ERROR" "Failed to pull file $remote_path from device."
return 1
fi
chmod 600 "$local_path"
echo "$local_path"
return 0
}
secure_delete_file() {
local file_path="$1"
if [ ! -f "$file_path" ]; then
log "DEBUG" "File $file_path does not exist, nothing to delete."
return 0
fi
log "DEBUG" "Securely deleting file: $file_path"
if command -v shred &>/dev/null; then
shred -uzn 3 "$file_path"
else
dd if=/dev/urandom of="$file_path" bs=1k count=1 conv=notrunc &>/dev/null
rm -f "$file_path"
fi
return 0
}
main_menu() {
local device_serial="$1"
echo "LockKnife - Security Research Tool"
echo "1. Password Recovery"
echo "2. Data Extraction"
echo "3. Live Analysis"
echo "4. Security Assessment"
echo "5. Custom Data Extraction"
echo "6. Forensic Analysis"
echo "7. Network Traffic Analysis"
read -p "Choice: " choice
case $choice in
1) submenu_password_recovery "$device_serial" ;;
2) submenu_data_extraction "$device_serial" ;;
3) live_analysis "$device_serial" ;;
4) check_security "$device_serial" ;;
5) custom_data_extraction "$device_serial" ;;
6) submenu_forensic_analysis "$device_serial" ;;
7) submenu_network_analysis "$device_serial" ;;
*) echo "[ERROR] Invalid choice." ;;
esac
}
submenu_password_recovery() {
local device_serial="$1"
log "INFO" "Password Recovery Options:"
echo "1. Gesture Lock"
echo "2. Password Lock"
echo "3. Wi-Fi Passwords"
echo "4. Locksettings DB"
echo "5. Variable-Length PIN Cracking"
echo "6. Alphanumeric Password Cracking"
echo "7. Gatekeeper HAL Analysis"
echo "8. Monitor Gatekeeper Responses"
read -p "Choice: " choice
case $choice in
1)
local gesture_file="$TEMP_DIR/gesture.key"
if secure_pull_file "$device_serial" "/data/system/gesture.key" > /dev/null; then
recover_password "$gesture_file" gesture
fi
;;
2)
local password_file="$TEMP_DIR/password.key"
if secure_pull_file "$device_serial" "/data/system/password.key" > /dev/null; then
recover_password "$password_file"
fi
;;
3) recover_wifi_passwords "$device_serial" ;;
4) recover_locksettings_db "$device_serial" ;;
5) read -p "Enter PIN length (e.g., 4, 6, 8): " pin_length
read -p "Enter the path to the lock file: " lock_file
brute_force_attack "$lock_file" "$pin_length" ;;
6) read -p "Enter the path to the lock file: " lock_file
dictionary_attack "$lock_file" ;;
7) analyze_gatekeeper "$device_serial" ;;
8) read -p "Enter monitoring duration in seconds: " duration
monitor_gatekeeper_responses "$device_serial" "$duration" ;;