|
| 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." |
0 commit comments