Skip to content

Commit 424ba6a

Browse files
committed
refactor classes to go into existing cryptography modules, added qhelp
1 parent 5abc596 commit 424ba6a

10 files changed

Lines changed: 105 additions & 93 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ 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+
(
184+
objectName = "system.security.cryptography." + algName or
185+
objectName = algName
186+
) and
187+
isHmacAlgorithm(algName)
188+
}
189+
190+
override string getName() { result = algName }
191+
}
192+
193+
class HmacAlgorithmCreateCall extends HmacAlgorithm, DataFlow::CallNode {
194+
string algName;
195+
196+
HmacAlgorithmCreateCall() {
197+
isHmacAlgorithm(algName) and
198+
this =
199+
API::getTopLevelMember("system")
200+
.getMember("security")
201+
.getMember("cryptography")
202+
.getMember(algName)
203+
.getMember(["create", "new"])
204+
.asCall()
205+
206+
}
207+
208+
override string getName() { result = algName }
209+
}
210+
211+
class HmacAlgorithmCreateFromNameCall extends HmacAlgorithm, CryptoAlgorithmCreateFromNameCall {
212+
string algName;
213+
214+
HmacAlgorithmCreateFromNameCall() {
215+
objectName = ["", "system.security.cryptography."] + algName and
216+
isHmacAlgorithm(algName)
217+
}
218+
219+
override string getName() { result = algName }
220+
}
221+
179222
class CipherBlockModeEnum extends BlockMode {
180223
string modeName;
181224

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/microsoft/security/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."

powershell/ql/src/queries/security/cwe-328/WeakHmac.ql

Lines changed: 0 additions & 92 deletions
This file was deleted.

powershell/ql/test/query-tests/security/cwe-328/WeakHmac/WeakHmac.expected renamed to powershell/ql/test/query-tests/security/cwe-327/WeakHmac/WeakHmac.expected

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/security/cwe-327/WeakHmac.ql

powershell/ql/test/query-tests/security/cwe-328/WeakHmac/test.ps1 renamed to powershell/ql/test/query-tests/security/cwe-327/WeakHmac/test.ps1

File renamed without changes.

powershell/ql/test/query-tests/security/cwe-328/WeakHmac/WeakHmac.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)