MacOS Executable Header and Data Handler¶
[1]:
from rich.console import Console
from rich.table import Table
from byteclasses.handlers.executables.mach import Mach64, MachHdr64
from byteclasses.print import byteclass_info, byteclass_inspect, collection_table
[2]:
with open("../../../tests/data/hello_world.mach-o", "rb") as file:
data = file.read()
mach64_hdr = MachHdr64()
mach64_hdr.attach(memoryview(data))
[3]:
byteclass_info(mach64_hdr)
Byteclass Info ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Property ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ type() │ MachHdr64 │ │ is_byteclass() │ True │ │ is_collection_instance() │ True │ │ is_primitive_instance() │ False │ │ mro │ MachHdr64 -> object │ │ len() │ 32 │ │ str() │ MachHdr64(magic=DWord(data=b'\xcf\xfa\xed\xfe', byte_order=b'@'), │ │ │ cputype=Int32(16777228), cpusubtype=Int32(0), filetype=UInt32(2), ncmds=UInt32(17), │ │ │ sizeofcmds=UInt32(1056), flags=MachFlags32(data=b'\x85\x00 \x00'), │ │ │ reserved=UInt32(0)) │ │ repr() │ MachHdr64(byte_order=b'@',data=bytearray(b'\xcf\xfa\xed\xfe\x0c\x00\x00\x01\x00\x00… │ │ │ \x04\x00\x00\x85\x00 \x00\x00\x00\x00\x00')) │ │ .data │ bytearray(b'\xcf\xfa\xed\xfe\x0c\x00\x00\x01\x00\x00\x00\x00\x02\x00\x00\x00\x11\x0… │ │ │ \x04\x00\x00\x85\x00 \x00\x00\x00\x00\x00') │ └──────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────┘
[4]:
byteclass_inspect(mach64_hdr)
╭────────────────── Byteclass Inspect ──────────────────╮ │ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │ │ ----------------------------------------------------- │ │ 0x0 |cf fa ed fe|0c 00 00 01|00 00 00 00|02 00 00 00 │ │ 0x10 |11 00 00 00|20 04 00 00|85 00 20 00|00 00 00 00 │ │ 0x20 │ ╰───────────────────────────────────────────────────────╯
Legend ┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ ┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ magic │ b'\xcf\xfa\xed\xfe' │ │ cputype │ 16777228 │ │ cpusubtype │ 0 │ │ filetype │ 2 │ │ ncmds │ 17 │ │ sizeofcmds │ 1056 │ │ flags │ MachFlags32(10100001000000000000010000000000, flags={'NOUNDEFS': True, 'INCRLINK': False, │ │ │ 'DYLDLINK': True, 'BINDATLOAD': False, 'PREBOUND': False, 'SPLIT_SEGS': False, 'LAZY_INIT': False, │ │ │ 'TWOLEVEL': True, 'FORCE_FLAT': False, 'NOMULTIDEFS': False, 'NOFIXPREBINDING': False, │ │ │ 'PREBINDABLE': False, 'ALLMODSBOUND': False, 'SUBSECTIONS_VIA_SYMBOLS': False, 'CANONICAL': False, │ │ │ 'WEAK_DEFINES': False, 'BINDS_TO_WEAK': False, 'ALLOW_STACK_EXECUTION': False, 'ROOT_SAFE': False, │ │ │ 'SETUID_SAFE': False, 'NO_REEXPORTED_DYLIBS': False, 'PIE': True, 'DEAD_STRIPPABLE_DYLIB': False, │ │ │ 'HAS_TLV_DESCRIPTORS': False, 'NO_HEAP_EXECUTION': False, 'APP_EXTENSION_SAFE': False, │ │ │ 'NLIST_OUTOFSYNC_WITH_DYLDINFO': False, 'SIM_SUPPORT': False, 'DYLIB_IN_CACHE': False}) │ │ reserved │ 0 │ └────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────┘
[5]:
collection_table(mach64_hdr)
MachHdr64 ┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓ ┃ Member ┃ Value ┃ Data ┃ ┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩ │ magic │ b'\xcf\xfa\xed\xfe' │ b'\xcf\xfa\xed\xfe' │ │ cputype │ 16777228 │ b'\x0c\x00\x00\x01' │ │ cpusubtype │ 0 │ b'\x00\x00\x00\x00' │ │ filetype │ 2 │ b'\x02\x00\x00\x00' │ │ ncmds │ 17 │ b'\x11\x00\x00\x00' │ │ sizeofcmds │ 1056 │ b' \x04\x00\x00' │ │ flags │ MachFlags32(10100001000000000000010000000000, flags={'NOUNDEFS': True, │ b'\x85\x00 \x00' │ │ │ 'INCRLINK': False, 'DYLDLINK': True, 'BINDATLOAD': False, 'PREBOUND': False, │ │ │ │ 'SPLIT_SEGS': False, 'LAZY_INIT': False, 'TWOLEVEL': True, 'FORCE_FLAT': │ │ │ │ False, 'NOMULTIDEFS': False, 'NOFIXPREBINDING': False, 'PREBINDABLE': False, │ │ │ │ 'ALLMODSBOUND': False, 'SUBSECTIONS_VIA_SYMBOLS': False, 'CANONICAL': False, │ │ │ │ 'WEAK_DEFINES': False, 'BINDS_TO_WEAK': False, 'ALLOW_STACK_EXECUTION': │ │ │ │ False, 'ROOT_SAFE': False, 'SETUID_SAFE': False, 'NO_REEXPORTED_DYLIBS': │ │ │ │ False, 'PIE': True, 'DEAD_STRIPPABLE_DYLIB': False, 'HAS_TLV_DESCRIPTORS': │ │ │ │ False, 'NO_HEAP_EXECUTION': False, 'APP_EXTENSION_SAFE': False, │ │ │ │ 'NLIST_OUTOFSYNC_WITH_DYLDINFO': False, 'SIM_SUPPORT': False, │ │ │ │ 'DYLIB_IN_CACHE': False}) │ │ │ reserved │ 0 │ b'\x00\x00\x00\x00' │ └────────────┴──────────────────────────────────────────────────────────────────────────────┴─────────────────────┘
[6]:
mach64 = Mach64(data)
[7]:
table = Table(title="Mach64 Handler")
table.add_column("Name")
table.add_column("Value")
table.add_row("len(mach64)", str(len(mach64)))
table.add_row("str(mach64)", str(mach64))
table.add_row("repr(mach64)", repr(mach64))
table.add_row("mach64.data", str(mach64.data))
table.add_row("mach64.hdr", str(mach64.hdr))
table.add_row("mach64.magic", str(mach64.magic))
table.add_row("mach64.cpu_type", str(mach64.cpu_type))
table.add_row("mach64.cpu_subtype", str(mach64.cpu_subtype))
table.add_row("mach64.filetype", str(mach64.filetype))
table.add_row("mach64.num_cmds", str(mach64.num_cmds))
table.add_row("mach64.cmd_size", str(mach64.cmd_size))
table.add_row("mach64.flags", str(mach64.flags))
console = Console()
console.print(table)
Mach64 Handler ┏━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Name ┃ Value ┃ ┡━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ len(mach64) │ 33440 │ │ str(mach64) │ Mach64(magic=b'\xcf\xfa\xed\xfe') │ │ repr(mach64) │ Mach64(data=<memory at 0x7fdf866784c0>) │ │ mach64.data │ <memory at 0x7fdf866784c0> │ │ mach64.hdr │ MachHdr64(magic=DWord(data=b'\xcf\xfa\xed\xfe', byte_order=b'@'), cputype=Int32(16777228), │ │ │ cpusubtype=Int32(0), filetype=UInt32(2), ncmds=UInt32(17), sizeofcmds=UInt32(1056), │ │ │ flags=MachFlags32(data=b'\x85\x00 \x00'), reserved=UInt32(0)) │ │ mach64.magic │ b'\xcf\xfa\xed\xfe' │ │ mach64.cpu_type │ ARM64 │ │ mach64.cpu_subtype │ ARM64_ALL │ │ mach64.filetype │ 2 │ │ mach64.num_cmds │ 17 │ │ mach64.cmd_size │ 1056 │ │ mach64.flags │ {'NOUNDEFS': True, 'INCRLINK': False, 'DYLDLINK': True, 'BINDATLOAD': False, 'PREBOUND': │ │ │ False, 'SPLIT_SEGS': False, 'LAZY_INIT': False, 'TWOLEVEL': True, 'FORCE_FLAT': False, │ │ │ 'NOMULTIDEFS': False, 'NOFIXPREBINDING': False, 'PREBINDABLE': False, 'ALLMODSBOUND': │ │ │ False, 'SUBSECTIONS_VIA_SYMBOLS': False, 'CANONICAL': False, 'WEAK_DEFINES': False, │ │ │ 'BINDS_TO_WEAK': False, 'ALLOW_STACK_EXECUTION': False, 'ROOT_SAFE': False, 'SETUID_SAFE': │ │ │ False, 'NO_REEXPORTED_DYLIBS': False, 'PIE': True, 'DEAD_STRIPPABLE_DYLIB': False, │ │ │ 'HAS_TLV_DESCRIPTORS': False, 'NO_HEAP_EXECUTION': False, 'APP_EXTENSION_SAFE': False, │ │ │ 'NLIST_OUTOFSYNC_WITH_DYLDINFO': False, 'SIM_SUPPORT': False, 'DYLIB_IN_CACHE': False} │ └────────────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘