Skip to content

Commit 0bb0031

Browse files
authored
Merge pull request #154 from microsoft/invoke-expression-ps-query
invoke expression powershell query
2 parents 4fd7aec + 6d55972 commit 0bb0031

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Use of Invoke-Expression
3+
* @description Do not use Invoke-Expression
4+
* @kind problem
5+
* @problem.severity error
6+
* @security-severity 9.8
7+
* @precision high
8+
* @id powershell/do-not-use-invoke-expression
9+
* @tags security
10+
*/
11+
import powershell
12+
import semmle.code.powershell.dataflow.DataFlow
13+
14+
from CmdCall call
15+
where call.getName() = "Invoke-Expression"
16+
select call, "Do not use Invoke-Expression. It is a command injection risk."
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
<code>Invoke-Expression</code> cmdlet should only be used as a last resort. In most scenarios, safer and more robust alternatives are available. Using <code>Invoke-Expression</code> can lead to arbitrary commands being executed</p>
8+
9+
</overview>
10+
<recommendation>
11+
12+
<p>Avoid using <code>Invoke-Expression</code> in your powershell code.</p>
13+
14+
<p>If you’re running some command and the command path has spaces in it, then you need the command invocation operator <code>&</code></p>
15+
</recommendation>
16+
17+
<references>
18+
19+
<li>
20+
Powershell:
21+
<a href="https://devblogs.microsoft.com/powershell/invoke-expression-considered-harmful/">Invoke-Expression considered harmful</a>.
22+
</li>
23+
<li>
24+
PSScriptAnalyzer:
25+
<a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/avoidusinginvokeexpression?view=ps-modules">AvoidUsingInvokeExpression</a>
26+
</li>
27+
<li>
28+
StackOverflow:
29+
<a href="https://stackoverflow.com/questions/51252465/in-what-scenario-was-invoke-expression-designed-to-be-used/51252636#51252636">In what scenario was Invoke-Expression designed to be used?</a>
30+
</li>
31+
32+
</references>
33+
</qhelp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.ps1:2:1:2:27 | call to Invoke-Expression | Do not use Invoke-Expression. It is a command injection risk. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-078/DoNotUseInvokeExpression.ql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$command = "Get-Process"
2+
Invoke-Expression $Command

0 commit comments

Comments
 (0)