CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
Description
The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Extended Description
Software or other automated logic has certain assumptions about what constitutes data and control respectively. It is the lack of verification of these assumptions for user-controlled input that leads to injection problems. Injection problems encompass a wide variety of issues -- all mitigated in very different ways and usually attempted in order to alter the control flow of the process. For this reason, the most effective way to discuss these weaknesses is to note the distinct features that classify them as injection weaknesses. The most important issue to note is that all injection problems share one thing in common -- i.e., they allow for the injection of control plane data into the user-controlled data plane. This means that the execution of the process may be altered by sending code in through legitimate data channels, using no other mechanism. While buffer overflows, and many other flaws, involve the use of some further issue to gain execution, injection problems need only for the data to be parsed.
Example - 1
This example code intends to take the name of a user and list the contents of that user's home directory. It is subject to the first variant of OS command injection. The $userName variable is not checked for malicious input. An attacker could set the $userName variable to an arbitrary OS command such as: Which would result in $command being: Since the semi-colon is a command separator in Unix, the OS would first execute the ls command, then the rm command, deleting the entire file system. Also note that this example code is vulnerable to Path Traversal (CWE-22) and Untrusted Search Path (CWE-426) attacks.
$userName = $_POST["user"];$command = 'ls -l /home/' . $userName;system($command);
;rm -rf /
ls -l /home/;rm -rf /
Example - 2
Consider the following program. It intends to perform an "ls -l" on an input filename. The validate_name() subroutine performs validation on the input to make sure that only alphanumeric and "-" characters are allowed, which avoids path traversal (CWE-22) and OS command injection (CWE-78) weaknesses. Only filenames like "abc" or "d-e-f" are intended to be allowed.
print "Error: name is not well-formed!\n";return;
// # build command//
my($fname) = @_;if (! validate_name($fname)) {}my $cmd = "/bin/ls -l $fname";system($cmd);
return(1);
return(0);
my($name) = @_;if ($name =~ /^[\w\-]+$/) {}else {}my $arg = GetArgument("filename");do_listing($arg);sub do_listing {}sub validate_name {}
if ($name =~ /^\w[\w\-]+$/) ...
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-20: Improper Input Validation
CWE-75: Failure to Sanitize Special Elements into a Different Plane (Special Element Injection)
CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection')
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')
CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
CWE-88: Improper Neutralization of Argument Delimiters in a Command ('Argument Injection')
CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
CWE-91: XML Injection (aka Blind XPath Injection)
CWE-93: Improper Neutralization of CRLF Sequences ('CRLF Injection')
CWE-94: Improper Control of Generation of Code ('Code Injection')
CWE-99: Improper Control of Resource Identifiers ('Resource Injection')
CWE-116: Improper Encoding or Escaping of Output
CWE-707: Improper Neutralization
CWE-917: Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection')
CWE-943: Improper Neutralization of Special Elements in Data Query Logic
CWE-1236: Improper Neutralization of Formula Elements in a CSV File
Visit http://cwe.mitre.org/ for more details.