Many protocols and products have their own custom command language. While OS or shell command strings are frequently discovered and targeted, developers may not realize that these other command languages might also be vulnerable to attacks.
| Impact | Details |
|---|---|
|
Execute Unauthorized Code or Commands |
Scope: Integrity, Confidentiality, Availability
If a malicious user injects a character (such as a semi-colon) that delimits the end of one command and the beginning of another, it may be possible to then insert an entirely new and unrelated command that was not intended to be executed. This gives an attacker a privilege or capability that they would not otherwise have.
|
| Phase(s) | Mitigation |
|---|---|
|
Architecture and Design |
If at all possible, use library calls rather than external processes to recreate the desired functionality.
|
|
Implementation |
If possible, ensure that all external commands called from the program are statically created.
|
|
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. |
|
Operation |
Run time: Run time policy enforcement may be used in an allowlist fashion to prevent use of any non-sanctioned commands.
|
|
System Configuration |
Assign permissions that prevent the user from accessing/opening privileged files.
|
| Nature | Type | ID | Name |
|---|---|---|---|
| ChildOf | Class Class - a weakness that is described in a very abstract fashion, typically independent of any specific language or technology. More specific than a Pillar Weakness, but more general than a Base Weakness. Class level weaknesses typically describe issues in terms of 1 or 2 of the following dimensions: behavior, property, and resource. | 74 | Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 78 | Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 88 | Improper Neutralization of Argument Delimiters in a Command ('Argument Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 624 | Executable Regular Expression Error |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 917 | Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 1427 | Improper Neutralization of Input Used for LLM Prompting |
| Nature | Type | ID | Name |
|---|---|---|---|
| ChildOf | Class Class - a weakness that is described in a very abstract fashion, typically independent of any specific language or technology. More specific than a Pillar Weakness, but more general than a Base Weakness. Class level weaknesses typically describe issues in terms of 1 or 2 of the following dimensions: behavior, property, and resource. | 74 | Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') |
| Nature | Type | ID | Name |
|---|---|---|---|
| MemberOf | Category Category - a CWE entry that contains a set of other entries that share a common characteristic. | 1019 | Validate Inputs |
| Nature | Type | ID | Name |
|---|---|---|---|
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 78 | Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 88 | Improper Neutralization of Argument Delimiters in a Command ('Argument Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 624 | Executable Regular Expression Error |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 917 | Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection') |
| Nature | Type | ID | Name |
|---|---|---|---|
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 78 | Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 88 | Improper Neutralization of Argument Delimiters in a Command ('Argument Injection') |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 624 | Executable Regular Expression Error |
| ParentOf | Base Base - a weakness that is still mostly independent of a resource or technology, but with sufficient details to provide specific methods for detection and prevention. Base level weaknesses typically describe issues in terms of 2 or 3 of the following dimensions: behavior, property, technology, language, and resource. | 917 | Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection') |
| Phase | Note |
|---|---|
| Implementation | Command injection vulnerabilities typically occur when:
|
| Implementation | REALIZATION: This weakness is caused during implementation of an architectural security tactic. |
Class: Not Language-Specific (Undetermined Prevalence)
AI/ML (Undetermined Prevalence)
Example 1
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:
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.
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:
Example 3
The following simple program accepts a filename as a command line argument and displays the contents of the file back to the user. The program is installed setuid root because it is intended for use as a learning tool to allow system administrators in-training to inspect privileged system files without giving them the ability to modify them or damage the system.
Because the program runs with root privileges, the call to system() also executes with root privileges. If a user specifies a standard filename, the call works as expected. However, if an attacker passes a string of the form ";rm -rf /", then the call to system() fails to execute cat due to a lack of arguments and then plows on to recursively delete the contents of the root partition, leading to OS command injection (CWE-78).
Note that if argv[1] is a very long argument, then this issue might also be subject to a buffer overflow (CWE-120).
Example 4
The following code is from an administrative web application designed to allow users to kick off a backup of an Oracle database using a batch-file wrapper around the rman utility and then run a cleanup.bat script to delete some temporary files. The script rmanDB.bat accepts a single command line parameter, which specifies what type of backup to perform. Because access to the database is restricted, the application runs the backup as a privileged user.
The problem here is that the program does not do any validation on the backuptype parameter read from the user. Typically the Runtime.exec() function will not execute multiple commands, but in this case the program first runs the cmd.exe shell in order to run multiple commands with a single call to Runtime.exec(). Once the shell is invoked, it will happily execute multiple commands separated by two ampersands. If an attacker passes a string of the form "& del c:\\dbms\\*.*", then the application will execute this command along with the others specified by the program. Because of the nature of the application, it runs with the privileges necessary to interact with the database, which means whatever command the attacker injects will run with those privileges as well.
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.
| Reference | Description |
|---|---|
|
injection of sed script syntax ("sed injection")
|
|
|
API service using a large generative AI model allows direct prompt injection to leak hard-coded system prompts or execute other prompts.
|
|
|
anti-spam product allows injection of SNMP commands into confiuration file
|
|
|
image program allows injection of commands in "Magick Vector Graphics (MVG)" language.
|
|
|
Python-based dependency management tool avoids OS command injection when generating Git commands but allows injection of optional arguments with input beginning with a dash (CWE-88), potentially allowing for code execution.
|
|
|
Canonical example of OS command injection. CGI program does not neutralize "|" metacharacter when invoking a phonebook program.
|
|
|
injection of sed script syntax ("sed injection")
|
|
|
injection of sed script syntax ("sed injection")
|
| Ordinality | Description |
|---|---|
|
Primary
|
(where the weakness exists independent of other weaknesses)
|
| Method | Details |
|---|---|
|
Automated Static Analysis |
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Effectiveness: High |
| Nature | Type | ID | Name |
|---|---|---|---|
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 713 | OWASP Top Ten 2007 Category A2 - Injection Flaws |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 722 | OWASP Top Ten 2004 Category A1 - Unvalidated Input |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 727 | OWASP Top Ten 2004 Category A6 - Injection Flaws |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 929 | OWASP Top Ten 2013 Category A1 - Injection |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 990 | SFP Secondary Cluster: Tainted Input to Command |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1005 | 7PK - Input Validation and Representation |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1027 | OWASP Top Ten 2017 Category A1 - Injection |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1179 | SEI CERT Perl Coding Standard - Guidelines 01. Input Validation and Data Sanitization (IDS) |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1308 | CISQ Quality Measures - Security |
| MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 1337 | Weaknesses in the 2021 CWE Top 25 Most Dangerous Software Weaknesses |
| MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 1340 | CISQ Data Protection Measures |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1347 | OWASP Top Ten 2021 Category A03:2021 - Injection |
| MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 1387 | Weaknesses in the 2022 CWE Top 25 Most Dangerous Software Weaknesses |
| MemberOf | CategoryCategory - a CWE entry that contains a set of other entries that share a common characteristic. | 1409 | Comprehensive Categorization: Injection |
| MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 1425 | Weaknesses in the 2023 CWE Top 25 Most Dangerous Software Weaknesses |
| MemberOf | ViewView - a subset of CWE entries that provides a way of examining CWE content. The two main view structures are Slices (flat lists) and Graphs (containing relationships between entries). | 1430 | Weaknesses in the 2024 CWE Top 25 Most Dangerous Software Weaknesses |
| Usage |
ALLOWED-WITH-REVIEW
(this CWE ID could be used to map to real-world vulnerabilities in limited situations requiring careful review)
|
||||
| Reason | Frequent Misuse | ||||
|
Rationale |
CWE-77 is often misused when OS command injection (CWE-78) was intended instead [REF-1287]. | ||||
|
Comments |
Ensure that the analysis focuses on the root-cause error that allows the execution of commands, as there are many weaknesses that can lead to this consequence. See Terminology Notes. If the weakness involves a command language besides OS shell invocation, then CWE-77 could be used. | ||||
|
Suggestions |
|
Terminology
The "command injection" phrase carries different meanings, either as an attack or as a technical impact. The most common usage of "command injection" refers to the more-accurate OS command injection (CWE-78), but there are many command languages.
In vulnerability-focused analysis, the phrase may refer to any situation in which the adversary can execute commands of their own choosing, i.e., the focus is on the risk and/or technical impact of exploitation. Many proof-of-concept exploits focus on the ability to execute commands and may emphasize "command injection." However, there are dozens of weaknesses that can allow execution of commands. That is, the ability to execute commands could be resultant from another weakness.
To some, "command injection" can include cases in which the functionality intentionally allows the user to specify an entire command, which is then executed. In this case, the root cause weakness might be related to missing or incorrect authorization, since an adversary should not be able to specify arbitrary commands, but some users or admins are allowed.
CWE-77 and its descendants are specifically focused on behaviors in which the product is intentionally building a command to execute, and the adversary can inject separators into the command or otherwise change the command being executed.
Other
Command injection is a common problem with wrapper programs.
| Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
|---|---|---|---|
| 7 Pernicious Kingdoms | Command Injection | ||
| CLASP | Command injection | ||
| OWASP Top Ten 2007 | A2 | CWE More Specific | Injection Flaws |
| OWASP Top Ten 2004 | A1 | CWE More Specific | Unvalidated Input |
| OWASP Top Ten 2004 | A6 | CWE More Specific | Injection Flaws |
| Software Fault Patterns | SFP24 | Tainted input to command | |
| SEI CERT Perl Coding Standard | IDS34-PL | CWE More Specific | Do not pass untrusted, unsanitized data to a command interpreter |
| CAPEC-ID | Attack Pattern Name |
|---|---|
| CAPEC-136 | LDAP Injection |
| CAPEC-15 | Command Delimiters |
| CAPEC-183 | IMAP/SMTP Command Injection |
| CAPEC-248 | Command Injection |
| CAPEC-40 | Manipulating Writeable Terminal Devices |
| CAPEC-43 | Exploiting Multiple Input Interpretation Layers |
| CAPEC-75 | Manipulating Writeable Configuration Files |
| CAPEC-76 | Manipulating Web Input to File System Calls |
| Submissions | |||
|---|---|---|---|
| Submission Date | Submitter | Organization | |
|
2006年07月19日
(CWE Draft 3, 2006年07月19日) |
7 Pernicious Kingdoms | ||
| Contributions | |||
| Contribution Date | Contributor | Organization | |
| 2022年05月20日 | Anonymous External Contributor | ||
| reported typo in Terminology note | |||
|
2024年02月29日
(CWE 4.15, 2024年07月16日) |
Abhi Balakrishnan | ||
| Provided diagram to improve CWE usability | |||
|
2024年07月01日
(CWE 4.15, 2024年07月16日) |
Eldar Marcussen | ||
| Suggested that CWE-77 should include more examples than CWE-78. | |||
| Modifications | |||
| Modification Date | Modifier | Organization | |
|
2024年11月19日
(CWE 4.16, 2024年11月19日) |
CWE Content Team | MITRE | |
| updated Demonstrative_Examples, Relationships | |||
|
2024年07月16日
(CWE 4.15, 2024年07月16日) |
CWE Content Team | MITRE | |
| updated Alternate_Terms, Applicable_Platforms, Common_Consequences, Demonstrative_Examples, Description, Diagram, Mapping_Notes, Modes_of_Introduction, Observed_Examples, Other_Notes, Terminology_Notes | |||
| 2023年06月29日 | CWE Content Team | MITRE | |
| updated Mapping_Notes, Relationships | |||
| 2023年04月27日 | CWE Content Team | MITRE | |
| updated Detection_Factors, Relationships, Time_of_Introduction | |||
| 2023年01月31日 | CWE Content Team | MITRE | |
| updated Description, Potential_Mitigations | |||
| 2022年10月13日 | CWE Content Team | MITRE | |
| updated Observed_Examples, References, Terminology_Notes | |||
| 2022年06月28日 | CWE Content Team | MITRE | |
| updated Observed_Examples, Relationships | |||
| 2021年10月28日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2021年07月20日 | CWE Content Team | MITRE | |
| updated Description, Observed_Examples, Relationships | |||
| 2021年03月15日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2020年12月10日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2020年08月20日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2020年06月25日 | CWE Content Team | MITRE | |
| updated Potential_Mitigations | |||
| 2020年02月24日 | CWE Content Team | MITRE | |
| updated Potential_Mitigations, References, Relationships | |||
| 2019年06月20日 | CWE Content Team | MITRE | |
| updated Related_Attack_Patterns, Relationships | |||
| 2019年01月03日 | CWE Content Team | MITRE | |
| updated Taxonomy_Mappings | |||
| 2018年03月27日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2017年11月08日 | CWE Content Team | MITRE | |
| updated Causal_Nature, Likelihood_of_Exploit, Modes_of_Introduction, References, Relationships, Taxonomy_Mappings | |||
| 2017年05月03日 | CWE Content Team | MITRE | |
| updated Potential_Mitigations, Related_Attack_Patterns, Relationships | |||
| 2015年12月07日 | CWE Content Team | MITRE | |
| updated Demonstrative_Examples, Relationships | |||
| 2014年07月30日 | CWE Content Team | MITRE | |
| updated Relationships, Taxonomy_Mappings | |||
| 2014年06月23日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2014年02月18日 | CWE Content Team | MITRE | |
| updated Applicable_Platforms, Demonstrative_Examples, Description, Other_Notes, Terminology_Notes | |||
| 2013年07月17日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2013年02月21日 | CWE Content Team | MITRE | |
| updated Relationships | |||
| 2012年10月30日 | CWE Content Team | MITRE | |
| updated Potential_Mitigations | |||
| 2012年05月11日 | CWE Content Team | MITRE | |
| updated Common_Consequences, Demonstrative_Examples, References, Related_Attack_Patterns, Relationships | |||
| 2011年06月01日 | CWE Content Team | MITRE | |
| updated Common_Consequences | |||
| 2011年03月29日 | CWE Content Team | MITRE | |
| updated Demonstrative_Examples | |||
| 2010年06月21日 | CWE Content Team | MITRE | |
| updated Description, Name | |||
| 2010年02月16日 | CWE Content Team | MITRE | |
| updated Potential_Mitigations, Relationships | |||
| 2009年10月29日 | CWE Content Team | MITRE | |
| updated Common_Consequences, Description, Other_Notes, Potential_Mitigations | |||
| 2009年07月27日 | CWE Content Team | MITRE | |
| updated Demonstrative_Examples, Description, Name | |||
| 2009年05月27日 | CWE Content Team | MITRE | |
| updated Demonstrative_Examples, Name | |||
| 2008年09月08日 | CWE Content Team | MITRE | |
| updated Common_Consequences, Relationships, Other_Notes, Taxonomy_Mappings, Weakness_Ordinalities | |||
| 2008年08月15日 | Veracode | ||
| Suggested OWASP Top Ten 2004 mapping | |||
| 2008年07月01日 | Eric Dalci | Cigital | |
| updated Time_of_Introduction | |||
| Previous Entry Names | |||
| Change Date | Previous Entry Name | ||
| 2008年04月11日 | Command Injection | ||
| 2009年05月27日 | Failure to Sanitize Data into a Control Plane (aka 'Command Injection') | ||
| 2009年07月27日 | Failure to Sanitize Data into a Control Plane ('Command Injection') | ||
| 2010年06月21日 | Improper Sanitization of Special Elements used in a Command ('Command Injection') | ||
Use of the Common Weakness Enumeration (CWE™) and the associated references from this website are subject to the Terms of Use. CWE is sponsored by the U.S. Department of Homeland Security (DHS) Cybersecurity and Infrastructure Security Agency (CISA) and managed by the Homeland Security Systems Engineering and Development Institute (HSSEDI) which is operated by The MITRE Corporation (MITRE). Copyright © 2006–2025, The MITRE Corporation. CWE, CWSS, CWRAF, and the CWE logo are trademarks of The MITRE Corporation.