Companies and individual software engineers are constantly developing innovative solutions to solve problems. Almost all of them are building the solution with web or mobile applications using different functionalities to make it more user-friendly. However, insecure implementation of some functions could lead to security breaches.

Today, let's talk about File Inclusion:

  • What and why this security vulnerability exist
  • How or what are the tools to exploit the vulnerability
  • Why cybercriminals have abused them, and;
  • How to prevent them from occurring in your own applications.

File Inclusion

File Inclusion is a security vulnerability in applications that allows users to read files from a file system, provide download features, search and list files, and other similar tasks.

The file inclusion can be chained or partnered with Directory Traversal.  It is a security vulnerability in applications that allows users to access directories which they should not be able to access by simply traversing the filesystem to restricted files or directories.

Local and Remote

Local File Inclusion (LFI) allows an attacker to read and execute files on the target application. It can be critical because if the web server is insecurely configured and running with high privileges, the attacker may gain access to sensitive files and information.

Remote File Inclusion (RFI) allows an attacker to execute codes from an external file you include that are not stored locally on the web server.

Exploiting Local File Inclusion (LFI)

To demonstrate this vulnerability, let's use the Damn Vulnerable Web Application (DVWA).

Here we have a link for viewing the content of a file:

DVWA Page with File Inclusion vulnerability

The vulnerable code is:

    $file = $_GET['page'];

The web app is getting the value of the page parameter to display its content on the page using the include() function. Changing the value from file2.php to file1.php will display the content of the file1.php file.

Combining File Inclusion with Directory Traversal

The file file2.php is currently stored at /var/www/dvwa/vulnerabilities/fi/.

Using the Directory Traversal or ../, we will traverse the directories 5x to get the content of passwd file located at /etc/ directory.

Getting the content of passwd file

The payload is

Getting the Source Code

It is also possible to retrieve the source code of the file or configuration files using the php://filter wrapper.

PHP wrappers allow us to make File Inclusion far more useful. The PHP interpreter can handle many things as streams of data, such as local files, remote URL, and even remote SSH systems.

The Payload is

Getting the content of config file

The result of the payload is encoded in Base64 format due to the wrapper we used.


Decoding it to Base64 will give us the following readable source code:

#I removed other unnecessary texts

$DBMS = 'MySQL';

$_DVWA = array();
$_DVWA[ 'db_server' ]   = '';
$_DVWA[ 'db_database' ] = 'dvwa';
$_DVWA[ 'db_user' ]     = 'root';
$_DVWA[ 'db_password' ] = 'p@ssw0rd';
$_DVWA[ 'db_port '] = '5432';

$_DVWA[ 'recaptcha_public_key' ]  = '';
$_DVWA[ 'recaptcha_private_key' ] = '';

$_DVWA[ 'default_security_level' ] = 'low';

$_DVWA[ 'default_phpids_level' ] = 'disabled';

$_DVWA[ 'default_phpids_verbose' ] = 'false';


Exploiting Remote File Inclusion (RFI)

We'll be using the same target website and we will use an external website to demonstrate the vulnerability.

The Payload is

Remote File Inclusion

The is an external website and the content of the test.php file is <?php phpinfo(); ?>.

The payload above will display the PHP Information which is very sensitive for a web application.

Spawning a System Shell

Using Remote File Inclusion vulnerability, it is possible to spawn a shell using an external malicious file.

I used pentestmonkey's shell file and changed the value of $ip and $port my own IP and Port using ngrok.

Ngrok is a free tool that allows us to tunnel from a public URL to our application running locally.

The payload is

I set up my netcat listener and once I visited the link above, I will automatically get a feedback and the shell will spawn.

Spawning a Shell

Bypasses and Techniques in Exploiting File Inclusion

All the bypasses and techniques can be found on the PayloadsAllTheThings GitHub repository.

For LFI, you may use different wrappers such as:

File Inclusion Tools

The Impact in Confidentiality, Integrity, and Availability (CIA)

The attacker may be able to retrieve and read the contents of files and expose sensitive data.

It may be possible as well to modify the files in the server if the attacker manages to spawn a shell to access the system.

Availability can also be affected if the attacker specifies a filename for a large file, or points to a special device or a file that does not have the format that the application expects leading to Denial of Service.

Preventing File Inclusion

  • Assume all input is malicious
    The most effective way to prevent this kind of attack is to perform input validation.
  • Check both sides
    Most defenses are being implemented on the client side only, but I highly recommend implementing the defense on the server side as well in order to avoid Client-Side Enforcement of Server-Side Security.
  • Use an Application Firewall
    Use an application firewall that can detect attacks against this vulnerability. It is a good addition to your security defenses.
  • Limit Privileges
    Run your application using the lowest privileges required to perform the necessary tasks. Create an isolated account with limited privileges if possible, that are only used for a single task. In this way, a successful attack will not immediately give the attacker access to the rest of the functionality - for example, deleting and changing files on the server.
  • Configure PHP
    If you are using PHP, set allow_url_fopen and allow_url_include to false. This limits the ability to include files from local and remote locations.

Secuna is a trusted cybersecurity testing platform that provides organizations a robust and secure platform that enables them to collaborate with the top security researchers from around the world to find and address security vulnerabilities.

Blog Post Photo from Christoph Bouvier