| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- # -*- coding: utf-8 -*-
- """纯静态内存分析:从 zip 直接读取 exe 字节做 PE/字符串/行为特征分析,不落地、不执行。"""
- import glob
- import hashlib
- import re
- import struct
- import sys
- import zipfile
- import zlib
- import pefile
- sys.stdout.reconfigure(encoding='utf-8', errors='replace')
- # ---------- 1. 从 zip 内存中读取 exe ----------
- zip_path = next(z for z in glob.glob(r'C:/Users/20175/Downloads/*.zip')
- if '3.24' in z.lower() or 'BBA' in z.lower())
- zf = zipfile.ZipFile(zip_path)
- exe_name = None
- for n in zf.namelist():
- if n.lower().endswith('.exe'):
- exe_name = n # 取最后一个 .exe,主程序按体积最大再校验
- data = zf.read(exe_name)
- print(f'来源: {zip_path} -> {exe_name}')
- print(f'大小: {len(data):,} bytes')
- print(f'MD5 : {hashlib.md5(data).hexdigest()}')
- print(f'SHA256: {hashlib.sha256(data).hexdigest()}')
- # ---------- 2. PE 结构 ----------
- pe = pefile.PE(data=data)
- print('\n=== PE 段表(熵>7.2 通常意味着加壳/加密) ===')
- for s in pe.sections:
- name = s.Name.decode(errors='ignore').strip('\x00')
- print(f' {name:<10} raw={s.SizeOfRawData:>10,} entropy={s.get_entropy():.2f}')
- print('\n=== 导入表与可疑 API ===')
- all_imports = []
- if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
- for entry in pe.DIRECTORY_ENTRY_IMPORT:
- dll = entry.dll.decode(errors='ignore')
- funcs = [imp.name.decode(errors='ignore') for imp in entry.imports if imp.name]
- all_imports += funcs
- hot = [f for f in funcs if re.search(
- r'Crypt|Http|Internet|Url|Socket|Hook|VirtualAlloc|WriteProcessMemory|'
- r'CreateProcess|ShellExecute|WinExec|RegSet|Connect|Send|Recv|Track', f, re.I)]
- if hot:
- print(f' [{dll}] 可疑: {", ".join(hot[:12])}')
- # ---------- 3. 字符串提取(ASCII / UTF-16 / GBK中文) ----------
- def extract_urls(strs):
- found = set()
- for s in strs:
- for m in re.findall(r'https?://[^\s"\'<>{}|\\^`\[\]]+', s):
- found.add(m.rstrip(').,;'))
- for m in re.findall(r'\b(?:[a-z0-9-]+\.)+(?:com|cn|net|top|xyz|vip|icu|cc|shop|fun|site|online|club|link|me|io)(?::\d+)?\b',
- s, re.I):
- found.add(m.lower())
- return sorted(found)
- A = [m.decode('ascii') for m in re.findall(rb'[\x20-\x7e]{6,}', data)]
- U = [m.decode('utf-16-le', errors='ignore')
- for m in re.findall(rb'(?:[\x20-\x7e]\x00){6,}', data)]
- G = []
- for m in re.findall(rb'(?:[\x20-\x7e\xa1-\xfe]{6,})', data):
- try:
- s = m.decode('gbk')
- if any('\u4e00' <= c <= '\u9fff' for c in s):
- G.append(s)
- except Exception:
- pass
- ALL = A + U + G
- print(f'\n=== 字符串统计: ASCII {len(A)} | UTF16 {len(U)} | GBK中文 {len(G)} ===')
- # ---------- 4. 框架识别 ----------
- print('\n=== 打包/框架特征 ===')
- flat = '\n'.join(ALL)
- for label, pats in {
- 'PyInstaller': [r'MEI\\x0c', r'pyi-', r'PyInstaller', r'python3\d'],
- '.NET': [r'mscoree\.dll', r'GetRuntimeInterfaceMapOffset'],
- '易语言': [r'krnln', r'e\.fnr', r'EPL'],
- 'aardio': [r'aardio'],
- 'AutoIt': [r'AutoIt', r'AU3!'],
- 'Delphi/C++Builder': [r'TForm', r'Borland', r'Embarcadero'],
- 'Electron/CEF': [r'app\.asar', r'CefSharp', r'libcef'],
- 'UPX壳': [r'UPX0', r'UPX1', r'UPX!'],
- }.items():
- hits = [p for p in pats if re.search(p, flat)]
- if hits:
- print(f' {label}: 命中 {hits}')
- # ---------- 5. 网络地址 ----------
- urls = extract_urls(A + U)
- print(f'\n=== URL/域名({len(urls)}) ===')
- for u in urls[:60]:
- print(f' {u}')
- # ---------- 6. 敏感行为路径 ----------
- print('\n=== 敏感路径/关键词命中 ===')
- KEYS = ['User Data', 'Cookies', 'Login Data', 'Local State', 'AppData',
- 'Chrome', 'Edge', 'Firefox', 'MetaMask', 'TronLink', 'wallet',
- 'Telegram', 'WeChat', 'wechat', 'wxid', 'keystone', 'password',
- 'passwd', '2fa', 'authenticator', 'screenshot', 'keylog', 'clipboard',
- 'kuaishou', 'douyin', 'passport', 'token', 'cookie']
- seen = set()
- for k in KEYS:
- hits = sorted({s.strip() for s in ALL if k in s and len(s) < 120})[:4]
- for h in hits:
- key = (k, h[:90])
- if key not in seen:
- seen.add(key)
- print(f' [{k}] {h[:110]}')
- # ---------- 7. PyInstaller 内部模块清单(若命中) ----------
- MAGIC = b'MEI\x0c\x0b\x0a\x0b\x0e'
- pos = data.rfind(MAGIC)
- if pos != -1:
- print(f'\n=== PyInstaller CArchive 命中 @{pos} ===')
- # cookie 结构: magic(8) + pkgLen(I) + toc(I) + tocLen(I) + pyver(i) + pylibname(64s, 仅新版)
- tail = data[pos:pos + 88]
- magic, pkg_len, toc_off, toc_len, pyver = struct.unpack('!8sIIIi', tail[:24])
- toc_abs = pos - pkg_len + toc_off # CArchive 起始 = cookie位置+24(or 88) - pkgLen
- toc = data[toc_abs:toc_abs + toc_len]
- entries, i = [], 0
- while i < len(toc):
- entry_size, = struct.unpack('!I', toc[i:i + 4])
- if entry_size == 0:
- break
- epos, clen, ulen, flag, ctype = struct.unpack('!IIIBc', toc[i + 4:i + 18])
- name = toc[i + 18:i + entry_size].rstrip(b'\x00').decode('utf-8', 'replace')
- entries.append((name, ctype.decode(), toc_abs_base := (pos + 24 - pkg_len) + epos, clen, ulen, flag))
- i += entry_size
- print(f' 内部条目 {len(entries)} 个, Python {pyver}')
- names = [e[0] for e in entries]
- for n in names[:80]:
- print(f' {n}')
- # 解压模块并抽字符串(仅看结构,不执行)
- print('\n=== 模块内容抽样字符串 ===')
- shown = 0
- for name, ctype, epos, clen, ulen, flag in entries:
- if shown >= 12:
- break
- if ctype in ('s', 'm', 'M') and clen < 3_000_000:
- try:
- raw = zlib.decompress(data[epos:epos + clen]) if flag else data[epos:epos + clen]
- strs = [m.decode('utf-8', 'replace') for m in re.findall(rb'[\x20-\x7e\xe4-\xe9][\x20-\x7e\x80-\xff]{8,}', raw)][:6]
- urls_m = extract_urls(strs)
- keep = [s for s in strs if any(k in s for k in ('http', '.com', '.cn', 'cookie', 'Cookie',
- 'token', 'upload', 'api', 'user', 'pass', '登录', '账号', '发布', '快手', '抖音'))]
- if keep:
- print(f' [{name}]')
- for s in keep[:6]:
- print(f' {s[:100]}')
- shown += 1
- except Exception:
- pass
- else:
- print('\n(未发现 PyInstaller CArchive 标记)')
|