Skip to content

Latest commit

 

History

History
321 lines (267 loc) · 16.9 KB

README.md

File metadata and controls

321 lines (267 loc) · 16.9 KB

PHP2Shell Test Lab

Status: RUNNING — container php2shell-lab serving on http://127.0.0.1:8090.

Purpose

Test target for the PHP2Shell PHP vulnerabilities (both CVSS 9.2, unauthenticated, triggered during request startup before any app code runs):

CVE Mechanism
CVE-2026-109618 RCE via OOM corruption: hitting memory_limit makes an emalloc failure call zend_bailout() → longjmp to the request boundary → cleanup is skipped → half-initialized structs are later freed/read
CVE-2026-109619 RCE via stale global state: bailout skips the per-request session-global reset; dangling zval* pointers (e.g. PS(http_session_vars), PS(rfc1867_progress)) survive into the next request on the same persistent mod_php worker → heap UAF, memory disclosure, RCE

Entry point: the default-on session.upload_progress feature. A multipart POST containing a PHP_SESSION_UPLOAD_PROGRESS field makes the PHP core write an upload-progress record into the session file during request startup (in the rfc1867 multipart parser callback, pre-RINIT). With session.use_strict_mode = 0 the attacker chooses the session ID via the PHPSESSID POST field.

  • Affected: PHP 5.4 → 8.5.11
  • Patched: 8.4.27 (this lab runs 8.4.26 — vulnerable); also 8.2.35 / 8.3.36 / 8.5.12 (Oct 21, 2026)
  • Highest-risk SAPI: Apache mod_php — this lab runs exactly that config

Image, version, SAPI

Property Value
Image docker.io/library/php:8.4.26-apache-bookworm (ID df868eae5dac, cached locally)
PHP version 8.4.26 (podman exec php2shell-lab php -v) — vulnerable, 8.4.26 < 8.4.27 (patch release of Oct 21, 2026)
SAPI apache2handler (Apache 2.4.68 mod_php, mpm_prefork) — the highest-risk configuration per the advisory
Patched image (for comparison) docker.io/library/php:8.4.27-apache-bookworm (ID e4da5b539e5c) — cached locally

Start / stop / remove

Reconstructed from podman inspect php2shell-lab (no volumes, no env overrides, no cap changes, default pasta network, image default command apache2-foreground):

# START (image is cached locally; --name must be free)
podman run -d --name php2shell-lab -p 8090:80 docker.io/library/php:8.4.26-apache-bookworm

# web root is the stock /var/www/html of the image; lab files are copied in (sources in this dir):
podman cp upload.php   php2shell-lab:/var/www/html/upload.php
podman cp version.php  php2shell-lab:/var/www/html/version.php
podman cp index.html   php2shell-lab:/var/www/html/index.html
podman cp probe2.php   php2shell-lab:/var/www/html/probe2.php   # diagnostic, optional
# (probe2.php was added by the takeover session; the other three are the original lab files)

# STOP
podman stop php2shell-lab

# REMOVE (use -f if running)
podman rm php2shell-lab

# OBSERVE
podman logs php2shell-lab 2>&1 | tail          # apache logs go to container stdout (log files are symlinks to /dev/stdout)
podman exec php2shell-lab php -v
podman exec php2shell-lab sh -c 'ls -la /tmp/sess_* 2>/dev/null'

Served configuration (verified, as seen by the apache worker)

From GET /version.php (live output, Oct 26 2026):

{
    "php_version": "8.4.26",
    "session.upload_progress.enabled": "1",
    "session.use_strict_mode": "0",
    "memory_limit": "128M",
    "post_max_size": "8M",
    "max_file_uploads": "20",
    "session.serialize_handler": "php",
    "sapi": "apache2handler",
    "session_id": ""
}

Additional defaults (verified via php -r 'ini_get(...)' inside the container; the image ships no active main php.ini — only conf.d/{opcache,sodium}.ini — so these are the PHP 8.4 compile-time defaults):

INI Value Note
session.upload_progress.enabled 1 entry point is on
session.upload_progress.cleanup 1 default ON: the progress record is deleted and the session file re-flushed (empty) at end of upload — see transcript
session.upload_progress.freq 1% byte threshold between record updates
session.upload_progress.min_freq 1 default 1 second minimum time between updates
session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS POST field that arms the feature
session.upload_progress.prefix upload_progress_ session-key prefix
session.use_strict_mode 0 attacker-chosen ID accepted
session.use_only_cookies 1 ⚠️ PHP 8.4 default is 1 (older PHP: 0). Handler-level session_start() will NOT adopt a POST/GET PHPSESSID — it generates a random ID. The upload-progress path is unaffected: it sets PS(id) directly from the POST field (see "Mechanism")
session.save_path '' (unset) files handler falls back to the temp dir → session files land in /tmp inside the container, not /var/lib/php/sessions
session.save_handler files
session.serialize_handler php
session.auto_start 0
memory_limit 128M OOM-bailout target (CVE-2026-109618)
post_max_size 8M keep uploads ≤ ~8M so parsing reaches the OOM path instead of a POST-size abort
max_file_uploads 20

Endpoints

Base URL: http://127.0.0.1:8090

Endpoint Purpose Expected response
GET /version.php JSON of the served config (php version, session/memory/post settings, SAPI) the JSON block above
POST /upload.php multipart sink; echoes a JSON summary of $_FILES/$_POST — the vulnerable progress path runs before this code 200 + JSON: files_uploaded, files[].name/.size, post_fields, raw_post_size, content_type, session_id (empty — handler never starts a session), php_version
POST /probe2.php diagnostic: reports session_status() before/after session_start() and $_SESSION contents 200 + JSON incl. session_status_before, session_id, session_keys, session
GET /index.html manual test form HTML form

Verification transcript (takeover session, Oct 26 2026)

1. Container + version

$ podman ps --format '{{.Names}}  {{.Ports}}  {{.Status}}'
php2shell-lab  0.0.0.0:8090->80/tcp  Up

$ podman exec php2shell-lab php -v
PHP 8.4.26 (cli) (built: Oct 21 2026 00:31:48) (NTS)
Zend Engine v4.4.26, Copyright (c), by the Zend Technologies
    with Zend OPcache v8.4.26, Copyright (c), by the Zend Technologies

2. Served config

$ curl -s http://127.0.0.1:8090/version.php
{
    "php_version": "8.4.26",
    "session.upload_progress.enabled": "1",
    "session.use_strict_mode": "0",
    "memory_limit": "128M",
    "post_max_size": "8M",
    "max_file_uploads": "20",
    "session.serialize_handler": "php",
    "sapi": "apache2handler",
    "session_id": ""
}

3. Upload-progress probe, attacker-chosen session ID (default cleanup=1)

$ head -c 200000 /dev/zero > /tmp/opencode/probe-200k.bin
$ curl -s -i --max-time 10 \
    -F "PHP_SESSION_UPLOAD_PROGRESS=1" -F "PHPSESSID=labcheck-0001" \
    -F "file=@/tmp/opencode/probe-200k.bin" \
    http://127.0.0.1:8090/upload.php
HTTP/1.1 200 OK
Date: Mon, 26 Oct 2026 21:19:24 GMT
Server: Apache/2.4.68 (Debian)
X-Powered-By: PHP/8.4.26
Content-Length: 402
Content-Type: application/json; charset=utf-8

{
    "files_uploaded": 1,
    "files": [ { "name": "probe-200k.bin", "size": 200000 } ],
    "post_fields": [ "PHP_SESSION_UPLOAD_PROGRESS", "PHPSESSID" ],
    "raw_post_size": 200458,
    "content_type": "multipart/form-data; boundary=...",
    "session_id": "",
    "php_version": "8.4.26"
}

Observations

  • HTTP 200, correct JSON summary.
  • No Set-Cookie header — expected: the upload-progress code sets PS(send_cookie) = 0 when the ID comes from a non-cookie source (verified in ext/session/session.c).
  • Session file evidence (note: files land in /tmp in this image, not /var/lib/php/sessions):
$ podman exec php2shell-lab sh -c 'ls -la /tmp/sess_labcheck-0001; cat /tmp/sess_labcheck-0001'
-rw------- 1 www-data www-data 0 Oct 26 21:19 /tmp/sess_labcheck-0001
(empty — 0 bytes)

The file is created during request startup under the attacker-chosen ID (timestamp matches the probe exactly; the handler never calls session_start()), then re-flushed empty because session.upload_progress.cleanup=1 deletes the record at end-of-upload (see Mechanism below).

4. Is the record visible to app code? (treatment vs control via /probe2.php)

$ curl -s --max-time 10 -F "PHP_SESSION_UPLOAD_PROGRESS=1" -F "PHPSESSID=labcheck-0002" \
    -F "file=@/tmp/opencode/probe-200k.bin" http://127.0.0.1:8090/probe2.php
{
    "session_status_before": 1,          # PHP_SESSION_NONE — the progress session is already gone
    "session_id": "b6ab3bce5b4798f8e38a47bd23927032",   # NEW random ID (use_only_cookies=1: POST ID not adopted by session_start())
    "session_keys": [],
    "session": [],
    "php_version": "8.4.26"
}
$ podman exec php2shell-lab ls -la /tmp/sess_labcheck-0002
-rw------- 1 www-data www-data 0 Oct 26 21:24 /tmp/sess_labcheck-0002

$ curl -s --max-time 10 -F "PHPSESSID=labcheck-ctrl1" \
    -F "file=@/tmp/opencode/probe-200k.bin" http://127.0.0.1:8090/probe2.php
{
    "session_status_before": 1,
    "session_id": "3b666df3ed05e5b579ee4fe0603d56b6",   # random ID again
    "session_keys": [],
    "session": [],
    "php_version": "8.4.26"
}
$ podman exec php2shell-lab ls -la /tmp/sess_labcheck-ctrl1
ls: cannot access '/tmp/sess_labcheck-ctrl1': No such file or directory   # control: no progress record path taken

Observations

  • With PHP_SESSION_UPLOAD_PROGRESS present → a session file is created under the POST ID at request startup (treatment). Without it → no file (control). This isolates the upload-progress code path as the creator.
  • The progress session does not stay active for the handler: session_status_before is PHP_SESSION_NONE in both cases, and session_start() in the handler generates a random ID because PHP 8.4's default session.use_only_cookies=1 blocks POST/GET ID adoption in php_session_start() (the progress path itself sets PS(id) directly and is unaffected).
  • $_SESSION in the handler is empty (record already cleaned up per cleanup=1).

5. Full progress record (with cleanup=0 toggle)

To prove the record content, session.upload_progress.cleanup=0 was temporarily set (echo session.upload_progress.cleanup=0 > /usr/local/etc/php/conf.d/zz-cleanup-test.ini, apachectl restart), probed, then the INI removed and Apache restarted (state restored — cleanup=1 re-verified).

$ curl -s --max-time 10 -F "PHP_SESSION_UPLOAD_PROGRESS=1" -F "PHPSESSID=labcheck-0004" \
    -F "file=@/tmp/opencode/probe-200k.bin" http://127.0.0.1:8090/upload.php
{ ... "files_uploaded": 1, ... "session_id": "", "php_version": "8.4.26" }

$ podman exec php2shell-lab sh -c 'ls -la /tmp/sess_labcheck-0004; cat /tmp/sess_labcheck-0004'
-rw------- 1 www-data www-data 367 Oct 26 21:29 /tmp/sess_labcheck-0004
upload_progress_1|a:5:{s:10:"start_time";i:1793050145;s:14:"content_length";i:200458;
s:15:"bytes_processed";i:200458;s:4:"done";b:1;s:5:"files";a:1:{i:0;a:7:{s:10:"field_name";
s:4:"file";s:4:"name";s:14:"probe-200k.bin";s:8:"tmp_name";s:27:"/tmp/phpq3b6uvlriagi6IxbJA0";
s:5:"error";i:0;s:4:"done";b:1;s:10:"start_time";i:1793050145;s:15:"bytes_processed";i:200000;}}}

This is the exact record format the progress path writes — key upload_progress_<POST value of PHP_SESSION_UPLOAD_PROGRESS> (here upload_progress_1), with start_time, content_length, bytes_processed, done, and a per-file array (field_name, name, tmp_name, error, done, start_time, bytes_processed).

Mechanism (from PHP 8.4 source, ext/session/session.c + main/rfc1867.c)

The session extension wraps the rfc1867 multipart-parser callback (php_session_rfc1867_callback, registered at module init). Relevant flow for one probe request:

  1. MULTIPART_EVENT_START (request startup, body parse, pre-RINIT): php_rinit_session_globals() — the per-request reset of session globals (PS(id)=NULL, PS(http_session_vars) UNDEF, PS(session_status)=none, …). A php_session_rfc1867_progress struct is allocated and stored in PS(rfc1867_progress).
  2. MULTIPART_EVENT_FORMDATA: captures PHPSESSID POST value into progress->sid and PHP_SESSION_UPLOAD_PROGRESS value into the session key.
  3. MULTIPART_EVENT_FILE_START: PS(id) = progress->sid (attacker ID, directly) — this is why the POST field works even with use_only_cookies=1; sets PS(send_cookie)=0 (hence no Set-Cookie); initializes the progress record array.
  4. FILE_START/FILE_DATA/FILE_END: php_session_rfc1867_update() — rate-limited by freq (1%) and min_freq (1 s) — calls php_session_initialize() (starts the session under the attacker ID, loads/creates /tmp/sess_<id>), inserts the record into PS(http_session_vars), and php_session_flush(1) writes it to disk.
  5. MULTIPART_EVENT_END: with default cleanup=1 → php_session_rfc1867_cleanup() deletes the record and re-flushes (→ 0-byte file, as observed), then php_rshutdown_session_globals() releases the session state. Progress struct freed.

The CVE surface: if zend_bailout() longjmps out of the mid-parse state (e.g. OOM under 128M memory_limit during step 4), steps 4–5 cleanup never run: PS(rfc1867_progress), PS(http_session_vars) (a live zval), PS(id), etc. are left dangling. The normal request-boundary resets (php_rinit_session_globals/php_rshutdown_session_globals) that would clear them are exactly what bailout skips → the stale globals (dangling zval*/zend_string* pointers) survive into the next request on the same prefork worker → heap UAF / memory disclosure / RCE (CVE-2026-109619), and half-initialized structs are freed/read on teardown (CVE-2026-109618).

Notes for the exploit / scanner agents

  • Vulnerable: 8.4.26 (apache2handler). Patched for comparison: 8.4.27 image is cached locally (e4da5b539e5c) — podman run -d -p 8091:80 php:8.4.27-apache-bookworm + same web files.
  • Trigger shape: multipart/form-data POST with fields PHP_SESSION_UPLOAD_PROGRESS=<any> + PHPSESSID=<attacker-chosen-id> and ≥1 file part. File part name is arbitrary (file used here).
  • Sizing knobs: memory_limit=128M, post_max_size=8M (stay under it so the request reaches the OOM path instead of a 2M/POST-abort), max_file_uploads=20, min_freq=1s, freq=1%.
  • Session files: inside the container at /tmp/sess_<id> (save_path unset → temp dir), mode 600, owner www-data. With defaults they end 0 bytes (cleanup=1); set session.upload_progress.cleanup=0 in /usr/local/etc/php/conf.d/ + apachectl restart to keep the record (format shown in §5).
  • No Set-Cookie comes back for POST-chosen IDs — don't use cookie presence as a success signal; use the created /tmp/sess_<id> file (timestamped at request startup) or the record content.
  • session.use_only_cookies=1 (PHP 8.4 default) only affects session_start() in handler code (random ID generated); it does not affect the upload-progress path, which sets PS(id) directly.
  • Worker stickiness: mod_php + mpm_prefork — a given client connection is handled by one prefork worker; to hit the same worker as the baiting request, reuse the same connection pattern / target the same child (workers are PIDs under the apache parent; see podman exec php2shell-lab ps aux).
  • Logs: Apache log files are symlinks to /dev/stdout//dev/stderr — use podman logs php2shell-lab, not tail /var/log/apache2/* (tail on the symlink will block).
  • Reset between experiments: apachectl restart inside the container gives fresh workers (clears any stale per-request state) — verified working during this session.
  • Cleanup of this takeover session: stray self-test containers removed: scanner-selftest-vuln-1664448 (8092), scanner-selftest-patched-1664448 (8093), exploit-selftest-1666661_7646 (8093), exploit-selftest-8094 (8094). podman ps -a now shows only php2shell-lab (+ pre-existing vrf-lab-redos-b3dc3b81, untouched); ports 8092/8093/8094 confirmed free (connection refused).
  • Diagnostic session files left in the container (harmless, evidence of probes): /tmp/sess_labcheck-0001 (0 B), sess_labcheck-0002 (0 B), sess_labcheck-sidtest (0 B), sess_labcheck-0004 (367 B, full record), plus two random-ID 0-byte files from /probe2.php. Remove with podman exec php2shell-lab rm -f /tmp/sess_* if you want a pristine start.

Local file inventory

File In container Purpose
version.php /var/www/html/version.php served-config JSON
upload.php /var/www/html/upload.php multipart sink / probe target
index.html /var/www/html/index.html manual test form
probe2.php /var/www/html/probe2.php diagnostic (session status/contents) — added by takeover session