DZone
Security Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Security Zone > What Is Local File Inclusion (LFI)?

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.

Ian Muscat user avatar by
Ian Muscat
·
Apr. 29, 17 · Security Zone · Tutorial
Like (1)
Save
Tweet
11.77K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Web application

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Edge Compute? It’s Kind of Like Knitting Dog Hats
  • How Template Literal Types Work in TypeScript
  • Kubernetes Service Types Explained In-Detail
  • How BDD Works Well With EDA

Comments

Security Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo