Difference between revisions of "Testing for Local File Inclusion"

From OWASP
Jump to: navigation, search
(Blackbox chapter is updated)
(Final edit)
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{Template:OWASP Testing Guide v4}}
 
{{Template:OWASP Testing Guide v4}}
 
 
== Brief Summary ==
 
== Brief Summary ==
 
   
 
   
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:
+
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 web server
Line 9: Line 11:
 
*Denial of Service (DoS)
 
*Denial of Service (DoS)
 
*Sensitive Information Disclosure
 
*Sensitive Information Disclosure
 +
 +
 +
The File inclusion vulnerability could be of two types:
 +
* Local
 +
* Remote
  
  
 
== Description of the Issue ==
 
== Description of the Issue ==
  
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.
+
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.
  
  
 
== Black Box testing and 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:
+
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:
 +
 
 
<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 53:
 
...
 
...
 
</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 59:
 
<?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
 
</pre>
 
</pre>
  
To be continued.
 
 
== Gray Box testing and example ==
 
 
Gray box
 
  
 
== Mitigation ==
 
== Mitigation ==
  
How to protect yourself from LFI
+
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.
 +
 
  
 
== References ==
 
== References ==
Line 63: Line 77:
 
* Wikipedia - http://www.wikipedia.org/wiki/Local_File_Inclusion
 
* Wikipedia - http://www.wikipedia.org/wiki/Local_File_Inclusion
 
* 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)
 

Revision as of 04:20, 19 May 2014

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

Brief 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


The File inclusion vulnerability could be of two types:

  • Local
  • Remote


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.


Black Box testing and example

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


Mitigation

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.


References