What is Local File Inclusion (LFI)?

Local File inclusion (LFI), or simply File Inclusion, refers to an inclusion attack through which an attacker can trick the web application in including files on the web server by exploiting functionality that dynamically includes local files or scripts. The consequence of a successful LFI attack includes Directory Traversal and Information Disclosure as well as Remote Code Execution.

Typically, Local File Inclusion (LFI) occurs, when an application gets the path to the file that has to be included as an input without treating it as untrusted input. This would allow a local file to be supplied to the include statement.

Local File Inclusion is very much like Remote File Inclusion (RFI), with the difference that with Local File Inclusion, an attacker can only include local files (not remote files like in the case of RFI).

The following is an example in PHP that is vulnerable to Local File Inclusion (LFI).

/**
* Get the filename from a GET input
* Example - http://example.com/?file=filename.php
*/
$file = $_GET['file'];

/**
* Unsafely include the file
* Example - filename.php
*/
include('directory/' . $file);

In the above example, an attacker could make the following request to trick the application into executing a malicious script such as a webshell that the attacker managed to upload to the web server.

http://example.com/?file=../../uploads/evil.php

In this example, the file the uploaded by the attacker will be included and run as the user running the web application. That would allow an attacker to run any code they wanted on the web server.

Directory Traversal

The above example is a worst-case scenario. The reality is that an attacker does not always have the ability to upload a malicious file to the application.

Even if the attacker did have the ability to upload arbitrary files, there is no guarantee that the application will save the file on the same server where the Local File Inclusion (LFI) vulnerability exists. Even then, the attacker would still need to know the path-on-disk to the uploaded file in order to include it in an LFI attack.

Even without the ability to upload and execute arbitrary code however, an Local File Inclusion vulnerability can be very useful for an attacker. An attacker can still perform a Directory Traversal attack using an LFI vulnerability as follows.

http://example.com/?file=../../../../etc/passwd

In the above example, an attacker can get the contents of /etc/passwd file containing a list of users on the server using the Local File Inclusion vulnerability to perform a Directory Traversal attack. Similarly, an attacker may leverage the Directory Traversal vulnerability to gain access to credentials, logs, source code and other sensitive information that may help advance an attack.

Preventing Local File Inclusion (LFI) vulnerabilities

The best way to eliminate Local File Inclusion (LFI) vulnerabilities is to avoid dynamically including files based on user input. If this is not possible, the application should maintain a whitelist of files that can be included in order to limit the attacker’s control over what gets included.

Share this post

Leave a Reply

Your email address will not be published.