File inclusion vulnerability
A file inclusion vulnerability is a type of web vulnerability that is most commonly found to affect web applications that rely on a scripting run time. This issue is caused when an application builds a path to executable code using an attacker-controlled variable in a way that allows the attacker to control which file is executed at run time. A file include vulnerability is distinct from a generic directory traversal attack, in that directory traversal is a way of gaining unauthorized file system access, and a file inclusion vulnerability subverts how an application loads code for execution. Successful exploitation of a file inclusion vulnerability will result in remote code execution on the web server that runs the affected web application. An attacker can use remote code execution to create a web shell on the web server, which can be used for website defacement.
Types of Inclusion
[edit]Remote file inclusion
[edit]Remote file inclusion (RFI) occurs when the web application downloads and executes a remote file. These remote files are usually obtained in the form of an HTTP or FTP URI as a user-supplied parameter to the web application.
Local file inclusion
[edit]Local file inclusion (LFI) is similar to a remote file inclusion vulnerability except instead of including remote files, only local files i.e. files on the current server can be included for execution. This issue can still lead to remote code execution by including a file that contains attacker-controlled data such as the web server's access logs.
Programming languages
[edit]File inclusion vulnerability primarily happens on programming languages that rely on dynamic inclusion of files, such as PHP and JavaScript. These languages are interpreted, rather than compiled. Languages that use file inclusion for including libraries, such as C and C++, do not necessarily suffer the same kind of vulnerability, due to being handled through preprocessing rather than dynamically. However, they are still vulnerable through path traversals by fopen() and buffer overflows.
PHP
[edit]In PHP the main cause is due to the use of unvalidated user-input with a filesystem function that includes a file for execution. Most notable are the include and require statements. Most of the vulnerabilities can be attributed to novice programmers not being familiar with all of the capabilities of the PHP programming language. The PHP language has a directive which, if enabled, allows filesystem functions to use a URL to retrieve data from remote locations.[1] The directive is allow_url_fopen in PHP versions <= 4.3.4 and allow_url_include since PHP 5.2.0. In PHP 5.x this directive is disabled by default, in prior versions it was enabled by default.[2] To exploit the vulnerability an attacker will alter a variable that is passed to one of these functions to cause it to include malicious code from a remote resource. To mitigate this vulnerability all user input needs to be validated before being used.[3][4]
Example
[edit]Consider this PHP script which includes a file specified by request:
<?php
if (isset($_GET['language'])) {
include($_GET['language'] . '.php');
}
?>
<form method="get">
<select name="language">
<option value="english">English</option>
<option value="french">French</option>
...
</select>
<input type="submit">
</