-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidExtract.py
2458 lines (2281 loc) · 115 KB
/
AndroidExtract.py
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
#!/usr/bin/python3
import os
import os.path
import sys
import subprocess
import argparse
import glob
from subprocess import call
from os.path import expanduser
#from fileinput import filename
##############################################################################################
##############################################################################################
#
# Made by Sam Simon with the generous help of FICS
# Submitted as senior project under Dr. Kevin Butler
# Spring 2020
#
# Original tool produced in bash by FICS,
# to check out their amazing tool; please visit link below:
#
# https://github.com/FICS/atcmd/tree/master/extract
#
# For more information and help using this tool, please type "AndroidExtract.py --help"
#
##############################################################################################
##############################################################################################
#
# NOTE: REQUIRED TOOLS:
# unzip, unrar, 7z, simg2img, mount, dex2jar, xxd, strings, jd-gui, jd-cli
# baksmali, smali, jadx, unkdz, undz, updata, unsparse,
# sdat2img, flashtool, sonyelf, imgtool, htcruudec, splitqsb, leszb, unyaffs
#
# NOTE: bingrep is limited and not recommended!
# NOTE: Script does not need to be run as root, but the mounting and unmounting
# of filesystem images will require sudo privileges
#
# two-column format: 1st column = filename of file containing command,
# : 2nd column = AT command.
#
# Ported Python3 code from bash
#
##############################################################################################
##############################################################################################
##########################
# Global Variables #
# ---------------------- #
# Some of these #
# values are set in main #
##########################
IMAGE = ""
VENDOR = ""
KEEPSTUFF = 0 # keep all the decompiled/unpackaged stuff for later analysis
VENDORMODE = 0 # should be provided as 0 unless alternate mode
HOME = str(expanduser("~"))
EXTUSER = "someuser" # TODO: replace with valid user to use keepstuff functionality
EXTGROUP = "somegroup" # TODO: replace with valid group to use keepstuff functionality
MY_TMP = "extract.sum"
MY_OUT = "extract.db"
MY_USB = "extract.usb"
MY_PROP = "extract.prop"
#MY_TIZ="extract.tizen" # used to mark presence of tizen image(s), replaced by TIZ_LOG
TIZ_LOG = "tizen.log" # samsung
PAC_LOG = "spd_pac.log" # lenovo
SBF_LOG = "sbf.log" # moto
MZF_LOG = "mzf.log" # moto
RAW_LOG = "raw.log" # asus
KDZ_LOG = "kdz.log" # lg
MY_DIR = "extract/" + VENDOR
MY_FULL_DIR = "/data/atdb/extract/" + VENDOR
TOP_DIR = "extract"
AT_CMD = 'AT\+|AT\*'
AT_CMD = 'AT\+|AT\*|AT!|AT@|AT#|AT\$|AT%|AT\^|AT&' # expanding target AT Command symbols
DIR_TMP = ""
MNT_TMP = ""
APK_TMP = ""
ZIP_TMP = ""
ODEX_TMP = ""
TAR_TMP = ""
MSC_TMP = ""
JAR_TMP = "dex.jar"
##############################################################################################
DEPPATH=""
USINGDEPPATH=0 # 1 = true, 0 = false
DEX2JAR=str(DEPPATH)+"/dex2jar/dex-tools/target/dex2jar-2.1-SNAPSHOT/d2j-dex2jar.sh"
JDCLI=str(DEPPATH)+"/jd-cmd/jd-cli/target/jd-cli.jar"
# These are the most recent versions of baksmali/smali that work with java 7 (needed for JADX-nohang)
BAKSMALI=str(DEPPATH)+"/baksmali-2.2b4.jar"
SMALI=str(DEPPATH)+"/smali-2.2b4.jar"
JADX=str(DEPPATH)+"/jadx/build/jadx/bin/jadx"
# ~~~The following tools needed to unpack LG images: avail https://github.com/ehem/kdztools~~~
UNKDZ=str(DEPPATH)+"/kdztools/unkdz"
UNDZ=str(DEPPATH)+"/kdztools/undz"
UPDATA=str(DEPPATH)+"/split_updata.pl/splitupdate"
UNSPARSE=str(DEPPATH)+"combine_unsparse.sh"
SDAT2IMG=str(DEPPATH)+"sdat2img/sdat2img.py"
SONYFLASH=str(DEPPATH)+"flashtool/FlashToolConsole"
SONYELF=str(DEPPATH)+"unpackelf/unpackelf"
IMGTOOL=str(DEPPATH)+"imgtool/imgtool.ELF64"
HTCRUUDEC=str(DEPPATH)+"htcruu-decrypt3.6.5/RUU_Decrypt_Tool" # rename libcurl.so to libcurl.so.4
SPLITQSB=str(DEPPATH)+"split_qsb.pl"
LESZB=str(DEPPATH)+"szbtool/leszb" # szb format1 for lenovo
UNYAFFS=str(DEPPATH)+"unyaffs/unyaffs" # yaffs2 format1 for sony
##############################################################################################
BOOT_OAT = ""
BOOT_OAT_64 = ""
AT_RES = ""
SUB_SUB_TMP = "extract_sub"
SUB_DIR = ""
CHUNKED = 0 # system.img
CHUNKEDO = 0 # oem.img
CHUNKEDU = 0 # userdata.img
COMBINED0 = 0 # system; may be a more elegant solution than this~
COMBINED1 = 0 # userdata
COMBINED2 = 0 # cache
COMBINED3 = 0 # factory or fac
COMBINED4 = 0 # preload
COMBINED5 = 0 # without_carrier_userdata
TARNESTED = 0
#########################
# Argument Parser #
#########################
def parse_arguments():
parser = argparse.ArgumentParser(description = 'Android image extraction tool. Type \'Android Extract -h\' for more information')
parser.add_argument('-f', dest='filepath', metavar='FIRMWARE IMG FILEPATH', type=str,
help = 'Path to the top-level packaged archive')
parser.add_argument('-vendor', dest='vendor', metavar='VENDOR NAME', type=str,
help = 'The vendor who produced the firmware image (e.g., Samsung, LG)')
parser.add_argument('-i', dest='index', metavar='INDEX', type=int,
help = 'To extract multiple images at the same time, temporary directories will need different indices. For best results, supply an integer value > 0')
parser.add_argument('-ks', dest='keepstuff', metavar='KEEP STUFF? [0 OR 1]',type=int,
help = 'if 0, will remove any extracted files after Processing them;\nif 1, extracted files (e.g., filesystem contents, apps) will be kept')
parser.add_argument('--vendor-mode', dest='vendormode', metavar='VENDOR MODE [0 OR 1]', type=int,
help = 'Supplying 1 as this optional argument will invoke an adjusted extraction')
return parser.parse_args()
##############################################################################################
#########################
# Help Menu #
#########################
def print_how_to(): #Help menu
print("This program must be run with AT LEAST the first 4 of the following options, 5th option is not mandatory:")
print("-f <FILEPATH> : to define package filepath")
print("-vendor <VENDOR NAME> : to define the vendor")
print("-i <INDEX> : to declare index number of directory")
print("-ks <KEEP STUFF? [0 OR 1]> : to declare whether to remove extracted files after Processing")
print("--vendor-mode <VENDOR MODE [0 OR 1]> : to configure specific vendor related settings")
fo2 = open("2", "wt")
print("ERROR: not enough arguments provided.",file=fo2)
print("USAGE: ./atextract.sh <firmware image file> <vendor> <index> <keepstuff flag> <vendor mode (optional)>",file=fo2)
print(" firmware image file = path to the top-level packaged archive (zip, rar, 7z, kdz, etc.)",file=fo2)
print(" (may be absolute or relative path)",file=fo2)
print(" vendor = the vendor who produced the firmware image (e.g., Samsung, LG)",file=fo2)
print(" currently supported = samsung, lg, lenovo, zte, huawei, motorola, asus, aosp,",file=fo2)
print(" nextbit, alcatel, blu, vivo, xiaomi, oneplus, oppo,",file=fo2)
print(" lineage, htc, sony",file=fo2)
print(" index = to extract multiple images at the same time, temporary directories will",file=fo2)
print(" need different indices. For best results, supply an integer value > 0.",file=fo2)
print(" keepstuff = 0/1",file=fo2)
print(" if 0, will remove any extracted files after processing them",file=fo2)
print(" if 1, extracted files (e.g., filesystem contents, apps) will be kept",file=fo2)
print(" (useful for later manual inspection)",file=fo2)
print(" vendor mode = some vendors will have several different image packagings",file=fo2)
print(" if so, supplying 1 as this optional argument will invoke an adjusted extraction",file=fo2)
print(" currently applies to:",file=fo2)
print(" password protected Samsung (.zip) image files from firmwarefile.com",file=fo2)
print(" extend as needed",file=fo2)
print("", file=fo2)
print("For additional guidance and a full list of dependencies, please refer to the provided README.",file=fo2)
fo2.close()
#####################################################################################################################
#####################################################################################################################
#########################
# HELPER METHODS #
#########################
def clean_up():
subprocess.run(["sudo", "umount", MNT_TMP, ">", "/dev/null"], shell=True)
subprocess.run(['rm', '-rf', DIR_TMP, '>', '/dev/null'], shell=True)
subprocess.run(['rm', '-rf', APK_TMP , '>', '/dev/null'], shell=True)
subprocess.run(['rm', '-rf', ZIP_TMP , '>', '/dev/null'], shell=True)
subprocess.run(['rm', '-rf', ODEX_TMP , '>', '/dev/null'], shell=True)
subprocess.run(['rm', '-rf', TAR_TMP , '>', '/dev/null'], shell=True)
subprocess.run(['rm', '-rf', MSC_TMP , '>', '/dev/null'], shell=True)
# Decompress the zip-like file
# Return 'True' if the decompression is successful
# Otherwise 'False'
# NOTE: to support more decompressing methods, please add them here:
def at_unzip(filename, filename2, directory):
# filename = "$1"
# directory = "$2"
# format = 'file -b "$filename" | cut -d" " -f1'
image_vendor = VENDOR
format3 = filename[-3:] # 3 character file extensions (i.e. .cpp)
format4 = filename[-4:] # 4 character file extensions (i.e. .java)
format5 = filename[-5:] # 5 character file extensions (i.e. .7-zip)
format6 = filename[-6:] # 6 character file extensions (i.e. .6chars)
format7 = filename[-7:] # 7 character file extensions (i.e. .7-chars)
if (filename2 is not None):
format2_3 = filename2[-3:] # 3 character file extensions (i.e. .cpp)
format2_4 = filename2[-4:] # 4 character file extensions (i.e. .java)
format2_5 = filename2[-5:] # 5 character file extensions (i.e. .7-zip)
format2_6 = filename2[-6:] # 6 character file extensions (i.e. .6chars)
format2_7 = filename2[-7:] # 7 character file extensions (i.e. .7-chars)
if (format3 == "zip" ) or (format3 == "ZIP" ) or ( format3 == "Zip" ):
if directory is None:
subprocess.run(['unzip', filename], shell=True)
else:
subprocess.run(['unzip', '-d', directory, filename], shell=True)
AT_RES = "good"
return True
elif (format4 == "Java"):
# mischaracterization of zip file as Java archive data for HTC
# or it is actually a JAR, but unzip works to extract contents
if directory is None:
subprocess.run(['unzip', filename], shell=True)
else:
subprocess.run(['unzip', '-d', directory, filename], shell=True)
AT_RES = "good"
return True
elif (format5 == "POSIX" and format2_3 == "tar"):
if directory is None:
subprocess.run(['tar', 'xvf', filename], shell=True)
else:
subprocess.run(['tar','xvf', filename, '-C', directory], shell=True)
AT_RES = "good"
return True
elif (format4 == "PE32" and image_vendor == "htc" ):
subprocess.run([HTCRUUDEC, '-sf', filename], shell=True)
decoutput = 'ls | grep \"OUT\"'
os.rmdir(directory)
subprocess.run(['mv', decoutput, directory], shell=True)
AT_RES = "good"
return True
elif (format3 == "RAR"):
if directory is None:
subprocess.run(['unrar','x', filename], shell=True)
if (image_vendor == "samsung"):
subprocess.run(['tar', 'xvf', 'basename', filename, ".md5"], shell=True)
else:
backfromrar = subprocess.run('pwd', shell=True)
subprocess.run(['cp', filename, directory], shell=True)
os.chdir(directory)
subprocess.run(['unrar','x', filename], shell=True)
if (image_vendor == "samsung"):
subprocess.run(['tar', 'xvf', 'basename', filename, ".md5"], shell=True)
os.remove(filename)
os.chdir(backfromrar)
AT_RES = "good"
return True
elif (format4 == "gzip"):
# gunzip is difficult to redirect
if directory is None:
subprocess.run(['gunzip', filename], shell=True)
else:
backfromgz = subprocess.run('pwd', shell=True)
subprocess.run(['cp', filename, directory], shell=True)
subprocess.run(['cd', directory], shell=True)
subprocess.run(['gunzip', filename], shell=True)
os.chdir(backfromgz)
os.remove(filename)
AT_RES = "good"
return True
elif (image_vendor == "motorola" and format7 == ".tar.gz"):
subprocess.run(['gunzip', filename], shell=True)
subprocess.run(['tar', 'xvf', 'basename', filename, ".gz"], shell=True)
AT_RES = "good"
return True
elif (image_vendor == "motorola" and format7 == ".tar.gz"):
backfromgz = subprocess.run('pwd', shell=True)
subprocess.run(['cp', filename, directory], shell=True)
subprocess.run(['cd', directory], shell=True)
subprocess.run(['gunzip', filename], shell=True)
subprocess.run(['tar', 'xvf', 'basename', filename, ".gz"], shell=True)
os.chdir(backfromgz)
AT_RES = "good"
return True
elif (format5 == "7-zip"):
if directory is None:
subprocess.run(['7z', 'x', filename], shell=True)
else:
subprocess.run(['unrar', 'x', '-o', directory, filename], shell=True)
AT_RES = "good"
return True
else:
AT_RES = "bad"
return False
# We are in sub_sub_dir
def handle_text(filename):
# grep $AT_CMD $1 >> ../$MY_TMP
subprocess.run(['grep', '-E', AT_CMD, '\"', filename, '\"', ' | ', 'awk', '-v', 'fname=\"', filename, '\" BEGIN {OFS=\"\t\"} {print fname,$0} >> ', MY_TMP], shell=True) #mod-filenameprint
def handle_binary(filename):
# strings -a $1 | grep $AT_CMD >> ../$MY_TMP
subprocess.run(['grep', '-E', AT_CMD, '\"', filename, '\"', ' | ', 'awk', '-v', 'fname=\"', filename, '\" BEGIN {OFS=\"\t\"} {print fname,$0} >> ', MY_TMP], shell=True) #mod-filenameprint
def handle_elf(filename):
handle_binary(filename)
# Can run bingrep, elfparser but they suck...
def handle_x86(filename):
# Currently no special handling for x86 boot sectors
handle_binary(filename)
def handle_bootimg(filename):
name = str(subprocess.run(["basename ", filename], shell=True))
if (name[4:] == "boot" or
name[8:] == "recovery" or
name[4:] == "hosd" or
name[9:] == "droidboot" or
name[8:] == "fastboot" or
name[10:] == "okrecovery" or
name[4:] == "BOOT" or
name[8:] == "RECOVERY" or
name[-4:] == ".bin" ):
subprocess.run([IMGTOOL, filename, "extract"], shell=True)
os.chdir("extracted")
format_ = subprocess.run(["file","-b","ramdisk", "|", "cut", "-d", " ", "-f1"], universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.rstrip("\n")
if (format_ == "LZ4"):
subprocess.run(["unlz4", "ramdisk", "ramdisk.out"], shell=True)
subprocess.run(["cat", "ramdisk.out", "|", "cpio", "-i"], shell=True)
os.remove('ramdisk.out')
elif (format_ == "gzip"):
subprocess.run(["gunzip", "-c", "ramdisk", "|", "cpio", "-i"], shell=True)
os.remove("ramdisk")
os.chdir("..")
find_out = subprocess.run(["find","extracted", "-print0"],universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.split_lines()
for line in find_out:
if (os.path.isfile(line)):
format_ = subprocess.run(["file","-b","ramdisk", "|", "cut", "-d", " ", "-f1"], universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.rstrip("\n")
if (format_ == "gzip"):
subprocess.run(["mv", line, line, ".gz"], shell=True)
subprocess.run(["gunzip", "-f", line, ".gz"], shell=True)
at_extract(line)
else:
at_extract(line)
print(line + "processed: " + AT_RES)
# ------------------------------------------
# Need corresponding piped while loop FIXME
# ------------------------------------------
if ( KEEPSTUFF == 1 ):
subprocess.run(["sudo", "cp", "-r", "extracted", MY_FULL_DIR + "/" + SUB_DIR + "/" + name], shell=True)
subprocess.run(["sudo", "chown", "-R", EXTUSER + ":" + EXTGROUP, MY_FULL_DIR + "/" + SUB_DIR + "/" + name], shell=True)
os.rmdir("extracted")
else:
handle_binary(filename)
def handle_zip(filename, filetype):
rtn = ""
print("Unzipping " + filename + " ...")
os.mkdir(ZIP_TMP)
subprocess.run(["cp",filename, ZIP_TMP] ,shell=True)
if (filetype == "zip"):
subprocess.run(["unzip","-d",str(ZIP_TMP),str(ZIP_TMP)+"/"+filename],shell=True)
elif (filetype == "gzip"):
if (filename[-7:] == ".img.gz"):
print("Handling a .img.gz file... ")
gzip = subprocess.run(["basename", filename, ".gz"], shell=True)
subprocess.run("gunzip" + " " + "-c" + " " + str(ZIP_TMP)+"/"+filename,shell=True,stdout=open(str(ZIP_TMP)+"/"+str(gzip),'wb'))
else:
print("Handling a .tar.gz file... ")
subprocess.run(["tar","xvf",str(ZIP_TMP)+"/"+filename,"-C",str(ZIP_TMP)],shell=True)
os.rmdir(ZIP_TMP+"/"+filename)
# ------------------------------------------
# Need corresponding piped while loop FIXME
# ------------------------------------------
if (KEEPSTUFF == 1):
subprocess.run(["sudo","cp","-r",str(ZIP_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+filename],shell=True)
subprocess.run(["sudo","chown","-R",str(EXTUSER)+":"+str(EXTGROUP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+filename],shell=True)
os.rmdir(ZIP_TMP+"/"+filename)
def handle_qsbszb(qsbszb, qsmode):
getback = str(os.getcwd())
os.mkdir(MSC_TMP)
subprocess.run(["cp",str(qsbszb),str(MSC_TMP)],shell=True)
qsbszb = os.popen("basename \""+str(qsbszb)+"\"").read().rstrip("\n")
os.chdir(MSC_TMP)
if (qsmode == 0):
print("Splitting qsb " + str(qsbszb) + " ...")
subprocess.run([str(SPLITQSB), str(qsbszb)], shell=True)
else:
print("Splitting szb " + str(qsbszb) + " ...")
subprocess.run([str(LESZB), "-x", str(qsbszb)], shell=True)
os.remove(qsbszb)
# ------------------------------------------
# Need corresponding piped while loop FIXME
# ------------------------------------------
os.chdir(getback)
os.rmdir(MSC_TMP)
def handle_apk(apk):
name = os.popen("basename \""+str(apk)+"\"").read().rstrip("\n")
print("Decompiling" + str(name) + " ...")
os.mkdir(APK_TMP)
subprocess.run(["cp",str(apk),str(APK_TMP)+"/"+str(name)],shell=True) # Dex2Jar
subprocess.run([str(DEX2JAR),str(APK_TMP)+"/"+str(name),"-o",str(APK_TMP)+"/"+str(JAR_TMP)],shell=True)
subprocess.run("java" + " " + "-jar" + " " + str(JDCLI) + " " + "-oc" + " " + str(APK_TMP)+"/"+str(JAR_TMP),shell=True,stdout=open(str(APK_TMP)+"/jdcli.out",'wb'))
subprocess.run(["grep","-E",str(AT_CMD),str(APK_TMP)+"/jdcli.out"],shell=True)
subprocess.run("awk" + " " + "-v" + " " + "apkname="+str(name) + " " + "BEGIN {OFS=\"\\t\"} {print apkname,$0}",shell=True,stdout=open(str(MY_TMP),'ab'))
if (KEEPSTUFF == 1 ):
subprocess.run(["cp","-r",str(APK_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(name)],shell=True)
os.rmdir(APK_TMP)
def handle_jar(filename):
subprocess.run(["java","-jar",str(JDCLI),"-oc",str(filename)],shell=True)
subprocess.run(["grep","-E",str(AT_CMD)],shell=True)
subprocess.run("awk" + " " + "-v" + " " + "fname="+str(filename) + " " + "BEGIN {OFS=\"\\t\"} {print fname,$0}",shell=True,stdout=open(str(MY_TMP),'ab'))
def handle_java(filename):
format4 = str(filename)[-4:]
if (format4 == ".apk" or format4 == ".APK" or format4 == ".Apk"):
handle_apk(filename)
else:
handle_jar(filename)
def handle_odex(odex):
name = os.popen("basename \""+str(odex)+"\"").read().rstrip("\n")
arch = ""
boot = ""
print("Processing odex...")
os.mkdir(ODEX_TMP)
subprocess.run(["cp",str(odex),str(ODEX_TMP)+"/"+str(name)],shell=True) # Dex2Jar
arch = str(os.popen("file -b "+str(ODEX_TMP)+"/\""+str(name)+"\" | cut -d\" \" -f2 | cut -d\"-\" -f1").read().rstrip("\n"))
if (arch == "64"):
boot = BOOT_OAT_64
else:
boot = BOOT_OAT
print("DEBUG: use boot.oat - " + boot)
if (boot is not ""):
print("Processing smali...")
# Try to recover some strings from smali
subprocess.run(["java","-jar",str(BAKSMALI),"deodex","-b",str(boot),str(ODEX_TMP)+"/"+str(name),"-o",str(ODEX_TMP)+"/out"],shell=True)
# grep -r $AT_CMD $ODEX_TMP/out >> ../$MY_TMP
ret = subprocess.run(["grep","-r","-E",str(AT_CMD),str(ODEX_TMP)+"/out"],shell=True)
subprocess.run("awk" + " " + "-v" + " " + "fname="+str(ret) + " " + "BEGIN {OFS=\"\\t\"} {print fname,$0}",shell=True,stdout=open(str(MY_TMP),'ab'))
# Try to decompile from smali->dex->jar->src
# May not work!
print("decompiling smali/dex...")
subprocess.run(["java","-jar",str(SMALI),"ass",str(ODEX_TMP)+"/out","-o",str(ODEX_TMP)+"/out.dex"],shell=True)
print("invoking jadx on smali/dex output...")
subprocess.run([str(JADX),"-d",str(ODEX_TMP)+"/out2",str(ODEX_TMP)+"/out.dex"],shell=True)
if (os.path.isdir(str(ODEX_TMP)+"/out2")):
subprocess.run(["grep","-r","-E",str(AT_CMD),str(ODEX_TMP)+"/out2"],shell=True)
subprocess.run("awk" + " " + "-v" + " " + "fname="+str(name) + " " + "BEGIN {OFS=\"\\t\"} {print fname,$0}",shell=True,stdout=open(str(MY_TMP),'ab'))
if (KEEPSTUFF == 1):
subprocess.run(["cp","-r",str(ODEX_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(name)],shell=True)
os.rmdir(ODEX_TMP)
def check_for_suffix(filename):
suffix = filename[-4:]
suffix2 = filename[-5:]
if (suffix == ".apk" or suffix == ".APK" or suffix == ".Apk"
or suffix == ".jar" or suffix == ".Jar" or suffix == ".JAR"):
AT_RES = "java"
elif (suffix2 == ".odex" or suffix2 == ".ODEX" or suffix2 == ".Odex"):
AT_RES == "odex"
else:
AT_RES = "TBD"
# Process special files
# All files which require special care should happen here
def handle_special(filename):
justname = str(os.popen("basename \""+str(filename)+"\"").read().rstrip("\n"))
usbFile = open(MY_USB)
propFile = open(MY_PROP)
tizFile = open(TIZ_LOG)
if (justname == str(glob.glob("init*usb.rc"))):
# Save init file for USB config analysis
# also need to capture e.g., init.hosd.usb.rc (notable: aosp sailfish)
# there's also init.tuna.usb.rc in aosp yakju, etc.
# init.steelhead.usb.rc in tungsten
print(filename, file = usbFile)
print("---------",file = usbFile)
subprocess.run("cat" + " " + str(filename),shell=True,stdout=usbFile)
print("=========",file=usbFile)
elif (justname == "build.prop" ):
# Save the contents of build.prop to get information about OS version, etc.
print(filename,file=propFile)
print("---------",file=propFile)
# in rare cases, permission denied when trying to access build.prop
subprocess.run("sudo" + " " + "cat" + " " + str(filename),shell=True,stdout=propFile)
print("=========",file=propFile)
elif ( VENDOR == "samsung" ) and ( justname == "dzImage" ):
# Tizen OS image detected. Should abort
# touch ../$MY_TIZ
AT_RES = "tizen"
print(str(filename)+" processed: "+str(AT_RES))
print(IMAGE,file=tizFile)
# for easier ID later, needs to be existing file
exit(55)
# exit immediately; no need to go further
def at_extract(filename):
filetype = str(os.popen("file -b \""+str(filename)+"\" | cut -d\" \" -f1").read().rstrip("\n"))
justname = (os.popen("basename \""+str(filename)+"\"").read().rstrip("\n"))
# Check for special files
handle_special(filename)
if (filetype == "apollo" or filetype == "FoxPro" or filetype == "Mach-O" or
filetype == "DOS/MBR" or filetype == "PE32" or filetype == "PE32+" or
filetype == "dBase" or filetype == "MS" or filetype == "PDP-11" or
filetype == "zlib" or filetype == "ISO-8859" or filetype == "Composite" or
filetype == "very" or filetype == "Hitachi" or filetype == "SQLite" ):
handle_binary(filename)
AT_RES = "good"
elif (filetype == "ELF"):
handle_elf(filename)
check_for_suffix(filename)
if (AT_RES == "odex"):
handle_odex(filename)
AT_RES = "good"
elif (filetype == "x86"):
handle_x86(filename)
AT_RES = "good"
elif (filetype == "DOS"):
handle_text(filename)
AT_RES = "good"
elif (filetype == "Java"):
handle_java(filename)
AT_RES = "good"
elif (filetype == "POSIX" or filetype == "Bourne-Again"):
handle_text(filename)
AT_RES = "good"
elif (filetype == "ASCII" or filetype == "XML" or filetype == "Tex" or filetype == "html"
or filetype == "UTF-8" or filetype == "C" or filetype == "Pascal" or filetype == "python"):
handle_text(filename)
AT_RES = "good"
elif (filetype == "Windows"):
handle_text(filename)
AT_RES = "good"
elif (filetype == "Zip"):
check_for_suffix(filename)
if ("AT_RES" == "java"):
handle_java(filename)
AT_RES = "good"
else:
handle_zip(filename, "zip")
AT_RES = "good"
elif (filetype == "gzip" or filetype == "XZ"):
handle_zip(filename, "gzip")
AT_RES = "good"
elif (format == "Android"):
print("Processing .img file as binary!")
handle_bootimg(filename)
AT_RES = "good"
elif (format == "broken" or format == "symbolic" or format == "SE" or
format == "empty" or format == "directory" or format == "Ogg" or
format == "PNG" or format == "JPEG" or format == "PEM" or
format == "TrueType" or format == "LLVM" or format == "Device"):
# format == dBase was being skipped before; now handled as binary (jochoi)
# format == Device Tree Blob after extracting boot/recovery img; ignoring
# Skip broken/symbolic/sepolicy/empty/dir/...
AT_RES = "skip"
else:
AT_RES = "bad"
def handle_ext4(imgPath):
ext = str(os.popen("basename \""+str(imgPath)+"\"").read().rstrip("\n"))
arch = ""
os.mkdir(DIR_TMP)
os.mkdir(MNT_TMP)
# Make a copy
subprocess.run(["cp",imgPath,DIR_TMP+"/"+(ext)],shell=True)
# NOTE: needs sudo or root permission
subprocess.run(["sudo","mount","-t","ext4",(DIR_TMP)+"/"+str(ext),str(MNT_TMP)],shell=True)
subprocess.run(["sudo","chown","-R",(EXTUSER)+":"+(EXTGROUP),str(MNT_TMP)],shell=True)
# Find the boot.oat for RE odex
BOOT_OAT = ""
BOOT_OAT_64 = ""
thisFile = open("newfile.txt")
while open(thisFile):
# Debug
#echo "DEBUG: boot.oat - $file"
arch = str(os.popen("file -b \""+str(thisFile)+"\" | cut -d\" \" -f2 | cut -d\"-\" -f1").read().rstrip("\n"))
if (arch == "64"):
BOOT_OAT_64=thisFile
else:
BOOT_OAT=thisFile
subprocess.run("arch" + " " + "find" + " " + str(MNT_TMP) + " " + "-name" + " " + "boot.oat" + " " + "-print",shell=True,stdin=open("sudo", 'rb'))
print("found boot.oat: "+str(BOOT_OAT)+", boot_oat(64): "+str(BOOT_OAT_64))
# Traverse the filesystem - root permission
find_out = subprocess.run(["find", MNT_TMP, "-print0"],universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.split_lines()
for line in find_out:
if (os.path.isfile(line)):
at_extract(line)
print(line + " processed: " + AT_RES)
# what we're interested is probably the contents of the FS once mounted, rather than DIR_TMP
if ( "$KEEPSTUFF" == "1" ):
# cp -r $DIR_TMP ../$ext
subprocess.run(["sudo","cp","-r",str(MNT_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","chown","-R",str(EXTUSER)+":"+str(EXTGROUP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","umount",str(MNT_TMP)],shell=True)
os.rmdir(DIR_TMP)
AT_RES = ("good")
def handle_chunk(imgFile, chunkMode):
# need the directory name, not the specific file name
ext= "system.img"
raw="system.img.raw"
container = str(os.popen("dirname \""+str(imgFile)+"\"").read().rstrip("\n"))
arch = ""
getback = str(os.popen("pwd").read().rstrip("\n"))
chunkdir = "system_raw"
# needs to be performed from within the directory
os.chdir(container) # simg2img must be performed from within the directory
os.mkdir(chunkdir)
subprocess.run(["cp",glob.glob("system.img_*"),str(chunkdir)],shell=True)
os.chdir(chunkdir)
subprocess.run(["simg2img",(glob.glob("*chunk*")),str(raw)],shell=True)
subprocess.run(["file",str(raw)],shell=True)
print("Stage 1 complete")
if (chunkMode == 0):
subprocess.run(["offset","=",os.popen("LANG=C grep -aobP -m1 \"\\x53\\xEF\" "+str(raw)+" | head -1 | gawk \"{print $1 - 1080}\"").read().rstrip("\n")],shell=True)
elif (chunkMode == 1):
subprocess.run(["mv",str(raw),str(ext)],shell=True) # no further Processing needed
print("Stage 2 complete")
subprocess.run(["mv", ext, ".."],shell=True)
os.chdir("..")
os.rmdir(chunkdir)
os.chdir(str(getback)) # return to directory of the script
handle_ext4(container + "/" + ext)
def handle_chunk_lax(imgFile, chunkMode):
container=str(os.popen("dirname \""+str(imgFile)+"\"").read().rstrip("\n"))
getback=str(os.popen("pwd").read().rstrip("\n"))
ext = ""
chunkdir = ""
os.chdir(container)
if (chunkMode == 0):
chunkdir = "oem_raw"
os.mkdir(chunkdir)
subprocess.run(["cp",str(glob.glob("oem.img_*")),str(chunkdir)],shell=True)
ext = "oem.img"
elif(chunkMode == 1):
chunkdir = "userdata_raw"
os.mkdir(chunkdir)
subprocess.run(["cp",str(glob.glob("userdata.img_*")),str(chunkdir)],shell=True)
ext = "userdata.img"
elif (chunkMode == 2):
chunkdir = "systemb_raw"
os.mkdir(chunkdir)
subprocess.run(["cp",str(glob.glob("systemb.img_*")),str(chunkdir)],shell=True)
ext = "systemb.img"
os.chdir(str(chunkdir))
subprocess.run(["simg2img",(glob.glob("*chunk*")),str(ext)],shell=True)
subprocess.run(["mv",str(ext),".."],shell=True)
os.chdir("..")
os.rmdir(chunkdir)
os.chdir(str(getback))
handle_ext4(container + "/" + ext)
def handle_sdat(img, path):
container=str(os.popen("dirname \""+str(img)+"\"").read().rstrip("\n"))
SDAT2IMG = "$container" + "/" + path + ".transfer.list" + img + container + "/" + ".img"
handle_ext4(container + "/" + path + ".img")
def handle_sin(img):
fullimg= str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(SUB_SUB_DIR)+"/"+os.popen("ls \""+str(img)+"\" | cut -d \"/\" -f2-").read().rstrip("\n")
container=str(os.popen("dirname \""+str(img)+"\"").read().rstrip("\n"))
base=str(os.popen("basename \""+str(img)+"\" .sin").read().rstrip("\n"))
subprocess.run([str(SONYFLASH),"--action=extract","--file="+str(fullimg)],shell=True) # will write to directory containing the img
getback=str(os.popen("pwd").read().rstrip("\n"))
# the result is observed to be ext4, elf, or unknown formats~
if (base[-5:] == ".ext4"):
handle_ext4(container + "/" + base + ".ext4")
elif (base[-4:] == ".elf"):
# need to specially manage kernel.elf
if (base == "kernel"):
print("Processing separate ramdisk img")
print("-----------------------------")
os.chdir(str(container))
os.mkdir("elfseperate")
subprocess.run(["mv","kernel.elf","elfseparate"],shell=True)
os.chdir("elfseparate")
subprocess.run([str(SONYELF),"-i","kernel.elf","-k","-r"],shell=True)
os.mkdir("ramdiskseparate")
subprocess.run(["mv","kernel.elf-ramdisk.cpio.gz","ramdiskseparate"],shell=True)
os.chdir("ramdiskseparate")
pipe1, pipe2 = os.pipe()
if os.fork():
os.close(pipe1)
os.dup2(pipe1, 0)
subprocess.run(["cpio","-i"],shell=True)
else:
os.close(pipe1)
os.dup2(pipe1, 1)
subprocess.run(["gunzip","-c","kernel.elf-ramdisk.cpio.gz"],shell=True)
sys.exit(0)
os.remove("kernel.elf-ramdisk.cpio.gz")
os.chdir("..")
find_out = subprocess.run(["find","ramdiskseparate", "-print0"],universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.split_lines()
for line in find_out:
if (os.path.isfile(line)):
at_extract(line)
print(line + "processed: " + AT_RES)
os.rmdir("ramdiskseparate")
os.chdir(getback)
print("-----------------------------")
else:
at_extract((container + "/" + base + ".elf"))
elif(base[-4:] == ".yaffs2"):
print("Processing yaffs2 img")
print("-----------------------------")
os.chdir(str(container))
os.mkdir("yaffsseperate")
subprocess.run(["mv",str(base)+".yaffs2","yaffsseparate"],shell=True)
os.chdir("yaffsseparate")
UNYAFFS = base + ".yaffs2"
os.remove(base + ".yaffs2")
find_out = subprocess.run(["find",".", "-print0"],universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.split_lines()
for line in find_out:
if (os.path.isfile(line)):
at_extract(line)
print(line + "processed: " + AT_RES)
os.chdir(getback)
print("--------------------------")
else:
at_extract((container + "/" + base + ".unknown"))
#currently not working FIXME
def handle_vfat(img):
ext = str(os.popen("basename \""+str(img)+"\"").read().rstrip("\n"))
arch = ""
os.mkdir(DIR_TMP)
os.mkdir(MNT_TMP)
# Make a copy
subprocess.run(["cp", str(img), str(DIR_TMP)+"/"+str(ext)],shell=True)
# NOTE: needs sudo or root permission
subprocess.run(["sudo", "mount", "-t", "vfat", str(DIR_TMP)+"/"+str(ext), str(MNT_TMP)],shell=True)
subprocess.run(["sudo", "chown", "-R", str(EXTUSER) + ":" + str(EXTGROUP), str(MNT_TMP)],shell=True)
# Find the boot.oat for RE odex
BOOT_OAT=""
BOOT_OAT_64= ""
thisFile = open()
while open(thisFile):
# Debug
#echo "DEBUG: boot.oat - $file"
arch = str(os.popen("file -b \""+str(thisFile)+"\" | cut -d\" \" -f2 | cut -d\"-\" -f1").read().rstrip("\n"))
if (arch == "64"):
BOOT_OAT_64=thisFile
else:
BOOT_OAT=thisFile
#Need while loop for at_extract FIXME
if (KEEPSTUFF == 1):
subprocess.run(["sudo","cp","-r",str(MNT_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","chown","-R",str(EXTUSER)+":"+str(EXTGROUP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","umount",str(MNT_TMP)],shell=True)
os.rmdir(DIR_TMP)
AT_RES = "good"
def handle_simg(img):
nam = str(os.popen("basename -s .img \""+str(img)+"\"").read().rstrip("\n"))
ext = str(nam)+".ext4"
arch = ""
os.mkdir(DIR_TMP)
os.mkdir(MNT_TMP)
subprocess.run(["simg2img",str(img),str(DIR_TMP)+"/"+str(ext)],shell=True)
# NOTE: needs sudo or root permission
subprocess.run(["sudo","mount","-t","ext4",str(DIR_TMP)+"/"+str(ext),str(MNT_TMP)],shell=True)
subprocess.run(["sudo","chown","-R",str(EXTUSER)+":"+str(EXTGROUP),str(MNT_TMP)],shell=True)
# Find the boot.oat for RE odex
BOOT_OAT= ""
BOOT_OAT_64= ""
######################################
# Need corresponding while loop FIXME
######################################
# Traverse the filesystem - root permission
subprocess.run(["sudo","find",str(MNT_TMP),"-print0"],shell=True)
find_out = subprocess.run(["find", MNT_TMP, "-print0"],universal_newlines=True, stdout=subprocess.PIPE, shell=True).stdout.split_lines()
for line in find_out:
if (os.path.isfile(line)):
at_extract(line)
print(line + "processed: " + AT_RES)
if (KEEPSTUFF == 1):
subprocess.run(["sudo","cp","-r",str(MNT_TMP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","chown","-R",str(EXTUSER)+":"+str(EXTGROUP),str(MY_FULL_DIR)+"/"+str(SUB_DIR)+"/"+str(ext)],shell=True)
subprocess.run(["sudo","umount",str(MNT_TMP)],shell=True)
os.rmdir(DIR_TMP)
AT_RES = "good"
def handle_unsparse(filename, prefix, xmlFile, imageVendor):
# handle_unsparse(filename, "system", "rawprogram0.xml", VENDOR)
container=str(os.popen("dirname \""+str(filename)+"\"").read().rstrip("\n"))
UNSPARSE = container + prefix + xmlFile + imageVendor
handle_ext4(container + "/" + prefix + ".img")
# Go thru each from within sub_sub_dir
# Currently no special handling for bootloader.img, radio.img and modem.img
def process_file(filename):
justname = str(os.popen("basename \""+str(filename)+"\"").read().rstrip("\n"))
# local format=`file -b $filename | cut -d" " -f1`
handled = False
# echo "Processing file: $filename" >> ../$MY_TMP # printing out the file being processed
# echo "IN process_file | handling file: $filename"
#-------------------------------------------------------------------------------
# IF THE VENDOR IS AOSP
#-------------------------------------------------------------------------------
if (VENDOR == "aosp"):
if (justname == "system.img" or justname == "system_other.img" or justname == "vendor.img"):
# Handle sparse ext4 fs image
print("Processing sparse ext4 img...")
print("-----------------------------")
handle_simg(filename)
print("-----------------------------")
handled = True
else:
print("Processing vfat img...")
print("-----------------------------")
handle_vfat(filename)
print("-----------------------------")
#-------------------------------------------------------------------------------
# IF THE VENDOR IS Samsung
#-------------------------------------------------------------------------------
elif (VENDOR == "samsung"):
samformat = str(os.popen("file -b \""+str(filename)+"\" | cut -d\" \" -f1").read().rstrip("\n"))
if (( justname == "persist.img.ext4" ) or ( justname == "system.img.ext4" ) or ( justname == "cache.img.ext4" ) or
( justname == "omr.img.ext4" ) or ( justname == "userdata.img.ext4" ) ):
if (samformat == "Linux"):
print("Processing ext4 img...")
print("-----------------------------")
handle_ext4(filename)
print("-----------------------------")
else:
print("Processing sparse ext4 img...")
print("-----------------------------")
handle_simg(filename)
print("-----------------------------")
handled = True
elif ( (justname == "cache.img" ) or ( justname == "hidden.img" ) or ( justname == "omr.img" )
or ( justname == "hidden.img.md5" ) or ( justname == "cache.img.md5" ) or ( justname == "persist.img" )
or ( justname == "factoryfs.img" ) ):
print("Processing sparse ext4 img...")
print("-----------------------------")
handle_simg(filename)
print("-----------------------------")
elif ( (justname == "system.img" ) or ( justname == "userdata.img" ) or ( justname == "system.img.md5" )
or ( justname == "userdata.img.md5" ) ):
if (samformat == "DOS/MBR"):
print("Processing vfat img...")
print("-----------------------------")
handle_vfat(filename)
print("-----------------------------")
else:
print("Processing sparse ext4 img...")
print("-----------------------------")
handle_simg(filename)
print("-----------------------------")
handled = True
elif (justname == "adspso.bin"):
print("Processing ext4 img...")
print("-----------------------------")
handle_ext4(filename)
print("-----------------------------")
handled = True
elif ( (justname == "system.rfs" ) or ( justname == "csc.rfs" ) or ( justname == "efs.img" )
or ( justname == "factoryfs.rfs" ) or ( justname == "cache.rfs" ) or ( justname == "hidden.rfs" ) ):
print("Processing vfat img...")
print("-----------------------------")
handle_vfat(filename)
print("-----------------------------")
handled = True
elif (justname == "fota.zip"):
print("WARNING: Skipping password-protected fota.zip!")
handled = True
elif ( justname == glob.glob("*.tar*") or justname == glob.glob("*.TAR*") ):
TARNESTED=((TARNESTED + 1))
# increment TARNESTED
os.mkdir("nestedPOSIXtar"+str(TARNESTED))
ret = subprocess.run(["tar","xvf",str(filename),"-C","nestedPOSIXtar"+str(TARNESTED)],shell=True)
#need recursive loop FIXME
if str(ret) == "55":
exit(55)
print("-------------------------")
os.rmdir("nestedPOSIXtar"+str(TARNESTED))
TARNESTED=((TARNESTED - 1))
handled = True
#-------------------------------------------------------------------------------
# IF THE VENDOR IS Motorola
#-------------------------------------------------------------------------------
elif (VENDOR == "motorola"):
motoformat = str(os.popen("file -b \""+str(filename)+"\" | cut -d\" \" -f1").read().rstrip("\n"))
if (justname == str(glob.glob("*.sbf"))):
# proprietary motorola format
# the only available tool, sbf_flash, is unreliable, skip and record only FIXME
print(IMAGE, file=open(SBF_LOG))
elif (justname == str(glob.glob("*.mzf"))):
# unclear how to extract from .mzf (no tools found) FIXME
print(IMAGE, file=open(MZF_LOG))
# not attempting to deal with shx or nb0 files either (5)
# nb0 utils unpacker simply unpacks as data, not imgs
elif (justname == str(glob.glob("system.img_sparsechunk.*"))):
if (CHUNKED == 0):
print("Processing sparsechunks into ext4...")
print("-----------------------------")
handle_chunk(filename, 0)
CHUNKED = 1 # no need to duplicate work per sparsechunk
print("-----------------------------")
handled = True
elif (justname == str(glob.glob("system.img_sparsechunk*"))):
# NOTE: this if does not include the period as last elif
if (CHUNKED == 0):
print("Processing sparsechunks into ext4...")
print("-----------------------------")
handle_chunk(filename, 1)
CHUNKED=1 # no need to duplicate work per sparsechunk
print("-----------------------------")
elif (justname == str(glob.glob("oem.img_sparsechunk.*"))):
if (CHUNKEDO == 0):
print("Processing sparsechunks into ext4...")
print("-----------------------------")
handle_chunk_lax(filename, 0)
CHUNKEDO=1 # no need to duplicate work per sparsechunk
print("-----------------------------")
elif (justname == str(glob.glob("userdata.img_sparsechunk*"))):
if (CHUNKEDU == 0):
print("Processing sparsechunks into ext4...")
print("-----------------------------")
handle_chunk_lax(filename, 1)
CHUNKEDU=1 # no need to duplicate work per sparsechunk
print("-----------------------------")
handled = True
elif (justname == str(glob.glob("system_b.img_sparsechunk.*"))):
if (CHUNKEDB == 0):
print("Processing sparsechunks into ext4...")
print("-----------------------------")
handle_chunk_lax(filename, 1)
CHUNKEDU=1 # no need to duplicate work per sparsechunk
print("-----------------------------")
handled = True
elif ( (justname == "adspo.bin") or (justname == "fsg.mbn" ) or (justname == "preinstall.img" ) or (justname == "radio.img" ) ):