Skip to content

Lesson 9 of 14 · 20m

When Your Server Becomes the Client: SSRF and Stream Wrappers

URL fetches reach 169.254.169.254 and localhost; how spellings, redirects, and rebinding beat string checks.

When PHP calls file_get_contents($url) or Guzzle's get($url), the URL is an instruction, not data — it names a TCP service your server will connect to and read from. Everything reachable from the box is reachable through your feature: localhost, RFC1918 neighbors, the cloud metadata endpoint, the Redis that has no password because it's "internal". SSRF is the bug where the attacker picks the destination.

Why string validation fails

The obvious check — parse_url($url) and require http/https — passes everything that matters:

  • Loopback has many spellings. http://2130706433/ (decimal), http://0x7f000001/ (hex), http://0177.0.0.1/ (octal), http://[::1]/, http://127.1/ — glibc's inet_aton resolves them all to 127.0.0.1, and FILTER_VALIDATE_URL accepts every one.
  • Redirects re-roll the dice. You checked example.com; example.com replies Location: http://169.254.169.254/latest/meta-data/ and cURL happily follows.
  • DNS is time-of-check, time-of-use. gethostbyname() returns a safe IP at validation; the fetch resolves again and gets the attacker's second answer. Rebinding is a TTL trick, not exotic tooling.
  • The scheme list is longer than you think. PHP's wrappers include php://, phar://, ftp://, compress.zlib://. cURL's protocol list is longer — gopher, dict, ldap. gopher:// writes raw TCP, which is how SSRF reaches Redis command injection and FastCGI payloads through a "URL fetch".

169.254.169.254 is the address with the case study behind it. Capital One, 2019: a web application firewall's feature issued requests containing an attacker-influenced URL; the response to 169.254.169.254/latest/meta-data/iam/security-credentials/ handed back IAM credentials, and 100 million customer records walked out through S3. The fine was $80 million. AWS's answer was IMDSv2 — a session-token handshake the metadata endpoint now requires — which helps exactly as much as your SSRF filter helps: an attacker who controls the request can usually fetch a token first. That endpoint is the poster child, but the reachable list on any real box is longer: Redis on 6379, Elasticsearch on 9200, the FPM status page, Prometheus, the Kubernetes API on 10.96.0.1. GCP and Azure put their own metadata on the same address — the magic IP is a cloud convention, not an AWS quirk.

The file-side sibling

php://filter is the same primitive pointed at your own disk: php://filter/convert.base64-encode/resource=config.php returns your source base64'd — bypassing "don't include PHP files" because it never executes them, it reads them. Synacktiv's php_filter_chains research (Charles Fol, 2023) pushed this further, showing filter chains alone can reach RCE-class primitives. Any function that takes a filename and does I/O on it is a candidate — file_get_contents, include, parse_ini_file, simplexml_load_file.

What actually fixes it

In order of what they buy you:

  1. Egress allowlist. The real fix lives at the firewall/proxy: the app may only reach enumerated hosts. Everything else is partial.
  2. allow_url_fopen=0 when nothing legitimately reads remote URLs — removes file_get_contents('http://…') as a primitive entirely.
  3. Resolve-then-pin in code. parse_url, resolve the host, validate the resolved IP against loopback/link-local/RFC1918/metadata ranges, then pin the resolution with CURLOPT_RESOLVE so the fetch uses the IP you checked — closing the rebinding gap.
  4. Bound cURL's reach: CURLOPT_PROTOCOLS_STR='http,https', CURLOPT_REDIR_PROTOCOLS_STR the same, CURLOPT_FOLLOWLOCATION off or re-validate every hop, CURLOPT_CONNECTTIMEOUT/CURLOPT_TIMEOUT always.
  5. Don't surface failure internals. An error body from an internal service leaks headers and banners. Return "fetch failed", not the response.

For real outbound work use Guzzle — same rules, better defaults — and timeout every call. An outbound request without a timeout is a worker you can't get back.

Try this

Stand up a listener on localhost (nc -l 127.0.0.1 8090), point your URL-fetch feature at http://2130706433:8090/, and watch it connect. Then fix it with the resolve-and-pin path above and watch the same request die at validation.

Pitfalls

  • FILTER_VALIDATE_URL validates syntax, not destination — http://0x7f000001/ passes.
  • IPv6 literals, IPv4-mapped IPv6 (::ffff:7f00:1), trailing-dot hosts (localhost.) — enumerate the spellings in your blocklist or lose.
  • Webhooks and "check this URL" features are SSRF by design; treat them as the most dangerous endpoint you ship.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.