367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
50 lines
2.7 KiB
Python
50 lines
2.7 KiB
Python
"""Observe a fixed source-traced helper at its natural first execution.
|
|
|
|
The target can legitimately be cold/NX before the client uses it. This observer
|
|
never invokes it or changes protection: hardware entry observation only. No
|
|
argument/endpoint/session buffers are read, including at a rejected caller.
|
|
"""
|
|
import hashlib,os,struct
|
|
from pathlib import Path
|
|
ENTRY=0x140885f60
|
|
PREFIX=b''
|
|
OBSERVE_NATURAL_FIRST_USE=True
|
|
CALLERS={0x141e4f620,0x142175750}
|
|
|
|
def inspect(read,executable,stack,aligned=True):
|
|
if not aligned:return {'available':False,'reason':'stack-alignment'}
|
|
caller=struct.unpack('<Q',read(stack,8))[0]
|
|
if caller not in CALLERS:return {'available':False,'reason':'unexpected-real-caller'}
|
|
call=read(caller-5,5)
|
|
if call[0]!=0xe8 or caller+struct.unpack('<i',call[1:])[0]!=ENTRY:return {'available':False,'reason':'caller-instruction-identity'}
|
|
result={'kind':'natural-endpoint-copy-code-observation','actual_caller':hex(caller),
|
|
'engine_calls_made':False,'argument_buffers_read':False,'memory_protection_changed':False,'pages':[]}
|
|
for address in (0x140885000,0x140886000):
|
|
if executable(address,4096):
|
|
code=read(address,4096)
|
|
result['pages'].append({'address':hex(address),'sha256':hashlib.sha256(code).hexdigest()})
|
|
return result
|
|
|
|
def capture(pid,registers):
|
|
intervals=[]
|
|
for line in Path(f'/proc/{pid}/maps').read_text().splitlines():
|
|
fields=line.split();a,b=(int(v,16) for v in fields[0].split('-'));intervals.append((a,b,fields[1]))
|
|
fd=os.open(f'/proc/{pid}/mem',os.O_RDONLY);saved={}
|
|
try:
|
|
def executable(address,size):return any(a<=address and address+size<=b and 'r' in p and 'x' in p for a,b,p in intervals)
|
|
def read(address,size):
|
|
code=address in (0x140885000,0x140886000) and size==4096
|
|
if not(code or (address==registers.rsp and size==8) or (address in {c-5 for c in CALLERS} and size==5)):raise ValueError('Fixed code/return-PC ranges only')
|
|
if not any(a<=address and address+size<=b and 'r' in p for a,b,p in intervals):raise ValueError('Fixed code unavailable')
|
|
result=os.pread(fd,size,address)
|
|
if len(result)!=size:raise ValueError('Incomplete fixed code')
|
|
if code:saved[address]=result
|
|
return result
|
|
try:
|
|
report=inspect(read,executable,registers.rsp,registers.rsp%16==8)
|
|
for address,code in saved.items():
|
|
path=Path(__file__).resolve().parents[1]/'runtime/native-handler-code'/f'{address:x}.bin';path.write_bytes(code);path.chmod(0o600)
|
|
return report
|
|
except (OSError,ValueError,struct.error):return {'available':False,'reason':'fixed-code-unavailable','argument_buffers_read':False}
|
|
finally:os.close(fd)
|