def test_hexdump(): offset = 57005 data = b'fuckyou\0\0\0\0' * 2 left_items = ["{:04x}".format(offset + i) for i in range(0, len(data), 16)] center_items = [ "{:02x}".format(item) for item in bytearray(data) ] center_items += [' '] * (16 - len(data) % 16) right_items = utils.printable(data) + ' ' * (16 - len(data) % 16) center_chunks = [] for i in range(0, len(center_items), 8): res = [] for j in range(0, 8): res.append(center_items[i + j]) center_chunks.append(res) left_iterable = iter(left_items) center_iterable = iter(center_chunks) right_iterable = iter(right_items) res = [] res.append(' '.join([next(left_iterable), ' '.join(next(center_iterable)), ' '.join(next(center_iterable)), str().join(itertools.islice(right_iterable, 16))])) res.append(' '.join([next(left_iterable), ' '.join(next(center_iterable)), ' '.join(next(center_iterable)), str().join(itertools.islice(right_iterable, 16))])) if utils.hexdump(data, offset=offset, width=16) == '\n'.join(res): raise Success
def do_diff_friendly(A, B): (a, inputa), (b, inputb) = A, B for o,l in getDifferences(a, b): print('\nDifference located at %08x:%08x'% (o, o+l)) o = (o) & ~0xf l = (l+0xf) & ~0xf # collect the actual file data ## it sucks that we're double-reading...oh well inputa.seek(o) inputb.seek(o) left = inputa.read(l) right = inputb.read(l) left, right = utils.hexdump(left, o), utils.hexdump(right, o) res = [ left, right ] res = [ s.split('\n') for s in res ] rows = [ ' | '.join(x) for x in zip(*res) ] print('\n'.join(rows))
def dumpValue(self, indent=''): # XXX: this code sucks symbols = iter(self['symbols'].value) def consume(iterable, count): return [iterable.next() for x in xrange(count)] res = [ord(x) for x in self['count'].value] counts = res res = [consume(symbols, x) for x in res] codes = res res = [ indent+'codes of length[%d] bits (%d total): %s'% (index, len(code), utils.hexdump(''.join(code))) for ((index, code), count) in zip(enumerate(codes), counts)] return '\n'.join(res)
def processexecutable(filename, address): # globals for interpretive use global mz, pe, imagebase, sections, datadirectory print('Query: %x\n' % address) print('Module: %s' % os.path.basename(filename)) print('ImageBase: %x' % imagebase) # try exe header first mz.setoffset(imagebase, recurse=True) if mz.contains(address): result = mz for item in mz.traverse(traverse_address(address)): x = item.__name__ print(rightjustify('------- %s' % x, 70, '-')) result = result[int(x) if isinstance(result, ptypes.parray.type ) else x] print(result) # try sections else: mz.setoffset(0, recurse=True) va = address - imagebase s = pe['Sections'].getsectionbyaddress(va) offset = va - s['VirtualAddress'].int() data = s['PointerToRawData'].d.load().serialize() left = offset - 8 left &= ~0xf right = left + 0x30 if left < 0: left = 0 if right > len(data): right = len(data) sectionname = s['Name'].get() print(rightjustify(' section %s' % sectionname, 76, '-')) print( utils.hexdump(data[left:right], offset=s['VirtualAddress'].int() + offset + imagebase)) mz.setoffset(0, recurse=True) return
def dumpValue(self): return utils.hexdump(self['value'].value, length=8)
def do_diff_friendly((a, inputa), (b, inputb)): for o, l in getDifferences(a, b): print '\nDifference located at %08x:%08x' % (o, o + l) o = (o) & ~0xf l = (l + 0xf) & ~0xf # collect the actual file data ## it sucks that we're double-reading...oh well inputa.seek(o) inputb.seek(o) left = inputa.read(l) right = inputb.read(l) left, right = utils.hexdump(left, o), utils.hexdump(right, o) res = [left, right] res = [s.split('\n') for s in res] rows = [' | '.join(x) for x in zip(*res)] print '\n'.join(rows) def do_diff_generate((a, inputa), (b, inputb), template='%s.diff'): assert '%s' in template, "Filename template `%s' needs a %%s for modification range" % template inputa.seek(0) inputb.seek(0) original = inputa.read() newer = array.array('c', inputb.read()) inputa.seek(0)
def do_diff_friendly( (a,inputa), (b,inputb) ): for o,l in getDifferences(a, b): print '\nDifference located at %08x:%08x'% (o, o+l) o = (o) & ~0xf l = (l+0xf) & ~0xf # collect the actual file data ## it sucks that we're double-reading...oh well inputa.seek(o) inputb.seek(o) left = inputa.read(l) right = inputb.read(l) left, right = utils.hexdump(left, o), utils.hexdump(right, o) res = [ left, right ] res = [ s.split('\n') for s in res ] rows = [ ' | '.join(x) for x in zip(*res) ] print '\n'.join(rows) def do_diff_generate((a,inputa),(b,inputb), template='%s.diff'): assert '%s' in template, "Filename template `%s' needs a %%s for modification range"% template inputa.seek(0); inputb.seek(0) original = inputa.read() newer = array.array('c', inputb.read()) inputa.seek(0); inputb.seek(0) count = 0