External programs are commonly invoked to perform a function required by the overall system. This practice is a form of reuse and might even be considered a crude form of component-based software engineering. Command and argument injection vulnerabilities occur when an application fails to sanitize untrusted input and uses it in the execution of external programs.
...
Because Runtime.exec()
receives unsanitized data originating from the environment, this code is susceptible to a command injection attack.
An attacker can exploit this program using the following command:
...
which first attempts to list a nonexistent dummy
folder , and then prints bad
to the console.
...
Compliant Solution (Sanitization)
This compliant solution solution sanitizes the untrusted user input by permitting only a small group of whitelisted characters in the argument that will be passed to Runtime.exec()
; all other characters are excluded.
...
Code Block | ||
---|---|---|
| ||
import java.io.File; class DirList { public static void main(String[] args) throws Exception { File dir = new File(System.getProperty("dir")); if (!dir.isDirectory()) { System.out.println("Not a directory"); } else { for (String file : dir.list()) { System.out.println(file); } } } } |
Risk Assessment
Passing untrusted, unsanitized data to the Runtime.exec()
method can result in command and argument injection attacks.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
IDS07-J | High | Probable | Medium | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
Coverity | 7.5 | OS_CMD_INJECTION | Implemented |
Related Vulnerabilities
...
ENV03-C. Sanitize the environment when invoking external programs | |
| |
ENV03-CPP. Sanitize the environment when invoking external programs | |
CERT Perl Secure Coding Standard | IDS34-PL. Do not pass untrusted, unsanitized data to a command interpreter |
Injection [RST] | |
CWE-78, Improper neutralization of special elements used Neutralization of Special Elements Used in an OS command Command ("OS command injectionCommand Injection") |
Android Implementation Details
...
Chapter 5, "Handling Input," section "Command Injection" | |
[OWASP 2005] | A Guide to Building Secure Web Applications and Web Services |
[Permissions 2008] | Permissions in the Java™ SE 6 Development Kit (JDK) |
[Seacord 2015] | IDS07-J. Do not pass untrusted, unsanitized data to the Runtime.exec() method LiveLesson |
...