"""Locate named Client method token checks in a supplied PE (read only). The correlation was observed for GridFightModule.GetSeason and _SyncSeason in this Client: the native check loads the low 24 bits of MetadataToken into ECX before a direct call. Report candidate sites; do not infer function boundaries or claim that every candidate is a method implementation. """ import argparse import hashlib import json import pathlib import re import struct def executable_sections(data): if data[:2] != b"MZ": raise ValueError("Expected a PE image") pe = struct.unpack_from(" len(data): raise ValueError("Truncated executable section") sections.append((rva, data[raw:raw + raw_size])) return sections def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--assembly", type=pathlib.Path, required=True) parser.add_argument("--tokens", type=pathlib.Path, required=True) parser.add_argument("--output", type=pathlib.Path, required=True) args = parser.parse_args() data = args.assembly.read_bytes() sections = executable_sections(data) methods, by_check = {}, {} for line in args.tokens.read_text(encoding="utf-8-sig").splitlines(): match = re.search(r"\[GAC_CW_METHOD_TOKEN\] METHOD (\S+) (\S+) (\d+)$", line) if not match: continue token = int(match[3]) name = match[1] + "." + match[2] + "#" + hex(token) if token & 0xff000000 != 0x06000000: # ECMA-335 MethodDef token table. raise SystemExit("Not a MethodDef token: " + name) methods[name] = {"metadata_token": hex(token), "check_id": hex(token & 0x00ffffff), "candidate_sites": []} by_check.setdefault(token & 0x00ffffff, []).append(name) if not methods: raise SystemExit("No captured method tokens found") # One section pass, rather than rereading a large executable for every # captured method. The byte pattern is deliberately only a candidate. pattern = re.compile(rb"(?=\xb9(.{4})\xe8(.{4}))", re.S) for section_rva, section in sections: for match in pattern.finditer(section): check_id = struct.unpack("