diff --git a/powershell/ql/src/queries/security/cwe-078/DoNotUseInvokeExpression.ql b/powershell/ql/src/queries/security/cwe-078/DoNotUseInvokeExpression.ql new file mode 100644 index 000000000000..c0b88293a011 --- /dev/null +++ b/powershell/ql/src/queries/security/cwe-078/DoNotUseInvokeExpression.ql @@ -0,0 +1,16 @@ +/** + * @name Use of Invoke-Expression + * @description Do not use Invoke-Expression + * @kind problem + * @problem.severity error + * @security-severity 9.8 + * @precision high + * @id powershell/do-not-use-invoke-expression + * @tags security + */ +import powershell +import semmle.code.powershell.dataflow.DataFlow + +from CmdCall call +where call.getName() = "Invoke-Expression" +select call, "Do not use Invoke-Expression. It is a command injection risk." \ No newline at end of file diff --git a/powershell/ql/src/queries/security/cwe-078/DoNotuseInvokeExpression.qhelp b/powershell/ql/src/queries/security/cwe-078/DoNotuseInvokeExpression.qhelp new file mode 100644 index 000000000000..cf7d1bbeb681 --- /dev/null +++ b/powershell/ql/src/queries/security/cwe-078/DoNotuseInvokeExpression.qhelp @@ -0,0 +1,33 @@ + + + +

+Invoke-Expression cmdlet should only be used as a last resort. In most scenarios, safer and more robust alternatives are available. Using Invoke-Expression can lead to arbitrary commands being executed

+ +
+ + +

Avoid using Invoke-Expression in your powershell code.

+ +

If you’re running some command and the command path has spaces in it, then you need the command invocation operator &

+
+ + + +
  • +Powershell: +Invoke-Expression considered harmful. +
  • +
  • +PSScriptAnalyzer: +AvoidUsingInvokeExpression +
  • +
  • +StackOverflow: +In what scenario was Invoke-Expression designed to be used? +
  • + +
    +
    diff --git a/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.expected b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.expected new file mode 100644 index 000000000000..3b7235e641f0 --- /dev/null +++ b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.expected @@ -0,0 +1 @@ +| test.ps1:2:1:2:27 | call to Invoke-Expression | Do not use Invoke-Expression. It is a command injection risk. | diff --git a/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.qlref b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.qlref new file mode 100644 index 000000000000..a006f78d20b4 --- /dev/null +++ b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/DoNotUseInvokeExpression.qlref @@ -0,0 +1 @@ +queries/security/cwe-078/DoNotUseInvokeExpression.ql \ No newline at end of file diff --git a/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/test.ps1 b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/test.ps1 new file mode 100644 index 000000000000..e075312b4b68 --- /dev/null +++ b/powershell/ql/test/query-tests/security/cwe-078/DoNotUseInvokeExpression/test.ps1 @@ -0,0 +1,2 @@ +$command = "Get-Process" +Invoke-Expression $Command \ No newline at end of file