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
In computing, file system or filesystem (often abbreviated to fs) is a method and data structure that the operating system uses to control how data is stored and retrieved. Without a file system, data placed in a storage medium would be one lar ...
.
Directory traversal is also known as the
../
(dot dot slash) attack, directory climbing, and backtracking. Some forms of this attack are also
canonicalization attacks.
Example
A typical example of a vulnerable application in
PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
code is:
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
include()
/code> to traverse to the root directory, and then include the Unix password file /etc/passwd
passwd is a command on Unix, Plan 9, Inferno, and most Unix-like operating systems used to change a user's password. The password entered by the user is run through a key derivation function to create a hashed version of the new password, whi ...
.
Unix /etc/passwd
passwd is a command on Unix, Plan 9, Inferno, and most Unix-like operating systems used to change a user's password. The password entered by the user is run through a key derivation function to create a hashed version of the new password, whi ...
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.
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.
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.
Percent encoding in URIs
Some web applications attempt to prevent directory traversal by scanning the path of a request URI Uri may refer to:
Places
* Canton of Uri, a canton in Switzerland
* Úri, a village and commune in Hungary
* Uri, Iran, a village in East Azerbaijan Province
* Uri, Jammu and Kashmir, a town in India
* Uri (island), an island off Malakula Isla ...
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.
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 in which illegal characters are replaced by their double-percent-encoded form in order to bypass security countermeasures. For example, in a double percent-encoding attack, ../
may be replaced by its double-percent-encoded form %252E%252E%252F
. This kind of vulnerability notably affected versions 5.0 and earlier of Microsoft
Microsoft Corporation is an American multinational corporation, multinational technology company, technology corporation producing Software, computer software, consumer electronics, personal computers, and related services headquartered at th ...
's IIS web server software.
UTF-8
A badly implemented UTF-8
UTF-8 is a variable-length character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode'' (or ''Universal Coded Character Set'') ''Transformation Format 8-bit''.
UTF-8 is capable of ...
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, but has still led to directory traversal vulnerabilities in software such as the IIS web server.
Archives
Some archive format
In computing, an archive file is a computer file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compre ...
s 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.
file'. '.html');
The user can use the NULL character
The null character (also null terminator) is a control character with the value zero.
It is present in many character sets, including those defined by the Baudot and ITA2 codes, ISO/IEC 646 (or ASCII), the C0 control code, the Universal Code ...
(indicating the end of the string) in order to bypass everything after the $_GET
. (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 descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier ( handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically ...
s to directories outside the jail. The working directory
In computing, the working directory of a process is a directory of a hierarchical file system, if any, dynamically associated with each process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd function, or just ...
is another possible attack vector.
* Insecure direct object reference
Insecure direct object reference (IDOR) is a type of access control vulnerability in digital security.
This can occur when a web application or application programming interface uses an identifier for direct access to an object in an internal d ...
References
{{Reflist
Resources
Open Web Application Security Project
The WASC Threat Classification – Path Traversal
External links
* DotDotPwn – The Directory Traversal Fuzzer �
* Conviction for using directory traversal
Bugtraq: IIS %c1%1c remote command execution
* Cryptogram Newsletter July 200
Web security exploits