Local File Inclusion
Local File Inclusion (LFI) is a web application vulnerability that allows an attacker to include files from the server's filesystem into the application's output. This vulnerability typically occurs when applications use user-supplied input to construct file paths without proper validation or sanitization.
Understanding the Vulnerability
LFI vulnerabilities emerge when web applications dynamically include files based on user input. The core issue is that applications trust user-supplied data to determine which file to include, allowing attackers to manipulate the file path to access unintended files on the server.
How LFI Works
When a web application needs to load different pages or templates, developers often use a parameter to specify which file to include. For example, a website might use a URL like example.com/index.php?page=about to load different content pages. If the application doesn't properly validate the page parameter, an attacker can manipulate it to access sensitive files.
Vulnerable Code Examples
Here's a classic example of vulnerable PHP code:
<?php
// Vulnerable to LFI
$page = $_GET['page'];
include($page . '.php');
?>This code directly uses user input from the URL parameter without any validation. An attacker could exploit this by requesting:
index.php?page=../../../../etc/passwd%00(using null byte injection to bypass the .php extension)index.php?page=../../../var/log/apache2/access.log
Another common vulnerable pattern occurs when developers attempt basic sanitization but fail:
This can be bypassed using nested directory traversal sequences like ....//....//etc/passwd.
Here's a vulnerable example in Python using Flask:
Exploitation Techniques
Attackers use several techniques to exploit LFI vulnerabilities:
Directory Traversal
The most common technique uses ../ sequences to navigate up the directory tree:
Null Byte Injection
In older PHP versions (before 5.3.4), null bytes could truncate strings:
Encoding Bypass
Attackers may use URL encoding or double encoding to bypass simple filters:
Wrapper Exploitation
PHP supports various stream wrappers that attackers can abuse:
Secure Code Examples
Here's how to properly protect against LFI vulnerabilities:
Whitelisting Approach (Most Secure)
Basename and Path Validation
Python Flask Secure Implementation
Prevention Best Practices
Use Whitelisting: Define an explicit list of allowed files or values rather than trying to blacklist dangerous patterns. Blacklists are easily bypassed.
Avoid User Input in File Paths: Whenever possible, don't use user input to construct file paths. Use indirect references like IDs that map to filenames in your code.
Validate and Sanitize: If you must use user input, apply strict validation using functions like basename() to strip directory components and realpath() to resolve the actual path.
Path Containment Checks: Always verify that the resolved file path stays within your intended directory using string comparison after resolving with realpath().
Disable Dangerous Functions: In PHP, disable dangerous functions and wrappers in php.ini:
Principle of Least Privilege: Run your web server with minimal permissions so even if LFI occurs, the damage is limited.
Input Type Enforcement: Use strong typing and validation frameworks that enforce expected data types and formats.
Detection and Testing
Security professionals can test for LFI using these approaches:
Manual testing with common payloads targeting known sensitive files
Automated scanning tools like Burp Suite, OWASP ZAP, or Nikto
Code review focusing on file inclusion functions:
include(),require(),include_once(),require_once(),file_get_contents(),fopen()Web Application Firewalls (WAF) can detect and block common LFI patterns
Real-World Impact
LFI vulnerabilities can lead to severe consequences including exposure of configuration files containing database credentials, disclosure of application source code revealing additional vulnerabilities, access to system files like password hashes, and in some cases, remote code execution when combined with file upload functionality or log poisoning techniques.
Understanding LFI is crucial for both developers building secure applications and security professionals protecting them. The key is treating all user input as untrusted and implementing defense in depth with multiple layers of validation and security controls.
References & More
Last updated