Skip to content
SSRF Write-up Intermediate

When PHP’s readfile() Goes Rogue: Exploiting LFI and SSRF in a

A file download feature passed user input directly into PHP's readfile(), allowing local file inclusion and, because readfile() can fetch remote URLs, server-side request forgery. The attacker read a backup file with admin credentials, then used SSRF to reach the EC2 metadata service and obtain AWS credentials with full S3 access, exposing customer data.

Avihay Eldar Follow

Published on medium.com

PHP’s readfile() is like that "too friendly" function in your codebase — it wants to help you serve files fast and easy, but if you’re not careful, it’ll spill secrets faster than your grandma at a family dinner.

Avihay Eldar, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

User input was concatenated into a file path passed to readfile() without proper sanitisation, and readfile() supports remote URLs when allow_url_fopen is enabled.

Impact

An attacker could read arbitrary local files, make the server send requests to internal services, steal AWS credentials, and access sensitive customer data in S3 buckets.

What the attacker needed

Access to the admin panel; allow_url_fopen enabled; EC2 instance with metadata service reachable

The fix

The text does not state that the issue was fixed; it should be fixed by sanitising and whitelisting user input, disabling allow_url_fopen unless needed, and applying least privilege to AWS roles.

How it was found

  1. Scan directories and find a backup file named admin.php.old containing plain-text admin credentials.
  2. Log in to the admin panel and intercept the file download request, which includes a parameter with the full local file path.
  3. Modify the file path parameter to read /etc/passwd, confirming local file inclusion.
  4. Read the main PHP file to discover the download feature uses readfile().
  5. Trigger the server to contact a Burp Collaborator listener, confirming SSRF.
  6. Query the EC2 metadata service to obtain temporary AWS credentials with full S3 access.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

The download page built a file path from whatever the user typed, so someone could ask for any file on the server, like the password file. The same function can also fetch web addresses, so the server could be tricked into contacting internal cloud services and handing over access keys.

What is SSRF?

The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.

Similar reports

View all

Same bug class and techniques, clearest first.

Server-side request forgery (SSRF)

The lesson behind this report.

The server can be made to fetch an address the attacker chooses, reaching internal services, cloud metadata or other customers' data.