155
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 specify restrictions on file operations. This may not be a feasible solution, and it
only limits the impact to the operating system; the rest of the application may still be subject to
compromise. Be careful to avoid CWE-243 and other weaknesses related to jails.
Effectiveness = Limited
The effectiveness of this mitigation depends on the prevention capabilities of the specific
sandbox or jail being used and might only help to reduce the scope of an attack, such as
restricting the attacker to certain system calls or limiting the portion of the file system that can be
accessed.
Phase: Architecture and Design
Strategy = Attack Surface Reduction
For any data that will be used to generate a command to be executed, keep as much of that data
out of external control as possible. For example, in web applications, this may require storing the
data locally in the session's state instead of sending it out to the client in a hidden form field.
Phase: 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.
Phase: Architecture and Design
Strategy = Libraries or Frameworks
Use a vetted library or framework that does not allow this weakness to occur or provides
constructs that make this weakness easier to avoid. For example, consider using the
ESAPI Encoding control [REF-45] or a similar tool, library, or framework. These will help the
programmer encode outputs in a manner less prone to error.
Phase: Implementation
Strategy = Output Encoding
While it is risky to use dynamically-generated query strings, code, or commands that mix control
and data together, sometimes it may be unavoidable. Properly quote arguments and escape
any special characters within those arguments. The most conservative approach is to escape or
filter all characters that do not pass an extremely strict allowlist (such as everything that is not
alphanumeric or white space). If some special characters are still needed, such as white space,
wrap each argument in quotes after the escaping/filtering step. Be careful of argument injection
(CWE-88).
Phase: Implementation
If the program to be executed allows arguments to be specified within an input file or from
standard input, then consider using that mode to pass arguments instead of the command line.
Phase: Architecture and Design
Strategy = Parameterization
If available, use structured mechanisms that automatically enforce the separation between
data and code. These mechanisms may be able to provide the relevant quoting, encoding,
and validation automatically, instead of relying on the developer to provide this capability at
every point where output is generated. Some languages offer multiple functions that can be
used to invoke commands. Where possible, identify any function that invokes a command shell
using a single string, and replace it with a function that requires individual arguments. These
CWE Version 4.14
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command
Injection')
CWE-78: Improper Neutralization of Special Elements
used in an OS Command ('OS Command Injection') 156
functions typically perform appropriate quoting and filtering of arguments. For example, in C, the
system() function accepts a string that contains the entire command to be executed, whereas
execl(), execve(), and others require an array of strings, one for each argument. In Windows,
CreateProcess() only accepts one command at a time. In Perl, if system() is provided with an
array of arguments, then it will quote each of the arguments.
Phase: Implementation
Strategy = Input Validation
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 related
fields, and conformance to business rules. As an example of business rule logic, "boat" may be
syntactically valid because it only contains alphanumeric characters, but it is not valid if the input
is only expected to contain colors such as "red" or "blue." Do not rely exclusively on looking for
malicious or malformed inputs. This is likely to miss at least one undesirable input, especially if
the code's environment changes. This can give attackers enough room to bypass the intended
validation. However, denylists can be useful for detecting potential attacks or determining
which inputs are so malformed that they should be rejected outright. When constructing OS
command strings, use stringent allowlists that limit the character set based on the expected
value of the parameter in the request. This will indirectly limit the scope of an attack, but this
technique is less important than proper output encoding and escaping. Note that proper output
encoding, escaping, and quoting is the most effective solution for preventing OS command
injection, although input validation may provide some defense-in-depth. This is because it
effectively limits what will appear in output. Input validation will not always prevent OS command
injection, especially if you are required to support free-form text fields that could contain arbitrary
characters. For example, when invoking a mail program, you might need to allow the subject
field to contain otherwise-dangerous inputs like ";" and ">" characters, which would need to
be escaped or otherwise handled. In this case, stripping the character might reduce the risk
of OS command injection, but it would produce incorrect behavior because the subject field
would not be recorded as the user intended. This might seem to be a minor inconvenience,
but it could be more important when the program relies on well-structured subject lines in order
to pass messages to other components. Even if you make a mistake in your validation (such
as forgetting one out of 100 input fields), appropriate encoding is still likely to protect you from
injection-based attacks. As long as it is not done in isolation, input validation is still a useful
technique, since it may significantly reduce your attack surface, allow you to detect some attacks,
and provide other security benefits that proper encoding does not address.
Phase: Architecture and Design
Strategy = Enforcement by Conversion
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.
Phase: Operation
Strategy = Compilation or Build Hardening
Run the code in an environment that performs automatic taint propagation and prevents any
command execution that uses tainted variables, such as Perl's "-T" switch. This will force the
program to perform validation steps that remove the taint, although you must be careful to
correctly validate your inputs so that you do not accidentally mark dangerous inputs as untainted
(see CWE-183 and CWE-184).
Phase: Operation
CWE Version 4.14
CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command
Injection')
CWE-78: Improper Neutralization of Special Elements
used in an OS Command ('OS Command Injection')