|
| 1 | +/** |
| 2 | + * @name Uncontrolled command line from param to CmdletBinding |
| 3 | + * @description Using externally controlled strings in a command line may allow a malicious |
| 4 | + * user to change the meaning of the command. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 9.8 |
| 8 | + * @precision high |
| 9 | + * @id powershell/microsoft/public/command-injection-critical |
| 10 | + * @tags correctness |
| 11 | + * security |
| 12 | + * external/cwe/cwe-078 |
| 13 | + * external/cwe/cwe-088 |
| 14 | + */ |
| 15 | + |
| 16 | +import powershell |
| 17 | +import semmle.code.powershell.security.CommandInjectionCustomizations::CommandInjection |
| 18 | +import semmle.code.powershell.dataflow.TaintTracking |
| 19 | +import semmle.code.powershell.dataflow.DataFlow |
| 20 | + |
| 21 | +abstract class CriticalSource extends DataFlow::Node { |
| 22 | + /** Gets a string that describes the type of this flow source. */ |
| 23 | + abstract string getSourceType(); |
| 24 | +} |
| 25 | + |
| 26 | +class CmdletBindingParam extends CriticalSource { |
| 27 | + CmdletBindingParam(){ |
| 28 | + exists(Attribute a, Function f | |
| 29 | + a.getName() = "CmdletBinding" and |
| 30 | + f = a.getEnclosingFunction() and |
| 31 | + this.asParameter() = f.getAParameter() |
| 32 | + ) |
| 33 | + } |
| 34 | + override string getSourceType(){ |
| 35 | + result = "param to CmdletBinding function" |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +private module Config implements DataFlow::ConfigSig { |
| 41 | + predicate isSource(DataFlow::Node source) { source instanceof CriticalSource } |
| 42 | + |
| 43 | + predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 44 | + |
| 45 | + predicate isBarrier(DataFlow::Node node) { node instanceof Sanitizer } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Taint-tracking for reasoning about command-injection vulnerabilities. |
| 50 | + */ |
| 51 | +module CommandInjectionCriticalFlow = TaintTracking::Global<Config>; |
| 52 | +import CommandInjectionCriticalFlow::PathGraph |
| 53 | + |
| 54 | +from CommandInjectionCriticalFlow::PathNode source, CommandInjectionCriticalFlow::PathNode sink, CriticalSource sourceNode |
| 55 | +where |
| 56 | + CommandInjectionCriticalFlow::flowPath(source, sink) and |
| 57 | + sourceNode = source.getNode() |
| 58 | +select sink.getNode(), source, sink, "This command depends on a $@.", sourceNode, |
| 59 | + sourceNode.getSourceType() |
0 commit comments