LFI happens when user-controlled input reaches a file-inclusion function without sanitization. The single most important thing to know is which function the app uses — because the function’s capabilities decide whether this stays a file-read or escalates to RCE.
The Capability Matrix
Before you do anything, identify (or guess) the inclusion function. Its capabilities decide the ceiling of what’s possible:
| Function | Read | Execute | Remote URL |
|---|---|---|---|
| PHP | |||
include() / include_once() | ✅ | ✅ | ✅ |
require() / require_once() | ✅ | ✅ | ❌ |
file_get_contents() | ✅ | ❌ | ✅ |
fopen() / file() | ✅ | ❌ | ❌ |
readfile() | ✅ | ❌ | ❌ |
| Node.js | |||
fs.readFile() | ✅ | ❌ | ❌ |
fs.sendFile() | ✅ | ❌ | ❌ |
res.render() | ✅ | ✅ | ❌ |
| Java | |||
include | ✅ | ❌ | ❌ |
import | ✅ | ✅ | ✅ |
| .NET | |||
@Html.Partial() | ✅ | ❌ | ❌ |
@Html.RemotePartial() | ✅ | ❌ | ✅ |
Response.WriteFile() | ✅ | ❌ | ❌ |
include | ✅ | ✅ | ✅ |
Why This Matters
- Read only → you can disclose files but not execute code. Hunt for config files with credentials, SSH keys, source code.
- Execute → you can turn LFI into RCE via wrappers, log poisoning, or session files.
- Remote → you can host your payload on your own server and have the app fetch it (RFI).
Identifying the Vulnerable Pattern
The app’s modification of your input defines the attack:
Pattern 1 — Unmodified Input
include($_GET['language']);Your input is passed straight through. Supply an absolute path:
?language=/etc/passwd
Easiest case. If the file loads without error, you have LFI.
Pattern 2 — Prepended Directory
include("./languages/" . $_GET['language']);Your input is appended to a base path. Use ../ traversal to escape:
?language=../../../../etc/passwd
Trigger an error first to learn the base path and count the depth to root.
Pattern 3 — Prepended Prefix (Not a Directory)
include("lang_" . $_GET['language']);Your input is concatenated with a string prefix. Use a leading / to make the prefix part of a directory name:
?language=/../../../../etc/passwd
This turns lang_ into lang_/ — a directory. The traversal then resolves. Caveat: fails if no such directory actually exists.
Pattern 4 — Appended Extension
include($_GET['language'] . ".php");Your input gets .php appended. Historically bypassable with path truncation or null byte, but both are obsolete (PHP ≥ 5.5). On modern targets you’ll need PHP wrappers to bypass (see PHP Wrappers section).
Detecting LFI in the Wild
- Look for parameters that reference files, pages, templates, or languages:
?file=,?page=,?view=,?template=,?lang=,?doc=,?include=
- Try a simple file read —
/etc/passwdon Linux,C:\Windows\win.inion Windows - Watch for distinctive strings in responses (
root:x:0:0:,[fonts]) - Watch for errors — they often leak the base path, confirming traversal is possible
Second-Order LFI
The payload isn’t fired on the request that stores it. Instead:
- One endpoint (say, profile update) accepts a filename into a stored field
- Another endpoint (say, dashboard) later reads that field and passes it to
include()
Second-order LFI hides in trusted data. The lesson: validate every input source, not just live request parameters.
