74
This example code intends to take the name of a user and list the contents of that user's 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 Path ( CWE-426 ) attacks.
The following code segment reads the name of the author of a weblog entry, author, from an HTTP request and sets it in a cookie header of an HTTP response.
Assuming a string consisting of standard alpha-numeric characters, such as "Jane Smith", is submitted in the request the HTTP response including this cookie might take the following form:
However, because the value of the cookie is composed of unvalidated user input, the response will only maintain this form if the value submitted for AUTHOR_PARAM does not contain any CR and LF characters. If an attacker submits a malicious string, such as
then the HTTP response would be split into two responses of the following form:
The second response is completely controlled by the attacker and can be constructed with any header and body content desired. The ability to construct arbitrary HTTP responses permits a variety of resulting attacks, including:
cross-user defacement web and browser cache poisoning cross-site scripting page hijacking
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.
However, validate_name() allows filenames that begin with a "-". An adversary could supply a filename like "-aR", producing the "ls -l -aR" command ( CWE-88 ), thereby getting a full recursive listing of the entire directory and all of its sub-directories. There are a couple possible mitigations for this weakness. One would be to refactor the code to avoid using system() altogether, instead relying on internal functions. Another option could be to add a "--" argument to the ls command, such as "ls -l --", so that any remaining arguments are treated as filenames, causing any leading "-" to be treated as part of a filename instead of another option. Another fix might be to change the regular expression used in validate_name to force the first character of the filename to be a letter or number, such as:
However, validate_name() allows filenames that begin with a "-". An adversary could supply a filename like "-aR", producing the "ls -l -aR" command ( CWE-88 ), thereby getting a full recursive listing of the entire directory and all of its sub-directories.
There are a couple possible mitigations for this weakness. One would be to refactor the code to avoid using system() altogether, instead relying on internal functions.
Another option could be to add a "--" argument to the ls command, such as "ls -l --", so that any remaining arguments are treated as filenames, causing any leading "-" to be treated as part of a filename instead of another option.
Another fix might be to change the regular expression used in validate_name to force the first character of the filename to be a letter or number, such as:
Consider a "CWE Differentiator" application that uses an an LLM generative AI based "chatbot" to explain the difference between two weaknesses. As input, it accepts two CWE IDs, constructs a prompt string, sends the prompt to the chatbot, and prints the results. The prompt string effectively acts as a command to the chatbot component. Assume that invokeChatbot() calls the chatbot and returns the response as a string; the implementation details are not important here.
To avoid XSS risks, the code ensures that the response from the chatbot is properly encoded for HTML output. If the user provides CWE-77 and CWE-78 , then the resulting prompt would look like:
However, the attacker could provide malformed CWE IDs containing malicious prompts such as:
This would produce a prompt like:
Instead of providing well-formed CWE IDs, the adversary has performed a "prompt injection" attack by adding an additional prompt that was not intended by the developer. The result from the maliciously modified prompt might be something like this:
While the attack in this example is not serious, it shows the risk of unexpected results. Prompts can be constructed to steal private information, invoke unexpected agents, etc.
In this case, it might be easiest to fix the code by validating the input CWE IDs:
The following code is a workflow job written using YAML. The code attempts to download pull request artifacts, unzip from the artifact called pr.zip and extract the value of the file NR into a variable "pr_number" that will be used later in another job. It attempts to create a github workflow environment variable, writing to $GITHUB_ENV. The environment variable value is retrieved from an external resource.
The code does not neutralize the value of the file NR, e.g. by validating that NR only contains a number ( CWE-1284 ). The NR file is attacker controlled because it originates from a pull request that produced pr.zip. The attacker could escape the existing pr_number and create a new variable using a "\n" ( CWE-93 ) followed by any environment variable to be added such as:
The code does not neutralize the value of the file NR, e.g. by validating that NR only contains a number ( CWE-1284 ). The NR file is attacker controlled because it originates from a pull request that produced pr.zip.
The attacker could escape the existing pr_number and create a new variable using a "\n" ( CWE-93 ) followed by any environment variable to be added such as:
This would result in injecting and running javascript code ( CWE-94 ) on the workflow runner with elevated privileges.
This would result in injecting and running javascript code ( CWE-94 ) on the workflow runner with elevated privileges.
Note: this is a curated list of examples for users to understand the variety of ways in which this weakness can be introduced. It is not a complete list of all CVEs that are related to this CWE entry.
Automated Static Analysis
The full story
This article is one source in a clustered incident — the cluster page carries the summary, timeline and every other outlet covering it.
