|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * command-injection vulnerabilities, as well as extension points for |
| 4 | + * adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import semmle.code.powershell.dataflow.DataFlow |
| 8 | +private import semmle.code.powershell.dataflow.flowsources.FlowSources |
| 9 | +private import semmle.code.powershell.Cfg |
| 10 | + |
| 11 | +module CommandInjection { |
| 12 | + /** |
| 13 | + * A data flow source for command-injection vulnerabilities. |
| 14 | + */ |
| 15 | + abstract class Source extends DataFlow::Node { |
| 16 | + /** Gets a string that describes the type of this flow source. */ |
| 17 | + abstract string getSourceType(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * A data flow sink for command-injection vulnerabilities. |
| 22 | + */ |
| 23 | + abstract class Sink extends DataFlow::Node { } |
| 24 | + |
| 25 | + /** |
| 26 | + * A sanitizer for command-injection vulnerabilities. |
| 27 | + */ |
| 28 | + abstract class Sanitizer extends DataFlow::Node { } |
| 29 | + |
| 30 | + /** A source of user input, considered as a flow source for command injection. */ |
| 31 | + class FlowSourceAsSource extends Source instanceof SourceNode { |
| 32 | + override string getSourceType() { result = "user-provided value" } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * A command argument to a function that initiates an operating system command. |
| 37 | + */ |
| 38 | + class SystemCommandExecutionSink extends Sink { |
| 39 | + SystemCommandExecutionSink() { |
| 40 | + // An argument to a call |
| 41 | + exists(DataFlow::CallNode call | |
| 42 | + call.getName() = "Invoke-Expression" |
| 43 | + or |
| 44 | + call instanceof DataFlow::CallOperatorNode |
| 45 | + | |
| 46 | + call.getAnArgument() = this |
| 47 | + ) |
| 48 | + or |
| 49 | + // Or the call command itself in case it's a use of operator &. |
| 50 | + any(DataFlow::CallOperatorNode call).getCommand() = this |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private class ExternalCommandInjectionSink extends Sink { |
| 55 | + ExternalCommandInjectionSink() { |
| 56 | + this = ModelOutput::getASinkNode("command-injection").asSink() |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments