Skip to content

Instantly share code, notes, and snippets.

@hlandau

hlandau/dsm2s.py Secret

Created December 5, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hlandau/951b39f8d2acc92d8a6d0ea9511e63f3 to your computer and use it in GitHub Desktop.
Save hlandau/951b39f8d2acc92d8a6d0ea9511e63f3 to your computer and use it in GitHub Desktop.
Script to convert retdec disassembly output into GNU as input
#!/usr/bin/env python3
import sys, argparse, re
#0x438e1d: 8b 45 84 mov eax, dword ptr [ebp - 0x7c]
#0x402a4c: cc cc cc cc |.... |
re_line = re.compile(r'''^0x([0-9a-f]+): ((?:[0-9a-f][0-9a-f] ?){1,}) *\t(.*)$''')
re_hex = re.compile(r'''^0x([0-9a-f]+): ((?:[0-9a-f?][0-9a-f?] ?){1,}) *\|(.{16})\|(.*)$''')
def run():
ap = argparse.ArgumentParser()
ap.add_argument('input-file')
args = vars(ap.parse_args())
with open(args['input-file'], 'r') as f:
for L in f:
L = L.rstrip('\r\n')
if L == '':
continue
if L.startswith('; section: '):
secName = L[11:]
print('.section %s' % secName)
continue
if L[0] == ';':
print('#'+L[1:])
continue
m = re_line.match(L)
if m:
addr = m.group(1)
mcode = ['0x'+x for x in m.group(2).strip(' ').replace(' ',' ').split(' ')]
disasm = m.group(3)
s = '.byte %s' % ','.join(mcode)
s = s.ljust(62,' ')
s += '#%s; %s' % (addr, disasm)
print(s)
continue
m = re_hex.match(L)
if m:
addr = m.group(1)
mcode = ['0x'+x for x in m.group(2).strip(' ').replace(' ',' ').split(' ')]
hexdump = m.group(3)
comment = m.group(4)
s = '.byte %s' % ','.join(mcode)
s = s.ljust(90,' ')
s += '#%s;|%s|%s' % (addr, hexdump, comment)
print(s)
continue
print('NOMATCH: %s' % L)
return 0
if __name__ == '__main__':
sys.exit(run())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment