1. What is Path Traversal?
Path traversal (also known as directory traversal) is a security vulnerability that allows an attacker to access files and directories that are stored outside the web root folder. By manipulating variables that reference files with “dot-dot-slash (../)” sequences and its variations, or by using absolute file paths, an attacker may be able to read arbitrary files on the server, including:
- Application code and data
- Credentials for back-end systems
- Sensitive operating system files
- Configuration files containing secrets
In some cases, an attacker might be able to write to arbitrary files on the server, allowing them to:
- Modify application data or behavior
- Take full control of the server
- Plant backdoors or malware
2. How Path Traversal Works
Basic Mechanism
Path traversal attacks work by exploiting insufficient validation of user-supplied input when constructing file paths. Here’s a basic example:
1 | <!-- Vulnerable HTML --> |
If the application constructs the file path by simply concatenating the base directory with the user input:
1 | // Vulnerable server-side code |
An attacker can manipulate the filename parameter to traverse directories:
1 | https://example.com/loadImage?filename=../../../etc/passwd |
This results in the application reading from:
1 | /var/www/images/../../../etc/passwd |
Which resolves to:
1 | /etc/passwd |
Directory Traversal Sequences
../- Move up one directory level (Unix/Linux)..\- Move up one directory level (Windows)....//- Nested traversal sequence....\/- Mixed slash traversal sequence
3. Common Scenarios for Path Traversal
File Inclusion Operations
1 | // Vulnerable PHP code |
File Reading Operations
1 | // Vulnerable Java code |
File Upload Operations
1 | # Vulnerable Python code |
Template Rendering
1 | # Vulnerable Ruby code |
Configuration File Loading
1 | // Vulnerable C# code |
4. Exploitation Techniques
Basic Path Traversal
1 | ../../../etc/passwd |
Windows Path Traversal
1 | ..\..\..\windows\win.ini |
URL Encoding
1 | %2e%2e%2f (URL-encoded ../) |
Non-Standard Encodings
1 | ..%c0%af |
Null Byte Injection
1 | ../../../etc/passwd%00.png |
Absolute Path
1 | /etc/passwd |
Nested Traversal Sequences
1 | ....//....//....//etc/passwd |
5. Bypassing Defenses
1. Stripping Directory Traversal Sequences
Defense: Application strips “../“ from user input
Bypass: Use nested traversal sequences:
1 | ....//etc/passwd |
When the application strips extra dots:
1 | Input: ....//etc/passwd |
2. URL Path Sanitization
Defense: Web server strips directory traversal sequences from URL paths
Bypass: Use URL encoding:
1 | ..%2F..%2F..%2Fetc%2Fpasswd |
Or double URL encoding:
1 | ..%252F..%252F..%252Fetc%252Fpasswd |
3. Base Folder Requirement
Defense: Application requires filename to start with expected base folder
Bypass: Include the required base folder followed by traversal sequences:
1 | /var/www/images/../../../etc/passwd |
4. File Extension Requirement
Defense: Application requires filename to end with expected extension
Bypass: Use null byte to terminate the file path:
1 | ../../../etc/passwd%00.png |
5. Unicode Normalization
Defense: Application normalizes Unicode characters
Bypass: Use Unicode characters that normalize to traversal sequences:
1 | ..%u2215%u2215%u2215etc%u2215passwd |
6. Vulnerable Code Examples
PHP Example
1 |
|
Java Example
1 | // Vulnerable code |
Node.js Example
1 | // Vulnerable code |
Python Example
1 | # Vulnerable code |
C# Example
1 | // Vulnerable code |
7. Prevention Strategies
1. Avoid User Input in Filesystem APIs
Best Practice: Don’t pass user-supplied input to filesystem functions.
Example:
1 | # Instead of this (vulnerable): |
2. Input Validation
Whitelist Approach:
1 | // Java example |
Character Validation:
1 | # Python example |
3. Path Canonicalization and Verification
Java Example:
1 | File file = new File(BASE_DIRECTORY, userInput); |
Python Example:
1 | import os |
Node.js Example:
1 | const path = require('path'); |
4. Use Indirect References
Database Approach:
1 | -- Store file metadata in a database |
Mapping Approach:
1 | # Python example |
5. Implement Least Privilege
- Run web applications with minimal privileges
- Use chroot jails or containers to isolate applications
- Restrict file system access to only necessary directories
6. Use Secure Framework Functions
Many modern frameworks provide built-in protection against path traversal:
Laravel (PHP):
1 | // Use Laravel's built-in functions |
Django (Python):
1 | # Use Django's secure file handling |
8. Detection Methods
1. Automated Scanning
Burp Suite:
- Use Burp Scanner to detect path traversal vulnerabilities
- Burp Intruder with predefined payload list “Fuzzing - path traversal”
OWASP ZAP:
- Active scanning mode can detect path traversal vulnerabilities
- Fuzzing with path traversal payloads
Nikto:
1 | nikto -h http://example.com |
2. Manual Testing
Basic Testing:
1 | https://example.com/page?file=../../../../etc/passwd |
Windows Testing:
1 | https://example.com/page?file=..\..\..\..\windows\win.ini |
URL Encoding:
1 | https://example.com/page?file=..%2F..%2F..%2Fetc%2Fpasswd |
Null Byte Injection:
1 | https://example.com/page?file=../../../etc/passwd%00.jpg |
3. Code Review
Look for patterns where user input is used in file operations:
- File inclusion functions
- File reading/writing operations
- File path construction
9. Tools for Testing
1. Burp Suite
- Intruder for automated fuzzing
- Scanner for automated detection
- Repeater for manual testing
2. OWASP ZAP
- Fuzzer for automated testing
- Active scanner for detection
3. Kali Linux Tools
- Dirb
- Dirbuster
- Gobuster
4. Custom Scripts
1 | # Python script for path traversal testing |
10. Real-World Examples
1. CVE-2021-41773 (Apache 2.4.49)
- Path traversal and file disclosure vulnerability in Apache HTTP Server 2.4.49
- Allowed attackers to map URLs to files outside the configured directories
- Exploited using:
1 | /cgi-bin/.%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd |
2. CVE-2020-8193 (Citrix ADC)
- Path traversal vulnerability in Citrix Application Delivery Controller
- Allowed unauthenticated attackers to read arbitrary files
- Exploited using:
1 | /vpn/../vpns/portal/scripts/newbm.pl |
3. CVE-2019-5418 (Rails)
- Path traversal vulnerability in Rails Action View
- Allowed attackers to render arbitrary files
- Exploited using specially crafted Accept headers
11. Related Vulnerabilities
1. Local File Inclusion (LFI)
- Similar to path traversal but focuses on including local files
- Often leads to Remote Code Execution (RCE)
2. Remote File Inclusion (RFI)
- Including remote files that can execute code
- More dangerous than LFI
3. Directory Listing
- Exposing directory contents when directory listing is enabled
4. Insecure Direct Object Reference (IDOR)
- Accessing objects by manipulating direct references
12. CTF Challenges
Common Challenge Types
Basic Path Traversal
- Simple file reading using
../sequences - Example:
?file=../../../../etc/passwd
- Simple file reading using
Filtered Path Traversal
- Application filters certain characters or sequences
- Requires bypassing filters using encoding or nested sequences
Null Byte Injection
- Application requires specific file extensions
- Use null bytes to terminate the path before the extension
Log Poisoning
- Injecting malicious content into log files
- Including log files to execute code
Blind Path Traversal
- No direct feedback about file content
- Requires exfiltration techniques like out-of-band requests
Example CTF Walkthrough
Challenge: Find the flag in a file on the server
URL: https://challenge.example.com/image?filename=picture.jpg
Solution:
Try basic path traversal:
1
?filename=../../../etc/passwd
- No success, likely filtered
Try URL encoding:
1
?filename=..%2F..%2F..%2Fetc%2Fpasswd
- No success, still filtered
Try double URL encoding:
1
?filename=..%252F..%252F..%252Fetc%252Fpasswd
- Success! Now we can read files
Look for the flag:
1
?filename=..%252F..%252F..%252Fhome%252Fuser%252Fflag.txt
- Flag found:
CTF{path_traversal_master}
- Flag found:
13. Interview Questions
Basic Questions
What is path traversal?
- Answer: Path traversal is a security vulnerability that allows an attacker to access files and directories outside the intended directory.
What are the common indicators of path traversal vulnerabilities?
- Answer: URL parameters that reference files, file inclusion functions, and file operations using user input.
How can you prevent path traversal attacks?
- Answer: Input validation, path canonicalization, avoiding user input in filesystem operations, using indirect references, and implementing least privilege.
Intermediate Questions
How would you bypass a filter that removes “../“ sequences?
- Answer: Use nested traversal sequences like “….//“, URL encoding like “%2e%2e%2f”, or double URL encoding like “%252e%252e%252f”.
What is null byte injection and how does it relate to path traversal?
- Answer: Null byte injection involves using a null byte (%00) to terminate a string before an unwanted extension, allowing attackers to bypass file extension checks.
How would you detect path traversal vulnerabilities in a code review?
- Answer: Look for instances where user input is used in file operations without proper validation, such as file inclusion functions, file reading/writing operations, and file path construction.
Advanced Questions
How would you exploit a blind path traversal vulnerability where there’s no direct feedback?
- Answer: Use techniques like exfiltrating data through out-of-band requests, timing attacks, or by writing to a location that can be accessed later.
What is the difference between path traversal and local file inclusion (LFI)?
- Answer: Path traversal focuses on accessing files outside the intended directory, while LFI focuses on including and executing local files, often leading to code execution.
How would you implement secure file handling in a web application?
- Answer: Use indirect references, validate input against a whitelist, canonicalize paths, implement least privilege, and use secure framework functions.
Practical Questions
Given this vulnerable code, how would you fix it?
1
2
3
4
$file = $_GET['file'];
include("/var/www/html/" . $file);- Answer: Use a whitelist of allowed files, validate input, or use indirect references.
How would you test for path traversal vulnerabilities in a black-box assessment?
- Answer: Use automated tools like Burp Suite or OWASP ZAP, manually test with various payloads, and try different encoding techniques.
What would be the impact of a successful path traversal attack on a web application?
- Answer: Impact could include reading sensitive files like configuration files with credentials, source code analysis for further vulnerabilities, or potentially writing files to achieve remote code execution.