Path Traversal
Table of Contents
Path Traversal
Path Traversal, also known as Directory Traversal, is a web application vulnerability that allows an attacker to access files and directories stored outside the intended scope of an application. This is achieved by manipulating file path parameters in user input, allowing the attacker to navigate through the server's directory structure and retrieve sensitive files such as configuration data, passwords, or source code.
These vulnerabilities often arise when applications dynamically generate file paths based on user input without proper validation or sanitization.
Common Exploitation Patterns and Examples
- Relative Path Manipulation: This is the most common technique, where attackers inject "dot-dot-slash" sequences (
../for Linux/UNIX or..\for Windows) to move up the directory tree and access restricted files.- Example: A vulnerable download function expecting
/images/user.pngmight receive/../../etc/passwdinstead, granting unauthorized access to the UNIX password file. Vulnerable Code Example (PHP):
<?php $template = 'blue.php'; if (isset($_COOKIE['TEMPLATE'])) { $template = $_COOKIE['TEMPLATE']; } include ("/home/users/phpguru/templates/" . $template); ?>
An attacker could set the
TEMPLATEcookie to../../../../etc/passwdto include the password file.
- Example: A vulnerable download function expecting
- Absolute Path Injection: Attackers can directly specify sensitive system paths, such as
/etc/shadoworC:\Windows\System32\config, to access critical files. This can be particularly effective if error messages provide clues about file locations. - Encoding and Obfuscation: Attackers use URL encoding (
%2e%2e%2ffor../) or Unicode representations to bypass weak validation rules that might block literal../sequences. - Null Byte Injection: Some applications might expect a specific file extension (e.g.,
.png). An attacker can use a null byte (%00) to terminate the file path before the expected extension, allowing access to a different file.- Example:
filename../../../etc/passwd%00.png= could lead to thepasswdfile being accessed despite the.pngexpectation.
- Example:
- Chained Attacks: Path traversal vulnerabilities can be combined with other flaws for more severe impacts.
- Reading configuration files via path traversal can reveal internal endpoints or credentials.
- Arbitrary file write combined with path traversal can lead to planting a payload, which can then be executed via a local file inclusion (LFI) vulnerability, resulting in remote code execution (RCE).
- Log injection through a crafted header combined with an LFI in a debug panel can also lead to RCE.
Real-world Examples of Directory Traversal Exploits
- CVE-2021-41773 in Apache HTTP Server 2.4.49: This vulnerability allowed attackers to escalate to remote code execution.
- CVE-2018-13379 in Fortinet FortiOS: A directory traversal vulnerability discovered in the operating system of FortiGate firewalls.
- Web interfaces storing logs or backups under predictable paths: This allowed attackers to exfiltrate system data remotely.
- Poorly validated file uploads: In some cases, this allowed attackers to overwrite configuration files and execute arbitrary code.
Path traversal attacks can occur in both web server software and application code. A successful attack can allow an attacker to view restricted files, potentially providing more information to further compromise the system, and in some cases, execute commands by impersonating the user associated with the website.