junk_aware_disasm.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. """
  3. 花指令感知递归反汇编器 (针对幸福BBA助手 EPL+壳变异镜像)
  4. 背景:
  5. - EPL(易语言)编译: 数据装载 mov eax/ebx, imm32 / push imm32, 库桥 call 0x639xxx
  6. - 壳变异: 字符串指针 imm32 的单字节被 XOR 污染(键集 00/07/20/23/3A/3E/71/A6/C0/D0/EA/FA)
  7. - 花指令: 随机垃圾字节穿插在指令流中, 线性反汇编失步
  8. - VA = 镜像偏移 + 0x401000
  9. 策略(锚点对齐 + 有界滑窗):
  10. 1. 扫描 push imm32 / mov r32,imm32, 容错解析 imm 为字符串 VA(单字节 XOR 修正)
  11. 2. 扫描 e8 call, 目标须落在镜像内
  12. 3. 从每个锚点(字符串引用/call)反向寻找最近的有效指令边界, 递归向两边扩展
  13. 4. 花指令字节(不构成有效装载/call/jcc 的孤立段)跳过并标注
  14. 输出:
  15. 每行: 偏移 | 指令 | 注解(字符串/call目标/花指令标记)
  16. """
  17. import sys
  18. import struct
  19. from capstone import Cs, CS_ARCH_X86, CS_MODE_32
  20. sys.stdout.reconfigure(encoding='utf-8', errors='replace')
  21. MIRROR = r'D:/视频去重/逆向分析/unpacked.bin'
  22. BASE = 0x401000
  23. XOR_KEYS = [0x00, 0x07, 0x20, 0x23, 0x3a, 0x3e, 0x71, 0xa6, 0xc0, 0xd0, 0xea, 0xfa]
  24. d = open(MIRROR, 'rb').read()
  25. md = Cs(CS_ARCH_X86, CS_MODE_32)
  26. def strings_table():
  27. """扫描镜像数据区, 提取可读字符串表: 偏移 -> 文本(>=3字符, ASCII/GBK)。"""
  28. table = {}
  29. i = 0
  30. n = len(d)
  31. while i < n:
  32. j = i
  33. while j < n and d[j] != 0:
  34. j += 1
  35. if j - i >= 3:
  36. raw = d[i:j]
  37. # ASCII 可读 or GBK 中文
  38. try:
  39. t = raw.decode('gbk')
  40. printable = sum(1 for c in t if c.isprintable())
  41. if printable / len(t) > 0.85 and len(t) >= 3:
  42. table[i] = t
  43. except (UnicodeDecodeError, ValueError):
  44. pass
  45. i = j + 1
  46. return table
  47. STRS = strings_table()
  48. def decode_imm32(pos):
  49. """pos 处 4 字节 imm32, 容错解析为字符串 VA。返回 (文本, 修正描述) 或 (None, None)。"""
  50. v = struct.unpack('<I', d[pos:pos+4])[0]
  51. off = v - BASE
  52. if off in STRS:
  53. return STRS[off], None
  54. for j in range(4):
  55. for k in XOR_KEYS:
  56. v2 = v ^ (k << (8*j))
  57. off2 = v2 - BASE
  58. if off2 in STRS:
  59. return STRS[off2], f'b{j}^{k:#04x}'
  60. return None, None
  61. def find_anchors(st, en):
  62. """锚点: (位置, 类型, 信息)。字符串装载与有效 call。"""
  63. anchors = []
  64. i = st
  65. while i < en - 5:
  66. b = d[i]
  67. # push imm32 / mov eax,imm32 (b8) / mov ebx,imold32 (bb) / be / bf / b9
  68. REG_MAP = {0xb8: 'eax', 0xb9: 'ecx', 0xbb: 'ebx', 0xbe: 'esi', 0xbf: 'edi'}
  69. if b == 0x68 or b in REG_MAP:
  70. s, corr = decode_imm32(i+1)
  71. if s and len(s) >= 2:
  72. reg = 'push' if b == 0x68 else f'mov {REG_MAP[b]},'
  73. anchors.append((i, 'str', (reg, s, corr)))
  74. elif b == 0xe8:
  75. rel = struct.unpack('<i', d[i+1:i+5])[0]
  76. tgt = i + 5 + rel
  77. if 0 <= tgt < len(d):
  78. anchors.append((i, 'call', tgt))
  79. i += 1
  80. return anchors
  81. def disasm_from(start, limit_addr, max_ins=400):
  82. """从 start 反汇编到 limit_addr, 返回指令列表; 遇到无法解码即停。"""
  83. out = []
  84. addr = start
  85. code = d[start:limit_addr]
  86. for ins in md.disasm(code, start):
  87. out.append(ins)
  88. if len(out) >= max_ins or ins.address + ins.size > limit_addr:
  89. break
  90. return out
  91. def recursive_parse(st, en, out=sys.stdout):
  92. """锚点间填充反汇编: 每对相邻锚点间, 尝试从多个对齐点反汇编, 取覆盖最大者。"""
  93. anchors = find_anchors(st, en)
  94. # 按锚点分段: 每段从锚点前 1-8 字节找指令头对齐
  95. points = [st] + [a[0] for a in anchors] + [en]
  96. lines = []
  97. covered = set()
  98. for idx, (pos, typ, info) in enumerate([(st, 'range', None)] + anchors):
  99. # 从锚点自身开始向后反汇编到下一锚点
  100. seg_start = pos
  101. seg_end = points[points.index(pos)+1] if pos in points else en
  102. if typ == 'str':
  103. reg, s, corr = info
  104. note = f'{reg} STR {s[:60]!r}' + (f' [corr {corr}]' if corr else '')
  105. lines.append((pos, note, 'STR'))
  106. elif typ == 'call':
  107. lines.append((pos, f'call {BASE+tgt_str(info)}', 'CALL'))
  108. # 简化: 直接输出锚点流 + 锚点间最短反汇编填充
  109. for pos, note, kind in lines:
  110. print(f'{pos:#08x} {note}', file=out)
  111. return anchors
  112. def tgt_str(off):
  113. return f'{BASE + off:#x}'
  114. def main():
  115. st, en = 0x22400, 0x22900
  116. print(f'=== 花指令感知反汇编 {st:#x}-{en:#x} ===')
  117. print(f'字符串表: {len(STRS)} 条')
  118. print()
  119. anchors = find_anchors(st, en)
  120. print(f'锚点数: {len(anchors)}')
  121. print()
  122. for pos, typ, info in anchors:
  123. if typ == 'str':
  124. reg, s, corr = info
  125. print(f'{pos:#08x} {reg:10} STR {s[:64]!r}' + (f' [corr {corr}]' if corr else ''))
  126. else:
  127. print(f'{pos:#08x} call -> {BASE+info:#x} (偏移 {info:#x})')
  128. if __name__ == '__main__':
  129. main()