"""Read-only relocation of a reviewed native ABI onto a new client image. A version number never grants compatibility. Match complete function bodies, their pointer/call relationships and RTTI tables. Every result stays unverified until that exact build and account have a confirmed first-send receipt. """ from __future__ import annotations import hashlib import mmap import os from pathlib import Path import re import struct from threading import RLock from wecom_native_signatures import REVISION, SIGNATURES MAX_IMAGE_BYTES = 512 * 1024 * 1024 EXECUTE, READ, WRITE = 0x20000000, 0x40000000, 0x80000000 _CACHE = {} _LOCK = RLock() class AdaptationUnavailable(ValueError): pass class PEImage: """Bounded PE32 reader. Never maps executable pages or loads client code.""" def __init__(self, data): self.data = data self.sections = [] if len(data) < 256 or len(data) > MAX_IMAGE_BYTES or data[:2] != b"MZ": raise AdaptationUnavailable("客户端 PE 文件无效") pe = self.u32(0x3c) if not 64 <= pe <= len(data) - 24 or data[pe:pe + 4] != b"PE\0\0": raise AdaptationUnavailable("客户端 PE 头无效") machine, count = struct.unpack_from(" len(data) or struct.unpack_from(" self.size or raw + raw_size > len(data) or any(rva < s[0] + s[1] and s[0] < rva + span for s in self.sections) or any(raw_size and s[3] and raw < s[2] + s[3] and s[2] < raw + raw_size for s in self.sections)): raise AdaptationUnavailable("客户端节区范围无效或重叠") self.sections.append((rva, span, raw, raw_size, flags)) def u32(self, offset): if not 0 <= offset <= len(self.data) - 4: raise AdaptationUnavailable("客户端地址超出文件范围") return struct.unpack_from(" raw_size: raise AdaptationUnavailable("接口地址不在文件数据内") return raw + delta def bytes_at(self, address, size): at = self.offset(address, size) return self.data[at:at + size] def find(self, needle, *, code=False, limit=64): hits = [] for rva, _, raw, size, flags in self.sections: if bool(flags & EXECUTE) != code or not flags & READ: continue position = raw while position < raw + size: position = self.data.find(needle, position, raw + size) if position < 0: break hits.append(self.base + rva + position - raw) if len(hits) > limit: raise AdaptationUnavailable("接口特征匹配过多,无法唯一定位") position += 1 return hits def vtable(self, type_name, object_offset): descriptors = self.find(type_name.encode("ascii") + b"\0") if len(descriptors) != 1: raise AdaptationUnavailable("客户端类型信息缺失或不唯一:" + type_name) descriptor = descriptors[0] - 8 tables = set() for reference in self.find(struct.pack(" limit: raise AdaptationUnavailable("接口函数匹配过多:" + name) return hits def analyze_image(image, version, digest): """Resolve all addresses with one consistent relationship map.""" from wecom_native_builds import NativeBuild, normalize_version version = normalize_version(version) if not version: raise AdaptationUnavailable("新客户端缺少可核对的版本号") message_table = image.vtable(".?AVMessageServiceImpl@logic@wework@@", 12) login_table = image.vtable(".?AVLoginServiceImpl@logic@wework@@", 12) manager_table = image.vtable(".?AVServiceManagerImpl@logic@wework@@", 0) send = struct.unpack("= 16: _CACHE.pop(next(iter(_CACHE))) _CACHE[digest] = profile return profile def validate_live_layout(profile, base, read): """Compare relocated full bodies against the running image, read-only.""" delta = base - 0x400000 for address, raw, offsets in profile.live_checks: expected = bytearray(raw) for at in offsets: value = struct.unpack_from("