49 lines
3.1 KiB
Python
49 lines
3.1 KiB
Python
"""Real hardware write-watch lifetime against an own ELF child; no WoW claim."""
|
|
import ctypes as C,json,os,struct,sys,tempfile,time
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import engine_readiness_watch as w
|
|
abi = Path(sys.argv[1]) if len(sys.argv) > 1 else None
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
root=Path(temp);(root/'runtime').mkdir();r,fd=os.pipe();pid=os.fork()
|
|
if pid==0:
|
|
try:
|
|
os.close(r);lib=C.CDLL(None);lib.mmap.restype=C.c_void_p;lib.mmap.argtypes=[C.c_void_p,C.c_size_t,C.c_int,C.c_int,C.c_int,C.c_long]
|
|
page=w.FLAG&~4095;code=0x140001000
|
|
collection=0x1456ad44d;collection_page=collection&~4095
|
|
if lib.mmap(collection_page,4096,3,0x100022,-1,0)!=collection_page:os._exit(5)
|
|
if lib.mmap(page,4096,3,0x100022,-1,0)!=page or lib.mmap(code,4096,3,0x100022,-1,0)!=code:os._exit(2)
|
|
one=b'\xc6\x05'+struct.pack('<i',w.FLAG-(code+7))+b'\x01\xc3'
|
|
zero=b'\xc6\x05'+struct.pack('<i',w.FLAG-(code+32+7))+b'\x00\xc3'
|
|
C.memmove(code,one,len(one));C.memmove(code+32,zero,len(zero))
|
|
C.memmove(code+64,b'\xc6\x05'+struct.pack('<i',collection-(code+71))+b'\x01\xc3',8)
|
|
C.memmove(code+96,b'\xc6\x05'+struct.pack('<i',collection-(code+103))+b'\x00\xc3',8)
|
|
if lib.mprotect(C.c_void_p(code),4096,5):os._exit(3)
|
|
os.write(fd,b'x');os.close(fd);time.sleep(.15)
|
|
a=C.CFUNCTYPE(None)(code);b=C.CFUNCTYPE(None)(code+32)
|
|
c=C.CFUNCTYPE(None)(code+64);d=C.CFUNCTYPE(None)(code+96)
|
|
end=time.monotonic()+3
|
|
while time.monotonic()<end:a();b();c();d();time.sleep(.02)
|
|
os._exit(0)
|
|
except BaseException:os._exit(4)
|
|
os.close(fd)
|
|
watch = None
|
|
try:
|
|
assert os.read(r,1)==b'x';w.ROOT=root
|
|
result=root/'runtime/watch.json';watch=w.Watch(pid,timeout=1,result_path=result,abi_path=abi)
|
|
deadline=time.monotonic()+2
|
|
while not watch.done and time.monotonic()<deadline:watch.poll();time.sleep(.005)
|
|
data=json.loads(result.read_text());assert data['status']=='public-ready-write-observed' and data['hardware_registers_restored'] and not watch.attached
|
|
assert data['metadata']['instruction_after_write']=='0x140001007'
|
|
assert data['metadata']['public_flag_value']==1 and not data['metadata']['authentication_buffers_read']
|
|
watch=w.Watch(pid,timeout=1,result_path=result,abi_path=abi,component='collection')
|
|
deadline=time.monotonic()+2
|
|
while not watch.done and time.monotonic()<deadline:watch.poll();time.sleep(.005)
|
|
data=json.loads(result.read_text());assert data['status']=='public-ready-write-observed' and data['hardware_registers_restored'] and not watch.attached
|
|
assert data['metadata']['instruction_after_write']=='0x140001067' and data['metadata']['public_flag_value']==0
|
|
os.waitpid(pid,0)
|
|
finally:
|
|
os.close(r)
|
|
if watch is not None and watch.attached:watch.cancel()
|
|
print('Own-child fixed write watch identifies the real instruction and restores hardware registers without flag/RIP writes.')
|