One of the Java examples for Command Injections is not vulnerable.
Pecisely, this code is only vulnerable to Argument Injections, not to Command Injections:
// Vunerable
String input = "&& cat /etc/passwd"; // value supplied by user input
Runtime r = Runtime.getRuntime();
r.exec("some_tool -t param1 param2 " + input);
This happens because under the hood Java:
- Splits the
exec argument by the spaces.
- Uses the first element of the array as the first argument of the
execve syscall.
- Uses all the remaining elements as the second argument of the
execve syscall.
This means that the current example would become execve("some_tool", ["-t","param1","param2","&&","cat","/etc/passwd], ...), therefore the cat /etc/passwd command would not be executed.
Argument Injections are still possible and Command injections are also possible in case the first arguments are opening a shell and accepting a list of commands (e.g. "cmd /c" + input or "/bin/bash -c" + input).
One of the Java examples for Command Injections is not vulnerable.
Pecisely, this code is only vulnerable to Argument Injections, not to Command Injections:
This happens because under the hood Java:
execargument by the spaces.execvesyscall.execvesyscall.This means that the current example would become
execve("some_tool", ["-t","param1","param2","&&","cat","/etc/passwd], ...), therefore thecat /etc/passwdcommand would not be executed.Argument Injections are still possible and Command injections are also possible in case the first arguments are opening a shell and accepting a list of commands (e.g.
"cmd /c" + inputor"/bin/bash -c" + input).