Local File Inclusion

User input selects the file that the server-side language includes and runs, so an attacker can include unintended local files, disclosing them or running code they contain.

SeverityHigh
CVSS 3.18.1 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H Open in the calculator
CVSS 4.09.2 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N Open in the calculator
CWECWE-98: Improper Control of Filename for Include/Require Statement in PHP Program ('PHP Remote File Inclusion')
OWASP Top 10A05:2025 Injection
MITRE ATT&CKT1190 Exploit Public-Facing Application
CategoryInjection

Description

The application passes a value supplied by the user to the language's file inclusion mechanism, most often PHP's include or require statements, to choose a page, template or language file, for example ?page=about. The included file is not simply read: the interpreter runs any code it contains and outputs the rest. Without a strict allowlist, traversal sequences or absolute paths let the input select any local file the application can read. This is what distinguishes local file inclusion from path traversal, where a file is only returned as data.

An attacker can therefore read local files that the application's account can access, including the application's source code and configuration secrets. They can often go on to run arbitrary code on the server with the web server's privileges by getting PHP code into any file the include can reach, such as a log file containing a crafted User-Agent header, a session file or an uploaded file, and in some configurations through chains of PHP filters without writing a file at all. Code execution depends on conditions such as writable log or session files and the form of the vulnerable path, so it is not always achieved, but where it is, the application host is fully compromised.

The weakness typically shows itself on a test when a system file such as /etc/passwd appears in the page, when an error message reveals the include path, or when a PHP stream wrapper such as php://filter with a Base64 conversion returns the encoded source of the application's own scripts. A fixed prefix or suffix added by the code, such as a directory name or .php, limits which files can be reached but rarely removes the issue. Scanners report it as local file inclusion or as path traversal.

Remediation

Keep user input away from the include mechanism:

  • Never pass user input to include, require or equivalent functions. Map each permitted value to a fixed file name in the code, and fall back to a default for anything else.
  • Where file names must be dynamic, validate them against an allowlist of known files rather than filtering traversal sequences.
  • Set open_basedir to limit the directories PHP can open, keep allow_url_include off, and keep uploads, logs and session files outside any directory the application includes from.
  • Run the web server with an account that cannot write to the application's code directories, and restrict read access to log and session files.
// PHP: choose from a fixed map, never from the raw value $pages = ['home' => 'home.php', 'about' => 'about.php']; $file = $pages[$_GET['page'] ?? 'home'] ?? 'home.php'; include __DIR__ . '/pages/' . $file;

Confirm the fix by repeating the original requests with traversal sequences, absolute paths and PHP wrappers, and checking that only the permitted pages can be loaded.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding