Skip to content

Commit 32ea6a8

Browse files
authored
Merge pull request #338 from microsoft/copilot/update-powershell-weak-hashes
Add Get-FileHash weak hash algorithm detection to powershell/weak-hashes query
2 parents 4f8cfb3 + a3f6a98 commit 32ea6a8

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

powershell/ql/lib/semmle/code/powershell/security/cryptography/CryptographyModule.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ class HashAlgorithmCreateFromNameCall extends HashAlgorithm, CryptoAlgorithmCrea
100100
override string getName() { result = algName }
101101
}
102102

103+
class GetFileHashWeakAlgorithm extends HashAlgorithm, DataFlow::CallNode {
104+
string algName;
105+
106+
GetFileHashWeakAlgorithm() {
107+
this.matchesName("Get-FileHash") and
108+
algName = this.getNamedArgument("algorithm").asExpr().getValue().asString().toLowerCase() and
109+
isHashingAlgorithm(algName)
110+
}
111+
112+
override string getName() { result = algName }
113+
}
114+
103115
class SymmetricAlgorithmObjectCreation extends SymmetricAlgorithm, CryptoAlgorithmObjectCreation {
104116
string algName;
105117

powershell/ql/src/queries/security/cwe-327/WeakHashes.qhelp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
or SHA-512. For password hashing, consider using a specialized password hashing function
1818
like PBKDF2, bcrypt, or Argon2.
1919
</p>
20+
<p>
21+
When using the <code>Get-FileHash</code> cmdlet, avoid specifying <code>-Algorithm MD5</code>
22+
or <code>-Algorithm SHA1</code>. Instead, use <code>-Algorithm SHA256</code> (the default),
23+
<code>-Algorithm SHA384</code>, or <code>-Algorithm SHA512</code>.
24+
</p>
2025
</recommendation>
2126

2227
<references>

powershell/ql/test/query-tests/security/cwe-327/WeakHashes/WeakHashes.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
| test.ps1:19:6:19:100 | Call to createfromname | Use of weak cryptographic hash algorithm: md5. |
77
| test.ps1:20:6:20:72 | Call to createfromname | Use of weak cryptographic hash algorithm: sha1. |
88
| test.ps1:21:6:21:101 | Call to createfromname | Use of weak cryptographic hash algorithm: sha1. |
9+
| test.ps1:25:1:25:47 | Call to get-filehash | Use of weak cryptographic hash algorithm: md5. |
10+
| test.ps1:28:1:28:48 | Call to get-filehash | Use of weak cryptographic hash algorithm: sha1. |

powershell/ql/test/query-tests/security/cwe-327/WeakHashes/test.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ $o = [System.Security.Cryptography.CryptoConfig]::CreateFromName("SHA1")
2121
$o = [System.Security.Cryptography.CryptoConfig]::CreateFromName("System.Security.Cryptography.SHA1")
2222

2323

24+
# BAD: Using Get-FileHash with MD5
25+
Get-FileHash -Path "C:\file.txt" -Algorithm MD5
26+
27+
# BAD: Using Get-FileHash with SHA1
28+
Get-FileHash -Path "C:\file.txt" -Algorithm SHA1
29+
2430
# ---------------------------------------------------------
2531
# GOOD: Safe usage of cryptographically secure algorithms
2632
# ---------------------------------------------------------
@@ -44,3 +50,15 @@ $sha384Hash = $sha384.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("passwor
4450
# GOOD: Using SHA512
4551
$sha512 = [System.Security.Cryptography.SHA512]::Create()
4652
$sha512Hash = $sha512.ComputeHash([System.Text.Encoding]::UTF8.GetBytes("password123"))
53+
54+
# GOOD: Using Get-FileHash with SHA256
55+
Get-FileHash -Path "C:\file.txt" -Algorithm SHA256
56+
57+
# GOOD: Using Get-FileHash with SHA384
58+
Get-FileHash -Path "C:\file.txt" -Algorithm SHA384
59+
60+
# GOOD: Using Get-FileHash with SHA512
61+
Get-FileHash -Path "C:\file.txt" -Algorithm SHA512
62+
63+
# GOOD: Using Get-FileHash without specifying algorithm (defaults to SHA256)
64+
Get-FileHash -Path "C:\file.txt"

0 commit comments

Comments
 (0)