|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * second order command injection, as well as |
| 4 | + * extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.frameworks.core.Gem::Gem as Gem |
| 9 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 10 | +private import codeql.ruby.Concepts as Concepts |
| 11 | + |
| 12 | +/** Classes and predicates for reasoning about second order command injection. */ |
| 13 | +module SecondOrderCommandInjection { |
| 14 | + /** A shell command that allows for second order command injection. */ |
| 15 | + private class VulnerableCommand extends string { |
| 16 | + VulnerableCommand() { this = ["git", "hg"] } |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets a vulnerable subcommand of this command. |
| 20 | + * E.g. `git` has `clone` and `pull` as vulnerable subcommands. |
| 21 | + * And every command of `hg` is vulnerable due to `--config=alias.<alias>=<command>`. |
| 22 | + */ |
| 23 | + bindingset[result] |
| 24 | + string getAVulnerableSubCommand() { |
| 25 | + this = "git" and result = ["clone", "ls-remote", "fetch", "pull"] |
| 26 | + or |
| 27 | + this = "hg" and exists(result) |
| 28 | + } |
| 29 | + |
| 30 | + /** Gets an example argument that can cause this command to execute arbitrary code. */ |
| 31 | + string getVulnerableArgumentExample() { |
| 32 | + this = "git" and result = "--upload-pack" |
| 33 | + or |
| 34 | + this = "hg" and result = "--config=alias.<alias>=<command>" |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** A source for second order command injection vulnerabilities. */ |
| 39 | + abstract class Source extends DataFlow::Node { |
| 40 | + /** Gets a string that describes the source. For use in the alert message. */ |
| 41 | + abstract string describe(); |
| 42 | + } |
| 43 | + |
| 44 | + /** A parameter of an exported function, seen as a source for second order command injection. */ |
| 45 | + class ExternalInputSource extends Source { |
| 46 | + ExternalInputSource() { this = Gem::getALibraryInput() } |
| 47 | + |
| 48 | + override string describe() { result = "library input" } |
| 49 | + } |
| 50 | + |
| 51 | + /** A source of remote flow, seen as a source for second order command injection. */ |
| 52 | + class RemoteFlowAsSource extends Source instanceof RemoteFlowSource { |
| 53 | + override string describe() { result = "a user-provided value" } |
| 54 | + } |
| 55 | + |
| 56 | + /** A sink for second order command injection vulnerabilities. */ |
| 57 | + abstract class Sink extends DataFlow::Node { |
| 58 | + /** Gets the command getting invoked. I.e. `git` or `hg`. */ |
| 59 | + abstract string getCommand(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets an example argument for the comand that allows for second order command injection. |
| 63 | + * E.g. `--upload-pack` for `git`. |
| 64 | + */ |
| 65 | + abstract string getVulnerableArgumentExample(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A sink that invokes a command described by the `VulnerableCommand` class. |
| 70 | + */ |
| 71 | + abstract class VulnerableCommandSink extends Sink { |
| 72 | + VulnerableCommand cmd; |
| 73 | + |
| 74 | + override string getCommand() { result = cmd } |
| 75 | + |
| 76 | + override string getVulnerableArgumentExample() { result = cmd.getVulnerableArgumentExample() } |
| 77 | + } |
| 78 | + |
| 79 | + private import codeql.ruby.typetracking.TypeTracker |
| 80 | + |
| 81 | + /** A sanitizer for second order command injection vulnerabilities. */ |
| 82 | + abstract class Sanitizer extends DataFlow::Node { } |
| 83 | + |
| 84 | + import codeql.ruby.typetracking.TypeTracker |
| 85 | + |
| 86 | + private DataFlow::LocalSourceNode usedAsArgList( |
| 87 | + TypeBackTracker t, Concepts::SystemCommandExecution exec |
| 88 | + ) { |
| 89 | + t.start() and |
| 90 | + result = exec.getArgumentList().getALocalSource() |
| 91 | + or |
| 92 | + exists(TypeBackTracker t2 | |
| 93 | + result = usedAsArgList(t2, exec).backtrack(t2, t) |
| 94 | + or |
| 95 | + // step through splat expressions |
| 96 | + t2 = t.continue() and |
| 97 | + result.asExpr().getExpr() = |
| 98 | + // TODO: flows-to. |
| 99 | + usedAsArgList(t2, exec).asExpr().getExpr().(Ast::SplatExpr).getOperand() |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + /** Gets a dataflow node that ends up being used as an argument list to an invocation of `git` or `hg`. */ |
| 104 | + private DataFlow::LocalSourceNode usedAsVersionControlArgs( |
| 105 | + TypeBackTracker t, DataFlow::Node argList, VulnerableCommand cmd |
| 106 | + ) { |
| 107 | + t.start() and |
| 108 | + // TODO: untested. |
| 109 | + exists(Concepts::SystemCommandExecution exec | |
| 110 | + exec.getACommandArgument().getConstantValue().getStringlikeValue() = cmd |
| 111 | + | |
| 112 | + exec.getArgumentList() = argList and |
| 113 | + result = argList.getALocalSource() |
| 114 | + ) |
| 115 | + or |
| 116 | + t.start() and |
| 117 | + exists( |
| 118 | + Concepts::SystemCommandExecution exec, Cfg::CfgNodes::ExprNodes::ArrayLiteralCfgNode arr |
| 119 | + | |
| 120 | + argList = usedAsArgList(TypeBackTracker::end(), exec) and |
| 121 | + arr = argList.asExpr() and |
| 122 | + arr.getArgument(0).getConstantValue().getStringlikeValue() = cmd |
| 123 | + | |
| 124 | + result = argList.getALocalSource() |
| 125 | + or |
| 126 | + result |
| 127 | + .flowsTo(any(DataFlow::Node n | |
| 128 | + n.asExpr().getExpr() = arr.getArgument(_).getExpr().(Ast::SplatExpr).getOperand() |
| 129 | + )) |
| 130 | + ) |
| 131 | + or |
| 132 | + exists(TypeBackTracker t2 | |
| 133 | + result = usedAsVersionControlArgs(t2, argList, cmd).backtrack(t2, t) |
| 134 | + or |
| 135 | + // step through splat expressions |
| 136 | + t2 = t.continue() and |
| 137 | + result |
| 138 | + .flowsTo(any(DataFlow::Node n | |
| 139 | + n.asExpr().getExpr() = |
| 140 | + usedAsVersionControlArgs(t2, argList, cmd) |
| 141 | + .asExpr() |
| 142 | + .getExpr() |
| 143 | + .(Ast::SplatExpr) |
| 144 | + .getOperand() |
| 145 | + )) |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + private import codeql.ruby.dataflow.internal.DataFlowDispatch as Dispatch |
| 150 | + |
| 151 | + class IndirectVCSCall extends DataFlow::CallNode { |
| 152 | + VulnerableCommand cmd; |
| 153 | + |
| 154 | + IndirectVCSCall() { |
| 155 | + exists(Dispatch::DataFlowCallable calleeDis, Ast::Callable callee, DataFlow::Node list | |
| 156 | + calleeDis = |
| 157 | + Dispatch::viableCallable(any(Dispatch::DataFlowCall c | c.asCall() = this.asExpr())) and |
| 158 | + calleeDis.asCallable() = callee and |
| 159 | + list.asExpr().getExpr() = callee.getParameter(0).(Ast::SplatParameter).getDefiningAccess() and |
| 160 | + // TODO: multiple nodes in the same position, i need to figure that out if this makes production. |
| 161 | + usedAsVersionControlArgs(TypeBackTracker::end(), _, cmd).getLocation() = list.getLocation() |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + VulnerableCommand getCommand() { result = cmd } |
| 166 | + } |
| 167 | + |
| 168 | + class IndirectCallSink extends Sink { |
| 169 | + VulnerableCommand cmd; |
| 170 | + |
| 171 | + IndirectCallSink() { |
| 172 | + exists(IndirectVCSCall call, int i | |
| 173 | + call.getCommand() = cmd and |
| 174 | + call.getArgument(i).getConstantValue().getStringlikeValue() = cmd.getAVulnerableSubCommand() and |
| 175 | + this = call.getArgument(any(int j | j > i)) |
| 176 | + ) |
| 177 | + } |
| 178 | + |
| 179 | + override string getCommand() { result = cmd } |
| 180 | + |
| 181 | + override string getVulnerableArgumentExample() { result = cmd.getVulnerableArgumentExample() } |
| 182 | + } |
| 183 | +} |
0 commit comments