Skip to content
Path traversal Write-up Beginner

Directory traversal in PDF viewing application. Leading to full database takeover

A PHP script that served PDF files accepted a filename parameter without sanitising directory traversal sequences, allowing an attacker to download arbitrary files from the server. The attacker used this to retrieve configuration files containing MySQL and SMTP credentials, which could have led to full database takeover and email hijacking.

Tom Wrinn Follow

Published on medium.com

I was looking at a new target, it was a English tutoring website with 50,000 active members. I started off with subdomain enumeration using subfind3r, not many subdomains were found, and the ones that i found were not very interesting.

Tom, 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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

The target was an English tutoring website with 50,000 active members. The researcher does not name the company, and the site is referred to throughout as redacted.org at the company's request. The feature involved is a PHP script that serves PDF files to visitors, taking the file name from a query parameter.

Setup

No accounts or special roles were needed. The work was done from the outside, using two recon tools:

  1. subfind3r for subdomain enumeration.
  2. waybackurls for URL recon against the archived URL data.

Subdomain enumeration produced few results and none of them were interesting. The waybackurls output was more useful: it surfaced many PHP web apps with parameters that looked like candidates for IDOR. One of them stood out:

Text
https://redacted.org/program/pdf/pdf_install.php?filename=sample.pdf

Finding it

The script takes a filename parameter. The obvious question was what happens if the parameter is given something that is not a PDF. The researcher replaced sample.pdf with index.php:

The download started, but the resulting file was only 251 bytes. Its contents were a PHP warning:

Text
Warning:  readfile(./index.php) [function.readfile]: failed to open stream: No such file or directory in /home/hosting_users/languageweb/www/program/pdf/pdf_install.php on line 10

This is the key observation. The warning shows readfile() failing on ./index.php and leaks the absolute path of the script on the server. The error only means there is no index.php in that particular folder. If the same request is pointed at a directory that does contain an index.php, the file should be returned.

Exploitation

Step 1: Traverse out of the script directory

To test the theory, the researcher went back two directories:

Text
redacted.org/program/pdf/pdf_install.php?filename=../../index.php

The response was a download of a 57.5 kb file. The traversal worked, and the researcher could now download files from the server.

Step 2: Hunt for configuration files

The completed waybackurls scan provided a large list of directories to try. Among them was a /mysql/ folder containing many .php configuration files. Reading those gave both SMTP and MySQL credentials. One of the credential files looked like this, with the values redacted here:

Text
<?$DBCONF[host] = "localhost";$DBCONF[dbname] = "languageweb";$DBCONF[user] = "REDACTED";$DBCONF[passwd] = "REDACTED";?>

With those files in hand the researcher was satisfied with the findings and reported them to the company's IT team.

Root cause

The filename parameter accepts ../ sequences, which the filesystem resolves, so files outside the script directory are served back as a download. The PHP warning also discloses the full server path, which makes it easier to reason about where to traverse to. The report does not describe any filtering that was present and bypassed, so the likely explanation is that no path validation was applied at all.

Impact

An attacker can read files from the server through the parameter. In this case that included MySQL and SMTP credentials, which the researcher notes could have led to a full database takeover and to hijacking of the company's email. The impact depends on what the web server user is permitted to read and on what secrets are stored on disk.

Fix

The IT team said on 25 October 2022 that a fix had been deployed, and the researcher confirmed on 1 November 2022 that it worked. A robust fix would be to stop passing user input to the filesystem at all: map an allow-listed identifier to a known file, or resolve the requested path and verify that it stays inside the intended directory before reading it.

Timeline

Date Event
9-28-22 Bug reported by email to the main contact address
10-16-22 No response after two weeks, so the secondary contact address was emailed
10-23-22 IT team made contact and said they were investigating
10-25-22 IT team said a fix had been deployed
11-1-22 Researcher confirmed the fix and asked to publish the report
11-3-22 IT team approved publication but asked that the company name be omitted
11-3-22 Researcher sent a draft of the article for review
11-4-22 IT team approved the article
11-5-22 Article published

Variants to test

  1. Other parameters feeding file functions. Look for parameters named like file, filename, path, template, download or page in archived URLs. A positive result is a response whose body or download contains a file you did not expect, or a PHP warning naming the function and path.
  2. Different traversal depths and encodings. Try ../, ....//, URL-encoded %2e%2e%2f and absolute paths. A positive result is any response that returns content from outside the intended directory.
  3. Error-driven path disclosure. Send a value that does not exist and read the warning. A positive result is a message revealing the absolute path, the function used and the line number, which tells you how far to traverse.
  4. Configuration directories. Enumerate folders such as /mysql/, /config/, /includes/ and /admin/ from archived URLs and request their .php files through the vulnerable parameter. A positive result is source containing credentials or connection strings.
  5. Log and key files. Try well-known paths such as SSH keys, .env files and application logs. A positive result is any readable file that should not be exposed.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

What happened, in plain words

A security researcher found a bug on a website that helps people learn English. The website had a feature that let visitors download PDF files. The researcher discovered that by changing a small part of the web address, he could make the website download any file from its server, not just PDFs. This let him read secret files that contained passwords for the website's database and email system. The researcher notes this could have resulted in a full database takeover and hijacking of the company's email. He reported the bug, the company fixed it, and he wrote this article to explain how he did it. This version explains every step in simple terms, assuming you know how to use a browser but nothing about security.

Introduction

The researcher was looking at a new target: an English tutoring website with 50,000 active members. He started by looking for subdomains (like blog.example.com or shop.example.com) using a tool called subfind3r. He didn't find many subdomains, and the ones he found weren't interesting. Then he used a tool called waybackurls to see old URLs that the website had used in the past. The results were intriguing: he saw many PHP web applications with parameters that might be vulnerable to IDOR (Insecure Direct Object Reference, where changing a number or name in a URL lets you access someone else's data). One PHP web app stood out:

Text
https://redacted.org/program/pdf/pdf_install.php?filename=sample.pdf

The website name is redacted at the company's request.

This URL points to a script called pdf_install.php that takes a parameter called filename. The value sample.pdf tells the script which PDF file to download. The researcher wondered: what if I change that value to something that isn't a PDF? So he replaced sample.pdf with index.php:

To his surprise, the download started, but the file was only 251 bytes (very small). When he opened it, he saw a PHP warning message:

Text
Warning:  readfile(./index.php) [function.readfile]: failed to open stream: No such file or directory in /home/hosting_users/languageweb/www/program/pdf/pdf_install.php on line 10

This message is an error from the PHP application. It says that the script tried to read a file called ./index.php (the ./ means "in the current folder") but couldn't find it. The error also reveals the full path of the script on the server: /home/hosting_users/languageweb/www/program/pdf/pdf_install.php. The error showed the script was trying to read ./index.php with readfile(). The error only happened because there was no index.php in that folder. If he could point the script to a folder that did contain an index.php, he should be able to download it. To test this, he went back two directories using ../ (which means "go up one folder"):

Text
redacted.org/program/pdf/pdf_install.php?filename=../../index.php

The response was a download of a file that was 57.5 KB. It worked! He had successfully downloaded a file from outside the intended folder.

Escalating Directory/Path Traversal

Now the researcher could download files from the server. Using the list of directories from his waybackurls scan, he looked for interesting folders. He found a /mysql/ folder that contained many .php configuration files. These files often store settings and passwords. By downloading them, he obtained both SMTP (email) and MySQL (database) credentials. This could have led to a full database takeover and hijacking of the company's email system. One of the credential files looked like this (with the actual values redacted here):

Text
<?$DBCONF[host] = "localhost";$DBCONF[dbname] = "languageweb";$DBCONF[user] = "REDACTED";$DBCONF[passwd] = "REDACTED";?>

This is a PHP code snippet that sets up an array called $DBCONF with the database host (localhost), database name (languageweb), username, and password. The username and password are redacted in the article, but in the real file they would be the actual credentials. With these files, the researcher felt he had enough to report the issue. He sent his findings to the company's IT team.

Timeline

  • 9-28-22: Reported the bug by email to the main contact email address.
  • 10-16-22: After two weeks with no response, emailed the secondary contact email.
  • 10-23-22: IT team reached out and said they were investigating.
  • 10-25-22: IT team said they had deployed a fix.
  • 11-1-22: Researcher confirmed the fix worked and asked to publish this report.
  • 11-3-22: IT team approved but requested that the company name be omitted.
  • 11-3-22: Researcher sent a draft of the article and asked if it complied.
  • 11-4-22: IT team approved the article.
  • 11-5-22: Article was published.

Root cause

The filename parameter accepts ../ sequences, which the file system resolves, letting an attacker move up directories. This means that sequences like ../ are interpreted by the file system, allowing an attacker to move up directories and read any file the web server can access. The PHP warning also reveals the full server path, which helps an attacker figure out where to go. The report does not mention any filtering that was bypassed, so it is likely that no validation was applied at all.

Impact

An attacker can read files from the server through the parameter. In this case, that included MySQL and SMTP credentials, which could lead to a full database takeover and hijacking of the company's email. The actual impact depends on what files the web server user is allowed to read and what secrets are stored on disk.

Fix

The IT team said on 25 October 2022 that a fix had been deployed, and the researcher confirmed on 1 November 2022 that it worked. The report does not describe what change was made. A strong fix would be to stop passing user input directly to the file system. Instead, the script could map an allowed identifier to a known file, or resolve the requested path and verify that it stays inside the intended directory before reading it.

Variants to test

  1. Other parameters that feed into file functions. Look for parameters named like file, filename, path, template, download, or page in archived URLs. A positive result is a response that contains a file you did not expect, or a PHP warning that names the function and path.
  2. Different traversal depths and encodings. Try ../, ....//, URL-encoded %2e%2e%2f, and absolute paths. A positive result is any response that returns content from outside the intended directory.
  3. Error-driven path disclosure. Send a value that does not exist and read the warning. A positive result is a message that reveals the absolute path, the function used, and the line number, which tells you how far to traverse.
  4. Configuration directories. Enumerate folders such as /mysql/, /config/, /includes/, and /admin/ from archived URLs and request their .php files through the vulnerable parameter. A positive result is source code containing credentials or connection strings.
  5. Log and key files. Try well-known paths such as SSH keys, .env files, and application logs. A positive result is any readable file that should not be exposed.

Glossary

  • Subdomain: A subdivision of a domain name, like blog.example.com is a subdomain of example.com.
  • Subdomain enumeration: The process of finding subdomains for a target domain.
  • Waybackurls: A tool that fetches URLs from the Internet Archive's Wayback Machine, which stores old versions of websites.
  • PHP: A programming language commonly used for web development.
  • IDOR (Insecure Direct Object Reference): A vulnerability where an application exposes a direct reference to an object (like a file or database record) and doesn't check if the user is authorised to access it.
  • Parameter: A variable in a URL that passes data to the server, usually after a ? and separated by &.
  • Directory traversal (path traversal): A vulnerability that allows an attacker to access files and directories outside the intended directory by using sequences like ../.
  • readfile(): A PHP function that reads a file and writes it to the output buffer.
  • SMTP: Simple Mail Transfer Protocol, used for sending email.
  • MySQL: A popular open-source database management system.
  • Credentials: A username and password pair used to authenticate.
  • Database takeover: Gaining unauthorised control over a database, often leading to data theft or manipulation.
  • Root cause: The fundamental reason why a vulnerability exists.
  • Impact: The potential damage or consequences of exploiting a vulnerability.
  • Fix: A change made to software to remove a vulnerability.
  • Encoding: Converting data into a different format, often to bypass filters (e.g. URL encoding turns ../ into %2e%2e%2f).
  • Absolute path: A file path that starts from the root directory, like /home/user/file.txt.
  • SSH keys: Cryptographic keys used for secure remote login.
  • .env files: Files that store environment variables, often including secrets.
  • Log files: Files that record events or errors, which may contain sensitive information.

More by Tom Wrinn

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.