Some web applications generate dynamic pages using values and parameters stored into a database; if an attacker could insert crafted directory traversal strings when the application saves the data, probably it will occurs a sort of "indirect" directory traversal attack. This kind of security problems is difficult to find because the parameters inside the inclusion functions seem internal and "safe" but otherwise they are not.

Some web applications generate dynamic pages using values and parameters stored into a database; if an attacker could insert crafted directory traversal strings when the application saves the data, probably it will occurs a sort of "indirect" directory traversal attack. This kind of security problems is difficult to find because the parameters inside the inclusion functions seem internal and "safe" but otherwise they are not.

−

Additionally, reviewing the source code, it is possible to analyze the functions that are supposed to handle invalid input: some developers try to massage invalid input to make it valid, avoiding warnings and errors. This functions are usually prone to security flaws.

+

Additionally, reviewing the source code, it is possible to analyze the functions that are supposed to handle invalid input: some developers try to massage invalid input to make it valid, avoiding warnings and errors. These functions are usually prone to security flaws.

Considering a web application with these instructions:

Considering a web application with these instructions:

Revision as of 08:17, 6 November 2006

Brief Summary

Nowadays, many web applications use and manage files. Using input validation methods not well designed, an aggressor could exploit the system in order to read/write files that are not intended to be accessible; in particular situations it could be possible to execute arbitrary code or system commands.

Description of the Issue

Usually, web servers and web applications implement authentication mechanisms in order to control the access to files and resources.
Web servers try to confine users' files inside a "root directory" or "web document root" which represents a physical directory on the file system; users have just to consider this directory as the base directory into the hierarchical structure of the web application.
The definition of the privileges is made using Access Control Lists (ACL) that identify which users and groups are supposed to be able to access, modify or execute a specific file on the server.
These mechanisms are designed to prevent the access to sensible files from malicious users (example: the common /etc/passwd into Unix-like platform) or to avoid the execution of system commands.

Many web applications use server-side scripts to include different kinds of files: is quite common to use this method to manage graphics templates, load static texts, and so on. Unfortunately, these applications show security issues if the input parameters used (form parameters, cookies values, ...) are not well validated.

In web servers and web applications too, this kind of problem arises in directory traversal/file include attacks; exploiting this kind of vulnerability an attacker is able read directory and files which normally he/she couldn't read, access data outside the web document root, include scripts and other kinds of files from external websites.

For the purpose of the OWASP Testing Guide, we will just consider the security threats related to web applications and not to web server (as the infamous "%5c escape code" into Microsoft IIS web server). We will provide further reading, in the references section, for the interested readers.

This kind of attack is also know as the dot-dot-slash attack (../), path traversal, directory climbing, backtracking.

During an assessment, in order to discover directory traversal and file include flaws, we need to perform two different stages:

As next step, during the assessment, we need to analyze the different attack techniques used by an aggressor in order to evaluate the input validation functions present into the web application.

Using the previous example, the dynamic page called getUserProfile.jsp loads static informations from a file, showing the content to users. An attacker could insert the malicious string "../../../../etc/passwd" to include the password hash file of a Linux/Unix system.
Obviously this kind of attack is possible only if the validation checkpoint fails; according to the filesystem privileges, the web application itself must be able to read the file.

To exploit this flaw, an aggressor needs some knowledge on where to blindly find any default files and directories on the system: guessing and trying again until the attack works is usually an easy way to perform a directory climbing.

http://example.com/getUserProfile.jsp?item=../../../../etc/passwd

For the cookies example, we have:

Cookie: USER=1826cc8f:PSTYLE=../../../../etc/passwd

It's also possible to include files (scripts too!) located on external website. Let's see:

The following example will demonstrate how is it possible to show the source code of a CGI component, without using any path traversal chars.

http://example.com/main.cgi?home=main.cgi

The component called "main.cgi" is located into the same directory of the normal HTML static files used by the application.
Sometimes, when the attackers are not so lucky, they need to use special chars (like the "." dot, "%00" null, ...) in order to bypass file extension controls and/or stop the script execution.

It's also very important to notice that when developers implement an input validation function sometimes they don't consider every possible chars encoding.
During a Black Box Test is useful to try to inject every kind of chars encoding and also every kind of path definition.

Unicode/UTF-8 Encoding (It just works in systems which are able to accept overlong UTF-8 sequences)

..%c0%af represents ../
..%c1%9c represents ..\

Gray Box testing and example

When the analysis is performed with a White Box approach, we have to follow the same methodology as in the Black Box Testing. However, since we can review the source code is possible to search the input vectors (stage (a) of the testing) more easily and accurately.
During a source code review we can use simple tools (as the grep command) to search one or more common patterns into the application code: inclusion functions/methods, filesystem operations and so on.

Using online code search engines (Google CodeSearch[1], Koders[2]) is also possible to find directory traversal flaws into OpenSource software published on Internet.

For PHP, we can use:

lang:php (include|require)(_once)?\s*['"(]?\s*\$_(GET|POST|COOKIE)

During a Gray Box Testing is usually possible to discover vulnerabilities that are impossible (or very difficult!) to find performing a Black Box assessment.

Some web applications generate dynamic pages using values and parameters stored into a database; if an attacker could insert crafted directory traversal strings when the application saves the data, probably it will occurs a sort of "indirect" directory traversal attack. This kind of security problems is difficult to find because the parameters inside the inclusion functions seem internal and "safe" but otherwise they are not.

Additionally, reviewing the source code, it is possible to analyze the functions that are supposed to handle invalid input: some developers try to massage invalid input to make it valid, avoiding warnings and errors. These functions are usually prone to security flaws.