Skip to content

Commit 021cdeb

Browse files
committed
initial query, updated .expected file
1 parent 4bdb2e2 commit 021cdeb

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @name Use of weak cryptographic hash
3+
* @description Using weak cryptographic hash algorithms like MD5 or SHA1 can compromise data integrity and security.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @security-severity 7.5
7+
* @precision high
8+
* @id powershell/weak-hash
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+
18+
class WeakHashAlgorithmObjectCreation extends DataFlow::ObjectCreationNode {
19+
WeakHashAlgorithmObjectCreation() {
20+
this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.MD5" or
21+
this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.MD5CryptoServiceProvider" or
22+
this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.SHA1" or
23+
this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.SHA1CryptoServiceProvider" or
24+
this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.SHA1Managed"
25+
}
26+
}
27+
28+
class WeakHashAlgorithmObjectCreate extends DataFlow::CallNode {
29+
WeakHashAlgorithmObjectCreate() {
30+
// System.Security.Cryptography.MD5
31+
this = API::getTopLevelMember("system")
32+
.getMember("security")
33+
.getMember("cryptography")
34+
.getMember("md5")
35+
.getMember("create")
36+
.asCall()
37+
}
38+
}
39+
40+
class ComputeHashSink extends DataFlow::Node {
41+
ComputeHashSink() {
42+
exists(DataFlow::ObjectCreationNode ocn, DataFlow::CallNode cn |
43+
(
44+
ocn.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.SHA1Managed" or
45+
ocn.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString() = "System.Security.Cryptography.SHA1CryptoServiceProvider"
46+
) and
47+
cn.getQualifier().getALocalSource() = ocn and
48+
cn.getLowerCaseName() = "computehash" and
49+
cn.getAnArgument() = this
50+
)
51+
}
52+
}
53+
54+
from DataFlow::Node sink
55+
where sink instanceof ComputeHashSink or
56+
sink instanceof WeakHashAlgorithmObjectCreation or
57+
sink instanceof WeakHashAlgorithmObjectCreate
58+
select sink, "Use of weak cryptographic hash algorithm."
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| test.ps1:4:8:4:51 | Call to create | Use of weak cryptographic hash algorithm. |
2+
| test.ps1:8:16:8:79 | Call to new-object | Use of weak cryptographic hash algorithm. |
3+
| test.ps1:16:17:16:81 | Call to new-object | Use of weak cryptographic hash algorithm. |
4+
| test.ps1:17:47:17:93 | Call to getbytes | Use of weak cryptographic hash algorithm. |
5+
| test.ps1:20:16:20:66 | Call to new-object | Use of weak cryptographic hash algorithm. |
6+
| test.ps1:21:45:21:89 | Call to getbytes | Use of weak cryptographic hash algorithm. |

0 commit comments

Comments
 (0)