File Inclusion
- CAPEC 252
- PCI 3.2-6.5.8
- WASC 33
- OWASP 2013-A4
File Inclusion vulnerabilities occur when an application constructs file paths or references to resources using untrusted input, and then includes, loads, or executes those files on the server. The two common flavors are Local File Inclusion (LFI) — where attackers cause the application to read or include files present on the local filesystem — and Remote File Inclusion (RFI) — where an attacker forces the application to fetch and execute a resource from an attacker-controlled remote server. Both can lead to sensitive information disclosure, arbitrary code execution, or full system compromise depending on the application logic, underlying platform, and available protections.
Typical vulnerable patterns:
- Dynamically building file paths from request parameters and passing them to include/require functions (e.g.,
include($_GET['page'])in PHP, orFile.ReadAllText(basePath + filename)patterns in other platforms). - Trusting user-supplied URLs or file identifiers to load templates, plugins, or modules without validation.
- Allowing file uploads without strict controls and then including or executing uploaded content.
- Using insufficiently restricted deserialization or plugin loading mechanisms that accept external inputs as filenames or module specifiers.
Common attacker goals and impacts:
- Information disclosure: reading sensitive files such as
/etc/passwd, configuration files, source code, or credentials. - Remote code execution: with RFI or by uploading a web shell and including it, attackers can execute arbitrary code under the web server’s privileges.
- Local file reading chain: LFI can be exploited to read logs, session stores, backup files, or to include files that contain attacker-controlled content (e.g., logs containing injected payloads).
- Privilege escalation and lateral movement: once code executes on the server, attackers may move laterally or escalate privileges.
- Persistent backdoors: uploading and including malicious scripts creates persistent access.
Why it happens:
- Direct concatenation of user input into file path construction without normalization or validation.
- Failure to restrict allowed files (no whitelisting), and relying on blacklists that can be bypassed with path encoding tricks (
../,%2e%2e/,..%5c, null bytes in legacy contexts). - Excessive functionality (including arbitrary templates/plugins) exposed to untrusted users.
- Misconfiguration that allows remote file fetching via include APIs or that permits unsafe upload locations.
Detection and indicators:
- Code review identifying includes/loads that use request parameters.
- Automated scanning that attempts path traversal strings, URL-based inclusion, or upload-and-include patterns.
- Application logs showing abnormal file read requests, 4xx/5xx errors following crafted inputs, or outbound HTTP requests to attacker-controlled domains (indicative of RFI).
- Unexpected file contents in rendered pages or responses that include raw file data.
Remediation
Preventing File Inclusion requires strict control over what files an application may access or execute, robust input validation, and secure platform configuration. Recommended mitigations:
Whitelist Allowed Files/Resources
Never use user input directly to reference files. Map user-facing identifiers to a controlled list of allowed files or templates (for example, an internal lookup table keyed by short names or numeric IDs). Whitelisting is the most reliable approach.Avoid Direct Inclusion from User Input
Do not pass user-supplied paths or URLs directly to include/require, file-load, or module import APIs. Load only files from trusted locations and never execute or include files originating from user-controllable locations.Canonicalize and Normalize Paths
Normalize and canonicalize file paths using platform APIs before any checking. Resolve symbolic links and remove./..sequences so that checks operate on the canonical path.Validate and Restrict File Names
If dynamic file names are necessary (e.g., user-selectable templates), validate against strict patterns (e.g., alphanumeric, no punctuation) and enforce maximum length. Prefer tokens/IDs mapped server-side to actual filenames.Disable Remote Inclusion and URL Wrappers
Configure interpreters and libraries to disallow loading remote resources via include APIs (e.g., disableallow_url_includein PHP) and block remote file retrieval where possible.Harden Upload Handling
Store uploaded files outside the web root, validate file types and contents, generate safe server-side filenames, and never include uploaded files as executable code. If uploads must be served, use safe download endpoints with proper content-type and content-disposition headers.Principle of Least Privilege
Ensure the web application runs with minimal filesystem privileges. The process should only have read/access rights to directories and files it legitimately needs and no write/execute permissions for code directories.Disable Dangerous Interpreter Features
Turn off or restrict scripting features that permit dynamic evaluation of remote content. Patch or configure interpreters to prevent inclusion of remote URLs or dynamic evaluation where feasible.Logging and Monitoring
Log file access attempts, unexpected include errors, and outbound requests. Alert on repeated path traversal attempts, suspicious patterns with encoded traversal sequences, and unexpected connections to external hosts (indicative of RFI).Defense-in-Depth
Use web server and application firewalls to detect and block known payloads (path traversal, suspicious URL patterns). Combine with runtime application self-protection (RASP) if available.Security Testing
Include File Inclusion scenarios in security testing and code review. Test for local and remote inclusion, traversal with different encodings, upload-and-include chains, and attempts to include special files (logs, config, sockets).Secure Defaults and Platform Updates
Keep the runtime, frameworks, and libraries patched. Use secure default configurations and minimize enabled features to only those required by the application.