Skip to content

Commit 93cf5b8

Browse files
committed
Weak Hashing Property initial query
1 parent dc9c538 commit 93cf5b8

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/** Provides classes and predicates to reason about property files and weak hashing algorithms. */
2+
3+
import java
4+
private import semmle.code.configfiles.ConfigFiles
5+
private import semmle.code.java.dataflow.DataFlow
6+
private import semmle.code.java.dataflow.TaintTracking
7+
private import semmle.code.java.security.Encryption
8+
private import semmle.code.java.frameworks.Properties
9+
private import semmle.code.java.dataflow.RangeUtils
10+
11+
class GetPropertyMethodAccess extends MethodAccess {
12+
GetPropertyMethodAccess() { this.getMethod() instanceof PropertiesGetPropertyMethod }
13+
14+
private ConfigPair getPair() {
15+
this.getArgument(0).(ConstantStringExpr).getStringValue() = result.getNameElement().getName()
16+
}
17+
18+
string getValue() {
19+
result = this.getPair().getValueElement().getValue() or
20+
result = this.getArgument(1).(ConstantStringExpr).getStringValue()
21+
}
22+
}
23+
24+
string getWeakHashingAlgorithm(DataFlow::Node node) {
25+
/**
26+
* Get the name of the weak cryptographic algorithm represented by `node`.
27+
*/
28+
string getWeakHashingAlgorithmName(DataFlow::Node node) {
29+
exists(MethodAccess ma, ConfigPair pair |
30+
node.asExpr() = ma and ma.getMethod() instanceof PropertiesGetPropertyMethod
31+
|
32+
ma.getArgument(0).(ConstantStringExpr).getStringValue() = pair.getNameElement().getName() and
33+
pair.getValueElement().getValue() = result and
34+
not pair.getValueElement().getValue().regexpMatch(getSecureAlgorithmRegex())
35+
)
36+
}
37+
38+
/**
39+
* Dataflow configuration from a configuration pair in a properties file to the use of a cryptographic algorithm.
40+
*/
41+
module InsecureAlgorithmPropertyConfig implements DataFlow::ConfigSig {
42+
predicate isSource(DataFlow::Node n) {
43+
exists(MethodAccess ma, ConfigPair pair |
44+
n.asExpr() = ma and ma.getMethod() instanceof PropertiesGetPropertyMethod
45+
|
46+
ma.getArgument(0).(ConstantStringExpr).getStringValue() = pair.getNameElement().getName() and
47+
not pair.getValueElement().getValue().regexpMatch(getSecureAlgorithmRegex())
48+
)
49+
}
50+
51+
predicate isSink(DataFlow::Node n) { n.asExpr() = any(CryptoAlgoSpec c).getAlgoSpec() }
52+
}
53+
54+
/**
55+
* Dataflow from a configuration pair in a properties file to the use of a cryptographic algorithm.
56+
*/
57+
module InsecureAlgorithmPropertyFlow = TaintTracking::Global<InsecureAlgorithmPropertyConfig>;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<include src="../CWE-327/BrokenCryptoAlgorithm.qhelp" /></qhelp>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Weak Hashing Property
3+
* @description Using weak cryptographic algorithms can allow an attacker to compromise security.
4+
* @id java/weak-hashing-property
5+
* @kind path-problem
6+
* @problem.severity error
7+
* @security-severity 7.5
8+
* @precision medium
9+
* @tags security
10+
* external/cwe/cwe-328
11+
*/
12+
13+
import java
14+
import semmle.code.java.security.WeakHashingAlgorithmPropertyQuery
15+
import InsecureAlgorithmPropertyFlow::PathGraph
16+
17+
from InsecureAlgorithmPropertyFlow::PathNode source, InsecureAlgorithmPropertyFlow::PathNode sink
18+
where InsecureAlgorithmPropertyFlow::flowPath(source, sink)
19+
select sink.getNode(), sink, source, "The $@ algorithm is insecure.", source,
20+
getWeakHashingAlgorithmName(source.getNode())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: newQuery
3+
---
4+
* Added the `java/weak-hashing-property` query to detect the use of weak cryptographic algorithms where the algorithm name comes from a `.properties` configuration file.
5+

0 commit comments

Comments
 (0)