Path traversal is when user input becomes part of a filesystem path without validation, letting an attacker walk out of the intended directory. A parameter meant to pick one of four templates instead reads the application's configuration file, its private keys, or its environment. Local file inclusion is the same bug where the file is then executed rather than merely read.
The vulnerable pattern
$file = $_GET['doc'];
readfile("/var/www/uploads/" . $file);
Request ?doc=../../../etc/passwd and the path resolves outside the uploads directory. The application never intended to allow it; it simply never checked.
The parameter is rarely called file. In real applications it is template, page, lang, theme, report, download, attachment, view, skin or locale. Anywhere a value selects something that lives on disk, the parameter is a candidate — including values in cookies and headers, which get far less scrutiny than query strings.
Why filters fail
The instinctive fix is to strip ../. It does not survive contact with an attacker:
- URL encoding —
%2e%2e%2f, and double encoding%252e%252e%252fwhere a layer decodes twice. - Nesting —
....//becomes../after a single non-recursive strip. The filter creates the attack. - Backslashes —
..\\on Windows, where both separators are accepted. - Absolute paths —
/etc/passwdneeds no traversal at all if the code concatenates rather than joins safely. - Null bytes —
../../etc/passwd%00.pngtruncates an appended extension in older runtimes. - Overlong UTF-8 and alternative encodings on systems that normalise after validating.
Each of these is a well-known bypass, which is the general lesson: a block-list of dangerous inputs is a guess about the attacker's vocabulary. The same reasoning applies to command injection and to SQL injection.
From reading files to running code
Disclosure is serious on its own — configuration files and .env contents usually carry database credentials, API keys and signing secrets, which converts a read primitive into full compromise by a different route. But where the included file is interpreted, the escalation is direct.
The common paths are log poisoning (write attacker-controlled text into a log by putting it in a User-Agent header, then include the log), session file inclusion (put a payload in a session value, then include the session file by its predictable path), and PHP wrappers such as php://filter, which returns source code and is the standard way to read an application's own files through an inclusion point.
A practical note for testers: /proc/self/environ and /proc/self/cmdline on Linux frequently expose secrets passed as environment variables to the process, which is the most common way application credentials are supplied in containers.
Fixing it
1. Do not put user input in a path at all. Map an identifier to a filename server-side:
$allowed = ['invoice' => 'invoice.html', 'receipt' => 'receipt.html'];
$file = $allowed[$_GET['doc']] ?? null;
if ($file === null) { http_response_code(404); exit; }
Nothing the user sends ever becomes part of a path. The class is eliminated rather than mitigated.
2. Where a path must be built, canonicalise then verify.
$base = realpath('/var/www/uploads');
$path = realpath($base . '/' . $input);
if ($path === false || !str_starts_with($path, $base . DIRECTORY_SEPARATOR)) {
http_response_code(404); exit;
}
The order matters: resolve first, compare second. Comparing the raw string before resolution checks a path that does not exist yet.
3. Watch for symlinks. realpath() resolves them, which is what you want — a symlink inside an uploads directory is otherwise a traversal that contains no ../ at all.
4. Store user files outside the web root and serve them through code, never by path. Combine with the upload controls in file upload vulnerabilities.
5. Disable remote inclusion. In PHP, allow_url_include=Off and allow_url_fopen=Off where the application does not need them.
6. Return 404 for anything rejected. Distinct errors for "not allowed" and "not found" let an attacker map the filesystem without reading a single file.
Testing it
Work through depth, encodings and both separators systematically rather than trying one payload and moving on. Use a canary that is readable on every system (/etc/passwd on Linux, C:\windows\win.ini on Windows) to confirm the primitive, then pivot to files that matter: application config, framework environment files, SSH keys, cloud credential files and the web server's own access log. Record the exact encoding that worked — remediation is frequently incomplete, and the retest should reuse every variant, not just the first one that succeeded.
The OWASP path traversal reference covers the encoding variants in detail. Where you want this checked against a live application together with the inclusion escalations, it sits inside our web application penetration testing scope.
0 comments
Leave a comment