| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- # -*- coding: utf-8 -*-
- """
- 花指令感知递归反汇编器 (针对幸福BBA助手 EPL+壳变异镜像)
- 背景:
- - EPL(易语言)编译: 数据装载 mov eax/ebx, imm32 / push imm32, 库桥 call 0x639xxx
- - 壳变异: 字符串指针 imm32 的单字节被 XOR 污染(键集 00/07/20/23/3A/3E/71/A6/C0/D0/EA/FA)
- - 花指令: 随机垃圾字节穿插在指令流中, 线性反汇编失步
- - VA = 镜像偏移 + 0x401000
- 策略(锚点对齐 + 有界滑窗):
- 1. 扫描 push imm32 / mov r32,imm32, 容错解析 imm 为字符串 VA(单字节 XOR 修正)
- 2. 扫描 e8 call, 目标须落在镜像内
- 3. 从每个锚点(字符串引用/call)反向寻找最近的有效指令边界, 递归向两边扩展
- 4. 花指令字节(不构成有效装载/call/jcc 的孤立段)跳过并标注
- 输出:
- 每行: 偏移 | 指令 | 注解(字符串/call目标/花指令标记)
- """
- import sys
- import struct
- from capstone import Cs, CS_ARCH_X86, CS_MODE_32
- sys.stdout.reconfigure(encoding='utf-8', errors='replace')
- MIRROR = r'D:/视频去重/逆向分析/unpacked.bin'
- BASE = 0x401000
- XOR_KEYS = [0x00, 0x07, 0x20, 0x23, 0x3a, 0x3e, 0x71, 0xa6, 0xc0, 0xd0, 0xea, 0xfa]
- d = open(MIRROR, 'rb').read()
- md = Cs(CS_ARCH_X86, CS_MODE_32)
- def strings_table():
- """扫描镜像数据区, 提取可读字符串表: 偏移 -> 文本(>=3字符, ASCII/GBK)。"""
- table = {}
- i = 0
- n = len(d)
- while i < n:
- j = i
- while j < n and d[j] != 0:
- j += 1
- if j - i >= 3:
- raw = d[i:j]
- # ASCII 可读 or GBK 中文
- try:
- t = raw.decode('gbk')
- printable = sum(1 for c in t if c.isprintable())
- if printable / len(t) > 0.85 and len(t) >= 3:
- table[i] = t
- except (UnicodeDecodeError, ValueError):
- pass
- i = j + 1
- return table
- STRS = strings_table()
- def decode_imm32(pos):
- """pos 处 4 字节 imm32, 容错解析为字符串 VA。返回 (文本, 修正描述) 或 (None, None)。"""
- v = struct.unpack('<I', d[pos:pos+4])[0]
- off = v - BASE
- if off in STRS:
- return STRS[off], None
- for j in range(4):
- for k in XOR_KEYS:
- v2 = v ^ (k << (8*j))
- off2 = v2 - BASE
- if off2 in STRS:
- return STRS[off2], f'b{j}^{k:#04x}'
- return None, None
- def find_anchors(st, en):
- """锚点: (位置, 类型, 信息)。字符串装载与有效 call。"""
- anchors = []
- i = st
- while i < en - 5:
- b = d[i]
- # push imm32 / mov eax,imm32 (b8) / mov ebx,imold32 (bb) / be / bf / b9
- REG_MAP = {0xb8: 'eax', 0xb9: 'ecx', 0xbb: 'ebx', 0xbe: 'esi', 0xbf: 'edi'}
- if b == 0x68 or b in REG_MAP:
- s, corr = decode_imm32(i+1)
- if s and len(s) >= 2:
- reg = 'push' if b == 0x68 else f'mov {REG_MAP[b]},'
- anchors.append((i, 'str', (reg, s, corr)))
- elif b == 0xe8:
- rel = struct.unpack('<i', d[i+1:i+5])[0]
- tgt = i + 5 + rel
- if 0 <= tgt < len(d):
- anchors.append((i, 'call', tgt))
- i += 1
- return anchors
- def disasm_from(start, limit_addr, max_ins=400):
- """从 start 反汇编到 limit_addr, 返回指令列表; 遇到无法解码即停。"""
- out = []
- addr = start
- code = d[start:limit_addr]
- for ins in md.disasm(code, start):
- out.append(ins)
- if len(out) >= max_ins or ins.address + ins.size > limit_addr:
- break
- return out
- def recursive_parse(st, en, out=sys.stdout):
- """锚点间填充反汇编: 每对相邻锚点间, 尝试从多个对齐点反汇编, 取覆盖最大者。"""
- anchors = find_anchors(st, en)
- # 按锚点分段: 每段从锚点前 1-8 字节找指令头对齐
- points = [st] + [a[0] for a in anchors] + [en]
- lines = []
- covered = set()
- for idx, (pos, typ, info) in enumerate([(st, 'range', None)] + anchors):
- # 从锚点自身开始向后反汇编到下一锚点
- seg_start = pos
- seg_end = points[points.index(pos)+1] if pos in points else en
- if typ == 'str':
- reg, s, corr = info
- note = f'{reg} STR {s[:60]!r}' + (f' [corr {corr}]' if corr else '')
- lines.append((pos, note, 'STR'))
- elif typ == 'call':
- lines.append((pos, f'call {BASE+tgt_str(info)}', 'CALL'))
- # 简化: 直接输出锚点流 + 锚点间最短反汇编填充
- for pos, note, kind in lines:
- print(f'{pos:#08x} {note}', file=out)
- return anchors
- def tgt_str(off):
- return f'{BASE + off:#x}'
- def main():
- st, en = 0x22400, 0x22900
- print(f'=== 花指令感知反汇编 {st:#x}-{en:#x} ===')
- print(f'字符串表: {len(STRS)} 条')
- print()
- anchors = find_anchors(st, en)
- print(f'锚点数: {len(anchors)}')
- print()
- for pos, typ, info in anchors:
- if typ == 'str':
- reg, s, corr = info
- print(f'{pos:#08x} {reg:10} STR {s[:64]!r}' + (f' [corr {corr}]' if corr else ''))
- else:
- print(f'{pos:#08x} call -> {BASE+info:#x} (偏移 {info:#x})')
- if __name__ == '__main__':
- main()
|