Directory Traversal or file path traversal is a web security vulnerability in which an attacker tries to gain the access to unauthorized or restricted files and directories. This might include :
- Application code and data.
- Credentials for back-end systems.
- Sensitive operating files.
How to read files via directory traversal?
For example, a shopping application that displays an image by using an image tag that looks like :
This image URL takes the filename as an input and then searches for the specified image file on /var/www/images folder where all images are generally stored.
If there is no defense applied for directory traversal attack then an attacker can give another file’s location and try to read it. Suppose the attacker wants to read the passwd file which is stored on the location /etc/passwd then he will modify the URL and request for the file :
../ is used to step up one level in the directory structure in Linux-based systems and on windows both ../ and ..\ can be used. By specifying ../../../ we land on the root directory of the server and from here we can access any file on the server by mentioning its path just like /etc/passwd which we mentioned above. The URL will cause the application to read from the following path :
Obstacles while exploiting file path traversal vulnerabilities.
Putting user's input into file paths
In many applications, user-supplied inputs are placed into the file paths which is a kind of defense against directory traversal attacks.
There are ways by which we can bypass this type of validation.
- Directly reference a file without using any traversal sequence.
- Using nested traversal sequences such as ….// or ….\/ will revert to a simple traversal sequence when the inner sequence is stripped.
- Sometimes the webserver strips the multipart/form-data request before passing the input to the application. We can bypass this by URL encoding the traversal sequence ../ resulting in ..%2F or double encoding ..%2F to ..%252F. Various non-standard encodings, such as
..%ef%bc%8f, may also do the trick.
- Mentioning the full path of the file which we want to access
- When an application requires a user-supplied filename must end with an expected file extension, such as .png, then it might be possible to use a null byte to effectively terminate the file path before the required extension.
TOOL FOR DIRECTORY TRAVERSAL
git clone https://github.com/wireghoul/dotdotpwn
perl dotdotpwn.pl -h 10.10.10.10 -m ftp -t 300 -f /etc/shadow -s -q -b
- List of traversal sequence
- 16 bit Unicode Encoding
. = %u002e
/ = %u2215
\ = %u2216
- UTF-8 Unicode Encoding
. = %c0%2e, %e0%40%ae, %c0ae
/ = %c0%af, %e0%80%af, %c0%2f
\ = %c0%5c, %c0%80%5c
- Bypass “../” replaced by “”
Sometimes you encounter a WAF which removes the “../” characters from the strings and just duplicates them.
- Bypass “../” with “;”
- Double URL Encoding
. = %252e
/ = %252f
\ = %255c
- UNC Bypass
- Java Bypass
- Linux files
/proc/[0-9]*/fd/[0-9]* (first number is the PID, second is the filedescriptor)
- Windows files
Always existing file in recent Windows machine. Ideal to test path traversal but nothing much interesting inside…
Interesting files to check out
c:/system volume information/wpsettings.dat
The following log files are controllable and can be included with an evil payload to achieve a command execution
- Portswigger Directory Traversal :
- Payload all the things :