-
Notifications
You must be signed in to change notification settings - Fork 44
/
macho_print.py
87 lines (76 loc) · 2.64 KB
/
macho_print.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
import lief
import argparse
import hashlib
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Print Mach-O information')
parser.add_argument('MACHO', help='Mach-o file')
args = parser.parse_args()
binary = lief.parse(args.MACHO)
with open(args.MACHO, 'rb') as f:
data = f.read()
# General information -> CPU Type
# Hash, CPU Type, Size
print("General Information")
print("=" * 80)
for algo in ["md5", "sha1", "sha256"]:
m = getattr(hashlib, algo)()
m.update(data)
print("{:15} {}".format(algo.upper()+":", m.hexdigest()))
print("{:15} {} bytes".format("Size:", len(data)))
print("{:15} {}".format("Type:", binary.header.cpu_type.name))
print("Entry point:\t0x%x" % binary.entrypoint)
print("")
# Commands
print("Commands")
print("=" * 80)
for c in binary.commands:
if c.command.name == "SEGMENT_64":
print("{:20} {:10} {:5} {:14} {}".format(
c.command.name,
c.name if hasattr(c, 'name') else '',
c.size,
hex(c.virtual_address) if hasattr(c, 'virtual_address') else "",
hex(c.file_offset) if hasattr(c, 'file_offset') else "",
))
elif c.command.name in ["LOAD_DYLIB", "LOAD_WEAK_DYLIB"]:
print("{:20} {} (version {})".format(
c.command.name,
c.name,
".".join([str(a) for a in c.current_version])
))
elif c.command.name == "UUID":
print("{:20} {}".format(
c.command.name,
''.join('{:02x}'.format(x) for x in c.uuid)
))
else:
print("{:20} {:20}".format(
c.command.name,
c.name if hasattr(c, 'name') else ''
))
print("")
# Sections
print("Sections")
print("=" * 80)
print("%-16s %-9s %-12s %-9s %-9s %-25s %s" % ( "Name", "Segname", "VirtAddr", "RawAddr", "Size", "type", "Md5"))
for s in binary.sections:
m = hashlib.md5()
m.update(bytearray(s.content))
print("%-16s %-9s %-12s %-9s %-9s %-25s %s" % (
s.name,
s.segment.name,
hex(s.virtual_address),
hex(s.offset),
s.size,
str(s.type).replace("SECTION_TYPES.", ""),
m.hexdigest()
))
print("")
# Imports (binding infos)
print("Imports")
print("=" * 80)
for f in binary.imported_symbols:
try:
print("{:35s} {}".format(f.name, f.binding_info.library.name))
except lief.not_found:
print(f.name)