# File Inclusion on TryHackMe: three defences and how each one failed

| Field | Value |
| --- | --- |
| Platform | TryHackMe, room [File Inclusion](https://tryhackme.com/room/fileinc) (Jr Penetration Tester path, section 8) |
| Difficulty | Easy / learning room with 3 hands-on challenges |
| OS | Linux, Apache + old PHP (5.2) |
| Date completed | 2026-11-18 |
| Time spent | ~2 h for the room incl. challenges (hints used: no) |
| Skills | Local file inclusion (LFI), path traversal, filter bypasses, request tampering with DevTools + Burp |
| Weakness | User input used as a file path in `include()` (CWE-98 / CWE-22) |
| OWASP Top 10 | A01 Broken Access Control (path traversal), A05 Injection |
| Status | Public THM room. This writeup describes the approach only: no task answers, no flags |

## TL;DR
Three small PHP apps each take a file name from the user and pass it to `include()`. Each one tries a different defence: a broken form, a value hidden in a cookie, and a filter that strips characters and adds `.php`. None of them holds, because the server still trusts whatever path reaches it. Tools: the browser, DevTools and Burp; no terminal needed.

## The idea in one paragraph
A file inclusion bug happens when an app builds a file path from user input, like `include($_GET['lang'] . '.php')`. With `../` you climb out of the web folder (`/var/www/html`) and reach any file the web server may read, e.g. `/etc/passwd`. Filters try to stop this by removing `../`, adding an extension, or only accepting certain request types. Each filter only blocks one shape of input, so the attacker changes the shape.

## Challenge 1: the "broken" form
**Defence:** the input form didn't seem to work; the request never carried the file name the way the app expected.

**What I did:** opened DevTools, looked at the form in the HTML and saw it sent the wrong request method. The server-side code read the parameter from a **POST** body, the form used **GET**. I edited the form's `method` in DevTools to `POST` and sent it again → the file was included.

**Why it fails:** the server treats "the form is broken" as protection. But the browser runs whatever HTML I give it, and any client can send any method.

## Challenge 2: the value in the cookie
**Defence:** no visible input to abuse; access depended on who the app thought I was.

**What I did:** clicked around without a plan first, then thought: *the app has to remember my role somewhere, probably in a cookie.* DevTools → **Application → Cookies** showed a cookie whose value the app used. Changing it showed that **the cookie value ended up in the file path**. From there it was the same traversal as before, just placed in the cookie instead of a form field: climb up with `../`, and stop the appended `.php` with a null byte (`%00`).

![Challenge 2: the traversal path in the cookie, file content shown (flag blurred)](https://raw.githubusercontent.com/0x8R41NR07/detection-lab/main/docs/img/fi-challenge2-cookie.png)

**Why it fails:** cookies are user input too. Anything the browser stores, the user can edit.

## Challenge 3: the filter
**Defence:** a plain absolute path didn't work. The error message showed why: the app **removed the `/` characters** and **appended `.php`**:

![Challenge 3: error message showing the stripped slashes and the appended .php](https://raw.githubusercontent.com/0x8R41NR07/detection-lab/main/docs/img/fi-challenge3-error.png)

That error message was a gift: it showed the filter, the full server path (`/var/www/html/chall3.php`) and the PHP version (5.2).

**What I did:**
1. The form used GET. Switched it to **POST** in DevTools → the slashes now survived (the filter only cleaned the GET parameter).
2. POST + traversal + null byte still failed, so I sent the request through **Burp**. There I saw two places carrying a path: the cookie left over from challenge 2 and a token with the file path.
3. Put the same traversal + null-byte payload into **both** → the file was included.

**Why it fails:** the filter only covered one input channel (GET). The null byte works because PHP before 5.3.4 cut the string at `%00`, so the appended `.php` was ignored.

## Remediation (how the developer fixes it)
- **Never pass user input to `include()` / file functions.** Map choices to an **allow-list**: `?lang=en` → the server picks `en.php` from a fixed list.
- If a file name must come from the user: take `basename()`, build the full path, **canonicalise it** (`realpath()`) and check it still starts with the allowed folder.
- **Treat every input channel the same**: GET, POST, cookies, headers. Validate on the server after the request arrives, not per channel.
- **Update PHP.** The null-byte trick has been dead since PHP 5.3.4.
- **Turn off detailed error messages** in production (`display_errors = Off`). Challenge 3's error gave away the filter and the server path.
- Disable `allow_url_include` so local inclusion can't turn into remote inclusion.
- A WAF can catch common `../` and `%00` payloads, but only as an extra layer, never as the fix.

## Defender's view
- **Web server logs** show it: `../` sequences, `%2e%2e%2f`, `%00` or `/etc/passwd` in a parameter, a POST body or a **cookie**. Cookies aren't in the default Apache access log, so log them (or use a WAF / reverse proxy log) if you want to see challenge 2-style attacks.
- **PHP warnings** like `include(...): failed to open stream` in the error log are a strong sign that someone is probing.
- In Wazuh: the web-attack rules (rule group `web` / `attack`) flag common traversal strings in access logs; a custom rule for `%00` and `../` in any request field would be a good next detection.

## Lessons learned
- **Error messages are recon.** One warning showed me the filter, the path and the PHP version.
- **Every part of a request is input:** the method, the body, cookies, hidden tokens. When one channel is filtered, try the others.
- **Think about where the app keeps state.** On challenge 2 I clicked around without a plan until I asked "where does it remember my role?" That question found the cookie.
- **Burp shows the whole request.** DevTools let me change the form, but only Burp showed me the second place (the token) that also carried the path.

## References
- [TryHackMe — File Inclusion](https://tryhackme.com/room/fileinc)
- [OWASP — Testing for Local File Inclusion](https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/07-Input_Validation_Testing/11.1-Testing_for_Local_File_Inclusion)
- [CWE-98: PHP Remote/Local File Inclusion](https://cwe.mitre.org/data/definitions/98.html)
- [CWE-22: Path Traversal](https://cwe.mitre.org/data/definitions/22.html)

