Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>The use of the AsPlainText parameter with the ConvertTo-SecureString command can expose secure information.</p>

</overview>
<recommendation>
<p>
If you do need an ability to retrieve the password from somewhere without prompting the user, consider using the <a href="https://www.powershellgallery.com/packages/Microsoft.PowerShell.SecretStore">SecretStore</a> module from the PowerShell Gallery.
</p>
</recommendation>
<references>

<li>
PSScriptAnalyzer:
<a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/avoidusingconverttosecurestringwithplaintext?view=ps-modules">AvoidUsingConvertToSecureStringWithPlainText</a>.
</li>

</references>
</qhelp>
19 changes: 19 additions & 0 deletions powershell/ql/src/experimental/ConvertToSecureStringAsPlainText.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @name Use of the AsPlainText parameter in ConvertTo-SecureString
* @description Do not use the AsPlainText parameter in ConvertTo-SecureString
* @kind problem
* @problem.severity error
* @security-severity 7.0
* @precision high
* @id powershell/microsoft/public/convert-to-securestring-as-plaintext
* @tags correctness
* security
*/

import powershell

from CmdCall c
where
c.getName() = "ConvertTo-SecureString" and
c.hasNamedArgument("asplaintext")
select c, "Use of AsPlainText parameter in ConvertTo-SecureString call"
25 changes: 25 additions & 0 deletions powershell/ql/src/experimental/HardcodedComputerName.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>The names of computers should never be hard coded as this will expose sensitive information. The <code>ComputerName</code> parameter should never have a hard coded value.
</p>

</overview>
<recommendation>

<p>Remove hardcoded computer names.</p>

</recommendation>
<references>

<li>
PSScriptAnalyzer:
<a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/avoidusingcomputernamehardcoded?view=ps-modules">AvoidUsingComputerNameHardcoded</a>.
</li>
<!-- LocalWords: CWE untrusted unsanitized Runtime
-->

</references>
</qhelp>
17 changes: 17 additions & 0 deletions powershell/ql/src/experimental/HardcodedComputerName.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @name Hardcoded Computer Name
* @description Do not hardcode computer names
* @kind problem
* @problem.severity error
* @security-severity 7.0
* @precision high
* @id powershell/microsoft/public/hardcoded-computer-name
* @tags correctness
* security
*/

import powershell

from Argument a
where a.getName() = "computername" and exists(a.getValue())
select a, "ComputerName argument is hardcoded to" + a.getValue()
26 changes: 26 additions & 0 deletions powershell/ql/src/experimental/UseOfReservedCmdletChar.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
You cannot use following reserved characters in a function or cmdlet name as these can cause parsing or runtime errors.

Reserved Characters include: #,(){}[]&/\\$^;:\"'<>|?@`*%+=~
</p>

</overview>
<recommendation>

<p>Remove reserved characters from names.</p>

</recommendation>
<references>

<li>
PSScriptAnalyzer:
<a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/reservedcmdletchar?view=ps-modules">ReservedCmdletChar</a>.
</li>

</references>
</qhelp>
30 changes: 30 additions & 0 deletions powershell/ql/src/experimental/UseOfReservedCmdletChar.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @name Reserved Characters in Function Name
* @description Do not use reserved characters in function names
* @kind problem
* @problem.severity error
* @security-severity 7.0
* @precision high
* @id powershell/microsoft/public/reserved-characters-in-function-name
* @tags correctness
* security
*/

import powershell

class ReservedCharacter extends string {
ReservedCharacter() {
this = [
"!", "@", "#", "$",
"&", "*", "(", ")",
"+", "=", "{", "^",
"}", "[", "]", "|",
";", ":", "'", "\"",
"<", ">", ",", "?",
"/", "~"]
}
}

from Function f, ReservedCharacter r
where f.getName().matches("%"+ r + "%")
select f, "Function name contains a reserved character: " + r
24 changes: 24 additions & 0 deletions powershell/ql/src/experimental/UsernameOrPasswordParameter.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>To standardize command parameters, credentials should be accepted as objects of type <code>PSCredential</code>. Functions should not make use of username or password parameters.
</p>

</overview>
<recommendation>

<p>Change the parameter to type <code>PSCredential</code>.</p>

</recommendation>
<references>


<li>
PSScriptAnalyzer:
<a href="https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/avoidusingusernameandpasswordparams?view=ps-modules">AvoidUsingUsernameAndPasswordParams</a>.
</li>

</references>
</qhelp>
17 changes: 17 additions & 0 deletions powershell/ql/src/experimental/UsernameOrPasswordParameter.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @name Use of Username or Password parameter
* @description Do not use username or password parameters
* @kind problem
* @problem.severity error
* @security-severity 7.0
* @precision high
* @id powershell/microsoft/public/username-or-password-parameter
* @tags correctness
* security
*/

import powershell

from Parameter p
where p.getName().toLowerCase() = ["username", "password"]
select p, "Do not use username or password parameters."
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.ps1:2:19:2:79 | Call to ConvertTo-SecureString | Use of AsPlainText parameter in ConvertTo-SecureString call |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental/ConvertToSecureStringAsPlainText.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$UserInput = Read-Host 'Please enter your secure code'
$EncryptedInput = ConvertTo-SecureString -String $UserInput -AsPlainText -Force

$SecureUserInput = Read-Host 'Please enter your secure code' -AsSecureString
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| test.ps1:3:44:3:65 | hardcoderemotehostname | ComputerName argument is hardcoded tohardcoderemotehostname |
| test.ps1:13:44:13:64 | hardcodelocalhostname | ComputerName argument is hardcoded tohardcodelocalhostname |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental/HardcodedComputerName.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Function Invoke-MyRemoteCommand ()
{
Invoke-Command -Port 343 -ComputerName hardcoderemotehostname
}

Function Invoke-MyCommand ($ComputerName)
{
Invoke-Command -Port 343 -ComputerName $ComputerName
}

Function Invoke-MyLocalCommand ()
{
Invoke-Command -Port 343 -ComputerName hardcodelocalhostname
}

Function Invoke-MyLocalCommand ()
{
Invoke-Command -Port 343 -ComputerName $env:COMPUTERNAME
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| test.ps1:1:1:2:5 | MyFunction[1] | Function name contains a reserved character: [ |
| test.ps1:1:1:2:5 | MyFunction[1] | Function name contains a reserved character: ] |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental/UseOfReservedCmdletChar.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function MyFunction[1]
{...}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| test.ps1:6:9:7:17 | Username | Do not use username or password parameters. |
| test.ps1:8:9:9:17 | Password | Do not use username or password parameters. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
experimental/UsernameOrPasswordParameter.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Test-Script
{
[CmdletBinding()]
Param
(
[String]
$Username,
[SecureString]
$Password
)
}