Skip to content

Commit aa9a718

Browse files
authored
Update to v2.1 & fix #18
1 parent 891b5e3 commit aa9a718

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

fat.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def show_banner():
2424
| | | | | | | | | | | | | | | | | | \__ \
2525
\_| |_| |_| |_| |_| \_| |_/ |_| |_| |___/
2626
27-
Welcome to the Firmware Analysis Plus - v2.0
28-
By lys - https://blog.csdn.net/song_lee | @liyansong
27+
Welcome to the Firmware Analysis Plus - v2.1
28+
By lys - https://github.com/liyansong2018/firmware-analysis-plus | @liyansong
2929
""")
3030

3131

@@ -36,38 +36,51 @@ def get_next_unused_iid():
3636
return ""
3737

3838

39-
def run_extractor(firm_name):
39+
def run_extractor(firm_name, binwalk):
4040
print ("[+] Firmware:", os.path.basename(firm_name))
4141
print ("[+] Extracting the firmware...")
4242

43-
extractor_cmd = os.path.join(firmadyne_path, "sources/extractor/extractor.py")
44-
extractor_args = [
45-
extractor_cmd,
46-
"-np",
47-
"-nk",
48-
firm_name,
49-
os.path.join(firmadyne_path, "images")
50-
]
51-
52-
child = pexpect.spawn("python3", extractor_args, timeout=None)
53-
child.expect_exact("Tag: ")
54-
tag = child.readline().strip().decode("utf8")
55-
child.expect_exact(pexpect.EOF)
56-
57-
image_tgz = os.path.join(firmadyne_path, "images", tag + ".tar.gz")
58-
59-
if os.path.isfile(image_tgz):
60-
iid = get_next_unused_iid()
61-
if iid == "" or os.path.isfile(os.path.join(os.path.dirname(image_tgz), iid + ".tar.gz")):
62-
print ("[!] Too many stale images")
63-
print ("[!] Please run reset.py or manually delete the contents of the scratch/ and images/ directory")
64-
return ""
43+
if binwalk == "1" or binwalk == "yes" or binwalk == None:
44+
extractor_cmd = os.path.join(firmadyne_path, "sources/extractor/extractor.py")
45+
extractor_args = [
46+
extractor_cmd,
47+
"-np",
48+
"-nk",
49+
firm_name,
50+
os.path.join(firmadyne_path, "images")
51+
]
52+
53+
child = pexpect.spawn("python3", extractor_args, timeout=None)
54+
child.expect_exact("Tag: ")
55+
tag = child.readline().strip().decode("utf8")
56+
child.expect_exact(pexpect.EOF)
57+
58+
image_tgz = os.path.join(firmadyne_path, "images", tag + ".tar.gz")
59+
60+
if os.path.isfile(image_tgz):
61+
iid = get_next_unused_iid()
62+
if iid == "" or os.path.isfile(os.path.join(os.path.dirname(image_tgz), iid + ".tar.gz")):
63+
print ("[!] Too many stale images")
64+
print ("[!] Please run reset.py or manually delete the contents of the scratch/ and images/ directory")
65+
return ""
66+
67+
os.rename(image_tgz, os.path.join(os.path.dirname(image_tgz), iid + ".tar.gz"))
68+
print ("[+] Image ID:", iid)
69+
return iid
70+
71+
return ""
72+
73+
else:
74+
tag = "1"
75+
image_path_name = os.path.join(firmadyne_path, "images", os.path.basename(firm_name))
76+
image_path = os.path.join(firmadyne_path, "images")
77+
os.system("./reset.py")
78+
os.system("cp {} {}".format(firm_name, image_path))
79+
os.system("mv {} {}".format(image_path_name, image_path + "/" + tag + ".tar.gz"))
6580

66-
os.rename(image_tgz, os.path.join(os.path.dirname(image_tgz), iid + ".tar.gz"))
67-
print ("[+] Image ID:", iid)
68-
return iid
81+
print ("[+] Image ID:", tag)
82+
return tag
6983

70-
return ""
7184

7285

7386
def identify_arch(image_id):
@@ -142,6 +155,7 @@ def main():
142155
parser = argparse.ArgumentParser()
143156
parser.add_argument("firm_path", help="The path to the firmware image", type=str)
144157
parser.add_argument("-q", "--qemu", metavar="qemu_path", help="The qemu version to use (must exist within qemu-builds directory). If not specified, the qemu version installed system-wide will be used", type=str)
158+
parser.add_argument("-b", "--binwalk", metavar="compiled_binwalk", help="Has binwalk been compiled? yes or no, 1 or 0", type=str)
145159
args = parser.parse_args()
146160

147161
qemu_ver = args.qemu
@@ -153,7 +167,7 @@ def main():
153167
print ("[+] Using system qemu")
154168
qemu_dir = None
155169

156-
image_id = run_extractor(args.firm_path)
170+
image_id = run_extractor(args.firm_path, args.binwalk)
157171

158172
if image_id == "":
159173
print ("[!] Image extraction failed")

0 commit comments

Comments
 (0)