Web security

File inclusion (LFI / RFI)

Definition

File inclusion is a PHP vulnerability in which an attacker controls the path passed to include or require. With LFI (local file inclusion), they load a file already on the server; with RFI (remote file inclusion), a file hosted elsewhere. It leads to leaked secrets or code execution.

File inclusion (LFI / RFI), Web security

In PHP, the include and require statements (and their _once variants) load a file and run the code it contains. They are used everywhere, for example to load a theme template. The danger arises when the file path depends on data sent by the visitor without sufficient validation. MITRE's catalog lists this weakness as CWE-98, with two common names: RFI for remote inclusion, LFI for local inclusion.

How the attack works

Code like this lets the visitor choose which file gets loaded:

// Don't do this
include( __DIR__ . '/templates/' . $_GET['view'] . '.php' );
  • LFI (Local File Inclusion): with ../ sequences, the attacker climbs up the directory tree and gets a file that already exists on the server loaded. At best, they read a file they shouldn't see; if they manage to include a file whose content they control (a log, an uploaded file), they get remote code execution.
  • RFI (Remote File Inclusion): the attacker supplies a URL pointing to their own server, and PHP includes their code directly. It is the most direct form, but it requires a setting that is rarely enabled.
Three-column comparison: LFI (including a file from the server, reading or executing wp-config.php through ../ paths, php://filter and phar:// wrappers, works with the default configuration), RFI (including an http or ftp URL containing the attacker's code, requires allow_url_include to be enabled, a setting off by default and deprecated in PHP 7.4) and defenses (fixed list of allowed files, never a path taken from the request, open_basedir and allow_url_include off, updates)

The role of PHP configuration

The PHP documentation is clear about two directives:

  • allow_url_include allows URLs in include and require. It defaults to 0, can only be changed in the system configuration (php.ini) and has been deprecated since PHP 7.4.0. It also requires allow_url_fopen to be enabled.
  • allow_url_fopen lets PHP open URLs as if they were files; it is enabled by default, because many scripts need it.

Disabling RFI doesn't solve everything: PHP wrappers such as php://filter can turn some LFI flaws into code execution on a default configuration, and phar:// was long used to trigger insecure deserialization. Our article on the php://filter and phar wrappers covers these techniques in detail.

How to protect against it

The best defense, recommended by MITRE, is to never build a path from received data, but to map fixed values to known files:

$views = array( 'list' => 'list.php', 'grid' => 'grid.php' );
$view  = isset( $views[ $_GET['view'] ?? '' ] ) ? $views[ $_GET['view'] ] : 'list.php';
include __DIR__ . '/templates/' . $view;
  • Keep included files outside the web root when possible.
  • Limit what PHP can read with open_basedir: a hijacked include won't be able to leave the allowed directories. Hardening PHP on your hosting explains this setting and its effects.
  • A web application firewall blocks typical patterns (../, php://), as an extra layer only.

For WordPress

WordPress core doesn't build its includes from visitor parameters: the risk comes from plugins and themes that load a template, a language file or a module based on a URL or AJAX parameter. The favorite LFI target is wp-config.php, which holds the database credentials and the security keys; a sensitive file forgotten in the web root, such as a .env file or a backup, is another easy catch, as our article on exposed sensitive files shows. Finally, check that your host keeps allow_url_include disabled.

Further reading

  • Article PHP file inclusion: LFI, RFI and the phar and php://filter wrappers

Sources

  1. MITRE CWE-98: PHP Remote File Inclusion (external link, opens in a new tab)
  2. PHP Manual: Filesystem and Streams Configuration Options (external link, opens in a new tab)
  3. PHP Manual: Backward Incompatible Changes in PHP 8.0 (Phar) (external link, opens in a new tab)

All definitions

Start free, go Pro when you’re ready

The free version already hides WordPress and moves the login page. Pro adds aliases, two-factor authentication and monitoring.