# Local File Inclusion > [[Path Traversal]] into an interpreter's include or template loader: the application executes or renders a local file chosen by the attacker, turning file selection into code execution whenever any reachable file contains, or can be made to contain, code (CWE-98). ## How it works - **Sink.** PHP `include`/`require` (and template resolution that ends in one) interpret the target as source code. The classic shape is a request parameter concatenated into an include path with no validation. [[raw/karmainsecurity.com/chaining-security-bugs-in-discuz-x5-0-from-race-condition-to-pre-auth-9623|Chaining Security Bugs in Discuz! X5.0: from Race Condition to Pre-Auth RCE]] - **Read vs execute.** Including a non-code file discloses it; including a file holding interpreter code runs it. That makes LFI a read primitive and, with a second step, an RCE primitive. - **Getting code onto disk.** The notes show three routes: - An uploaded file the app thinks is an image: upload, then include it. Discuz! documents this chain; the PayloadPlayground upload guide names upload + LFI as an execution path when the upload directory itself won't execute. [[raw/payloadplayground.com/file-upload-vulnerability-testing-bypassing-filters-and-getting-rce-3487|File Upload Vulnerability Testing: Bypassing Filters and Getting RCE]] - A file already on the box that acts as a gadget: PEAR's `pearcmd.php` when `register_argc_argv` is On (default in the official `php` Docker image and in default cPanel with PHP before 8.5). Used against WordPress core and in HTB Pterodactyl. [[raw/github.com/wordpress-unauthenticated-path-traversal-leading-to-conditional-rce-12075|WordPress: Unauthenticated path traversal leading to conditional RCE]] [[raw/0xdf.gitlab.io/0xdf-hacks-stuff-ctf-solutions-malware-analysis-home-lab-development-115|0xdf hacks stuff]] - Session or other server-written files: Wiz observed PHP2Shell-branded tools that actually relied on session poisoning plus a separate LFI. [[raw/wiz.io/php2shell-exploitation-attempts-and-remediation-trends-12404|PHP2Shell: Exploitation Attempts and Remediation Trends]] - **Second-order directory.** In Discuz! X5.0 the include filename was regex-checked, but the plugin *directory* came from importable plugin data stored in the DB, so the check passed while the resolved path pointed at the attachments directory. - **Path from the URL itself.** WP Ghost rebuilt a filesystem path from the URL-decoded request URI in its 404 handler and passed it to `include`, reachable unauthenticated. [[raw/patchstack.com/critical-lfi-to-rce-in-wp-ghost-plugin-affecting-200k-sites-3488|Critical LFI to RCE in WP Ghost Plugin Affecting 200k+ Sites]] - **Non-PHP "LFI".** In Python/Flask apps the term is used for arbitrary file read through a file parameter; the impact comes from what is read (app secrets, source), e.g. leaking a `SECRET` env var to forge an admin cookie, then [[Jinja2 SSTI]]. [[raw/n0xshell.dev/poet-writeup-lfi-to-forged-cookie-to-jinja2-ssti-rce-15665|Poet writeup: LFI to forged cookie to Jinja2 SSTI RCE]] A URL fetcher accepting `file://` gives the same read; see [[Local File Read via SSRF]]. [[raw/cloudsek.com/how-an-unauthenticated-mcp-server-led-to-ssrf-lfi-and-aws-credential-t-8426|How an Unauthenticated MCP Server Led to SSRF, LFI, and AWS Credential Theft - CloudSEK]] - **Strip-once filters fail.** The TryHackMe "Include" room needed a non-recursive-filter bypass, i.e. the app removed traversal sequences once instead of rejecting them. [[raw/medium.com/tryhackme-include-bopla-ssrf-lfi-and-ssh-13100|TryHackMe Include: BOPLA, SSRF, LFI and SSH]] ## What to check - [ ] Grep for `include`, `require`, `include_once`, `require_once` and template/view loaders whose argument contains any variable; trace each variable to a source (request, URI, DB, import data, config). - [ ] Treat DB- and import-sourced path components as untrusted (second-order). - [ ] Is the target matched against a fixed allowlist, or only filtered? Strip/replace filters are a finding by themselves. - [ ] Does the 404/fallback or rewrite layer map URLs to files? - [ ] Which writable locations can the web user reach (uploads, attachments, sessions, logs)? Are any includable? - [ ] Is `pearcmd.php` present and `register_argc_argv` On? Is the app on the official `php` Docker image? - [ ] For theme/template resolution (WordPress), do theme directories create unexpected match points (the core bug needed a top-level theme directory starting with `page-`)? - [ ] For read-only LFI, what secrets would hand over the next step (signing keys, env, source)? ## Notable cases - WordPress core 4.7–7.1.1 — `get_page_template()` includes a chosen local `.php` outside theme dirs, unauthenticated, conditional RCE; fixed in 7.1.2 and backported. [[raw/github.com/wordpress-unauthenticated-path-traversal-leading-to-conditional-rce-12075|WordPress path traversal]] - WP Ghost — CVE-2025-26909 — unauthenticated LFI to RCE, CVSS 9.6, 200k+ sites, fixed in 5.4.02. [[raw/patchstack.com/critical-lfi-to-rce-in-wp-ghost-plugin-affecting-200k-sites-3488|WP Ghost LFI]] - Discuz! X5.0 (and X3.5) — KIS-2026-11 — admin plugin LFI, final link of a pre-auth RCE chain after a race-condition auth bypass. [[raw/karmainsecurity.com/chaining-security-bugs-in-discuz-x5-0-from-race-condition-to-pre-auth-9623|Discuz! X5.0 chain]] - Pterodactyl panel (HTB) — unauthenticated locale-endpoint traversal makes PHP include files, chained with pearcmd. [[raw/0xdf.gitlab.io/0xdf-hacks-stuff-ctf-solutions-malware-analysis-home-lab-development-115|0xdf hacks stuff]] - Unauthenticated MCP server — media fetch tool accepted `file://`, read the process environment, then AWS creds via IMDS. [[raw/cloudsek.com/how-an-unauthenticated-mcp-server-led-to-ssrf-lfi-and-aws-credential-t-8426|CloudSEK MCP Server]] - YesWeHack snippet series includes LFI via `include()` and LFI-to-RCE via pearcmd as review exercises. [[raw/github.com/yeswehack-vulnerable-code-snippets-twitter-vulnerable-snippets-1498|GitHub - yeswehack/vulnerable-code-snippets]] ## Defenses - **Allowlist, don't filter.** Map a small set of keys to fixed files; never place request data in an include path. ```php $pages = ['home' => 'home.php', 'about' => 'about.php']; $key = $_GET['page'] ?? 'home'; if (!isset($pages[$key])) { http_response_code(404); exit; } include __DIR__ . '/pages/' . $pages[$key]; ``` - **If a dynamic path is unavoidable**, canonicalize and verify it sits under the intended directory, and validate every component including DB/import-derived directories (see [[Path Traversal#Prevention]]). - **Remove gadgets:** drop PEAR from production images, keep `register_argc_argv` Off. - **Keep code and data apart:** store uploads and attachments outside any includable tree, randomize names, and don't let uploads land in executable or includable paths ([[Unrestricted File Upload]]). - **Restrict fetchers to `http(s)`** so URL tools can't become file readers ([[Local File Read via SSRF]], [[MCP Security]]). - **Patch the platform:** WordPress 7.1.2 (and backports), WP Ghost 5.4.02. ## Related - [[Path Traversal]] (parent guide) · [[Zip Slip and Archive Extraction]] · [[Unrestricted File Upload]] · [[Web Shells]] · [[Remote Code Execution]] · [[Local File Read via SSRF]] · [[Server-Side Template Injection]] · [[Jinja2 SSTI]] · [[Race Conditions]] · [[Vulnerability Chaining]] ## Sources - [[raw/karmainsecurity.com/chaining-security-bugs-in-discuz-x5-0-from-race-condition-to-pre-auth-9623|Chaining Security Bugs in Discuz! X5.0: from Race Condition to Pre-Auth RCE]] - [[raw/payloadplayground.com/file-upload-vulnerability-testing-bypassing-filters-and-getting-rce-3487|File Upload Vulnerability Testing: Bypassing Filters and Getting RCE]] - [[raw/github.com/wordpress-unauthenticated-path-traversal-leading-to-conditional-rce-12075|WordPress: Unauthenticated path traversal leading to conditional RCE]] - [[raw/0xdf.gitlab.io/0xdf-hacks-stuff-ctf-solutions-malware-analysis-home-lab-development-115|0xdf hacks stuff]] - [[raw/wiz.io/php2shell-exploitation-attempts-and-remediation-trends-12404|PHP2Shell: Exploitation Attempts and Remediation Trends]] - [[raw/patchstack.com/critical-lfi-to-rce-in-wp-ghost-plugin-affecting-200k-sites-3488|Critical LFI to RCE in WP Ghost Plugin Affecting 200k+ Sites]] - [[raw/n0xshell.dev/poet-writeup-lfi-to-forged-cookie-to-jinja2-ssti-rce-15665|Poet writeup: LFI to forged cookie to Jinja2 SSTI RCE]] - [[raw/cloudsek.com/how-an-unauthenticated-mcp-server-led-to-ssrf-lfi-and-aws-credential-t-8426|How an Unauthenticated MCP Server Led to SSRF, LFI, and AWS Credential Theft - CloudSEK]] - [[raw/medium.com/tryhackme-include-bopla-ssrf-lfi-and-ssh-13100|TryHackMe Include: BOPLA, SSRF, LFI and SSH]] - [[raw/github.com/yeswehack-vulnerable-code-snippets-twitter-vulnerable-snippets-1498|GitHub - yeswehack/vulnerable-code-snippets: Twitter vulnerable snippets]]