Skip to content

Commit e4eb875

Browse files
committed
Java: Add more tests for CWE-078 and update expected test outout.
1 parent 2f461c9 commit e4eb875

File tree

9 files changed

+289
-12
lines changed

9 files changed

+289
-12
lines changed

java/test/security/CWE-078/CommandInjectionRuntimeExecLocal.expected

Lines changed: 95 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: security/CWE-078/CommandInjectionRuntimeExecLocal.ql
2+
postprocess: TestUtilities/PrettyPrintModels.ql

java/test/security/CWE-078/CommandInjectionRuntimeExecTest.expected

Lines changed: 42 additions & 6 deletions
Large diffs are not rendered by default.

java/test/security/CWE-078/CommandInjectionRuntimeExecTestPath.expected

Lines changed: 42 additions & 6 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#select
2+
| JSchOSInjectionTest.java:27:52:27:68 | ... + ... | JSchOSInjectionTest.java:14:30:14:60 | getParameter(...) : String | JSchOSInjectionTest.java:27:52:27:68 | ... + ... | This command line depends on a $@. | JSchOSInjectionTest.java:14:30:14:60 | getParameter(...) | user-provided value |
3+
| JSchOSInjectionTest.java:53:36:53:52 | ... + ... | JSchOSInjectionTest.java:40:30:40:60 | getParameter(...) : String | JSchOSInjectionTest.java:53:36:53:52 | ... + ... | This command line depends on a $@. | JSchOSInjectionTest.java:40:30:40:60 | getParameter(...) | user-provided value |
4+
edges
5+
| JSchOSInjectionTest.java:14:30:14:60 | getParameter(...) : String | JSchOSInjectionTest.java:27:52:27:68 | ... + ... | provenance | Src:MaD:2 Sink:MaD:1 |
6+
| JSchOSInjectionTest.java:40:30:40:60 | getParameter(...) : String | JSchOSInjectionTest.java:53:36:53:52 | ... + ... | provenance | Src:MaD:2 Sink:MaD:1 |
7+
models
8+
| 1 | Sink: com.jcraft.jsch; ChannelExec; true; setCommand; ; ; Argument[0]; command-injection; manual |
9+
| 2 | Source: javax.servlet; ServletRequest; false; getParameter; (String); ; ReturnValue; remote; manual |
10+
nodes
11+
| JSchOSInjectionTest.java:14:30:14:60 | getParameter(...) : String | semmle.label | getParameter(...) : String |
12+
| JSchOSInjectionTest.java:27:52:27:68 | ... + ... | semmle.label | ... + ... |
13+
| JSchOSInjectionTest.java:40:30:40:60 | getParameter(...) : String | semmle.label | getParameter(...) : String |
14+
| JSchOSInjectionTest.java:53:36:53:52 | ... + ... | semmle.label | ... + ... |
15+
subpaths
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: security/CWE-078/ExecTainted.ql
2+
postprocess: TestUtilities/PrettyPrintModels.ql
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import com.jcraft.jsch.*;
2+
3+
import javax.servlet.http.*;
4+
import javax.servlet.ServletException;
5+
import java.io.IOException;
6+
7+
public class JSchOSInjectionTest extends HttpServlet {
8+
9+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
10+
throws ServletException, IOException {
11+
String host = "sshHost";
12+
String user = "user";
13+
String password = "password";
14+
String command = request.getParameter("command");
15+
16+
java.util.Properties config = new java.util.Properties();
17+
config.put("StrictHostKeyChecking", "no");
18+
19+
JSch jsch = new JSch();
20+
try {
21+
Session session = jsch.getSession(user, host, 22);
22+
session.setPassword(password);
23+
session.setConfig(config);
24+
session.connect();
25+
26+
Channel channel = session.openChannel("exec");
27+
((ChannelExec) channel).setCommand("ping " + command);
28+
channel.setInputStream(null);
29+
((ChannelExec) channel).setErrStream(System.err);
30+
31+
channel.connect();
32+
} catch (JSchException e) { }
33+
}
34+
35+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
36+
throws ServletException, IOException {
37+
String host = "sshHost";
38+
String user = "user";
39+
String password = "password";
40+
String command = request.getParameter("command");
41+
42+
java.util.Properties config = new java.util.Properties();
43+
config.put("StrictHostKeyChecking", "no");
44+
45+
JSch jsch = new JSch();
46+
try {
47+
Session session = jsch.getSession(user, host, 22);
48+
session.setPassword(password);
49+
session.setConfig(config);
50+
session.connect();
51+
52+
ChannelExec channel = (ChannelExec)session.openChannel("exec");
53+
channel.setCommand("ping " + command);
54+
channel.setInputStream(null);
55+
channel.setErrStream(System.err);
56+
57+
channel.connect();
58+
} catch (JSchException e) { }
59+
}
60+
}

java/test/security/CWE-078/options

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../codeql/java/ql/test/stubs/servlet-api-2.4:${testdir}/../../../../codeql/java/ql/test/stubs/jsch-0.1.55

java/test/security/CWE-078/src/main/java/com/github/githubsecuritylab/command_injection_test/Main.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,35 @@ public static void main(String[] args) {
4040
} catch (Exception e) {
4141
System.err.println("ERROR: " + e.getMessage());
4242
}
43+
44+
String script = System.getenv("SCRIPTNAME");
45+
46+
if (script != null) {
47+
try {
48+
// 1. array literal in the args
49+
Runtime.getRuntime().exec(new String[]{"/bin/sh", script});
50+
51+
// 2. array literal with dataflow
52+
String[] commandArray1 = new String[]{"/bin/sh", script};
53+
Runtime.getRuntime().exec(commandArray1);
54+
55+
// 3. array assignment after it is created
56+
String[] commandArray2 = new String[4];
57+
commandArray2[0] = "/bin/sh";
58+
commandArray2[1] = script;
59+
Runtime.getRuntime().exec(commandArray2);
60+
61+
// 4. Stream concatenation
62+
Runtime.getRuntime().exec(
63+
Stream.concat(
64+
Arrays.stream(new String[]{"/bin/sh"}),
65+
Arrays.stream(new String[]{script})
66+
).toArray(String[]::new)
67+
);
68+
69+
} catch (Exception e) {
70+
System.err.println("ERROR: " + e.getMessage());
71+
}
72+
}
4373
}
4474
}

0 commit comments

Comments
 (0)