125 lines
5.7 KiB
Python
125 lines
5.7 KiB
Python
"""Bounded, read-only 70334 M2 lookup snapshot for an already stopped debugger.
|
|
|
|
Pure capture() takes a memory reader and register dictionary. Sourcing this file
|
|
in GDB registers gw-m2-stall-snapshot OUTPUT [IMAGE_BASE]; it does not attach,
|
|
interrupt, continue, alter registers, or install any breakpoint.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
import struct
|
|
|
|
CODE = ((0x1DF4D40, 0x305, 'd1c7ba4515b097e53d16cad5b2cb5846e9f5edea9ac979820e5baae8f9732b1b'),
|
|
(0x1D6F840, 0xAA, '31444fce84519cb9d5b75c06815238dca826f74a13851644a6569f3d2915291e'))
|
|
# Only instructions where R13/R14 and the fixed stack layout remain valid.
|
|
LOOP_RANGES = ((0x1DF4EA0, 0x1DF4EAF), (0x1DF4F4F, 0x1DF4F93),
|
|
(0x1DF501B, 0x1DF5045))
|
|
MAX_BYTES = 5 * 1024 * 1024
|
|
|
|
|
|
def probe(ids, table, wanted, limit=4096):
|
|
"""Replay the original uint32 cumulative-square probe, with a hard budget."""
|
|
if not table:
|
|
return {'result': 'linear-table', 'found': wanted in ids}
|
|
if any(i != 65535 and i >= len(ids) for i in table):
|
|
return {'result': 'invalid-sequence-index'}
|
|
slot = wanted % len(table)
|
|
for step in range(limit):
|
|
index = table[slot]
|
|
if index == 65535:
|
|
return {'result': 'sentinel', 'steps': step, 'slot': slot}
|
|
if ids[index] == wanted:
|
|
return {'result': 'found', 'steps': step, 'slot': slot, 'index': index}
|
|
p = (step + 1) & 0xFFFFFFFF
|
|
slot = ((slot + p * p) & 0xFFFFFFFF) % len(table)
|
|
impossible = 65535 not in table and all(ids[i] != wanted for i in table)
|
|
return {'result': 'no-exit' if impossible else 'budget-exhausted', 'steps': limit}
|
|
|
|
|
|
def capture(read_memory, registers, image_base=0x140000000):
|
|
used = 0
|
|
|
|
def read(address, size):
|
|
nonlocal used
|
|
if address <= 0 or size < 0 or address + size >= 1 << 48:
|
|
raise ValueError('invalid memory range')
|
|
used += size
|
|
if used > MAX_BYTES:
|
|
raise ValueError('snapshot read budget exceeded')
|
|
data = bytes(read_memory(address, size))
|
|
if len(data) != size:
|
|
raise ValueError('short memory read')
|
|
return data
|
|
|
|
def u64(address):
|
|
return struct.unpack('<Q', read(address, 8))[0]
|
|
|
|
def descriptor(address, stride):
|
|
count, high, low = struct.unpack('<HHI', read(address, 8))
|
|
pointer = (high << 32) | low
|
|
if not count:
|
|
raise ValueError('empty descriptor is not the sampled hash loop')
|
|
return count, pointer, read(pointer, count * stride)
|
|
|
|
rip, rsp = registers['rip'], registers['rsp']
|
|
if not any(lo <= rip - image_base < hi for lo, hi in LOOP_RANGES):
|
|
raise ValueError('not at a supported sequence-probe instruction')
|
|
for offset, size, digest in CODE:
|
|
if hashlib.sha256(read(image_base + offset, size)).hexdigest() != digest:
|
|
raise ValueError('client lookup/caller code differs from pinned 70334')
|
|
if u64(rsp + 0x88) != image_base + 0x1D6F8D4:
|
|
raise ValueError('unsupported immediate caller')
|
|
wanted = struct.unpack('<I', read(rsp + 0x90, 4))[0]
|
|
model = u64(rsp + 0xA0) # lookup prologue saved the caller's RBX
|
|
state = u64(model + 0x878)
|
|
sequence, hashed = u64(state + 0x10), u64(state + 0x18)
|
|
if sequence != registers['r14'] or hashed != registers['r13']:
|
|
raise ValueError('model descriptors do not own the sampled lookup')
|
|
count, seq_pointer, seq_data = descriptor(sequence, 64)
|
|
hash_count, hash_pointer, hash_data = descriptor(hashed, 2)
|
|
ids = [struct.unpack_from('<H', seq_data, i * 64)[0] for i in range(count)]
|
|
table = list(struct.unpack('<' + 'H' * hash_count, hash_data))
|
|
# Retain only animation IDs/indices, never arbitrary heap or stack bytes.
|
|
return {'schema': 1, 'client': '70334-pinned-lookup', 'rip_rva': rip - image_base,
|
|
'model_pointer': hex(model), 'model_state_pointer': hex(state),
|
|
'sequence_descriptor': hex(sequence), 'hash_descriptor': hex(hashed),
|
|
'sequence_pointer': hex(seq_pointer), 'hash_pointer': hex(hash_pointer),
|
|
'requested_animation': wanted, 'sequence_ids': ids, 'hash_indices': table,
|
|
'table_signature': hashlib.sha256(struct.pack('<' + 'H' * len(ids), *ids) + hash_data).hexdigest(),
|
|
'probe': probe(ids, table, wanted), 'read_bytes': used,
|
|
'identity_status': 'model pointer proven; FileDataID not yet resolved'}
|
|
|
|
|
|
def register_gdb(gdb):
|
|
class Snapshot(gdb.Command):
|
|
def __init__(self):
|
|
super().__init__('gw-m2-stall-snapshot', gdb.COMMAND_DATA)
|
|
|
|
def invoke(self, argument, from_tty):
|
|
args = gdb.string_to_argv(argument)
|
|
if len(args) not in (1, 2):
|
|
raise gdb.GdbError('usage: gw-m2-stall-snapshot OUTPUT [IMAGE_BASE]')
|
|
thread = gdb.selected_thread()
|
|
if thread is None or not thread.is_stopped():
|
|
raise gdb.GdbError('select an already stopped frozen thread')
|
|
frame = gdb.newest_frame()
|
|
registers = {r: int(frame.read_register(r)) for r in ('rip', 'rsp', 'r13', 'r14')}
|
|
try:
|
|
result = capture(gdb.selected_inferior().read_memory, registers,
|
|
int(args[1], 0) if len(args) == 2 else 0x140000000)
|
|
# Exclusive creation preserves any earlier capture.
|
|
with open(args[0], 'x', encoding='utf-8') as output:
|
|
json.dump(result, output, indent=2)
|
|
output.write('\n')
|
|
except (ValueError, OSError) as error:
|
|
raise gdb.GdbError(str(error)) from error
|
|
gdb.write('Saved bounded lookup snapshot; FileDataID remains unresolved.\n')
|
|
Snapshot()
|
|
|
|
|
|
try:
|
|
import gdb
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
register_gdb(gdb)
|