๐Ÿ› ๏ธ Directory traversal

Theory

Directory traversal (or Path traversal) is a vulnerability that allows an individual to read arbitrary files on a web server. Inputs that are not validated by the back-end server may be vulnerable to payloads such as "../../../". Using this method, an attacker can go beyond the root directory of the website, thus reaching arbitrary files hosted on the web server (/etc/passwd, /etc/hosts, c:/boot.ini, etc.).

Notes

Some details are important to know beforehand.

Path separator

As the Owasp mentions, each operating system uses different characters as a path separator.
Unix-like OS:
1
root directory: "/"
2
directory separator: "/"
Copied!
Windows OS' Shell':
1
root directory: "<drive letter>:\"
2
directory separator: "\" or "/"
Copied!
Classic Mac OS:
1
root directory: "<drive letter>:"
2
directory separator: ":"
Copied!

Windows

Files and directories are case-insensitive, so there's no need to try different payloads based on case sensitivity. Also, one has to make sure that the payloads don't use a fixed drive letter ("C:"), but more ("D:", "E:"...).
Directory traversal could lead to Remote Code Execution (RCE).

Practice

Tool

The tool dotdotpwn (Perl) can help in finding and exploiting directory traversal vulnerabilities by fuzzing the web app. However, manual testing is usually more efficient.
1
# With a request file where /?argument=TRAVERSAL (request file must be in /usr/share/dotdotpwn)
2
dotdotpwn.pl -m payload -h $RHOST -x $RPORT -p $REQUESTFILE -k "root:" -f /etc/passwd
3
โ€‹
4
# Generate a wordlist in STDOUT that can be used by other fuzzers (ffuf, gobuster...)
5
dotdotpwn -m stdout -d 5
Copied!

Manual testing

Reconnaissance

The first step is to find what kind of system is used (Linux, Windows...). One could do that by checking on which web technology is used (some technologies run on Linux while others run on Windows).
Next, finding the right parameter to inject is essential. Usually, a vulnerable parameter is one that requires a file that will be fetched by the back-end server using a path (form parameters, cookies...).
1
# Example
2
http://example.com/getItem.jsp?item=file.html
Copied!
Then, to construct a payload, it's interesting to have a set of important files to search:

Filter bypass

Various filters could be set for a web application (using a Web Application Firewall for example). A set of bypass payloads can be found in PayloadsAllTheThings.

User privilege

If you can successfully retrieve one of the following files, you are at least a member of the Administrators group:
1
c:/documents and settings/administrator/ntuser.ini
2
c:/documents and settings/administrator/desktop/desktop.ini
3
c:/users/administrator/desktop/desktop.ini
4
c:/users/administrator/ntuser.ini
Copied!
There may be no "administrator" account, you have to guess the right one in that case.
If you can read either of these files, the file reading process has LocalSystemprivileges.
1
c:/system volume information/wpsettings.dat
2
C:/Windows/CSC/v2.0.6/pq
3
C:/Windows/CSC/v2.0.6/sm
4
C:/$Recycle.Bin/S-1-5-18/desktop.ini
Copied!

References

PayloadsAllTheThings/Directory Traversal at master ยท swisskyrepo/PayloadsAllTheThings
GitHub
Testing Directory traversal/file include (OTG-AUTHZ-001) | Owasp Testing Guide v4
What is directory traversal, and how to prevent it? | Web Security Academy
WebSecAcademy
Exploiting Blind File Reads / Path Traversal Vulnerabilities on Microsoft Windows Operating Systems
soffensive blog
Last modified 17d ago