Skip to content

Commit 1f5f1fc

Browse files
author
owen-jones-diffblue
authored
Merge pull request diffblue#580 from diffblue/owen-jones-diffblue/command-injection
SEC-662: Added first test for command injection
2 parents 29573c9 + 288a1b2 commit 1f5f1fc

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<project name="Main" basedir="." default="compile">
2+
3+
<property name="root.dir" value="./"/>
4+
<property name="src.dir" value="${root.dir}/src"/>
5+
<property name="classes.dir" value="${root.dir}/build"/>
6+
7+
<target name="compile">
8+
<antcall target="clean" />
9+
<mkdir dir="${classes.dir}"/>
10+
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="on" />
11+
</target>
12+
13+
<target name="clean">
14+
<delete dir="${classes.dir}"/>
15+
</target>
16+
17+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"namespace": "com.diffblue.security",
3+
"rules":
4+
[
5+
{
6+
"comment": "Obtaining tainted string.",
7+
"class": "command_injection.Main",
8+
"method": "make_tainted:(Ljava/lang/String;)Ljava/lang/String;",
9+
"result": {
10+
"location": "returns",
11+
"taint": "command injection taint"
12+
}
13+
},
14+
{
15+
"comment": "Executing a tainted string.",
16+
"class": "java.lang.Runtime",
17+
"method": "exec:(Ljava/lang/String;)Ljava/lang/Process;",
18+
"sinkTarget": {
19+
"location": "arg1",
20+
"taint": "command injection taint"
21+
},
22+
"message": "A tainted string was executed."
23+
}
24+
]
25+
}
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package command_injection;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
public class Main {
7+
8+
private static String make_tainted(String s) {
9+
return s;
10+
}
11+
12+
private static void test_exec(String string_from_attacker) {
13+
try {
14+
Process p = Runtime.getRuntime().exec(string_from_attacker);
15+
}
16+
catch (IOException e) {
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
if (args.length < 1)
22+
return;
23+
24+
String string_from_attacker = make_tainted(args[0]);
25+
26+
test_exec(string_from_attacker);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fasteners
2+
import os
3+
import subprocess
4+
5+
from regression.end_to_end.driver import run_security_analyser_pipeline
6+
import regression.utils as utils
7+
8+
9+
@fasteners.interprocess_locked(os.path.join(os.path.dirname(__file__), ".build_lock"))
10+
def test_command_injection(load_strategy):
11+
with utils.working_dir(os.path.abspath(os.path.dirname(__file__))):
12+
subprocess.check_call(["ant"])
13+
with run_security_analyser_pipeline(
14+
"build",
15+
"rules.json",
16+
os.path.realpath(os.path.dirname(__file__)),
17+
"command_injection.Main.main",
18+
load_strategy,
19+
extra_args=["--use-models-library"]) as traces:
20+
assert traces.count_traces() == 1
21+
assert traces.trace_exists(
22+
"java::command_injection.Main.test_exec:(Ljava/lang/String;)V", 14)

0 commit comments

Comments
 (0)