367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
156 lines
6.7 KiB
Python
156 lines
6.7 KiB
Python
"""Read-only hardware watch for Forever's public race/class table header.
|
|
|
|
Armed only by the diagnostic parent of its pinned disposable WowB process.
|
|
It observes writes to the fixed pointer or count fields and records the
|
|
post-write instruction address plus the public 24-byte vector header. It never
|
|
reads auth packets, account state, character fields, or row payloads, and it
|
|
never writes client memory or redirects execution.
|
|
"""
|
|
import ctypes as C
|
|
import json
|
|
import os
|
|
import signal
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from engine_main_thread_probe import Probe, Registers, ROOT
|
|
|
|
TABLE = 0x147C67110
|
|
FIELDS = (('pointer', TABLE), ('count', TABLE + 8))
|
|
ENTRY_MIN = 0x140001000
|
|
ENTRY_MAX = 0x144A00000
|
|
|
|
|
|
class Watch(Probe):
|
|
def __init__(self, pid, timeout=60, result_path=None, abi_path=None,
|
|
fields=('pointer', 'count')):
|
|
super().__init__(pid, observe_only=True, armed_timeout=timeout,
|
|
result_path=result_path or ROOT / 'runtime/engine-race-class-table-watch.json',
|
|
abi_path=abi_path)
|
|
by_name=dict(FIELDS)
|
|
if not fields or len(fields)>2 or len(set(fields))!=len(fields) or any(name not in by_name for name in fields):
|
|
raise ValueError('Only fixed public race/class header fields may be watched')
|
|
self.fields=tuple(fields)
|
|
self.targets=tuple(by_name[name] for name in self.fields)
|
|
self.target = self.targets[0]
|
|
self.stage = 'attach'
|
|
self.deadline = time.monotonic() + timeout
|
|
|
|
def public_header(self):
|
|
fd = os.open(f'/proc/{self.pid}/mem', os.O_RDONLY)
|
|
try:
|
|
raw = os.pread(fd, 24, TABLE)
|
|
finally:
|
|
os.close(fd)
|
|
if len(raw) != 24:
|
|
raise ValueError('Fixed public race/class header unavailable')
|
|
import struct
|
|
pointer, count, capacity = struct.unpack('<QQQ', raw)
|
|
if count > 32 or count > (capacity & 0x7FFFFFFFFFFFFFFF):
|
|
raise ValueError('Fixed public race/class header bounds differ')
|
|
return {'pointer_present': bool(pointer), 'count': count,
|
|
'capacity': capacity & 0x7FFFFFFFFFFFFFFF}
|
|
|
|
def finish(self, status, **extra):
|
|
self.restore_debug()
|
|
self.ptrace('detach', 0, self.pending_signal)
|
|
self.attached = False
|
|
self.done = True
|
|
self.stopped = False
|
|
self.record(status, hardware_registers_restored=True,
|
|
instruction_pointer_changed=False, execution_redirected=False,
|
|
target=hex(TABLE), packet_payloads_read=False,
|
|
authentication_buffers_read=False, **extra)
|
|
|
|
def poll(self):
|
|
if self.done:
|
|
return
|
|
if not self.attached:
|
|
self.public_header()
|
|
self.ptrace('seize')
|
|
self.attached = True
|
|
self.stage = 'initial-stop'
|
|
self.ptrace('interrupt')
|
|
return
|
|
if time.monotonic() > self.deadline and self.stage != 'cancel-stop':
|
|
self.stage = 'cancel-stop'
|
|
self.ptrace('interrupt')
|
|
got, status = os.waitpid(self.pid, os.WNOHANG | self.abi['wall'])
|
|
if not got:
|
|
return
|
|
if os.WIFEXITED(status) or os.WIFSIGNALED(status):
|
|
self.attached = False
|
|
self.done = True
|
|
self.record('client-exited-during-watch',
|
|
hardware_registers_restored=False,
|
|
packet_payloads_read=False, authentication_buffers_read=False)
|
|
return
|
|
if not os.WIFSTOPPED(status):
|
|
return
|
|
self.stopped = True
|
|
self.stops += 1
|
|
stop = os.WSTOPSIG(status)
|
|
self.pending_signal = 0 if status >> 16 == 128 else stop
|
|
if self.stage == 'initial-stop':
|
|
self.saved = {i: self.debug(i) for i in (0, 1, 2, 3, 6, 7)}
|
|
if self.saved[7] & 0xFF:
|
|
self.ptrace('detach', 0, self.pending_signal)
|
|
self.attached = False
|
|
self.done = True
|
|
self.record('existing-debug-registers-preserved',
|
|
hardware_registers_restored=False,
|
|
packet_payloads_read=False, authentication_buffers_read=False)
|
|
return
|
|
control=0
|
|
for index,address in enumerate(self.targets):
|
|
self.debug(index,address)
|
|
# Each register is a local, write-only, one-byte watchpoint;
|
|
# any write to its low byte is enough to catch the scalar store.
|
|
control|=(1 << (2*index)) | (1 << (16+4*index))
|
|
self.debug(6, 0)
|
|
self.debug(7, control)
|
|
self.stage = 'armed'
|
|
self.record('public-race-class-table-watch-armed',
|
|
fields=list(self.fields),initial_header=self.public_header())
|
|
self.ptrace('cont', 0, self.pending_signal)
|
|
self.stopped = False
|
|
return
|
|
bits = self.debug(6) if stop == signal.SIGTRAP else 0
|
|
own_bits=bits & ((1 << len(self.targets))-1)
|
|
own = bool(own_bits)
|
|
if own:
|
|
self.pending_signal = 0
|
|
if self.stage == 'cancel-stop':
|
|
self.finish('no-race-class-header-write-before-timeout', final_header=self.public_header())
|
|
return
|
|
if own:
|
|
registers = Registers()
|
|
self.ptrace('getregs', 0, C.addressof(registers))
|
|
header = self.public_header()
|
|
index=next(i for i in range(len(self.targets)) if own_bits & (1 << i))
|
|
if ENTRY_MIN <= registers.rip < ENTRY_MAX:
|
|
maps = Path(f'/proc/{self.pid}/maps').read_text().splitlines()
|
|
executable = any(
|
|
int(row.split()[0].split('-')[0], 16) <= registers.rip - 1 <
|
|
int(row.split()[0].split('-')[1], 16) and 'x' in row.split()[1]
|
|
for row in maps)
|
|
if executable:
|
|
page = (registers.rip - 1) & ~4095
|
|
fd = os.open(f'/proc/{self.pid}/mem', os.O_RDONLY)
|
|
try:
|
|
code = os.pread(fd, 4096, page)
|
|
finally:
|
|
os.close(fd)
|
|
if len(code) == 4096:
|
|
folder = ROOT / 'runtime/native-handler-code'
|
|
folder.mkdir(exist_ok=True)
|
|
path = folder / f'{page:x}.bin'
|
|
if not path.exists():
|
|
path.write_bytes(code)
|
|
path.chmod(0o600)
|
|
self.finish('public-race-class-field-write-observed',
|
|
field=self.fields[index],instruction_after_write=hex(registers.rip), final_header=header)
|
|
return
|
|
self.ptrace('cont', 0, self.pending_signal)
|
|
self.stopped = False
|