shulkwisec
php to shell/Research//5 min read

PHP to Shell, Part 2: Unrestricted Upload to Webshell

A webshell from an upload endpoint needs two independent conditions, not one: the file has to land somewhere the web server will serve, and the server has to route that specific name to the PHP interpreter. Every bypass below attacks the second condition, or the gap between the two. I ran the ones that fit against the lab from part 1, and the result for each one is in the text, including the ones that didn’t fire — a bypass that fails on a hardened config is evidence about the config, worth keeping rather than dropping.

Extension is a filter concept, the handler map is a server concept

The upload filter almost always checks the filename against a blocklist. Execution, though, is decided by the web server’s handler map, which the filter usually never consults. Any extension the handler map routes to PHP executes, whether the filter’s blocklist knows the extension or not.

My lab’s Apache maps only .php, anchored (<FilesMatch "\.php$">). A plain .php upload executed immediately:

$ curl -F "file=@p1.php;filename=shell.php" http://127.0.0.1:8080/upload.php
stored: /uploads/shell.php
$ curl http://127.0.0.1:8080/uploads/shell.php
MARK1:49

The useful negative result came right after. Guides commonly list .phtml, .php5, .pht as “also execution extensions,” and on some LAMP configs they are — but on this one, where the handler map lists only .php, both .phtml and .php5 came back as literal source text, unexecuted, HTTP 200:

$ curl http://127.0.0.1:8080/uploads/shell.phtml
<?php echo "MARK2:".(7*7)."\n"; ?>

That’s the whole lesson in one request: spraying a wordlist of extensions is the wrong method. Read the server’s own config surface first — response headers, an exposed phpinfo(), a .htaccess the app happens to echo back, the Server banner — and test the exact suffixes that config actually maps, not a generic list.

The double extension that is actually reliable

shell.jpg.php is the shape worth trusting, because it doesn’t depend on which handler directive a given Apache build prefers for multi-extension names (AddHandler and AddType disagree on that in ways that are version-dependent). The final extension is .php, so any \.php$-anchored match still routes it:

$ curl -F "file=@p.php;filename=shell.jpg.php" http://127.0.0.1:8080/upload.php
$ curl http://127.0.0.1:8080/uploads/shell.jpg.php
MARK2:49

It only fails against a filter that checks the whole filename for .php rather than just the last extension — which is the actual fix, not “block more extensions.”

A polyglot passes the filter; routing decides if it runs

A GIF89a header is six bytes, which is enough for getimagesize() to accept the file as a valid 1×1 image — whether or not anything meaningful follows those six bytes. I built one with a PHP tag appended after the header and uploaded it under two names:

stored as poly.php -> GET returns: GIF89a...POLY:42     (executed)
stored as poly.gif -> GET returns: image/gif, 75 bytes   (inert)

Same bytes, same successful upload, opposite outcome. The filter passed both times — it’s a real, working polyglot — but execution is a separate question decided entirely by what the file is named and how the server routes that name. A report that calls “the polyglot was accepted” an RCE finding on its own is wrong; it’s a necessary condition, not the exploit.

.htaccess and .user.ini: two files, two different gates, and I hit both negative controls

.htaccess can rewrite Apache’s handler map for a directory — AddType application/x-httpd-php .jpg turns every .jpg in that folder into PHP. It needs AllowOverride to permit it. My lab’s config ships AllowOverride None, and the test showed exactly what that buys:

$ grep AllowOverride apache/httpd.conf
    AllowOverride None
$ curl http://127.0.0.1:8080/uploads/shell2.jpg
<?php echo "HTACCESS_RCE:".(8*8)."\n"; ?>    <- literal, unexecuted

The uploaded .htaccess had zero effect. That’s AllowOverride None doing exactly what it’s supposed to, confirmed rather than assumed.

.user.ini is a different mechanism entirely — it’s read by PHP itself (auto_prepend_file), which is why it works under nginx + php-fpm where .htaccess is meaningless. The flip side: mod_php never reads it. My lab is mod_php, and the result matched:

$ curl http://127.0.0.1:8080/uploads/probe.php
PROBE_ONLY              <- ran standalone, no prepend fired

Two different controls, two different negative results, each isolating exactly one thing: the Apache override policy in the first case, the choice of SAPI in the second. Neither negative control generalizes to “this app is safe” — they generalize to “this specific gate is closed here”; a php-fpm deployment with the same upload bug would need the .user.ini test re-run against that stack to know anything.

The upload race nobody gets right on the first try

PHP writes an in-flight upload to a temp path before the application runs. If something discloses that path (a phpinfo() dump of $_FILES, a debug page), a second request can include() it while the first is still uploading. I raced this against the lab’s LFI sink and got it wrong once before getting it right — worth keeping both attempts, because the failure is the actual lesson:

Attempt 1: <?php echo "RACE:".(9*9); <- no closing ?>, padded with 2MB of 'A'
  -> PHP Parse error: unexpected end of file

Attempt 2: <?php echo "RACE:".(9*9); ?> <- closing tag, then 2MB of 'A'
  -> included mid-upload: RACE:81

PHP compiles the entire included file before running any of it. Padding after an unclosed <?php isn’t harmless filler, it’s more PHP source with no terminator — the parse fails before your payload ever runs. Close the tag first, and everything after ?> is ordinary HTML the parser skips. Also worth noting for anyone still writing this up from an old reference: the temp filename on PHP 8.4 is php plus 21 random characters, not the historical six-character phpXXXXXX some tooling still assumes.

What actually kills this class

Every bypass above attacks the gap between “accepted by the filter” and “served as a static asset.” Close that gap and the whole category goes away at once:

  1. Server-generated storage names — the original filename survives only as metadata, never as the path anything resolves to.
  2. Store outside the document root, serve through a controller that sets its own Content-Type.
  3. Re-encode images through GD or Imagick when the asset actually is an image — this destroys anything appended after the real image data, which is the entire polyglot technique.
  4. Apply the extension allowlist to the final, server-chosen name, not the client’s.
  5. AllowOverride None; empty user_ini.filename; php_admin_flag engine off on anything writable by the app.

None of that is exotic. It’s the difference between a filter that checks a string and a pipeline that never gives the string a chance to become a path.

Part 3 moves to the other common entry point — include() reachable on user input — where the same question comes up in a different shape: what has to be readable, and what has to be includable, before a read becomes a shell.