| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- # -*- coding: utf-8 -*-
- """
- 差分测试:Unicorn 模拟执行 stub 里的 NRV2E 解压函数本体,与手写 Python 解压器输出逐字节对比。
- 纯内存模拟:只执行解压循环(纯算术+内存),不触任何 API,不运行样本。
- """
- import glob
- import sys
- import zipfile
- sys.stdout.reconfigure(encoding='utf-8', errors='replace')
- from unicorn import Uc, UC_ARCH_X86, UC_MODE_32, UC_HOOK_CODE
- from unicorn.x86_const import (UC_X86_REG_EAX, UC_X86_REG_ESI, UC_X86_REG_EDI,
- UC_X86_REG_EBX, UC_X86_REG_ESP)
- zp = next(z for z in glob.glob(r'C:/Users/20175/Downloads/*.zip') if '3.24' in z.lower())
- d = zipfile.ZipFile(zp).read('幸福BBA助手3.24/幸福BBA助手3.24.exe')
- SRC_FILE_OFF = 0x1004
- CLEN = 0x9fc65a
- USIZE = 0x1014000
- STUB_FILE_OFF = 0x9fd000
- STUB_VA = 0x1e7f000
- ENTRY = 0x1e80112
- STOP = 0x1e80572 # EOF成功路径与失败路径都会经过这里
- SRC_VA = 0x10000000
- DST_VA = 0x20000000
- STACK_VA = 0x30000000
- uc = Uc(UC_ARCH_X86, UC_MODE_32)
- uc.mem_map(STUB_VA, 0x4000)
- uc.mem_map(SRC_VA, ((CLEN + 0xFFFF) // 0x1000 + 2) * 0x1000)
- uc.mem_map(DST_VA, ((USIZE + 0xFFFF) // 0x1000 + 2) * 0x1000)
- uc.mem_map(STACK_VA, 0x200000)
- uc.mem_write(STUB_VA, d[STUB_FILE_OFF:STUB_FILE_OFF + 0x2000])
- uc.mem_write(SRC_VA, d[SRC_FILE_OFF:SRC_FILE_OFF + CLEN])
- ESP = STACK_VA + 0x100000
- uc.mem_write(ESP + 0x34, USIZE.to_bytes(4, 'little')) # usize
- uc.mem_write(ESP + 0x3c, DST_VA.to_bytes(4, 'little')) # dst缓冲
- uc.mem_write(ESP + 0x2c, (0).to_bytes(4, 'little'))
- uc.mem_write(ESP + 0x10, (STACK_VA + 0x180000).to_bytes(4, 'little')) # 块表(本阶段不用)
- uc.reg_write(UC_X86_REG_ESI, SRC_VA)
- uc.reg_write(UC_X86_REG_EDI, CLEN)
- uc.reg_write(UC_X86_REG_EBX, USIZE) # 0x1e800e9 已设好,此处等价预置
- uc.reg_write(UC_X86_REG_ESP, ESP)
- print('模拟解压函数本体...', flush=True)
- uc.emu_start(ENTRY, STOP, timeout=0, count=0)
- print(f'停止于 RIP={uc.reg_read(UC_X86_REG_EAX):#x}(EOF路径=已产出字节数)')
- emu_out = bytes(uc.mem_read(DST_VA, USIZE))
- mine = open(r'C:/Users/20175/AppData/Local/Temp/unpacked.bin', 'rb').read()
- open(r'C:/Users/20175/AppData/Local/Temp/emu_unpacked.bin', 'wb').write(emu_out)
- diffs = [i for i in range(USIZE) if emu_out[i] != mine[i]]
- print(f'模拟产出 {len(emu_out):,} 字节; 与手写解压器差异数: {len(diffs):,}')
- for i in diffs[:30]:
- print(f' @{i:#x} 模拟={emu_out[i]:02x} 手写={mine[i]:02x}')
- if not diffs:
- print('>>> 完全一致:手写解压器忠实于 stub,变异字节是原数据自带 <<<')
|