11
11
import powershell
12
12
13
13
predicate containsScope ( VarAccess outer , VarAccess inner ) {
14
- outer .getUserPath ( ) = inner .getUserPath ( ) and
14
+ outer .getVariable ( ) . getName ( ) = inner .getVariable ( ) . getName ( ) and
15
15
outer != inner
16
16
}
17
17
@@ -23,16 +23,16 @@ predicate constantBinaryExpression(BinaryExpr binary) {
23
23
onlyConstantExpressions ( binary .getLeft ( ) ) and onlyConstantExpressions ( binary .getRight ( ) )
24
24
}
25
25
26
- predicate onlyConstantExpressions ( Expr expr ) {
27
- expr instanceof StringConstExpr or constantBinaryExpression ( expr ) or constantTernaryExpression ( expr )
26
+ predicate onlyConstantExpressions ( Expr expr ) {
27
+ expr instanceof StringConstExpr or
28
+ constantBinaryExpression ( expr ) or
29
+ constantTernaryExpression ( expr )
28
30
}
29
31
30
32
VarAccess getNonConstantVariableAssignment ( VarAccess varexpr ) {
31
- (
32
- exists ( AssignStmt assignment |
33
- not onlyConstantExpressions ( assignment .getRightHandSide ( ) .( CmdExpr ) .getExpr ( ) ) and
34
- result = assignment .getLeftHandSide ( )
35
- )
33
+ exists ( AssignStmt assignment |
34
+ not onlyConstantExpressions ( assignment .getRightHandSide ( ) ) and
35
+ result = assignment .getLeftHandSide ( )
36
36
) and
37
37
containsScope ( result , varexpr )
38
38
}
@@ -44,31 +44,35 @@ VarAccess getParameterWithVariableScope(VarAccess varexpr) {
44
44
)
45
45
}
46
46
47
- Expr getAllSubExpressions ( Expr expr )
48
- {
47
+ Expr getAllSubExpressions ( Expr expr ) {
49
48
result = expr or
50
- result = getAllSubExpressions ( expr .( ArrayLiteral ) .getAnElement ( ) ) or
51
- result = getAllSubExpressions ( expr .( ArrayExpr ) .getStmtBlock ( ) .getAStmt ( ) .( Pipeline ) .getAComponent ( ) .( CmdExpr ) .getExpr ( ) )
49
+ result = getAllSubExpressions ( expr .( ArrayLiteral ) .getAnExpr ( ) ) or
50
+ result =
51
+ getAllSubExpressions ( expr .( ArrayExpr )
52
+ .getStmtBlock ( )
53
+ .getAStmt ( )
54
+ .( ExprStmt )
55
+ .getExpr ( )
56
+ .( Pipeline )
57
+ .getAComponent ( ) )
52
58
}
53
59
54
- Expr dangerousCommandElement ( Cmd command )
55
- {
60
+ Expr dangerousCommandElement ( CallExpr command ) {
56
61
(
57
- command . getKind ( ) = 28 or
58
- command .getCommandName ( ) = "Invoke-Expression"
62
+ command instanceof CallOperator or
63
+ command .getName ( ) = "Invoke-Expression"
59
64
) and
60
65
result = getAllSubExpressions ( command .getAnArgument ( ) )
61
66
}
62
67
63
68
from Expr commandarg , VarAccess unknownDeclaration
64
69
where
65
- exists ( Cmd command |
70
+ exists ( CallExpr command |
66
71
(
67
72
unknownDeclaration = getNonConstantVariableAssignment ( commandarg ) or
68
73
unknownDeclaration = getParameterWithVariableScope ( commandarg )
69
- )
70
- and
74
+ ) and
71
75
commandarg = dangerousCommandElement ( command )
72
76
)
73
77
select commandarg .( VarAccess ) .getLocation ( ) , "Unsafe flow to command argument from $@." ,
74
- unknownDeclaration , unknownDeclaration .getUserPath ( )
78
+ unknownDeclaration , unknownDeclaration .getVariable ( ) . getName ( )
0 commit comments