🔍 Search

Directory Traversal Attack

This comprehensive guide will explain what directory traversal is, detail the different types of directory traversal attacks with examples, and provide effective prevention and mitigation strategies.

What is Directory Traversal?

Directory traversal, also known as path traversal, is a web security vulnerability that allows an attacker to access files and folders stored outside the intended web root directory.

This occurs when an application does not properly validate and sanitize user input, allowing an attacker to manipulate file paths to access sensitive files or execute malicious code.

How Path Traversal Works?

To understand directory traversal, it's important to know about paths.

A path is a string that provides the location of a file or directory in a file system. In Unix-like systems, the path starts from the root directory '/' and in Windows, it starts from a drive letter 'C:'. Special directories like '.' (current directory) and '..' (parent directory) are integral to paths.

In a typical scenario, web applications use user input to construct file paths for operations such as file display, download, or upload. Attackers manipulate these inputs by using '../' or '..' (or URL-encoded equivalents) to traverse to a higher level in the directory tree than intended. This can allow the attacker to access arbitrary files and directories stored on file system including application source code, configuration, and critical system files.

Common Patterns of Directory Traversal

1. Relative Path Traversal

This involves using dot-dot-slash '../' sequences to move up the directory tree. For instance, accessing '/var/www/html/app/../../etc/passwd' would attempt to reach the 'etc/passwd' file, a critical system file on Unix-based systems.

2. Absolute Path Traversal

This uses absolute file paths, directly pointing to a file location. An example would be requesting 'C:\Windows\system32\drivers\etc\hosts' on a Windows server.

Impact of Directory Traversal

If an attacker is able to exploit the directory traversal vulnerability the impact could range from mild to severe depending on the files and folders exposed.

  • Confidential Data Exposure: Access to configuration files can expose sensitive information like database credentials, API keys, etc.
  • Data Tampering: Ability to write files could result in website defacement or more severe system manipulations.
  • System Compromise: Access to system files can lead to complete system takeover or server control.

Types of Directory Traversal Attacks

1. Basic Directory Traversal

Here the attacker tries to access files or directories by using '..' or '../' sequences to navigate upward through the directory structure.


http://example.com/files/../../secrets/passwords.txt The attacker tries to move up the directory tree to access the "passwords.txt" file.

2. Null Byte Injection

Null byte injection, also known as null byte poisoning, involves appending a null byte (%00) or its URL-encoded equivalent (%2500) to the input to terminate a filename prematurely. This can bypass security checks and access sensitive files.


http://example.com/files/../../../../etc/passwd%00 The null byte truncates the filename, allowing the attacker to access the "/etc/passwd" file.

3. Unicode Encoding

In this attack, an attacker leverages Unicode encoding to obfuscate the traversal attempt. Unicode characters that represent path traversal sequences can be used to deceive input validation.


http://example.com/files/%e0%80%af%e0%80%af../../secrets/passwords.txt The Unicode encoding disguises the "../" sequence, enabling the attacker to access the "passwords.txt" file.

4. Double Encoding

Double encoding occurs when an attacker applies multiple encoding schemes to their payload to evade detection.


http://example.com/files/%252e%252e%252f%252e%252e%252fsecrets%252fpasswords.txt Double encoding of "../" confuses input filters and allows the attacker to access "passwords.txt".

Prevention and Mitigation

To protect web applications from Directory Traversal attacks, several preventive measures and mitigation strategies should be implemented:

1. Input Validation and Sanitization

Implement strict input validation and sanitization mechanisms to filter out malicious input. Ensure that user inputs are validated to prevent the use of "../" and other traversal sequences.

2. Use Whitelists

Define a list of allowed files and only serve requests matching this list. Avoid blacklists, as they can often be bypassed.

3. Web Application Firewalls (WAF)

Deploy a Web Application Firewall that can detect and block Directory Traversal attempts. Configure the WAF to monitor and protect against common attack patterns.

4. Use of Chroot Jails

Consider using chroot jails or similar mechanisms to isolate processes from the underlying file system. This restricts an attacker's ability to traverse directories outside their jail.

5. Least Privilege Principle

Follow the principle of least privilege by granting only the necessary permissions to applications and users. Avoid running services with excessive privileges that could lead to more extensive damage if compromised.

Like this Article? Please Share & Help Others: