"""Read-only pinned-image bindings for the original current trade-flow source."""
import hashlib
import json
import struct
from pathlib import Path
import pefile
from capstone import Cs, CS_ARCH_X86, CS_MODE_64
image = Path('artifacts/ship-model-reference/client-copy/EliteDangerous64.exe')
raw = image.read_bytes()
expected = '6891f70a021fa9f3a0e00458c284f48d1226e68ae7fc02ad48081aed47857e93'
if hashlib.sha256(raw).hexdigest() != expected:
raise ValueError('Expected pinned source image')
pe = pefile.PE(data=raw, fast_load=True)
base = pe.OPTIONAL_HEADER.ImageBase
disassembler = Cs(CS_ARCH_X86, CS_MODE_64)
tables = {
hex(start): [dict(slot=hex(offset), target=hex(struct.unpack('<Q', pe.get_data(start + offset, 8))[0] - base))
for offset in range(0, size, 8)]
for start, size in ((0x552EBD0, 0x78), (0x552CF88, 0x68), (0x5134AE0, 0x48))
}
leaves = {}
for start in (0x3B12E00, 0x3B12EE0, 0x9A73E0, 0x9A6DE0, 0x9A8FC0,
0x58DED0, 0x3B80AA0, 0x8AE8F0, 0xB27050):
lines = []
for instruction in disassembler.disasm(pe.get_data(start, 64), start):
lines.append(dict(rva=hex(instruction.address), bytes=instruction.bytes.hex(),
mnemonic=instruction.mnemonic, operands=instruction.op_str))
if instruction.mnemonic == 'ret':
break
if not lines or lines[-1]['mnemonic'] != 'ret':
raise ValueError(f'Expected bounded leaf at {start:x}')
leaves[hex(start)] = lines
json_keys = {
hex(rva): pe.get_data(rva, 128).split(b'\0', 1)[0].decode('ascii')
for rva in (0x5538478, 0x5538608, 0x552EC68, 0x511B028, 0x52215D0,
0x519A680, 0x552EC78, 0x552EC88, 0x50CB6E4, 0x512568C,
0x552EC98, 0x552ECA8, 0x5195A88, 0x5193840)
}
constants = {hex(rva): struct.unpack('<f', pe.get_data(rva, 4))[0]
for rva in (0x5220F7C, 0x4E340F8, 0x4DE165C)}
result = dict(imageSha256=expected, tables=tables, leaves=leaves, originalJsonKeys=json_keys,
constants=constants, scope='Offline static evidence; no game memory or network access')
destination = Path('artifacts/galaxy-query-research/account-sources/current-trade-evidence.json')
destination.write_text(json.dumps(result, indent=2), encoding='utf-8')
print(destination)
"""Read-only pinned-image bindings for the original current trade-flow source."""
import hashlib
import json
import struct
from pathlib import Path
import pefile
from capstone import Cs, CS_ARCH_X86, CS_MODE_64
image = Path('artifacts/ship-model-reference/client-copy/EliteDangerous64.exe')
raw = image.read_bytes()
expected = '6891f70a021fa9f3a0e00458c284f48d1226e68ae7fc02ad48081aed47857e93'
if hashlib.sha256(raw).hexdigest() != expected:
raise ValueError('Expected pinned source image')
pe = pefile.PE(data=raw, fast_load=True)
base = pe.OPTIONAL_HEADER.ImageBase
disassembler = Cs(CS_ARCH_X86, CS_MODE_64)
tables = {
hex(start): [dict(slot=hex(offset), target=hex(struct.unpack('<Q', pe.get_data(start + offset, 8))[0] - base))
for offset in range(0, size, 8)]
for start, size in ((0x552EBD0, 0x78), (0x552CF88, 0x68), (0x5134AE0, 0x48))
}
leaves = {}
for start in (0x3B12E00, 0x3B12EE0, 0x9A73E0, 0x9A6DE0, 0x9A8FC0,
0x58DED0, 0x3B80AA0, 0x8AE8F0, 0xB27050):
lines = []
for instruction in disassembler.disasm(pe.get_data(start, 64), start):
lines.append(dict(rva=hex(instruction.address), bytes=instruction.bytes.hex(),
mnemonic=instruction.mnemonic, operands=instruction.op_str))
if instruction.mnemonic == 'ret':
break
if not lines or lines[-1]['mnemonic'] != 'ret':
raise ValueError(f'Expected bounded leaf at {start:x}')
leaves[hex(start)] = lines
json_keys = {
hex(rva): pe.get_data(rva, 128).split(b'\0', 1)[0].decode('ascii')
for rva in (0x5538478, 0x5538608, 0x552EC68, 0x511B028, 0x52215D0,
0x519A680, 0x552EC78, 0x552EC88, 0x50CB6E4, 0x512568C,
0x552EC98, 0x552ECA8, 0x5195A88, 0x5193840)
}
constants = {hex(rva): struct.unpack('<f', pe.get_data(rva, 4))[0]
for rva in (0x5220F7C, 0x4E340F8, 0x4DE165C)}
result = dict(imageSha256=expected, tables=tables, leaves=leaves, originalJsonKeys=json_keys,
constants=constants, scope='Offline static evidence; no game memory or network access')
destination = Path('artifacts/galaxy-query-research/account-sources/current-trade-evidence.json')
destination.write_text(json.dumps(result, indent=2), encoding='utf-8')
print(destination)