Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: done

...

Consequently, command injection doesn't work attacks can not succeed unless a command interpreter is explicitly invoked. However, particularly on Windows, there can be vulnerabilities where argument injection attacks can occur when arguments have spaces, double quotes, and so forth, or start with a - or / to indicate a switch.

...

Noncompliant Code Example (Windows)

...

This noncompliant code example provides a directory listing using the dir command. It accomplishes this by using This is implemented using the Runtime.exec() to invoke the Windows dir command.

...

This noncompliant code example provides the same functionality, but uses the POSIX ls command. The only difference from the Windows version is that the argument is passed to proc.

Code Block
bgColor#FFcccc
  
class DirList {
  public static void main(String[] args) throws Exception {
    String dir = System.getProperty("dir");
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(new String[] {"sh", "-c", "ls " + dir});
    int result = proc.waitFor();
    if (result != 0) {
      System.out.println("process error: " + result);
    }
    InputStream in = (result == 0) ? proc.getInputStream() : proc.getErrorStream();
    int c;
    while ((c = in.read()) != -1) {
      System.out.print((char) c);
    }
  }
}

The attacker can supply the same command , with the shown in the previous noncompliant code example with similar same effects as above. The command executed is actually:

...

This compliant solution sanitizes the untrusted user input by permitting only allowing a handful small number of correct whitelisted characters to appearbe passed as part of the argument to the Runtime.exec() method.

Code Block
bgColor#ccccff
// ...
if (!Pattern.matches("[0-9A-Za-z@.]+", dir)) {
  // Handle error
}
// ...

Although this is a compliant solution, the sanitization method is weak because it will reject rejects valid directories. Also, because the command interpreter invoked is system dependent, it is difficult to say establish that this solution will not allow command injection on every possible platform in which a Java program might run.

...

This compliant solution prevents command injection by only passing trusted strings to Runtime.exec(). While the user has control over which string gets is used, the user cannot send strings provide string data directly to Runtime.exec().

...

This solution can quickly become unmanageable if you have many available directories. A more scalable solution is to read all the email addresses from a properties file into a java.util.Properties object. Alternately, the switch statement can operator on an enumenumerated type.

Compliant Solution (Avoid Runtime.exec())

When the task performed by executing a system command can be accomplished by some other means, it is almost always advisable to do so. This compliant solution uses the File.list() method to provide directory listing, thereby preventing command injectioneliminating the possibility of command or argument injection attacks.

Code Block
bgColor#ccccff
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);
      }
    }
  }
}

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="16d8f8f2432a32fe-b89b2a5f-4b054743-92429fd1-db28a249c504215f00293e4d"><ac:plain-text-body><![CDATA[

[CVE-2010-0886]

[Sun Java Web Start Plugin Command Line Argument Injection

http://www.securitytube.net/video/1465]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7166598220e07d04-af9f9608-44a24e56-988f9f84-25e25ab4967d284e84c066d2"><ac:plain-text-body><![CDATA[

[CVE-2010-1826]

[Command injection in updateSharingD's handling of Mach RPC messages

http://securitytracker.com/id/1024617]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4d384e626e21c64a-b8811918-4a604273-ad3a8e57-cc50953aedee355f1c2dc85c"><ac:plain-text-body><![CDATA[

[T-472]

[Mac OS X Java Command Injection Flaw in updateSharingD Lets Local Users Gain Elevated Privileges

http://www.doecirc.energy.gov/bulletins/t-472.shtml]

]]></ac:plain-text-body></ac:structured-macro>

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

The CERT C Secure Coding Standard

ENV04-C. Do not call system() if you do not need a command processor

The CERT C++ Secure Coding Standard

ENV04-CPP. Do not call system() if you do not need a command processor

MITRE CWE

CWE ID 78

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="440c7d64-54e2-4681-87e5-d411a3a4f85b"><ac:plain-text-body><![CDATA[

[[MITRE 2009

AA. Bibliography#MITRE 09]]

[CWE ID 78

http://cwe.mitre.org/data/definitions/78.html] "Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection')" ]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2135c79ca093fcaf-5b7f9a06-486441d3-8ccda76c-36c017ba66039cdf7516df28"><ac:plain-text-body><![CDATA[

[[Chess 2007

AA. Bibliography#Chess 07]]

Chapter 5: Handling Input, "Command Injection"]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fd47aa5fc4142284-df1e600e-45b84fc5-934e8ae5-0abe3adcf9272ccaad4a0c99"><ac:plain-text-body><![CDATA[

[[OWASP 2005

AA. Bibliography#OWASP 05]]

[Reviewing Code for OS Injection

http://www.owasp.org/index.php/Reviewing_Code_for_OS_Injection]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="45f6dc6912cfb11e-cc4d728a-45144f3d-8402b3c9-020cf2b1de77597bbed0ec76"><ac:plain-text-body><![CDATA[

[[Permissions 2008

AA. Bibliography#Permissions 08]]

[Permissions in the Java™ SE 6 Development Kit (JDK)

http://java.sun.com/javase/6/docs/technotes/guides/security/permissions.html], Sun Microsystems, Inc. (2008)

]]></ac:plain-text-body></ac:structured-macro>

...