-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathbinwalk.py
More file actions
executable file
·89 lines (72 loc) · 2.49 KB
/
Copy pathbinwalk.py
File metadata and controls
executable file
·89 lines (72 loc) · 2.49 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
#!/usr/bin/env python3
import argparse
import yaml
import struct
import hashlib
def cstr_len(data):
for i in range(len(data)):
if data[i] == 0:
return i
return None
def analyze(filename, extract=False, full=False):
with open(filename, "rb") as f:
data = f.read()
yaml_len = cstr_len(data)
if yaml_len is None:
print("Broken dump")
return
try:
descr = yaml.safe_load(data[:yaml_len])
except yaml.YAMLError as exc:
print(exc)
return
if full:
ff = open("ff.img", "wb")
partitions = descr["rom"][0]["partitions"]
ptr = yaml_len + 1
for i in range(len(partitions)):
if ptr >= len(data):
break
(next_len, ) = struct.unpack("<I", data[ptr:ptr+4])
ptr += 4
partition = partitions[i]
name = partition["name"]
psize = partition["size"]
if psize != next_len:
print(f"For '{name}' expected size {hex(psize)}, actual"
f" {hex(next_len)}, skipping ❌")
continue
chunk = data[ptr:ptr+psize]
sha1 = partition.get("sha1", "")
if sha1 != "":
m = hashlib.sha1()
m.update(chunk)
short_hash = m.hexdigest()[:8]
if short_hash != partition["sha1"]:
print(f"Checking SHA1 digest failed, "
f"expected {short_hash} ❌")
continue
if extract:
with open(name, "wb") as b:
print(f"Writing {name} {psize//1024}Kb...")
b.write(chunk)
if full:
ff.write(chunk)
else:
print(f"✅ {name: <10}{hex(psize): <16}{sha1}")
ptr += next_len
if ptr != len(data):
print("Broken dump")
if full:
ff.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="binary file with dump")
parser.add_argument("-e", "--extract", action="store_true",
help="Automatically extract binary content")
parser.add_argument("-f", "--full", action="store_true", help="Write all"
"partitions to single file")
args = parser.parse_args()
analyze(args.filename, args.extract, args.full)
if __name__ == '__main__':
main()