shulkwisec
php to shell/Research//4 min read

PHP to Shell, Part 1: Building an Isolated Lab Worth Trusting

Before testing any upload or inclusion bug, I wanted a target where “it can’t reach the internet” was a measured fact, not a sentence in a README. This post is that target: how it’s built, what it actually runs, and the seven checks that prove the containment claim instead of assuming it from the config.

The target

Two deliberately broken endpoints, nothing else:

  • POST /upload.php — move_uploaded_file() with no extension check, no MIME check, no content check, straight into the web root.
  • GET /?page= — include($_GET['page']), no allowlist, no realpath(), no suffix.

Everything in parts 2 and 3 of this series runs against one or the other of those two lines.

Why native packages, not a container

libapache2-mod-php8.4 was already installed on the build host and matched the CLI version exactly, so a standalone httpd.conf plus mod_php gives the target in one command. A container would have needed a pull plus an in-image PHP 8.4 install (the base image ships 8.2), which is more network dependency and a version-drift risk for no isolation gain — the containment in both cases comes from the same kernel primitive, a network namespace. docker --network none and bwrap --unshare-net are the same idea with a different frontend.

Stack, verified with php -v, apache2 -v, dpkg-query:

Component Version
OS Debian-based Linux, kernel 7.1.5
Apache 2.4.68, prefork MPM
PHP 8.4.23, mod_php, SAPI apache2handler
bubblewrap 0.11.2
socat 1.8.1

Extension dir has phar.so and nothing else beyond the Core/standard set — no zip, no curl, no opcache. That matters later: it closes off zip:// as a wrapper and keeps results reproducible between runs, at the cost of not being able to reproduce zip://-based inclusion here at all.

How it’s wired

host                                        sandbox (new network namespace)
┌──────────────────────────────┐            ┌──────────────────────────────────┐
│ curl 127.0.0.1:8080           │            │  lo 127.0.0.1/8   (no other iface)│
│        │                      │            │  ip route: (empty)                │
│        ▼                      │            │                                    │
│ socat  TCP-LISTEN 127.0.0.1   │  unix      │  socat UNIX-LISTEN bridge.sock     │
│        └──── UNIX-CONNECT ────┼──socket────┼──►      │                          │
└────────────────────────────── ┘  (file)    │         ▼ TCP 127.0.0.1:8080       │
                                             │  apache2 (prefork, mod_php 8.4)   │
                                             │  docroot: lab/www                 │
                                             └──────────────────────────────────┘

bwrap --unshare-net --ro-bind / / --bind <lab> <lab> --tmpfs /tmp puts Apache in a network namespace that comes up with only a loopback interface — no veth, no default route, nowhere to send a packet. The door in is a Unix socket, not a network path: socat inside the namespace listens on a file (bridge.sock) and forwards to 127.0.0.1:8080; socat on the host listens on 127.0.0.1:8080 and connects to that file. Traffic crosses the boundary over the filesystem, and the only thing with a port open on the host side is bound to loopback. No Docker, no bridge interface, no NAT, no published port — the usual ways a “sandboxed” service quietly gets a real route out don’t exist here because nothing was built that could carry one.

The filesystem split is the opposite of the network split, on purpose: the host filesystem is mounted read-only inside the sandbox (so LFI against real files works like it would on a real box), except the lab’s own directory, which is read-write, and /tmp, which is a private tmpfs that disappears with the process.

Fail-closed is the property that makes this worth trusting: if bubblewrap is missing or user namespaces are disabled, the start script exits non-zero and nothing listens on :8080. There is no code path that falls back to running Apache unsandboxed.

Proving it instead of asserting it

A verification script runs seven checks and exits non-zero if any fail. The two that matter most:

Namespace separation. Compare /proc/<pid>/ns/net for Apache against the verifying shell:

host shell netns: net:[4026531833]
apache    netns: net:[4026532951]

Different numbers, confirmed for the master process and all three prefork children.

Egress, proven blocked from inside, with a control proving the host itself still has internet (so “blocked” means the namespace, not a dead network link):

# inside the sandboxed PHP process
tcp 1.1.1.1:443  = BLOCKED (101 Network is unreachable)
http example.com = BLOCKED (getaddrinfo failed: Temporary failure in name resolution)
dns example.com  = BLOCKED (no resolver reachable)

# on the host, same moment
host curl https://example.com -> 200

The differential matters: if the host also had no connectivity, “BLOCKED” inside the sandbox would prove nothing about the sandbox. It proves something here because the host’s own curl succeeds in the same breath.

The filesystem claim gets the same treatment — a PHP script run inside the sandbox writes a probe file, and the check looks for that file on the host afterward, confirming /tmp really is a private tmpfs and not just a directory PHP happened not to touch.

What this lab is not

Worth stating plainly, because every result in this series depends on knowing the edges of the claim:

  • Not a VM. It’s namespaces and bind mounts sharing the host kernel. A kernel-level escape is out of scope for anything built this way; that needs a VM, not a netns.
  • Not a confidentiality boundary. The host filesystem is readable (read-only, but readable) from inside, which is the point for LFI realism — PHP can read /etc/passwd the same way it could on a real box. If the goal were “nothing in this sandbox can see anything about the host,” the bind would need to be an explicit allowlist instead of --ro-bind / /.
  • No callbacks work. A reverse shell, an SSRF to an external host, a payload fetch from the internet — all fail with Network is unreachable. That’s the isolation working, not a bug, and it means any demo that needs a callback (reverse shell to a listener) needs a second, deliberate bridge added on purpose, not a quiet config tweak.

Parts 2 and 3 run against this lab and say, line by line, which results were actually observed here versus sourced from public research. That distinction is the whole point of building it.