# 带符号的反汇编:按 dynsym 定义符号与 PLT 导入名标注 bl/b/adrp 目标。 # 用法:sdis.py <原映像 .orig.bin> <符号名|0x偏移> [条数=200] # sdis.py <原映像> --sym 0x偏移... 偏移 → 函数+偏移 # sdis.py <原映像> --xref 0x地址... 谁用 adrp+add 引用了该地址 # sdis.py <原映像> --fn 0x地址... 所在函数起点(eh_frame 函数表) # sdis.py <原映像> --find <子串> 列出名字含子串的定义符号 # sdis.py <原映像> --callers <符号名> 列出直接 bl/b 到该符号的调用点 # Disassembly with symbols: annotates bl/b/adrp targets with dynsym defined symbols and PLT import names. # Usage: sdis.py [count=200] # sdis.py --sym 0xoffset... offset → function+offset # sdis.py --xref 0xaddr... who references this address with adrp+add # sdis.py --fn 0xaddr... start of the containing function (eh_frame function table) # sdis.py --find list defined symbols whose name contains the substring # sdis.py --callers list call sites that directly bl/b to this symbol import struct, sys, bisect from capstone import * from capstone.arm64 import ARM64_OP_IMM img = open(sys.argv[1], 'rb').read() md = Cs(CS_ARCH_ARM64, CS_MODE_ARM); md.detail = True m = struct.unpack_from('> 32) * 24)[0]) plt = {} def plt_scan(): lo = t[23] # PLT 紧随 text 末尾附近;直接扫 text 里 adrp x16 序列 | The PLT is right near the end of text; scan text directly for adrp x16 sequences for o in range(0, len(img) - 16, 4): w = struct.unpack_from(' --sym 0x偏移... 逐个给出所在函数 | sdis.py --sym 0xoffset... give the containing function for each one plt_scan() for a in sys.argv[3:]: print(a, name(int(a, 16))) sys.exit() if sys.argv[2] == '--xref': # sdis.py <映像> --xref 0x地址... 找 adrp+add/ldr 拼出该地址的指令 | sdis.py --xref 0xaddr... find the instructions that build this address with adrp+add/ldr want = {int(a, 16) for a in sys.argv[3:]} textend = max(v + s for v, s in defs.values() if v < 0x10000000 and s) for o in range(0, min(len(img), textend), 4): w = struct.unpack_from('> 5) & 0x7FFFF) << 2) | ((w >> 29) & 3) if imm & (1 << 20): imm -= 1 << 21 page = (o & ~0xFFF) + (imm << 12) for i in md.disasm(img[o + 4:o + 24], o + 4): ops = i.operands if i.mnemonic == 'add' and len(ops) == 3 and ops[1].reg and i.reg_name(ops[1].reg) == f'x{rd}' and ops[2].type == ARM64_OP_IMM: if page + ops[2].imm in want: print(f'{i.address:#x} {name(i.address)} -> {page + ops[2].imm:#x}') break sys.exit() if sys.argv[2] == '--fn': # sdis.py <映像> --fn 0x地址... 所在函数起点(eh_frame) | sdis.py --fn 0xaddr... start of the containing function (eh_frame) st = fn_starts() for a in sys.argv[3:]: a = int(a, 16); k = bisect.bisect_right(st, a) - 1 if k < 0 or k + 1 >= len(st): print(f'{a:#x} 不在函数表'); continue # 首函数前 / 末函数后(如改写区):单条报,不连累整批 | Before the first function / after the last function (e.g. the rewrite area): report it on its own, without dragging down the whole batch print(f'{a:#x} 函数 {st[k]:#x} +{a - st[k]:#x} 下一函数 {st[k + 1]:#x}') sys.exit() if sys.argv[2] == '--find': for n, (v, sz) in sorted(defs.items(), key=lambda x: x[1]): if sys.argv[3] in n: print(f'{v:#x} {sz:#x} {n}') sys.exit() if sys.argv[2] == '--callers': tgt = resolve(sys.argv[3]); textend = max(v + s for v, s in defs.values() if v < 0x10000000 and s) # 粗上界 | Coarse upper bound for o in range(0, min(len(img), textend), 4): w = struct.unpack_from('> 26 in (0x25, 0x05) and o + ((((w & 0x3ffffff) ^ 0x2000000) - 0x2000000) * 4) == tgt: print(f'{o:#x} {name(o)}') sys.exit() plt_scan() a = resolve(sys.argv[2]); n = int(sys.argv[3]) if len(sys.argv) > 3 else 200 for ins in md.disasm(img[a:a + n * 4], a): ann = '' if ins.mnemonic in ('bl', 'b') or ins.mnemonic.startswith('b.') or ins.mnemonic in ('cbz', 'cbnz', 'tbz', 'tbnz', 'adr'): op = ins.operands[-1] if op.type == ARM64_OP_IMM: ann = ' ; ' + name(op.imm) print(f'{ins.address:#9x} {ins.mnemonic:8s} {ins.op_str}{ann}') if ins.mnemonic == 'ret' and n > 60 and ins.address > a + 8 and len(sys.argv) <= 3: break