Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
This rule detects command injection, a serious security vulnerability that occurs when an attacker is able to manipulate a command that the application executes. Command injection attacks can lead to data loss, corruption, or unauthorized access to sensitive system data.
Command injection vulnerabilities generally occur when user input is used unsanitized in a command that is executed by the application. In the non-compliant code samples, the user input is directly added to a command that is executed by the application, allowing an attacker to potentially execute arbitrary commands.
To avoid this vulnerability, user input should never be used directly in a command that is executed by the application. Instead, use safe APIs that allow you to execute commands without the risk of injection, or ensure that user input is properly sanitized before it is used. If you must use user input in a command, ensure that it is properly escaped or quoted to prevent the injection of additional commands.
Non-Compliant Code Examples
/**
* OWASP Benchmark v1.2
*
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For
* details, please see <a
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>.
*
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, version 2.
*
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* @author Dave Wichers
* @created 2015
*/packageorg.owasp.benchmark.testcode;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(value="/cmdi-00/BenchmarkTest00015")publicclassBenchmarkTest00015extendsHttpServlet{privatestaticfinallongserialVersionUID=1L;@OverridepublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}@OverridepublicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{// some coderesponse.setContentType("text/html;charset=UTF-8");Stringparam="";java.util.Enumeration<String>headers=request.getHeaders("BenchmarkTest00015");if(headers!=null&&headers.hasMoreElements()){param=headers.nextElement();// just grab first element}// URL Decode the header value since req.getHeaders() doesn't. Unlike req.getParameters().param=java.net.URLDecoder.decode(param,"UTF-8");java.util.List<String>argList=newjava.util.ArrayList<String>();StringosName=System.getProperty("os.name");if(osName.indexOf("Windows")!=-1){argList.add("cmd.exe");argList.add("/c");}else{argList.add("sh");argList.add("-c");}argList.add("echo "+param);ProcessBuilderpb=newProcessBuilder();pb.command(argList);try{Processp=pb.start();org.owasp.benchmark.helpers.Utils.printOSCommandResults(p,response);}catch(IOExceptione){System.out.println("Problem executing cmdi - java.lang.ProcessBuilder(java.util.List) Test Case");thrownewServletException(e);}}}
/**
* OWASP Benchmark v1.2
*
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For
* details, please see <a
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>.
*
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, version 2.
*
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* @author Dave Wichers
* @created 2015
*/packageorg.owasp.benchmark.testcode;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(value="/cmdi-00/BenchmarkTest00006")publicclassBenchmarkTest00006extendsHttpServlet{privatestaticfinallongserialVersionUID=1L;@OverridepublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}@OverridepublicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{// some coderesponse.setContentType("text/html;charset=UTF-8");Stringparam="";if(request.getHeader("BenchmarkTest00006")!=null){param=request.getHeader("BenchmarkTest00006");}// URL Decode the header value since req.getHeader() doesn't. Unlike req.getParameter().param=java.net.URLDecoder.decode(param,"UTF-8");java.util.List<String>argList=newjava.util.ArrayList<String>();StringosName=System.getProperty("os.name");if(osName.indexOf("Windows")!=-1){argList.add("cmd.exe");argList.add("/c");}else{argList.add("sh");argList.add("-c");}argList.add("echo "+param);ProcessBuilderpb=newProcessBuilder();pb.command(argList);try{Processp=pb.start();org.owasp.benchmark.helpers.Utils.printOSCommandResults(p,response);}catch(IOExceptione){System.out.println("Problem executing cmdi - java.lang.ProcessBuilder(java.util.List) Test Case");thrownewServletException(e);}}}
Compliant Code Examples
/**
* OWASP Benchmark Project v1.2
*
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For
* details, please see <a
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>.
*
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, version 2.
*
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* @author Nick Sanidas
* @created 2015
*/packageorg.owasp.benchmark.testcode;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;@WebServlet(value="/cmdi-01/BenchmarkTest00905")publicclassBenchmarkTest00905extendsHttpServlet{privatestaticfinallongserialVersionUID=1L;@OverridepublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}@OverridepublicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType("text/html;charset=UTF-8");org.owasp.benchmark.helpers.SeparateClassRequestscr=neworg.owasp.benchmark.helpers.SeparateClassRequest(request);Stringparam=scr.getTheValue("BenchmarkTest00905");Stringbar=param;Stringcmd="";Stringa1="";Stringa2="";String[]args=null;StringosName=System.getProperty("os.name");if(osName.indexOf("Windows")!=-1){a1="cmd.exe";a2="/c";cmd=org.owasp.benchmark.helpers.Utils.getOSCommandString("echo");args=newString[]{a1,a2,cmd,bar};}else{a1="sh";a2="-c";cmd=org.owasp.benchmark.helpers.Utils.getOSCommandString("ping -c1 ");args=newString[]{a1,a2,cmd+bar};}Runtimer=Runtime.getRuntime();try{Processp=r.exec(args);org.owasp.benchmark.helpers.Utils.printOSCommandResults(p,response);}catch(IOExceptione){System.out.println("Problem executing cmdi - TestCase");response.getWriter().println(org.owasp.esapi.ESAPI.encoder().encodeForHTML(e.getMessage()));return;}}}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- java-security # Rules to enforce Java security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines