def test_MACHO_prebind_64(assertion): global log_history macho_64 = open(__dir__ + 'macho_64.out', 'rb').read() e = MACHO(macho_64) d = e.virt[0x100000f50:0x100000f62] assertion('structure definie\0', d.decode('latin1'), 'Extract chunk from mapped memory, in a section (64 bits)') e.virt[0x100000f50:0x100000f5c] = 'Hello World\0'.encode('latin1') d = e.pack() assertion('b29fe575093a6f68a54131e59138e1d8', hashlib.md5(d).hexdigest(), 'Writing in memory (interval) (64 bits)') e.virt[0x100000f50] = 'Hello World\0'.encode('latin1') d = e.pack() assertion('b29fe575093a6f68a54131e59138e1d8', hashlib.md5(d).hexdigest(), 'Writing in memory (address) (64 bits)') e.add( macho.Section(parent=macho.sectionHeader(parent=e.load), content='arbitrary content'.encode('latin1'))) d = e.pack() assertion('be836b2b8adcff60bcc7ca1d712a92a9', hashlib.md5(d).hexdigest(), 'Adding a section (64 bits)') e = MACHO(macho_64) e.add(type=macho.LC_SEGMENT_64, segname='__NEWTEXT', initprot=macho.VM_PROT_READ | macho.VM_PROT_EXECUTE, content='some binary data'.encode('latin1')) d = e.pack() assertion('b4ad381503c51b6dc9dc3d79fb8ca568', hashlib.md5(d).hexdigest(), 'Adding a segment (64 bits)')
def test_MACHO_virt(assertion): macho_32 = open(__dir__ + 'macho_32.out', 'rb').read() e = MACHO(macho_32) d = e.virt[0x1f9c:0x1fae] assertion('structure definie\0', d.decode('latin1'), 'Extract chunk from mapped memory, in a section (32 bits)') e.virt[0x1f9c] = 'Hello World\0'.encode('latin1') d = e.pack() assertion('16db05dfe60b5ac86c45d8324ef5cfc6', hashlib.md5(d).hexdigest(), 'Writing in memory (address) (32 bits)') e.virt[0x1f9c:0x1fa8] = 'Hello World\0'.encode('latin1') d = e.pack() assertion('16db05dfe60b5ac86c45d8324ef5cfc6', hashlib.md5(d).hexdigest(), 'Writing in memory (interval) (32 bits)') e.add( macho.Section(parent=macho.sectionHeader(parent=e.load), content='arbitrary content'.encode('latin1'))) d = e.pack() assertion('b61b686819bd3c94e765b220ef708353', hashlib.md5(d).hexdigest(), 'Adding a section (32 bits)')
def test_MACHO_prebind_32(assertion): global log_history macho_32 = open(__dir__ + 'macho_32.out', 'rb').read() e = MACHO(macho_32) e.add(macho.LoadCommand(sex='<', wsize=32, cmd=0)) d = e.pack() assertion('6fefeaf7b4de67f8270d3425942d7a97', hashlib.md5(d).hexdigest(), 'Adding an empty command (32 bits)') f = struct.pack("<III", macho.LC_ROUTINES_64, 12, 0) l = macho.prebind_cksum_command(parent=None, sex='<', wsize=32, content=f) assertion( [('warn', ('Incoherent input cmd=%#x for %s', 26, 'prebind_cksum_command'), {})], log_history, 'Parsing incoherent load command prebind_cksum_command with LC_ROUTINES_64 tag (logs)' ) log_history = [] assertion( f, l.pack(), 'Creating a LC_PREBIND_CKSUM (with content and incoherent subclass)') f = struct.pack("<III", macho.LC_PREBIND_CKSUM, 12, 0) l = macho.prebind_cksum_command(parent=None, sex='<', wsize=32, content=f) assertion(f, l.pack(), 'Creating a LC_PREBIND_CKSUM (with content and subclass)') l = macho.LoadCommand(parent=None, sex='<', wsize=32, content=f) assertion(f, l.pack(), 'Creating a LC_PREBIND_CKSUM (from "content")') l = macho.LoadCommand(sex='<', wsize=32, cmd=macho.LC_PREBIND_CKSUM) assertion(f, l.pack(), 'Creating a LC_PREBIND_CKSUM (from "cmd")') e = MACHO(macho_32) e.add(l) d = e.pack() assertion('d7a33133a04126527eb6d270990092fa', hashlib.md5(d).hexdigest(), 'Adding a LC_PREBIND_CKSUM command') e = MACHO(macho_32) e.add(type=macho.LC_SEGMENT, segname='__NEWTEXT', initprot=macho.VM_PROT_READ | macho.VM_PROT_EXECUTE, content='some binary data'.encode('latin1')) d = e.pack() assertion('c4ad6da5422642cb15b91ccd3a09f592', hashlib.md5(d).hexdigest(), 'Adding a segment (32 bits)')