Files
Neutron/tools/binary/vr04_generated_bodies.py

105 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Guard bounded VR04 generated-helper constructor and registration leads."""
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,rel_call_target
from pe_inspect import PE
from rtti_map import rtti_map
CALLS = (
('Jimmy baseball loop',0x422633,0x40fc50),
('Player jetpack fire',0x4374df,0x421d80),
('Jimmy owned Rocketship',0x42243f,0x43d840),
('Rocketship FireStrato',0x43d9bc,0x4192e0),
('Jimmy Bubble',0x42232c,0x410840),
('Jimmy Shadow',0x42250c,0x43f640),
('Jimmy TargetCursor',0x42255b,0x4453b0),
('Jimmy PointCursor',0x4225be,0x43bc70),
('Jimmy GraplingHook',0x422800,0x41eeb0),
('Jimmy PostLoad Goddard',0x423c98,0x41c810),
('Jimmy PostLoad Jeep',0x423d1a,0x4211a0),
('Smoke SmokePuff',0x4406b1,0x440980),
('Yokian Shield',0x44a826,0x44b510),
('Yokian conditional Shadow',0x44a88f,0x43f640),
('Goddard Cursor first',0x41ca9c,0x415760),
('Goddard Cursor second',0x41cb09,0x415760),
('Bubble registration',0x410992,0x464210),
('PointCursor registration',0x43bd67,0x4623d0),
('GraplingHook registration',0x41efb4,0x4623d0),
('SmokePuff registration',0x440acc,0x464210),
('Cursor registration',0x415857,0x4623d0),
)
CLASSES = (
('C3DBaseball',0x49141c,0xc8),('C3DJetpackFire',0x4a3160,0xc0),
('C3DFireStrato',0x49bdd4,0xc0),('C3DBubble',0x49250c,0xc8),
('C3DShadow',0x4b37b8,0xc8),('C3DTargetCursor',0x4b9f48,0xc8),
('C3DPointCursor',0x4b0390,0xc0),('C3DGraplingHook',0x49fafc,0xc0),
('C3DJeep',0x4a2aec,0xc0),('C3DSmokePuff',0x4b545c,0xc8),
('C3DYokianShield',0x4c0e24,0xc8),('C3DCursor',0x497620,0xc0),
)
SITES = (
('Baseball explicit PostLoad registration',0x41000c,'ff502c'),
('Jimmy own selected-start registration',0x4238b4,'ff502c'),
('Jimmy owner-driven fire registration',0x4238ef,'ff522c'),
('Jeep InitObject registration',0x421552,'ff522c'),
('Jeep pointer overwritten before allocation test',0x423792,'89bd70090000'),
('Jeep allocation null branch',0x423cee,'85c00f8585000000'),
('Jeep overwrite source register is zero',0x4236fa,'33ff'),
('Baseball loop count20',0x422610,'be14000000'),
('Baseball loop decrement/backedge',0x422695,'4e0f8579ffffff'),
)
def collect(original,rea=None):
pe=PE(find_case(Path(original),'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest()!=EXE_SHA256:raise ValueError('Original identity mismatch')
calls=[]
for name,va,target in CALLS:
if rel_call_target(pe,va)!=target:raise ValueError(name)
calls.append(dict(name=name,va=hex(va),file_offset=hex(pe.offset(va-pe.base)),target=hex(target)))
sites=[]
for name,va,expected in SITES:
if read_va(pe,va,len(bytes.fromhex(expected))).hex()!=expected:raise ValueError(name)
sites.append(dict(name=name,va=hex(va),file_offset=hex(pe.offset(va-pe.base)),bytes=expected))
classes=[]
for name,table,displacement in CLASSES:
candidates=rtti_map(pe,name)['vtables']
if not any(int(t['va'],16)==table and int(t['object_displacement'],16)==displacement for t in candidates):
raise ValueError('Helper RTTI/table mismatch')
classes.append(dict(name=name,table=hex(table),displacement=hex(displacement),
methods={hex(slot):hex(struct.unpack('<I',read_va(pe,table+slot,4))[0]) for slot in (0x28,0x2c,0x40c)}))
result=dict(source_sha256=EXE_SHA256,calls=calls,sites=sites,classes=classes,
scope='Bounded generated helper identities/creation/registration sites; not a complete body count, retained cleanup proof or original runtime observation.')
if rea:
evidence_id='ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904'
record=next(r for r in json.loads(Path(rea).read_text())['records'] if r['evidence_id']==evidence_id)
if record['subject']['digest']['sha256']!=EXE_SHA256:raise ValueError('REA identity 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))
ranges=((0x422609,0x42269c),(0x4236fa,0x423798),(0x4238b0,0x4238f5),
(0x423ce8,0x423d7b),(0x410000,0x410020),(0x40e7b0,0x40e812),
(0x421540,0x421580),(0x440530,0x440780),(0x44a730,0x44a980),
(0x4354c0,0x43554a),(0x445580,0x4455b0),(0x44b6c0,0x44b6eb),
(0x464810,0x46489a))
result['rea_evidence_id']=evidence_id
result['ranges']=[]
for a,b in ranges:
excerpt='\n'.join(line for va,line in rows if a<=va<b)
if not excerpt:raise ValueError('Missing REA range')
result['ranges'].append(dict(va=hex(a),file_offset=hex(pe.offset(a-pe.base)),
length=b-a,sha256=hashlib.sha256(read_va(pe,a,b-a)).hexdigest(),rea_extract=excerpt))
return result
if __name__=='__main__':
cli=argparse.ArgumentParser(description=__doc__);cli.add_argument('original',type=Path)
cli.add_argument('--rea',type=Path);args=cli.parse_args()
print(json.dumps(collect(args.original,args.rea),indent=2))