generate_artifacts.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. """
  3. 固化逆向分析产物:从解包镜像生成全量字符串/URL/SQL/函数入口候选/关键标记索引。
  4. 可重复运行,产物全部写入本目录。源数据: /tmp/unpacked.bin (由 ../unpack_stub.py 从原始zip静态解出)
  5. """
  6. import hashlib
  7. import re
  8. import sys
  9. import zipfile
  10. import os
  11. from pathlib import Path
  12. sys.stdout.reconfigure(encoding='utf-8', errors='replace')
  13. HERE = Path(__file__).parent
  14. SRC = Path(os.environ.get('TEMP', r'C:/Users/20175/AppData/Local/Temp')) / 'unpacked.bin'
  15. d = SRC.read_bytes()
  16. print(f'源镜像: {SRC} {len(d):,} bytes sha256={hashlib.sha256(d).hexdigest()[:16]}...')
  17. # ---------- 1. 落盘解包镜像 + zip 备份 ----------
  18. (HERE / 'unpacked.bin').write_bytes(d)
  19. with zipfile.ZipFile(HERE / 'unpacked.bin.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=6) as zf:
  20. zf.write(HERE / 'unpacked.bin', 'unpacked.bin')
  21. # ---------- 2. 字符串全量 ----------
  22. def gbk_strings(b):
  23. out = []
  24. for m in re.finditer(rb'(?:[\x20-\x7e\xa1-\xfe]{4,})', b):
  25. try:
  26. s = m.group().decode('gbk')
  27. if any('\u4e00' <= c <= '\u9fff' for c in s):
  28. out.append((m.start(), s))
  29. except Exception:
  30. pass
  31. return out
  32. G = gbk_strings(d)
  33. A = [(m.start(), m.group().decode('ascii', 'replace'))
  34. for m in re.finditer(rb'[\x20-\x7e]{6,}', d)]
  35. with open(HERE / 'strings_gbk.txt', 'w', encoding='utf-8') as f:
  36. for off, s in G:
  37. f.write(f'{off:#010x} {s}\n')
  38. with open(HERE / 'strings_ascii.txt', 'w', encoding='utf-8') as f:
  39. for off, s in A:
  40. f.write(f'{off:#010x} {s}\n')
  41. print(f'GBK中文串 {len(G):,} 条 -> strings_gbk.txt')
  42. print(f'ASCII串 {len(A):,} 条 -> strings_ascii.txt')
  43. # ---------- 3. URL/域名清单(带偏移) ----------
  44. urls = []
  45. for off, s in A + G:
  46. for m in re.findall(r'https?://[^\s"\'<>]+', s):
  47. urls.append((off, m[:120]))
  48. for m in re.findall(r'\b(?:[a-z0-9-]+\.)+(?:com|cn|net|top|xyz|vip|cc|shop|icu|io|me)(?::\d+)?\b', s, re.I):
  49. urls.append((off, m.lower()))
  50. urls = sorted(set(urls))
  51. with open(HERE / 'urls.txt', 'w', encoding='utf-8') as f:
  52. for off, u in urls:
  53. f.write(f'{off:#010x} {u}\n')
  54. print(f'URL/域名 {len(urls)} 条 -> urls.txt')
  55. # ---------- 4. SQL 语句清单 ----------
  56. sqls = set()
  57. for m in re.findall(rb'(?:(?:select|insert|update|delete|create)[\x20-\x7e]{6,})', d, re.I):
  58. sqls.add(m.decode('ascii', 'replace'))
  59. with open(HERE / 'sql.txt', 'w', encoding='utf-8') as f:
  60. f.write('\n'.join(sorted(sqls)))
  61. print(f'SQL 语句 {len(sqls)} 条 -> sql.txt')
  62. # ---------- 5. 函数入口候选(EPL特征码) ----------
  63. # EPL 初始化入口: cld; fninit (ELangPatcher README 证实的特征)
  64. # EPL 控件事件: call dword[ebp-4]
  65. # EPL 子程序常见序言: push ebp; mov ebp,esp 且尾部 ret 前有 lib call
  66. marks = {}
  67. marks['cld_fninit(FC DB E3)'] = [m.start() for m in re.finditer(rb'\xfc\xdb\xe3', d)]
  68. marks['call_[ebp-4](FF 55 FC)'] = [m.start() for m in re.finditer(rb'\xff\x55\xfc', d)]
  69. marks['push_ebp_mov_ebp_esp'] = [m.start() for m in re.finditer(rb'\x55\x8b\xec', d)]
  70. marks['int3_padding(CC x4)'] = [m.start() for m in re.finditer(rb'\xcc\xcc\xcc\xcc', d)]
  71. with open(HERE / 'function_candidates.txt', 'w', encoding='utf-8') as f:
  72. for name, offs in marks.items():
  73. f.write(f'=== {name}: {len(offs)} 处 ===\n')
  74. f.write(', '.join(f'{o:#x}' for o in offs[:400]) + '\n\n')
  75. for name, offs in marks.items():
  76. print(f'{name}: {len(offs)} 处')
  77. # ---------- 6. 关键标记索引(下一阶段导航用) ----------
  78. KEYS = ['122.51.38.41', 'datawuyou', 'doulaoban', 'ksapisrv', 'kuaishouzt', 'kwaixiaodian',
  79. 'niu.e.kuaishou.com', 'kuaishoupay', 'passToken', 'upload', 'fragment', 'anchor',
  80. 'publishvideo', 'jihua', 'money', 'ips', 'cookie1', '磁力', '充值', '爆单',
  81. '上热门', '抖老板', '云推', '挂车', '置顶', '定时', '监控', '投递', '标题',
  82. 'biaoti', 'download', 'http_', 'POST', 'Content-Type']
  83. with open(HERE / 'markers_index.txt', 'w', encoding='utf-8') as f:
  84. for k in KEYS:
  85. kb = k.encode('gbk', errors='ignore') or k.encode()
  86. offs = [m.start() for m in re.finditer(re.escape(kb), d)]
  87. f.write(f'{k}: {len(offs)} 处 {", ".join(hex(o) for o in offs[:12])}\n')
  88. print('关键标记索引 -> markers_index.txt')
  89. # ---------- 7. 产物清单与哈希 ----------
  90. print('\n=== 产物清单 ===')
  91. for p in sorted(HERE.iterdir()):
  92. if p.is_file():
  93. h = hashlib.md5(p.read_bytes()).hexdigest()[:12]
  94. print(f' {p.name:<28} {p.stat().st_size:>12,} bytes md5:{h}')