Path Traversal
Path traversal, also called directory traversal, is a vulnerability that allows an attacker to access files outside the intended directory.
It happens when an application uses user-controlled input to build a file path without proper validation.
Warning
Test path traversal only in authorized environments.
Overview
Path traversal is commonly found in features that read files from the server.
Examples:
- Image loading
- File downloads
- Report viewing
- Template loading
- Language selection
- Document preview
- Static file retrieval
A vulnerable request may look like this:
The server may build a file path like this:
If the application trusts the filename parameter, an attacker may be able to escape the intended directory.
Dot-Dot-Slash
The sequence ../ means “go up one directory.”
Example payload:
If the application appends user input to a base directory:
Base directory: /var/www/images/
User input: ../../../etc/passwd
Final path: /var/www/images/../../../etc/passwd
Resolved path: /etc/passwd
The attacker has escaped the image directory and reached a system file.
Linux Targets
Linux and Unix systems use / as the directory separator.
Common proof-of-concept files:
| File | Purpose |
|---|---|
/etc/passwd |
User account information |
/etc/hosts |
Local host mappings |
/proc/self/environ |
Process environment variables |
/var/log/apache2/access.log |
Apache access logs |
/var/www/html/index.php |
Possible web source file |
Example:
Windows Targets
Windows supports both backslash and forward slash in many contexts.
Common proof-of-concept files:
| File | Purpose |
|---|---|
C:\Windows\win.ini |
Classic Windows proof of concept |
C:\Windows\System32\drivers\etc\hosts |
Local host mappings |
C:\boot.ini |
Older Windows boot configuration |
Example payloads:
Common Payloads
../../../etc/passwd
../../../../etc/passwd
../../../../../etc/passwd
..%2f..%2f..%2fetc%2fpasswd
..%252f..%252f..%252fetc%252fpasswd
....//....//....//etc/passwd
Windows examples:
Encoding Bypasses
Applications may block obvious traversal strings like ../.
Encoding can sometimes bypass weak filters.
| Payload | Meaning |
|---|---|
%2e%2e%2f |
URL-encoded ../ |
%2e%2e/ |
Partially encoded ../ |
..%2f |
Encoded slash |
..%5c |
Encoded backslash |
%252e%252e%252f |
Double-encoded ../ |
Example:
Forced Extension Bypass
Some applications append a required extension.
Example backend logic:
A payload like this:
becomes:
In older or misconfigured systems, null byte injection may terminate the string early:
Result:
The .png part may be ignored in vulnerable legacy environments.
Note
Null byte injection is mostly relevant to older systems, but it is useful to understand historically and in labs.
Filter Bypass Patterns
Weak filters often remove traversal sequences only once.
Example filter:
Payload:
After one filter pass, this may collapse into:
Other bypass ideas:
Impact
Path traversal can expose sensitive files.
Possible impact:
- Source code disclosure
- Configuration file disclosure
- Credential leakage
- API key leakage
- Log file access
- User enumeration
- Application secret exposure
- Further exploitation through file write or log poisoning in some cases
Sensitive application files may include:
Testing Checklist
Check parameters related to files or paths:
Testing steps:
- Identify a parameter that loads a file.
- Try a normal valid file.
- Add
../sequences. - Try known Linux or Windows files.
- Try encoded traversal.
- Try absolute paths.
- Check whether errors reveal the final path.
- Test whether the application appends extensions.
- Confirm impact without damaging the system.
Remediation
Safe file handling should avoid direct user-controlled paths.
Recommended defenses:
- Avoid passing file paths directly.
- Use indirect file IDs mapped server-side.
- Store allowed files in a database or allowlist.
- Normalize and canonicalize paths before access.
- Verify the resolved path stays inside the intended base directory.
- Reject path separators and traversal sequences.
- Apply strict allowlists for file names and extensions.
- Run the application with least-privilege filesystem access.
Example secure logic:
User requests file ID 123
Server looks up file ID 123 in database
Server resolves safe known path
Server verifies user is allowed to access it
Server returns the file
Quick Reference
| Target | Payload |
|---|---|
| Linux users | ../../../../etc/passwd |
| Linux hosts file | ../../../../etc/hosts |
| Windows proof | ..\..\..\windows\win.ini |
| Encoded traversal | ..%2f..%2f..%2fetc%2fpasswd |
| Extension bypass | ../../../etc/passwd%00 |
| Filter bypass | ....//....//etc/passwd |
Notes to Remember
- Path traversal abuses unsafe file path construction.
../moves up one directory.- Linux uses
/; Windows often accepts\and/. - Encoding can bypass weak filters.
- Never trust user-supplied file paths.
- Canonicalize paths before validating them.
- File IDs are safer than raw file names.