In Part 1, we built a reproducible PHP2Shell debugging lab with Docker, DWARF symbols, and LLDB. Now that the exploit works and we can step through PHP’s source code, it’s time to answer a more interesting question: why does it work?
What You’ll Learn
In this article, I’ll use a simple vulnerability analysis workflow that I call RIWV.
It consists of four steps:
- R — Reproduce: Reproduce the vulnerability in a controlled environment.
- I — Inspect: Examine the program state at the exploit’s critical point—the crime scene.
- W — Work Backwards: Reconstruct the causal chain that led to the vulnerability, not just the call stack.
- V — Validate: Test the root cause hypothesis against source code and runtime evidence.
We’ll apply RIWV to PHP2Shell, starting from a working exploit and gradually working our way back to the underlying vulnerability.
By the end, you’ll not only understand what makes PHP2Shell possible, but also have a practical workflow you can apply to other known vulnerabilities.
The Crime Scene
We’ve already completed the first step—Reproduce—in Part 1.
Now let’s move on to Inspect.
From the exploit script, we know zif_system() is called to execute the payload, let us try to set a breakpoint on zif_system(). Below is a screenshot when zif_system() is hit during exploit execution: (NOTE: if you are curious why windbg command sytax works in my lldb, that is because I am using my own WinLLDB, https://github.com/albertlab0/WinLLDB , an extension to support windbg command syntax)

From this “crime scene”, we notice that zif_system() with the first argument execute_data (type zend_execute_data) leads to the final payload execution via system() call.
Let us dive into the first parameter execute_data.
dt execute_data -r2Local var @ 0x00007658`b4f6d1f8 Type Ptr64 _zend_execute_data0x00007658`b4f6d1f8 +0x000 opline : 0x00000000`00000000 _zend_op +0x008 call : 0x00000000`00000000 _zend_execute_data +0x010 return_value : 0x000060ce`03f0dd70 _zval_struct +0x018 func : 0x00000000`00000000 _zend_function +0x020 This : _zval_struct +0x000 value : _zend_value +0x000 lval : 0n0 +0x000 dval : 0 +0x000 counted : 0x00000000`00000000 _zend_refcounted +0x000 str : 0x00000000`00000000 _zend_string +0x000 arr : 0x00000000`00000000 _zend_array +0x000 obj : 0x00000000`00000000 _zend_object +0x000 res : 0x00000000`00000000 _zend_resource +0x000 ref : 0x00000000`00000000 _zend_reference +0x000 ast : 0x00000000`00000000 _zend_ast_ref +0x000 zv : 0x00000000`00000000 _zval_struct +0x000 ptr : 0x00000000`00000000 Void +0x000 ce : 0x00000000`00000000 _zend_class_entry +0x000 func : 0x00000000`00000000 _zend_function +0x000 ww : (unnamed struct) +0x008 u1 : (unnamed union) +0x000 type_info : 0x0 +0x000 v : (unnamed struct) +0x00c u2 : (unnamed union) +0x000 next : 0x1 +0x000 cache_slot : 0x1 +0x000 opline_num : 0x1 +0x000 lineno : 0x1 +0x000 num_args : 0x1 +0x000 fe_pos : 0x1 +0x000 fe_iter_idx : 0x1 +0x000 guard : 0x1 +0x000 constant_flags : 0x1 +0x000 extra : 0x1 +0x030 prev_execute_data : 0x00000000`00000000 _zend_execute_data +0x038 symbol_table : 0x00000000`00000000 _zend_array +0x040 run_time_cache : 0x00000000`00000000 +0x048 extra_named_params : 0x00000000`00000000 _zend_array
execute_data structure size is 0x50 bytes. At offset 0x20+0xc This.u2.num_args = 1. which means it has 1 parameter.and after that, The parameter is defined by ZEND_CALL_ARG, the arguments infomration.
ZEND_CALL_FRAME_SLOT ZEND_CALL_VAR_NUM(call, n) ZEND_CALL_ARG(call, n)
So ZEND_CALL_ARG(call, 1) expands to:
ZEND_CALL_VAR_NUM(call, 0)
((zval*)(call)) + ZEND_CALL_FRAME_SLOT
ZEND_CALL_FRAME_SLOT = sizeof(zend_execute_data) / sizeof(zval) = 80 / 16 = 5
Which gives: (zval*)call + 5 = call + 5×16 = call + 0x50
Each parameter’s type is zend_value:
struct _zval_struct zval;...struct _zval_struct { zend_value value; // +0x00 (8 bytes) — union of lval/dval/str/arr/obj/... union { uint32_t type_info; // +0x08 — full type tag (type + flags) struct { uint8_t type; // active type (IS_STRING=6, IS_ARRAY=7, …) uint8_t type_flags; union { uint16_t extra; } u; } v; } u1; // +0x08 (4 bytes) union { uint32_t next; // hash collision chain uint32_t num_args; // argument count (for EX(This)) uint32_t cache_slot; // ... other interpretations } u2; // +0x0C (4 bytes)};// total: 16 bytes// zend_types.h:315typedef union _zend_value { zend_long lval; double dval; zend_string *str; // ← arg1.value.str zend_array *arr; zend_object *obj; // ... other types} zend_value;// zend_types.h:335struct _zval_struct { zend_value value; // +0x00 (8 bytes) — the payload union { uint32_t type_info; // ← arg1.u1.type_info struct { uint8_t type; // active type (IS_STRING = 6) uint8_t type_flags; ... } v; } u1; // +0x08 (4 bytes) — type tag union { uint32_t num_args; // ← used for EX(This).u2 at execute_data+0x2C uint32_t next; ... } u2; // +0x0C (4 bytes)};
That’s why arg1 lives at execute_data+0x50 in the diagram — it’s the first zval slot right after the 80-byte zend_execute_data frame. The first argument is type is IS_STRING(6), and the string value is at 0x60, a structure of zend_string.
Let us dump it to see whether it matchefs our payload string:
00007658`b4f6d270 "i=$(awk '$4=="01"{print $10;exit}' /proc/net/tcp"00007658`b4f6d2a0 "*);f=5;for x in /proc/self/fd/*;do [ "$(readlink"00007658`b4f6d2d0 " "$x")" = "socket:[$i]" ]&&f=${x##*/};done;eval "00007658`b4f6d300 ""{ echo 46a3c599b8b8066b;id;sha256sum \"$(which "00007658`b4f6d330 "print-flag)\";print-flag;}>&$f 2>&$f""
Below is the full memory layout of the crafted execute_data

Eventually, zif_system() ends up with calling system() and execute the payload.

To be continued.
Leave a Reply