Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What Is Local File Inclusion (LFI)?

DZone's Guide to

What Is Local File Inclusion (LFI)?

In this article, we discuss a Hollywood-type hack, Local File Inclusion, in which an attacker can trick your web app into including malicious files.

· Security Zone
Free Resource

Discover an in-depth knowledge about the different kinds of iOS hacking tools and techniques with the free iOS Hacking Guide from Security Innovation.

Local File inclusion (LFI), or simply File Inclusion, refers to an inclusion attack through which an attacker can trick the web application into including files on the web server by exploiting a functionality that dynamically includes local files or scripts. The consequences of a successful LFI attack include 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 included 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, a 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.

Learn about the importance of a strong culture of cybersecurity, and examine key activities for building – or improving – that culture within your organization.

Topics:
security ,local file inclusion ,remote file inclusion ,vulnerabilities

Published at DZone with permission of Ian Muscat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}