Skip to content

Commit c76571c

Browse files
authored
Merge pull request #348 from microsoft/users/chanely/weak-hmac
Use of weak hmac alg
2 parents 702deb4 + 313a9c8 commit c76571c

8 files changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ predicate isSymmetricAlgorithm(string name) {
2222
]
2323
}
2424

25+
predicate isHmacAlgorithm(string name) {
26+
name =
27+
[
28+
"hmacmd5", "hmacsha1", "hmacripemd160", "hmacsha256", "hmacsha384", "hmacsha512",
29+
"hmacsha3256", "hmacsha3384", "hmacsha3512"
30+
]
31+
}
32+
2533
predicate isCipherBlockModeAlgorithm(string name) {
2634
name = ["cbc", "gcm", "ccm", "cfb", "ofb", "cfb8", "ctr", "openpgp", "xts", "eax", "siv", "ecb"]
2735
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ abstract class SymmetricAlgorithm extends CryptographicAlgorithm {
2929
}
3030
}
3131

32+
abstract class HmacAlgorithm extends CryptographicAlgorithm {
33+
final string getHmacName() {
34+
if exists(string n | n = this.getName() and isHmacAlgorithm(n))
35+
then result = this.getName()
36+
else result = unknownAlgorithm()
37+
}
38+
}
39+
3240
abstract class BlockMode extends CryptographicAlgorithm {
3341
final string getBlockModeName() {
3442
if exists(string n | n = this.getName() and isCipherBlockModeAlgorithm(n))

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,46 @@ class CipherBlockStringConstExpr extends BlockMode {
176176
override string getName() { result = modeName }
177177
}
178178

179+
class HmacAlgorithmObjectCreation extends HmacAlgorithm, CryptoAlgorithmObjectCreation {
180+
string algName;
181+
182+
HmacAlgorithmObjectCreation() {
183+
objectName = ["", "system.security.cryptography."] + algName and
184+
isHmacAlgorithm(algName)
185+
}
186+
187+
override string getName() { result = algName }
188+
}
189+
190+
class HmacAlgorithmCreateCall extends HmacAlgorithm, DataFlow::CallNode {
191+
string algName;
192+
193+
HmacAlgorithmCreateCall() {
194+
isHmacAlgorithm(algName) and
195+
this =
196+
API::getTopLevelMember("system")
197+
.getMember("security")
198+
.getMember("cryptography")
199+
.getMember(algName)
200+
.getMember(["create", "new"])
201+
.asCall()
202+
203+
}
204+
205+
override string getName() { result = algName }
206+
}
207+
208+
class HmacAlgorithmCreateFromNameCall extends HmacAlgorithm, CryptoAlgorithmCreateFromNameCall {
209+
string algName;
210+
211+
HmacAlgorithmCreateFromNameCall() {
212+
objectName = ["", "system.security.cryptography."] + algName and
213+
isHmacAlgorithm(algName)
214+
}
215+
216+
override string getName() { result = algName }
217+
}
218+
179219
class CipherBlockModeEnum extends BlockMode {
180220
string modeName;
181221

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
HMAC (Hash-based Message Authentication Code) algorithms are used to verify both the
6+
integrity and authenticity of messages. Using weak HMAC algorithms such as HMACMD5,
7+
HMACSHA1, or HMACRIPEMD160 can compromise message authentication, as the underlying
8+
hash functions have known cryptographic weaknesses.
9+
</p>
10+
</overview>
11+
<recommendation>
12+
<p>
13+
Use a strong HMAC algorithm such as HMACSHA256, HMACSHA384, or HMACSHA512. These are
14+
based on the SHA-2 family of hash functions and provide adequate security for message
15+
authentication.
16+
</p>
17+
</recommendation>
18+
19+
<references>
20+
<li>NIST, SP 800-131A: <a href="https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final">Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li>
21+
<li>CWE-327: <a href="https://cwe.mitre.org/data/definitions/327.html">Use of a Broken or Risky Cryptographic Algorithm</a>.</li>
22+
<li>CWE-328: <a href="https://cwe.mitre.org/data/definitions/328.html">Use of Weak Hash</a>.</li>
23+
</references>
24+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Use of weak HMAC algorithm
3+
* @description Using weak HMAC algorithms like HMACMD5 or HMACSHA1 can compromise message authentication.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @security-severity 7.5
7+
* @precision high
8+
* @id powershell/weak-hmac
9+
* @tags security
10+
* external/cwe/cwe-327
11+
* external/cwe/cwe-328
12+
*/
13+
14+
import powershell
15+
import semmle.code.powershell.ApiGraphs
16+
import semmle.code.powershell.dataflow.DataFlow
17+
import semmle.code.powershell.security.cryptography.Concepts
18+
19+
from HmacAlgorithm hmacAlg
20+
where not hmacAlg.getHmacName() = ["hmacsha256", "hmacsha384", "hmacsha512"]
21+
select hmacAlg, "Use of weak HMAC algorithm: " + hmacAlg.getHmacName() + ". Use HMACSHA256 or stronger."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| test.ps1:6:9:6:55 | Call to new-object | Use of weak HMAC algorithm: hmacmd5. Use HMACSHA256 or stronger. |
2+
| test.ps1:9:9:9:56 | Call to new-object | Use of weak HMAC algorithm: hmacsha1. Use HMACSHA256 or stronger. |
3+
| test.ps1:12:9:12:56 | Call to create | Use of weak HMAC algorithm: hmacmd5. Use HMACSHA256 or stronger. |
4+
| test.ps1:15:9:15:54 | Call to new | Use of weak HMAC algorithm: hmacsha1. Use HMACSHA256 or stronger. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-327/WeakHmac.ql
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ===================================================================
2+
# ========== TRUE POSITIVES (should trigger alert) ==================
3+
# ===================================================================
4+
5+
# --- Case 1: HMACMD5 via New-Object ---
6+
$hmac = New-Object System.Security.Cryptography.HMACMD5 # BAD
7+
8+
# --- Case 2: HMACSHA1 via New-Object ---
9+
$hmac = New-Object System.Security.Cryptography.HMACSHA1 # BAD
10+
11+
# --- Case 3: HMACMD5 via static Create ---
12+
$hmac = [System.Security.Cryptography.HMACMD5]::Create() # BAD
13+
14+
# --- Case 4: HMACSHA1 via ::new() ---
15+
$hmac = [System.Security.Cryptography.HMACSHA1]::new() # BAD
16+
17+
# ===================================================================
18+
# ========== TRUE NEGATIVES (should NOT trigger alert) ==============
19+
# ===================================================================
20+
21+
# --- Safe: HMACSHA256 ---
22+
$hmac = New-Object System.Security.Cryptography.HMACSHA256 # GOOD
23+
24+
# --- Safe: HMACSHA384 ---
25+
$hmac = [System.Security.Cryptography.HMACSHA384]::new() # GOOD
26+
27+
# --- Safe: HMACSHA512 ---
28+
$hmac = [System.Security.Cryptography.HMACSHA512]::Create() # GOOD

0 commit comments

Comments
 (0)