Overview
File Inclusion vulnerabilities occur when web applications allow user-supplied input to specify files that will be processed or included in the application’s context. This can lead to:- Local File Inclusion (LFI): Including files from the local server filesystem
- Remote File Inclusion (RFI): Including files from remote servers
- Source code disclosure
- Sensitive data exposure
- Remote code execution
- Server compromise
LFI vs RFI vs File Disclosure
Important distinction:- File Inclusion: The file is executed/processed in the application context (can lead to code execution)
- Arbitrary File Access/Disclosure: The file content is read/displayed (information disclosure)
RFI Requirements
Remote File Inclusion requires specific PHP configuration:allow_url_include = On(disabled by default in modern PHP)allow_url_fopen = On(enabled by default)
Objective
Read all five famous quotes from the file/hackable/flags/fi.php using only the file inclusion vulnerability.
Note: The file path is ../hackable/flags/fi.php relative to the vulnerability page.
Security Levels
- Low
- Medium
- High
- Impossible
Vulnerability Analysis
The low security level has absolutely no input validation. User input is directly used to include files, making it trivially exploitable for both LFI and RFI.Vulnerable Code
$file variable is then used directly in an include statement (in the main page logic):Why It’s Vulnerable
- No input validation whatsoever
- No path sanitization
- No whitelist of allowed files
- Direct use of user input in
include() - Accepts both absolute and relative paths
Testing Approach
Local File Inclusion (LFI):-
Directory traversal to read local files:
-
Null byte injection (PHP < 5.3.4):
-
PHP wrapper filters to read source code:
allow_url_include is enabled:Show Hint
Show Hint
You can navigate to parent directories using
../ sequences. Count how many levels up you need to go to reach the root directory, then navigate to the target file.Alternatively, use PHP stream wrappers to read the file content. The php://filter wrapper is particularly useful for reading PHP source code.Show Spoiler
Show Spoiler
LFI Examples:The last example is important because it encodes the PHP file as base64, so you can see the source code instead of executing it.RFI Examples (if enabled):For the objective, you need to read the quotes from
fi.php. Using the base64 filter lets you see the actual PHP code with all 5 quotes.Testing Methodology
Local File Inclusion (LFI)
Basic Directory Traversal:Remote File Inclusion (RFI)
Basic RFI:Automated Testing
LFI/RFI Scanner Tools:- dotdotpwn: Automated LFI/RFI fuzzer
- fimap: LFI/RFI exploitation tool
- Burp Suite: Manual testing with Intruder
Defense Strategies
Primary Defenses
-
Avoid Dynamic File Inclusion
-
Strict Whitelist Validation
-
Use basename() and Validate
Additional Defenses
-
Disable allow_url_include
-
Use open_basedir Restriction
-
Input Validation
What Doesn’t Work
- Blacklisting patterns: Too many bypass techniques
- Single-pass filtering: Can be bypassed with nesting
- Filtering only http://: Many other wrappers exist
- Using wildcards in whitelist: Too permissive (as shown in high level)
- Client-side validation: Easily bypassed
