directory traversal attack
{{Short description|Computer security vulnerability to gain unauthorized access to the file system}}
A directory traversal (or path traversal) attack exploits insufficient security validation or sanitization of user-supplied file names, such that characters representing "traverse to parent directory" are passed through to the operating system's file system API. An affected application can be exploited to gain unauthorized access to the file system.
Examples
= In PHP =
A typical example of a vulnerable application in PHP code is:
$template = "red.php";
if (isset($_COOKIE["TEMPLATE"])) {
$template = $_COOKIE["TEMPLATE"];
}
include "/home/users/phpguru/templates/" . $template;
An attack against this system could be to send the following HTTP request:
GET /vulnerable.php HTTP/1.0
Cookie: TEMPLATE=../../../../../../../../../etc/passwd
The server would then generate a response such as:
HTTP/1.0 200 OK
Content-Type: text/html
Server: Apache
root:fi3sED95ibqR6:0:1:System Operator:/:/bin/ksh
daemon:*:1:1::/tmp:
phpguru:f8fk3j1OIf31.:182:100:Developer:/home/users/phpguru/:/bin/csh
The repeated ../
characters after /home/users/phpguru/templates/
have caused
[https://www.php.net/manual/en/function.include.php include()]
to traverse to the root directory, and then include the Unix password file /etc/passwd
.
Unix /etc/passwd
is a common file used to demonstrate directory traversal, as it is often used by crackers to try cracking the passwords. However, in more recent Unix systems, the /etc/passwd
file does not contain the hashed passwords, and they are instead located in the /etc/shadow
file, which cannot be read by unprivileged users on the machine. Even in that case, though, reading /etc/passwd
does still show a list of user accounts, which could then become a starting point for further attacks.
= Zip Slip vulnerability =
Another example is the "Zip Slip" vulnerability that affects several archive file formats like ZIP.{{cite web|url=https://security.snyk.io/research/zip-slip-vulnerability|title=Zip Slip Vulnerability|publisher=Snyk|quote=The vulnerability is exploited using a specially crafted archive that holds directory traversal filenames (e.g. ../../evil.sh). The Zip Slip vulnerability can affect numerous archive formats, including tar, jar, war, cpio, apk, rar and 7z.}}
Variations
Directory traversal in its simplest form uses the ../
pattern. Some common variations are listed below:
= Microsoft Windows =
Microsoft Windows and DOS directory traversal uses the ..\
or ../
patterns.{{cite news|url=https://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx |title=Naming Files, Paths, and Namespaces |publisher=Microsoft |quote=File I/O functions in the Windows API convert '/' to '\' as part of converting the name to an NT-style name}}
Each partition has a separate root directory (labeled C:\
where C could be any partition), and there is no common root directory above that. This means that for most directory vulnerabilities on Windows, attacks are limited to a single partition.
Directory traversal has been the cause of numerous Microsoft vulnerabilities.{{cite web |url=http://www.securityfocus.com/columnists/285 |title=Security Holes That Run Deep |first=Mark |last=Burnett |date=December 20, 2004 |publisher=SecurityFocus |access-date=March 22, 2016 |archive-date=February 2, 2021 |archive-url=https://web.archive.org/web/20210202211420/https://www.securityfocus.com/columnists/285 |url-status=dead }}{{cite web|url=https://www.cvedetails.com/vulnerability-list/vendor_id-26/opdirt-1/Microsoft.html |title=Microsoft: Security Vulnerabilities (Directory Traversal) |publisher=CVE Details}}
= Percent encoding in URIs =
Some web applications attempt to prevent directory traversal by scanning the path of a request URI for patterns such as ../
. This check is sometimes mistakenly performed before percent-decoding, causing URIs containing patterns like %2e%2e/
to be accepted despite being decoded into ../
before actual use.{{cite web|url=https://owasp.org/www-community/attacks/Path_Traversal|title=Path Traversal|publisher=OWASP}}
== Double encoding ==
Percent decoding may accidentally be performed multiple times; once before validation, but again afterwards, making the application vulnerable to Double percent-encoding attacks{{cite web |title=CWE-174: Double Decoding of the Same Data |url=https://cwe.mitre.org/data/definitions/174.html |website=cwe.mitre.org |access-date=24 July 2022 |quote=The software decodes the same input twice, which can limit the effectiveness of any protection mechanism that occurs in between the decoding operations.}} in which illegal characters are replaced by their double-percent-encoded form in order to bypass security countermeasures.{{cite web |title=CAPEC-120: Double Encoding |url=https://capec.mitre.org/data/definitions/120.html |website=capec.mitre.org |access-date=23 July 2022 |quote=This[double encoding] may allow the adversary to bypass filters that attempt to detect illegal characters or strings, such as those that might be used in traversal or injection attacks. [...] Try double-encoding for parts of the input in order to try to get past the filters.}} For example, in a double percent-encoding attack, ../
may be replaced by its double-percent-encoded form %252E%252E%252F
.{{cite web |title=Double Encoding |url=https://owasp.org/www-community/Double_Encoding |website=owasp.org |access-date=23 July 2022 |quote=For example, ../ (dot-dot-slash) characters represent %2E%2E%2F in hexadecimal representation. When the % symbol is encoded again, its representation in hexadecimal code is %25. The result from the double encoding process ../ (dot-dot-slash) would be %252E%252E%252F}} This kind of vulnerability notably affected versions 5.0 and earlier of Microsoft's IIS web server software.{{cite web|url=https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2001-0333|title=CVE-2001-0333|publisher=Common Vulnerabilities and Exposures}}
== UTF-8 ==
A badly implemented UTF-8 decoder may accept characters encoded using more bytes than necessary, leading to overlong encodings, such as %c0%ae
instead of %2e
to represent .
. This is specifically forbidden by the UTF-8 standard,{{cite web|url=https://tools.ietf.org/html/rfc3629|title=RFC 2279 - UTF-8, a transformation format of ISO 10646|year=2003 |publisher=IETF|doi=10.17487/RFC3629 |last1=Yergeau |first1=F. |doi-access=free }} but has still led to directory traversal vulnerabilities in software such as the IIS web server.{{cite web|url=https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2002-1744|title=CVE-2002-1744|publisher=Common Vulnerabilities and Exposures}}
= Archives =
Some archive formats like zip allow for directory traversal attacks: files in the archive can be written such that they overwrite files on the filesystem by backtracking. Code that extracts archive files can be written to check that the paths of the files in the archive do not engage in path traversal.
Prevention
A possible algorithm for preventing directory traversal would be to:
- Process URI requests that do not result in a file request, e.g., executing a hook into user code, before continuing below.
- When a URI request for a file/directory is to be made, build a full path to the file/directory if it exists, and normalize all characters (e.g.,
%20
converted to spaces). - It is assumed that a 'Document Root' fully qualified, normalized, path is known, and this string has a length N. Assume that no files outside this directory can be served.
- Ensure that the first N characters of the fully qualified path to the requested file is exactly the same as the 'Document Root'.
- If so, allow the file to be returned.
- If not, return an error, since the request is clearly out of bounds from what the web-server should be allowed to serve.
Using a hard-coded predefined file extension to suffix the path does not necessarily limit the scope of the attack to files of that file extension.
include $_GET["file"] . ".html";
The user can use the NULL character (indicating the end of the string) in order to bypass everything after the
. (This is PHP-specific.)
See also
- Chroot jails may be subject to directory traversal if incorrectly created. Possible directory traversal attack vectors are open file descriptors to directories outside the jail. The working directory is another possible attack vector.
- Insecure direct object reference
References
{{Reflist}}
Resources
- [https://owasp.org/ Open Web Application Security Project]
- [http://www.webappsec.org/projects/threat/classes/path_traversal.shtml The WASC Threat Classification – Path Traversal]
- [https://www.htbridge.com/vulnerability/path-traversal.html Path Traversal Vulnerability Exploitation and Remediation]{{Dead link|date=August 2023 |bot=InternetArchiveBot |fix-attempted=yes }}
- [http://cwe.mitre.org/data/definitions/22.html CWE Common Weakness Enumeration - Path Traversal]
External links
- [http://dotdotpwn.sectester.net/ DotDotPwn – The Directory Traversal Fuzzer]
- [https://www.theregister.co.uk/2005/10/05/dec_case/ Conviction for using directory traversal.]
- {{cite web|url=http://comment.zdnet.co.uk/0,39020505,39226981,00.htm|website=comment.zdnet.co.uk|archive-url= https://web.archive.org/web/20061008115042/http://comment.zdnet.co.uk/0,39020505,39226981,00.htm |archive-date=2006-10-08|title=Only bad guys benefit from bad law|date=October 7, 2005 }}
- [http://seclists.org/lists/bugtraq/2000/Oct/0264.html Bugtraq: IIS %c1%1c remote command execution]
- [http://www.schneier.com/crypto-gram-0007.html Cryptogram Newsletter July 2001.]