Files
Neutron/tools/binary/pair_lifecycle.py
T

139 lines
7.8 KiB
Python

#!/usr/bin/env python3
"""Guard the original ordered pair pass; generate a copied-code contact probe."""
import argparse
import hashlib
import json
from pathlib import Path
import re
import struct
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels'))
from gam import parse
REA_ID = 'ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904'
RANGES = (
(0x47a430, 0x47a5de, '68ff890cd3f3526f8d4dd936b72748fdeb549cc97b56068ccc75c23df2c81ae6'),
(0x47b3e0, 0x47b4a1, '008cc7fa18ca1b59567f4388928045dcdfc9ac85d25dce277d2270378b18f5df'),
(0x47ca90, 0x47caba, '1d5618a99b80fec594c14a36b837e10456790bc44cac4306f8f7e66fd3a757b2'),
(0x470a60, 0x470a88, '6fd5f554e28bd9d733f561a1e755689f11d86997c2fda2ccefdbeab6946fe9f3'),
(0x470a90, 0x470b19, 'bfc56f226eb97077379bbb4f273ae47f0c0c1f97a1ec335996435a5c4f650038'),
(0x470b20, 0x470bb8, 'a84cc049930fd1301bb89a6a84255ebc4fe9515e26ce1d7be75c743574e31202'),
(0x470bc0, 0x470bea, '09bfd61be8128cdbf9c1f1aed7a416cd5ae3c735abf588106d17d744a233951a'),
(0x470bf0, 0x470c44, '5211f48735716c7a7426486f0b44ae9e93388546d825437edc6ef3f787e2aa8d'),
)
TARGETS = (0x4f69e4, 0x695d3c, 0x695d47, 0x4f6b08, 0x48d104, 0x509948)
TABLES = {'3JIM': 0x4a373c, '3ROC': 0x4b25ac, '3SOL': 0x4c19c4, '3TRO': 0x4be404}
COMMON = {0x178: 0x471720, 0x210: 0x4719f0, 0x220: 0x471a30,
0x3a0: 0x472f80, 0x4c: 0x470bf0}
def collect(original):
original = Path(original)
pe = PE(find_case(original, 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Original identity mismatch')
ranges, relocations, bindings = [], [], []
for start, end, digest in RANGES:
code = read_va(pe, start, end-start)
if hashlib.sha256(code).hexdigest() != digest:
raise ValueError(f'Changed source body {start:#x}')
ranges.append(dict(va=hex(start), file_offset=hex(pe.offset(start-pe.base)),
length=end-start, sha256=digest))
for target in TARGETS:
encoded = struct.pack('<I', target)
for index in range(len(code)-3):
if code[index:index+4] == encoded:
relocations.append(dict(va=hex(start+index), target=hex(target)))
for kind, table in TABLES.items():
for slot, expected in COMMON.items():
if struct.unpack('<I', read_va(pe, table+slot, 4))[0] != expected:
raise ValueError('Changed actor binding')
bindings.append(dict(kind=kind, table=hex(table), slot=hex(slot), target=hex(expected),
file_offset=hex(pe.offset(table+slot-pe.base))))
gam_path = find_case(original, 'gam/VR04.gam')
candidates = []
for entity in parse(gam_path.read_bytes()).entities:
if entity.kind in TABLES:
values = entity.values()
candidates.append(dict(kind=entity.kind, tag=values['ObjectTag'], file_offset=hex(entity.offset),
has_collision=values['HasCollision'], can_move=values['CanMove'],
task=values['TaskName'], required_level=values['RequiredLevel'],
table=hex(TABLES[entity.kind])))
return dict(source_sha256=EXE_SHA256,
omt_sha256=hashlib.sha256(find_case(original, 'OMT2.dll').read_bytes()).hexdigest(),
ranges=ranges, relocations=relocations,
bindings=bindings, vr04_sha256=hashlib.sha256(gam_path.read_bytes()).hexdigest(),
vr04_candidates=candidates)
def generate(original, output, rea):
original, output = Path(original).resolve(), Path(output).resolve()
if output.is_relative_to(original) or original.is_relative_to(output):
raise ValueError('Separate generated directory required')
if output.exists() and any(p.is_symlink() for p in output.rglob('*')):
raise ValueError('Generated output contains symlinks')
report = collect(original)
record = next(r for r in json.loads(Path(rea).read_text())['records'] if r['evidence_id'] == REA_ID)
if record['subject']['digest']['sha256'] != EXE_SHA256:
raise ValueError('REA subject mismatch')
rows = []
for line in record['raw_result']['stdout'].splitlines():
match = re.match(r'\s*([0-9a-f]+):', line)
if match:
rows.append((int(match[1], 16), line))
for entry, (start, end, _) in zip(report['ranges'], RANGES):
excerpt = '\n'.join(line for va, line in rows if start <= va < end)
if not excerpt:
raise ValueError('Missing retained REA instructions')
entry['rea_extract'] = excerpt
output.mkdir(parents=True, exist_ok=True)
probe = Path(__file__).with_name('pair_lifecycle_probe.c').read_text()
probe = probe.replace('/* RELOCATIONS */', ','.join('{%s,%s}' % (row['va'], row['target']) for row in report['relocations']))
(output/'pair_lifecycle_probe.c').write_text(probe)
(output/'grappler_scalar_probe.c').write_text(Path(__file__).with_name('grappler_scalar_probe.c').read_text())
report.update(rea_evidence_id=REA_ID,
scope='Copied complete pair/sphere/exclusion/base-contact routines; synthetic actors/permissions/response and box adapters, not whole original gameplay.')
(output/'source.json').write_text(json.dumps(report, indent=2)+'\n')
return dict(source_sha256=EXE_SHA256, guarded_ranges=len(report['ranges']),
relocated_operands=len(report['relocations']), guarded_bindings=len(report['bindings']))
def capture(output, directory):
directory, output = Path(directory), Path(output)
metadata = json.loads((directory/'capture.json').read_text())
log = (directory/'wine.log').read_text()
if metadata['captures'][-1]['wineExit'] != 0 or 'PAIR_LIFECYCLE_COMPLETE' not in log:
raise ValueError('Incomplete probe')
cases = [json.loads(line[5:]) for line in log.splitlines() if line.startswith('PAIR ')]
if len(cases) != 28 or len({c['name'] for c in cases}) != 28:
raise ValueError('Missing or duplicate case')
source = json.loads((output/'source.json').read_text())
for filename, field in (('Neutron.exe', 'source_sha256'), ('OMT2.dll', 'omt_sha256')):
if hashlib.sha256(find_case(directory/'game', filename).read_bytes()).hexdigest() != source[field]:
raise ValueError('Copied original identity mismatch')
identity = json.loads((directory/'probe-identity.json').read_text())
if identity['sha256'] != hashlib.sha256((output/'probe.exe').read_bytes()).hexdigest():
raise ValueError('Observed probe binary identity mismatch')
files = [directory/'wine.log', directory/'capture.json', directory/'probe-identity.json',
output/'source.json', output/'pair_lifecycle_probe.c', output/'probe.exe']
report = dict(cases=cases, provenance={str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in files},
scope='Isolated original component execution under Wine32; no original entrypoint, constructor or real class callback.')
(output/'observations.json').write_text(json.dumps(report, indent=2)+'\n')
return dict(cases=len(cases), scope=report['scope'])
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
cli.add_argument('--output', type=Path)
cli.add_argument('--rea', type=Path)
cli.add_argument('--capture', type=Path)
args = cli.parse_args()
if args.capture and not args.output or args.output and not (args.capture or args.rea):
cli.error('--output needs --rea for generation, or --capture for verification')
print(json.dumps(capture(args.output, args.capture) if args.capture else
generate(args.original, args.output, args.rea) if args.output else collect(args.original), indent=2))