883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
95 lines
6.4 KiB
Python
95 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Own synthetic child: argument extension, real call/return and cleanup.
|
|
No game functions or account; real-client evidence is separately required.
|
|
"""
|
|
import ctypes as C
|
|
import json,os,signal,struct,sys,tempfile,time,threading
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'ops'))
|
|
import native_namespace_probe as ns
|
|
|
|
with tempfile.TemporaryDirectory(prefix='native-namespace-') as directory:
|
|
temporary=Path(directory);runtime=temporary/'runtime';runtime.mkdir()
|
|
module=0x150000000
|
|
exports={'gw_bridge_callback':0x1000,'gw_registration_get_state':0x1100,'gw_bridge_data':0x2000,
|
|
'gw_registration_data':0x7000,'gw_registration_rows':0x8000,'gw_registration_name':0x9000}
|
|
dll=runtime/'native-wine-prefix/drive_c/windows/system32/GWNativeBridge.dll';dll.parent.mkdir(parents=True)
|
|
dll.write_bytes(bytes(0x10000))
|
|
(runtime/'native-engine-pe.json').write_text(json.dumps({'exports':exports}))
|
|
readfd,writefd=os.pipe();pid=os.fork()
|
|
if pid==0:
|
|
try:
|
|
os.close(readfd)
|
|
libc=C.CDLL(None);libc.mmap.argtypes=[C.c_void_p,C.c_size_t,C.c_int,C.c_int,C.c_int,C.c_long];libc.mmap.restype=C.c_void_p
|
|
def page(address):
|
|
start=address&~4095
|
|
if libc.mmap(start,4096,3,0x100022,-1,0)!=start:raise RuntimeError('fixture page')
|
|
return start
|
|
fd=os.open(dll,os.O_RDWR)
|
|
if libc.mmap(module,0x10000,3,0x100001,fd,0)!=module:raise RuntimeError('fixture DLL')
|
|
os.close(fd)
|
|
for key in ('gw_bridge_callback','gw_registration_get_state'):C.memmove(module+exports[key],b'\xc3',1)
|
|
if libc.mprotect(C.c_void_p(module+0x1000),4096,5):raise RuntimeError('fixture RX')
|
|
C.memmove(module+exports['gw_registration_data'],struct.pack('<II',1,56),8)
|
|
C.memmove(module+exports['gw_registration_name'],b'GetTurtleRegistrationState\0',26)
|
|
start=page(ns.READY_ADAPTER);C.memmove(ns.READY_ADAPTER,b'\xcc'*12,12);C.memmove(ns.GETTER_ADAPTER,b'\xcc'*12,12)
|
|
if libc.mprotect(C.c_void_p(start),4096,5):raise RuntimeError('fixture adapter RX')
|
|
page(ns.TABLE);page(ns.LUA_GLOBAL);C.c_ulonglong.from_address(ns.LUA_GLOBAL).value=0x12345000
|
|
name=0x144ddfcb0;page(name);C.memmove(name,b'C_Login\0',8)
|
|
original=struct.pack('<QQ',name,ns.READY)*29;C.memmove(ns.TABLE,original,len(original))
|
|
start=page(ns.REGISTER)
|
|
# Real helper reads the extended last row, performs its native bind
|
|
# call and returns the supplied array. No foreign call is injected.
|
|
code=b'\x53\x48\x83\xec\x20\x48\x89\xcb\x48\x8b\x91\xd8\x01\x00\x00\x48\xb8'+struct.pack('<Q',ns.BIND)+b'\xff\xd0\x48\x89\xd8\x48\x83\xc4\x20\x5b\xc3'
|
|
C.memmove(ns.REGISTER,code,len(code))
|
|
bind_page=page(ns.BIND);C.memmove(ns.BIND,b'\xc3',1)
|
|
if libc.mprotect(C.c_void_p(bind_page),4096,5):raise RuntimeError('fixture bind RX')
|
|
if libc.mprotect(C.c_void_p(start),4096,5):raise RuntimeError('fixture helper RX')
|
|
start=page(0x140002000);position=start;functions=[]
|
|
for target,rcx,rdx,r8 in ((ns.REGISTER,ns.TABLE,29,name),(ns.READY_ADAPTER,0x12345000,0,0)):
|
|
code=b'\x48\xb9'+struct.pack('<Q',rcx)+b'\x48\xba'+struct.pack('<Q',rdx)+b'\x49\xb8'+struct.pack('<Q',r8)+b'\x48\xb8'+struct.pack('<Q',target)+b'\x48\x83\xec\x28\xff\xd0\x48\x83\xc4\x28\xc3'
|
|
C.memmove(position,code,len(code));functions.append(C.CFUNCTYPE(C.c_ulonglong)(position));position+=128
|
|
if libc.mprotect(C.c_void_p(start),4096,5):raise RuntimeError('fixture thunk RX')
|
|
def install_local_adapters():
|
|
address=module+exports['gw_registration_data']+8
|
|
while C.c_uint.from_address(address).value!=1:time.sleep(.001)
|
|
start=ns.READY_ADAPTER&~4095
|
|
if libc.mprotect(C.c_void_p(start),4096,7):os._exit(3)
|
|
for cave,target in ((ns.READY_ADAPTER,module+exports['gw_bridge_callback']),(ns.GETTER_ADAPTER,module+exports['gw_registration_get_state'])):
|
|
C.memmove(cave,b'\x48\xb8'+struct.pack('<Q',target)+b'\xff\xe0',12)
|
|
if libc.mprotect(C.c_void_p(start),4096,5):os._exit(4)
|
|
C.c_uint.from_address(address).value=2
|
|
threading.Thread(target=install_local_adapters,daemon=True).start()
|
|
libc.prctl(15,C.c_char_p(b'WowB.exe'),0,0,0);os.write(writefd,b'ready');os.close(writefd)
|
|
extended=False;until=time.monotonic()+3
|
|
while time.monotonic()<until:
|
|
if not extended:extended=functions[0]()==module+exports['gw_registration_rows']
|
|
if extended:functions[1]()
|
|
time.sleep(.005)
|
|
assert extended
|
|
rows=C.string_at(module+exports['gw_registration_rows'],30*16)
|
|
assert rows[:25*16]==original[:25*16] and rows[26*16:29*16]==original[26*16:]
|
|
assert C.string_at(ns.TABLE,29*16)==original
|
|
assert struct.unpack_from('<QQ',rows,29*16)==(module+exports['gw_registration_name'],ns.GETTER_ADAPTER)
|
|
os._exit(0)
|
|
except BaseException:os._exit(2)
|
|
os.close(writefd)
|
|
try:
|
|
assert os.read(readfd,5)==b'ready'
|
|
ns.ROOT=temporary
|
|
probe=ns.NamespaceProbe(pid,Path(sys.argv[1]) if len(sys.argv)>1 else ROOT/'build/native-ptrace-abi')
|
|
until=time.monotonic()+2
|
|
while not probe.done and time.monotonic()<until:probe.poll();time.sleep(.001)
|
|
assert probe.done and not probe.attached
|
|
report=json.loads((runtime/'native-namespace-install.json').read_text())
|
|
assert report['status']=='native-namespace-bound-and-callback-observed'
|
|
assert report['closure_bound'] and report['helper_returned'] and report['original_ready_invoked'] and report['hardware_registers_restored']
|
|
assert not report['engine_calls_injected'] and report['code_bytes_modified'] and not report['lua_memory_modified']
|
|
assert len(report['adapters'])==2
|
|
got,status=os.waitpid(pid,0);assert got==pid and os.WIFEXITED(status) and os.WEXITSTATUS(status)==0
|
|
print('Own child: extended native registration arguments, observed closure/return/callback, preserved original rows and restored debug slots.')
|
|
finally:
|
|
os.close(readfd)
|
|
try:os.kill(pid,signal.SIGTERM)
|
|
except ProcessLookupError:pass
|