tools/client/inventory.py (5898B)
1 import datetime 2 import hashlib 3 import json 4 import pathlib 5 import re 6 7 import pefile 8 9 10 # Inventory the selected client without loading or modifying its modules. 11 root = pathlib.Path(__file__).resolve().parents[2] 12 client = root / 'anarchy-vanilla-14.9.1' 13 paths = sorted(path for path in client.iterdir() 14 if path.suffix.lower() in ('.exe', '.dll')) 15 modules = [] 16 for path in paths: 17 data = path.read_bytes() 18 binary = pefile.PE(data=data) 19 module = { 20 'path': path.relative_to(client).as_posix(), 21 'size': len(data), 22 'sha256': hashlib.sha256(data).hexdigest(), 23 'machine': hex(binary.FILE_HEADER.Machine), 24 'pe_magic': hex(binary.OPTIONAL_HEADER.Magic), 25 'timestamp_utc': datetime.datetime.fromtimestamp( 26 binary.FILE_HEADER.TimeDateStamp, datetime.timezone.utc).isoformat(), 27 'image_base': hex(binary.OPTIONAL_HEADER.ImageBase), 28 'entry_point_rva': hex(binary.OPTIONAL_HEADER.AddressOfEntryPoint), 29 'linker_version': '{}.{}'.format(binary.OPTIONAL_HEADER.MajorLinkerVersion, 30 binary.OPTIONAL_HEADER.MinorLinkerVersion), 31 'file_characteristics': hex(binary.FILE_HEADER.Characteristics), 32 'dll_characteristics': hex(binary.OPTIONAL_HEADER.DllCharacteristics), 33 'sections': [], 34 'imports': [], 35 'delay_imports': [], 36 'exports': [], 37 } 38 39 # Keep section layout, relocation presence, and packing indicators explicit. 40 for section in binary.sections: 41 module['sections'].append({ 42 'name': section.Name.rstrip(b'\0').decode('ascii', errors='replace'), 43 'rva': hex(section.VirtualAddress), 44 'virtual_size': section.Misc_VirtualSize, 45 'raw_size': section.SizeOfRawData, 46 'characteristics': hex(section.Characteristics), 47 'entropy': round(section.get_entropy(), 3), 48 'executable': bool(section.Characteristics & 0x20000000), 49 'writable': bool(section.Characteristics & 0x80000000), 50 }) 51 module['directories'] = { 52 entry.name: {'rva': hex(entry.VirtualAddress), 'size': entry.Size} 53 for entry in binary.OPTIONAL_HEADER.DATA_DIRECTORY 54 } 55 module['overlay_size'] = len(binary.get_overlay() or b'') 56 57 # Preserve decorated names, ordinals, IAT addresses, and forwarded exports. 58 for directory, key in [(1, 'imports'), (13, 'delay_imports')]: 59 if not binary.OPTIONAL_HEADER.DATA_DIRECTORY[directory].Size: 60 continue 61 entries = (binary.DIRECTORY_ENTRY_IMPORT if directory == 1 62 else binary.DIRECTORY_ENTRY_DELAY_IMPORT) 63 for entry in entries: 64 module[key].append({ 65 'dll': entry.dll.decode('ascii', errors='replace'), 66 'symbols': [ 67 {'name': symbol.name.decode('ascii', errors='replace') 68 if symbol.name else None, 69 'ordinal': symbol.ordinal, 70 'iat_va': hex(symbol.address)} 71 for symbol in entry.imports 72 ], 73 }) 74 if binary.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size: 75 module['exports'] = [ 76 {'name': symbol.name.decode('ascii', errors='replace') 77 if symbol.name else None, 78 'ordinal': symbol.ordinal, 79 'rva': hex(symbol.address), 80 'forwarder': symbol.forwarder.decode('ascii', errors='replace') 81 if symbol.forwarder else None} 82 for symbol in binary.DIRECTORY_ENTRY_EXPORT.symbols 83 ] 84 85 # Strings are discovery hints only; they do not establish executed call paths. 86 strings = re.findall(rb'[\x20-\x7e]{5,}', data) 87 module['dll_string_candidates'] = sorted({ 88 value.decode('ascii') for value in strings 89 if re.fullmatch(rb'[A-Za-z0-9_.-]+\.dll', value, re.IGNORECASE) 90 }) 91 module['pdb_strings'] = [value.decode('ascii') for value in strings 92 if b'.pdb' in value.lower()] 93 module['runtime_imports'] = [entry['dll'] for entry in module['imports'] 94 if entry['dll'].lower().startswith( 95 ('msvc', 'mfc', 'ucrt', 'vcruntime'))] 96 module['dynamic_loading_imports'] = [ 97 symbol['name'] for entry in module['imports'] for symbol in entry['symbols'] 98 if symbol['name'] in ('LoadLibraryA', 'LoadLibraryW', 'LoadLibraryExA', 99 'LoadLibraryExW', 'GetProcAddress') 100 ] 101 module['pe_warnings'] = binary.get_warnings() 102 modules.append(module) 103 binary.close() 104 105 # Resolve dependency names conservatively; duplicate basenames remain ambiguous. 106 by_name = {} 107 for module in modules: 108 by_name.setdefault(pathlib.PurePosixPath(module['path']).name.lower(), []).append( 109 module['path']) 110 dependencies = [] 111 for module in modules: 112 for key in ('imports', 'delay_imports'): 113 for entry in module[key]: 114 dependencies.append({ 115 'source': module['path'], 116 'dll': entry['dll'], 117 'kind': key, 118 'local_candidates': by_name.get(entry['dll'].lower(), []), 119 }) 120 121 output = root / 'local/client/game-14.9.1/inventory.json' 122 output.parent.mkdir(parents=True, exist_ok=True) 123 output.write_text(json.dumps({'client': client.name, 'pefile': pefile.__version__, 124 'modules': modules, 'dependencies': dependencies}, 125 indent=2) + '\n', encoding='ascii') 126 print(f'Inventoried {len(modules)} modules: {output}') 127 for module in sorted(modules, key=lambda item: item['size']): 128 if '/' not in module['path']: 129 imports = ', '.join(entry['dll'] for entry in module['imports']) 130 print('{}: {} bytes, {} exports; {}'.format( 131 module['path'], module['size'], len(module['exports']), imports))