import sys, capstone
sys.stdout.reconfigure(encoding='utf-8')

DRV = r'E:\ios\work319\patch_src\Payload\Driver.app\Driver'
BI  = r'E:\ios\work319\patch_src\Payload\Driver.app\Frameworks\build-info.framework\build-info'

md = capstone.Cs(capstone.CS_ARCH_ARM64, capstone.CS_MODE_ARM)
md.detail = False

def dis(path, start, end, label, base=0):
    d = open(path, 'rb').read()
    print('\n===== %s  %#x-%#x =====' % (label, start, end))
    for ins in md.disasm(d[start:end], start):
        addr = base + ins.address
        tgt = ''
        if ins.mnemonic in ('b', 'bl', 'cbz', 'cbnz', 'tbz', 'tbnz') and ins.op_str:
            # last operand is usually the target
            parts = ins.op_str.split(', ')
            last = parts[-1]
            try:
                t = int(last, 0)
                tgt = '  -> %#x' % (base + t)
            except Exception:
                pass
        print('  %#010x  %-8s %-28s%s' % (addr, ins.mnemonic, ins.op_str, tgt))

dis(DRV, 0x31e00, 0x32900, 'Driver 0x31e00-0x32900', 0x100000000)

chain = [0x4cb4c, 0x4d458, 0x2051cc, 0x2a2010, 0x2965fc, 0x2c996c, 0x2c5a48]
for a in chain:
    lo = max(0, a - 0x80)
    hi = a + 0x180
    dis(BI, lo, hi, 'build-info around +%#x' % a, 0)
