|
| 1 | +import lief |
| 2 | +import argparse |
| 3 | +import hashlib |
| 4 | + |
| 5 | + |
| 6 | +if __name__ == '__main__': |
| 7 | + parser = argparse.ArgumentParser(description='Print Mach-O information') |
| 8 | + parser.add_argument('MACHO', help='Mach-o file') |
| 9 | + args = parser.parse_args() |
| 10 | + |
| 11 | + |
| 12 | + binary = lief.parse(args.MACHO) |
| 13 | + |
| 14 | + with open(args.MACHO, 'rb') as f: |
| 15 | + data = f.read() |
| 16 | + |
| 17 | + # General information -> CPU Type |
| 18 | + # Hash, CPU Type, Size |
| 19 | + print("General Information") |
| 20 | + print("=" * 80) |
| 21 | + for algo in ["md5", "sha1", "sha256"]: |
| 22 | + m = getattr(hashlib, algo)() |
| 23 | + m.update(data) |
| 24 | + print("{:15} {}".format(algo.upper()+":", m.hexdigest())) |
| 25 | + print("{:15} {} bytes".format("Size:", len(data))) |
| 26 | + print("{:15} {}".format("Type:", binary.header.cpu_type.name)) |
| 27 | + print("Entry point:\t0x%x" % binary.entrypoint) |
| 28 | + print("") |
| 29 | + |
| 30 | + # Commands |
| 31 | + print("Commands") |
| 32 | + print("=" * 80) |
| 33 | + for c in binary.commands: |
| 34 | + if c.command.name == "SEGMENT_64": |
| 35 | + print("{:20} {:10} {:5} {:14} {}".format( |
| 36 | + c.command.name, |
| 37 | + c.name if hasattr(c, 'name') else '', |
| 38 | + c.size, |
| 39 | + hex(c.virtual_address) if hasattr(c, 'virtual_address') else "", |
| 40 | + hex(c.file_offset) if hasattr(c, 'file_offset') else "", |
| 41 | + )) |
| 42 | + elif c.command.name in ["LOAD_DYLIB", "LOAD_WEAK_DYLIB"]: |
| 43 | + print("{:20} {} (version {})".format( |
| 44 | + c.command.name, |
| 45 | + c.name, |
| 46 | + ".".join([str(a) for a in c.current_version]) |
| 47 | + )) |
| 48 | + elif c.command.name == "UUID": |
| 49 | + print("{:20} {}".format( |
| 50 | + c.command.name, |
| 51 | + ''.join('{:02x}'.format(x) for x in c.uuid) |
| 52 | + )) |
| 53 | + else: |
| 54 | + print("{:20} {:20}".format( |
| 55 | + c.command.name, |
| 56 | + c.name if hasattr(c, 'name') else '' |
| 57 | + )) |
| 58 | + print("") |
| 59 | + |
| 60 | + # Sections |
| 61 | + print("Sections") |
| 62 | + print("=" * 80) |
| 63 | + print("%-16s %-9s %-12s %-9s %-9s %-25s %s" % ( "Name", "Segname", "VirtAddr", "RawAddr", "Size", "type", "Md5")) |
| 64 | + for s in binary.sections: |
| 65 | + m = hashlib.md5() |
| 66 | + m.update(bytearray(s.content)) |
| 67 | + print("%-16s %-9s %-12s %-9s %-9s %-25s %s" % ( |
| 68 | + s.name, |
| 69 | + s.segment.name, |
| 70 | + hex(s.virtual_address), |
| 71 | + hex(s.offset), |
| 72 | + s.size, |
| 73 | + str(s.type).replace("SECTION_TYPES.", ""), |
| 74 | + m.hexdigest() |
| 75 | + )) |
| 76 | + print("") |
| 77 | + |
| 78 | + # Imports (binding infos) |
| 79 | + print("Imports") |
| 80 | + print("=" * 80) |
| 81 | + for f in binary.imported_symbols: |
| 82 | + try: |
| 83 | + print("{:35s} {}".format(f.name, f.binding_info.library.name)) |
| 84 | + except lief.not_found: |
| 85 | + print(f.name) |
| 86 | + |
| 87 | + |
0 commit comments