|
| 1 | +/** |
| 2 | + * @name Injection in Jython |
| 3 | + * @description Evaluation of a user-controlled malicious expression in Java Python |
| 4 | + * interpreter may lead to remote code execution. |
| 5 | + * @kind path-problem |
| 6 | + * @id java/jython-injection |
| 7 | + * @tags security |
| 8 | + * external/cwe/cwe-094 |
| 9 | + * external/cwe/cwe-095 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.dataflow.FlowSources |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/** The class `org.python.util.PythonInterpreter`. */ |
| 17 | +class PythonInterpreter extends RefType { |
| 18 | + PythonInterpreter() { this.hasQualifiedName("org.python.util", "PythonInterpreter") } |
| 19 | +} |
| 20 | + |
| 21 | +/** A method that evaluates, compiles or executes a Jython expression. */ |
| 22 | +class InterpretExprMethod extends Method { |
| 23 | + InterpretExprMethod() { |
| 24 | + this.getDeclaringType().getAnAncestor*() instanceof PythonInterpreter and |
| 25 | + ( |
| 26 | + getName().matches("exec%") or |
| 27 | + hasName("eval") or |
| 28 | + hasName("compile") or |
| 29 | + getName().matches("run%") |
| 30 | + ) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** The class `org.python.core.BytecodeLoader`. */ |
| 35 | +class BytecodeLoader extends RefType { |
| 36 | + BytecodeLoader() { this.hasQualifiedName("org.python.core", "BytecodeLoader") } |
| 37 | +} |
| 38 | + |
| 39 | +/** Holds if a Jython expression if evaluated, compiled or executed. */ |
| 40 | +predicate runCode(MethodAccess ma, Expr sink) { |
| 41 | + exists(Method m | m = ma.getMethod() | |
| 42 | + m instanceof InterpretExprMethod and |
| 43 | + sink = ma.getArgument(0) |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** A method that loads Java class data. */ |
| 48 | +class LoadClassMethod extends Method { |
| 49 | + LoadClassMethod() { |
| 50 | + this.getDeclaringType().getAnAncestor*() instanceof BytecodeLoader and |
| 51 | + ( |
| 52 | + hasName("makeClass") or |
| 53 | + hasName("makeCode") |
| 54 | + ) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** Holds if a Java class file is loaded. */ |
| 59 | +predicate loadClass(MethodAccess ma, Expr sink) { |
| 60 | + exists(Method m, int i | m = ma.getMethod() | |
| 61 | + m instanceof LoadClassMethod and |
| 62 | + m.getParameter(i).getType() instanceof Array and // makeClass(java.lang.String name, byte[] data, ...) |
| 63 | + sink = ma.getArgument(i) |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +/** The class `org.python.core.Py`. */ |
| 68 | +class Py extends RefType { |
| 69 | + Py() { this.hasQualifiedName("org.python.core", "Py") } |
| 70 | +} |
| 71 | + |
| 72 | +/** A method that compiles code with `Py`. */ |
| 73 | +class PyCompileMethod extends Method { |
| 74 | + PyCompileMethod() { |
| 75 | + this.getDeclaringType().getAnAncestor*() instanceof Py and |
| 76 | + getName().matches("compile%") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** Holds if source code is compiled with `PyCompileMethod`. */ |
| 81 | +predicate compile(MethodAccess ma, Expr sink) { |
| 82 | + exists(Method m | m = ma.getMethod() | |
| 83 | + m instanceof PyCompileMethod and |
| 84 | + sink = ma.getArgument(0) |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** Sink of an expression loaded by Jython. */ |
| 89 | +class CodeInjectionSink extends DataFlow::ExprNode { |
| 90 | + CodeInjectionSink() { |
| 91 | + runCode(_, this.getExpr()) or |
| 92 | + loadClass(_, this.getExpr()) or |
| 93 | + compile(_, this.getExpr()) |
| 94 | + } |
| 95 | + |
| 96 | + MethodAccess getMethodAccess() { |
| 97 | + runCode(result, this.getExpr()) or |
| 98 | + loadClass(result, this.getExpr()) or |
| 99 | + compile(result, this.getExpr()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class CodeInjectionConfiguration extends TaintTracking::Configuration { |
| 104 | + CodeInjectionConfiguration() { this = "CodeInjectionConfiguration" } |
| 105 | + |
| 106 | + override predicate isSource(DataFlow::Node source) { |
| 107 | + source instanceof RemoteFlowSource |
| 108 | + or |
| 109 | + source instanceof LocalUserInput |
| 110 | + } |
| 111 | + |
| 112 | + override predicate isSink(DataFlow::Node sink) { sink instanceof CodeInjectionSink } |
| 113 | + |
| 114 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 115 | + // @RequestBody MyQueryObj query; interpreter.exec(query.getInterpreterCode()); |
| 116 | + exists(MethodAccess ma | ma.getQualifier() = node1.asExpr() and ma = node2.asExpr()) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +from DataFlow::PathNode source, DataFlow::PathNode sink, CodeInjectionConfiguration conf |
| 121 | +where conf.hasFlowPath(source, sink) |
| 122 | +select sink.getNode().(CodeInjectionSink).getMethodAccess(), source, sink, "Jython evaluate $@.", |
| 123 | + source.getNode(), "user input" |
0 commit comments