This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org

Difference between revisions of "Testing for Local File Inclusion"

From OWASP
Jump to: navigation, search
(Blackbox chapter is updated)
m (Local File Inclusion)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Template:OWASP Testing Guide v4}}
 
{{Template:OWASP Testing Guide v4}}
 +
== Summary ==
 +
 +
The File Inclusion vulnerability allows an attacker to include a file, usually exploiting a "dynamic file inclusion" mechanisms implemented in the target application. The vulnerability occurs due to the use of user-supplied input without proper validation.
  
== Brief Summary ==
+
 
+
This can lead to something as outputting the contents of the file, but depending on the severity, it can also lead to:
File Inclusion vulnerability allows an attacker to include a file, usually through a script on the web server. The vulnerability occurs due to the use of user-supplied input without proper validation. This can lead to something as minimal as outputting the contents of the file, but depending on the severity, to list a few it can lead to:
 
  
 
*Code execution on the web server
 
*Code execution on the web server
Line 11: Line 13:
  
  
== Description of the Issue ==
+
'''Local File Inclusion''' (also known as LFI) is the process of including files, that are already locally present on the server, through the exploiting of vulnerable inclusion procedures implemented in the application. This vulnerability occurs, for example, when a page receives, as input, the path to the file that has to be included and this input is not properly sanitized, allowing directory traversal characters (such as dot-dot-slash) to be injected. Although most examples point to vulnerable PHP scripts, we should keep in mind that it is also common in other technologies such as JSP, ASP and others.
 +
 
 +
 
 +
== How to Test ==
 +
 +
Since LFI occurs when paths passed to "include" statements are not properly sanitized, in a blackbox testing approach, we should look for scripts which take filenames as parameters.
  
Local File Inclusion (also known as LFI) is the process of including local files on a server through the web browser. This vulnerability occurs when a page include is not properly sanitized, and allows directory traversal characters (such as dot-dot-slash) to be injected. Although most examples point to vulnerable PHP scripts, one should keep in mind it is also common in other technologies such as JSP, ASP and others.
 
  
 +
Consider the following example:
  
== Black Box testing and example ==
 
 
Since LFI occurs when page includes are not properly sanitized, in a blackbox testing approach one should look for scripts which take filenames as parameters. Consider the following example:
 
 
<pre>
 
<pre>
 
http://vulnerable_host/preview.php?file=example.html
 
http://vulnerable_host/preview.php?file=example.html
 
</pre>
 
</pre>
 +
  
 
This looks as a perfect place to try for LFI. If an attacker is lucky enough, and instead of selecting the appropriate page from the array by its name, the script directly includes the input parameter, it is possible to include arbitrary files on the server.
 
This looks as a perfect place to try for LFI. If an attacker is lucky enough, and instead of selecting the appropriate page from the array by its name, the script directly includes the input parameter, it is possible to include arbitrary files on the server.
 +
 +
 
Typical proof-of-concept would be to load passwd file:
 
Typical proof-of-concept would be to load passwd file:
 
<pre>
 
<pre>
 
http://vulnerable_host/preview.php?file=../../../../etc/passwd
 
http://vulnerable_host/preview.php?file=../../../../etc/passwd
 
</pre>
 
</pre>
 +
  
 
If the above mentioned conditions are met, an attacker would see something like the following:
 
If the above mentioned conditions are met, an attacker would see something like the following:
Line 38: Line 46:
 
...
 
...
 
</pre>
 
</pre>
 +
  
 
Very often, even when such vulnerability exists, its exploitation is a bit more complex. Consider the following piece of code:
 
Very often, even when such vulnerability exists, its exploitation is a bit more complex. Consider the following piece of code:
Line 43: Line 52:
 
<?php “include/”.include($_GET['filename'].“.php”); ?>
 
<?php “include/”.include($_GET['filename'].“.php”); ?>
 
</pre>
 
</pre>
 +
  
 
In the case, simple substitution with arbitrary filename would not work as the postfix 'php' is appended. In order to bypass it, a technique with null-byte terminators is used. Since %00 effectively presents the end of the string, any characters after this special byte will be ignored. Thus, the following request will also return an attacker list of basic users attributes:
 
In the case, simple substitution with arbitrary filename would not work as the postfix 'php' is appended. In order to bypass it, a technique with null-byte terminators is used. Since %00 effectively presents the end of the string, any characters after this special byte will be ignored. Thus, the following request will also return an attacker list of basic users attributes:
 +
 
<pre>
 
<pre>
 
http://vulnerable_host/preview.php?file=../../../../etc/passwd%00
 
http://vulnerable_host/preview.php?file=../../../../etc/passwd%00
 +
http://vulnerable_host/preview.php?file=../../../../etc/passwd%00jpg
 
</pre>
 
</pre>
  
To be continued.
 
 
== Gray Box testing and example ==
 
 
Gray box
 
 
== Mitigation ==
 
 
How to protect yourself from LFI
 
  
 
== References ==
 
== References ==
Line 64: Line 67:
 
* Hakipedia - http://hakipedia.com/index.php/Local_File_Inclusion
 
* Hakipedia - http://hakipedia.com/index.php/Local_File_Inclusion
  
<br>
+
 
--[[User:Alexander Antukh|Alexander Antukh]] ([[User talk:Alexander Antukh|talk]]) 07:17, 28 September 2013 (CDT)
+
== Remediation ==
 +
 
 +
The most effective solution to eliminate file inclusion vulnerabilities is to avoid passing user-submitted input to any filesystem/framework API. If this is not possible the application can maintain a white list of files, that may be included by the page, and then use an identifier (for example the index number) to access to the selected file. Any request containing an invalid identifier has to be rejected, in this way there is no attack surface for malicious users to manipulate the path.

Latest revision as of 05:20, 25 June 2017

This article is part of the new OWASP Testing Guide v4.
Back to the OWASP Testing Guide v4 ToC: https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents Back to the OWASP Testing Guide Project: https://www.owasp.org/index.php/OWASP_Testing_Project

Summary

The File Inclusion vulnerability allows an attacker to include a file, usually exploiting a "dynamic file inclusion" mechanisms implemented in the target application. The vulnerability occurs due to the use of user-supplied input without proper validation.


This can lead to something as outputting the contents of the file, but depending on the severity, it can also lead to:

  • Code execution on the web server
  • Code execution on the client-side such as JavaScript which can lead to other attacks such as cross site scripting (XSS)
  • Denial of Service (DoS)
  • Sensitive Information Disclosure


Local File Inclusion (also known as LFI) is the process of including files, that are already locally present on the server, through the exploiting of vulnerable inclusion procedures implemented in the application. This vulnerability occurs, for example, when a page receives, as input, the path to the file that has to be included and this input is not properly sanitized, allowing directory traversal characters (such as dot-dot-slash) to be injected. Although most examples point to vulnerable PHP scripts, we should keep in mind that it is also common in other technologies such as JSP, ASP and others.


How to Test

Since LFI occurs when paths passed to "include" statements are not properly sanitized, in a blackbox testing approach, we should look for scripts which take filenames as parameters.


Consider the following example:

http://vulnerable_host/preview.php?file=example.html


This looks as a perfect place to try for LFI. If an attacker is lucky enough, and instead of selecting the appropriate page from the array by its name, the script directly includes the input parameter, it is possible to include arbitrary files on the server.


Typical proof-of-concept would be to load passwd file:

http://vulnerable_host/preview.php?file=../../../../etc/passwd


If the above mentioned conditions are met, an attacker would see something like the following:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
alex:x:500:500:alex:/home/alex:/bin/bash
margo:x:501:501::/home/margo:/bin/bash
...


Very often, even when such vulnerability exists, its exploitation is a bit more complex. Consider the following piece of code:

<?php “include/”.include($_GET['filename'].“.php”); ?>


In the case, simple substitution with arbitrary filename would not work as the postfix 'php' is appended. In order to bypass it, a technique with null-byte terminators is used. Since %00 effectively presents the end of the string, any characters after this special byte will be ignored. Thus, the following request will also return an attacker list of basic users attributes:

http://vulnerable_host/preview.php?file=../../../../etc/passwd%00
http://vulnerable_host/preview.php?file=../../../../etc/passwd%00jpg


References


Remediation

The most effective solution to eliminate file inclusion vulnerabilities is to avoid passing user-submitted input to any filesystem/framework API. If this is not possible the application can maintain a white list of files, that may be included by the page, and then use an identifier (for example the index number) to access to the selected file. Any request containing an invalid identifier has to be rejected, in this way there is no attack surface for malicious users to manipulate the path.