shulkwisec
php to shell/Research//5 min read

PHP to Shell, Part 3: File Inclusion to Code Execution

Every escalation in this post starts from the same sink: include($_GET['page']), no allowlist, no suffix appended. On its own that’s a read primitive — file_get_contents with extra steps. It becomes RCE only when the included content can be made to contain PHP, which is a separate question with its own gates. I ran what I could against the lab from part 1; where a technique needed a stack I don’t have here — nginx, php-cgi, PHP 8.6 — I say so instead of reporting the literature claim as if I’d watched it happen.

Baseline first, because it’s easy to skip past: ?page=/etc/passwd against the unguarded include() returns the file verbatim. Everything past this point is about turning that read into execution.

Log poisoning, and the failure mode nobody mentions

Apache’s access log records the User-Agent header verbatim. Send a PHP tag as the UA, then include the log, and it should execute. My first attempt didn’t:

$ curl -A '<?php echo "LOGMARK".(7*7); ?>' http://127.0.0.1:8080/logme.php
$ tail -1 access.log
... "GET /logme.php HTTP/1.1" 200 226 "-" "<?php echo \"LOGMARK\".(7*7); ?>"

Apache’s log writer escapes embedded double quotes as \" — which is a PHP parse error the moment that line gets included. Worse: that one bad line then broke every subsequent include of the whole log file, including a second, correctly single-quoted attempt sent right after it. The log had to be truncated before the retry could prove anything:

$ curl -A "<?php echo 'LOGMARK'.(7*7); ?>" http://127.0.0.1:8080/logme.php
$ curl --data-urlencode 'page=<lab>/run/logs/access.log' http://127.0.0.1:8080/
LOGMARK49

Two things worth carrying forward: use single quotes in the payload if the included PHP needs any quoting at all, and know that one malformed line in a poisoned log can mask a working payload sent later — if an “it didn’t work” result might actually be a stale broken line ahead of it, check the log before concluding the technique failed. The more common real-world blocker, separate from this one, is permissions — on a stock Debian box the access log is 640 root:adm, and the web user usually isn’t in adm. My lab runs Apache as an unprivileged user over its own log directory, so that gate wasn’t exercised here; the technique working in this lab says nothing about whether it would work against a default Debian install.

Session files are a write primitive wearing a different costume

session.save_path holds one file per session, and the content is whatever the application wrote into $_SESSION, serialized. Any page that stores user input in the session — a display name, a language preference — gives you a write primitive into that file. I set a client-chosen session ID and a payload field, then included the session file by its known path:

$ curl -b 'PHPSESSID=labfixed01' --data-urlencode "note=<?php echo 'SESSMARK'.(8*8); ?>" \
    http://127.0.0.1:8080/session.php
$ curl --data-urlencode 'page=<lab>/run/sessions/sess_labfixed01' http://127.0.0.1:8080/
SESSMARK64

The session ID itself isn’t a usable injection point — PHP restricts it to [a-zA-Z0-9,-] and replaces anything invalid — but you don’t need the ID to carry the payload when the application will happily carry it for you in a field it trusts.

phar:// still executes; zip:// I couldn’t test here

phar:///path/to/archive.phar/shell.php includes the PHP file stored inside the archive — ordinary include semantics through a different wrapper, nothing deserialization-specific about it (that’s a separate trigger, covered in part 4). I built one with the CLI, where phar.readonly=0 can override the web SAPI’s default of 1 (which only blocks creating phars, not reading them):

$ php -d phar.readonly=0 make-phar.php lab.phar '<?php echo "PHARMARK".(6*6); ?>'
$ curl --data-urlencode 'page=phar://<lab>/lab.phar/payload.php' http://127.0.0.1:8080/
PHARMARK36

zip:// is the same idea against a zip entry instead of a phar entry, and I’d call it the equivalent vector — but my lab’s PHP build has no ext-zip installed, and installing it needed root I didn’t want to use for this build. That’s an environment gap, not a finding about the technique: don’t read “I didn’t test zip://” as “zip:// doesn’t work.”

The filter-chain trick: I confirmed the mechanism, not the exploit

This is the one I want to be most precise about, because it’s easy to overstate. php://filter applies conversions to a resource, and some iconv conversions don’t just transcode — they prepend bytes (escape sequences, BOMs) to the stream. Chain enough of them together, and you can manufacture arbitrary output from an input that was never written to disk, including a <?php ?> tag that then executes on include. The reference tool for building the full chain is Synacktiv’s php_filter_chain_generator.

What I actually ran was the underlying primitive, not the full chain:

$ php -r 'echo bin2hex(file_get_contents(
    "php://filter/convert.iconv.UTF8.CSISO2022KR/resource=php://temp"));'
1b242943

Four bytes, injected into what should have been an empty stream, confirming that a single iconv step really does prepend attacker-influenced bytes rather than just transcoding. I used that same wrapper to disclose source instead of executing anything:

$ curl --data-urlencode \
  'page=php://filter/convert.iconv.UTF8.CSISO2022KR/resource=<lab>/www/index.php' \
  http://127.0.0.1:8080/ | xxd | head -1
3c21 646f 6374 7970 6520 6874 6d6c          <!doctype html

That’s a read, through include, not an execution. Building the full chain that lands on a working <?php ?> tag — hundreds of filter segments, roughly 150 bytes per output byte — is more work than this session reached. So: the mechanism is real and I watched the bytes get injected, but I have not personally watched this chain execute code, and I’m not going to write “I confirmed RCE via filter chain” when what I confirmed is one step short of that. Two things are still worth knowing if you’re chasing this yourself: it needs iconv compiled in (check with a plain convert.base64-encode against a benign path first), and PHP 8.6 caps php://filter at 16 filters per chain by default, which closes the generated-chain approach entirely on anything running that version or newer.

With the default allow_url_include=Off, both data:// and php://input came back blocked, as expected — I didn’t flip the ini to confirm the positive case, so that half of the claim stays sourced to the PHP manual rather than to this lab.

What actually closes this category

  1. open_basedir scoped to the application’s own tree — logs and session directories outside it kills most of this post in one setting.
  2. allow_url_include=Off, which is the default since PHP 5.2 and worth stating explicitly as a confirmed mitigation whenever you check it, rather than assuming it.
  3. No include() on user input at all — map to an allowlist of template identifiers instead of a filesystem path, which removes the entire category rather than patching each wrapper.
  4. PHP 8.6 or later closes the filter-chain approach specifically, via the 16-filter cap.

Part 4 moves from a read-turned-write problem to a pure logic one: unserialize() on attacker-controlled input, and why “a gadget chain exists” and “this target is exploitable” are two different claims that get conflated constantly.