Crusader_Decomp/disasm_helper.py

41 lines
1.4 KiB
Python
Raw Permalink Normal View History

import struct, os, sys
BIN_PATH = r'k:\ghidra\Crusader_Decomp\NE_segments\seg001_code_off_37600_len_8400.bin'
TARGET = 0x265B
with open(BIN_PATH, 'rb') as f:
f.seek(TARGET - 0x200)
data = f.read(0x280)
try:
import capstone
md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_16)
for ins in md.disasm(data, TARGET - 0x200):
print(' 0x%04x: %s %s' % (ins.address, ins.mnemonic, ins.op_str))
if ins.address > TARGET + 0x40:
break
except ImportError:
print('capstone not available, trying ndisasm...')
import subprocess, tempfile
tmp = os.path.join(os.environ.get('TEMP', '.'), 'seg001_chunk.bin')
with open(tmp, 'wb') as f2:
f2.write(data)
result = subprocess.run(
['ndisasm', '-b', '16', '-o', '0x%x' % (TARGET - 0x200), tmp],
capture_output=True, text=True, timeout=15
)
if result.returncode == 0:
for line in result.stdout.split('\n'):
try:
addr = int(line.split()[0], 16)
if TARGET - 0x200 <= addr <= TARGET + 0x40:
print(line)
except:
pass
else:
print('ndisasm failed:', result.stderr)
# Fallback: hex dump
offset = TARGET - 0x200
for i in range(0, len(data), 16):
hexb = ' '.join('%02x' % b for b in data[i:i+16])
print('0x%04x: %s' % (offset+i, hexb))