A typical example of a vulnerable application in PHP code is:
An attack against this system could be to send the following HTTP request:
The server would then generate a response such as:
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 [http://php.net/manual/en/function.include.php include]
to traverse to the root directory, and then include the Unix password file [[Passwd#Password file|/etc/passwd]]
.
Unix [[Passwd#Password file|/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.
Another example is the Zip Slip vulnerability that affects several archive file formats like ZIP.[1]
Directory traversal in its simplest form uses the ../
pattern. Some common variations are listed below:
Microsoft Windows and DOS directory traversal uses the ..\
or ../
patterns.[2]
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.[3] [4]
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.[5]
Percent decoding may accidentally be performed multiple times; once before validation, but again afterwards, making the application vulnerable to Double percent-encoding attacks[6] in which illegal characters are replaced by their double-percent-encoded form in order to bypass security countermeasures.[7] For example, in a double percent-encoding attack, ../
may be replaced by its double-percent-encoded form %252E%252E%252F
.[8] This kind of vulnerability notably affected versions 5.0 and earlier of Microsoft's IIS web server software.[9]
A badly implemented UTF-8 decoder may accept characters encoded using more bytes than necessary, leading to alternative character representations, such as %2e
and %c0%ae
both representing .
. This is specifically forbidden by the UTF-8 standard,[10] but has still led to directory traversal vulnerabilities in software such as the IIS web server.[11]
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.
A possible algorithm for preventing directory traversal would be to:
%20
converted to spaces).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.
The user can use the NULL character (indicating the end of the string) in order to bypass everything after the