|
| 1 | +/** |
| 2 | + * @id java/hash-without-salt |
| 3 | + * @name Use of a One-Way Hash without a Salt |
| 4 | + * @description Hashed passwords without a salt are vulnerable to dictionary attacks. |
| 5 | + * @kind path-problem |
| 6 | + * @tags security |
| 7 | + * external/cwe-759 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.dataflow.TaintTracking |
| 12 | +import DataFlow::PathGraph |
| 13 | + |
| 14 | +/** The Java class `java.security.MessageDigest` */ |
| 15 | +class MessageDigest extends RefType { |
| 16 | + MessageDigest() { this.hasQualifiedName("java.security", "MessageDigest") } |
| 17 | +} |
| 18 | + |
| 19 | +/** The method `digest()` declared in `java.security.MessageDigest`. */ |
| 20 | +class MDDigestMethod extends Method { |
| 21 | + MDDigestMethod() { |
| 22 | + getDeclaringType() instanceof MessageDigest and |
| 23 | + hasName("digest") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** The method `update()` declared in `java.security.MessageDigest`. */ |
| 28 | +class MDUpdateMethod extends Method { |
| 29 | + MDUpdateMethod() { |
| 30 | + getDeclaringType() instanceof MessageDigest and |
| 31 | + hasName("update") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Gets a regular expression for matching common names of variables that indicate the value being held is a password. |
| 37 | + */ |
| 38 | +string getPasswordRegex() { result = "(?i).*pass(wd|word|code|phrase).*" } |
| 39 | + |
| 40 | +/** Finds variables that hold password information judging by their names. */ |
| 41 | +class PasswordVarExpr extends Expr { |
| 42 | + PasswordVarExpr() { |
| 43 | + exists(Variable v | this = v.getAnAccess() | v.getName().regexpMatch(getPasswordRegex())) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** Taint configuration tracking flow from an expression whose name suggests it holds password data to a method call that generates a hash without a salt. */ |
| 48 | +class HashWithoutSaltConfiguration extends TaintTracking::Configuration { |
| 49 | + HashWithoutSaltConfiguration() { this = "HashWithoutSaltConfiguration" } |
| 50 | + |
| 51 | + override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof PasswordVarExpr } |
| 52 | + |
| 53 | + override predicate isSink(DataFlow::Node sink) { |
| 54 | + exists( |
| 55 | + MethodAccess mda, MethodAccess mua // invoke `md.digest()` with only one call of `md.update(password)`, that is, without the call of `md.update(digest)` |
| 56 | + | |
| 57 | + sink.asExpr() = mda.getQualifier() and |
| 58 | + mda.getMethod() instanceof MDDigestMethod and |
| 59 | + mda.getNumArgument() = 0 and // md.digest() |
| 60 | + mua.getMethod() instanceof MDUpdateMethod and // md.update(password) |
| 61 | + mua.getQualifier() = mda.getQualifier().(VarAccess).getVariable().getAnAccess() and |
| 62 | + not exists(MethodAccess mua2 | |
| 63 | + mua2.getMethod() instanceof MDUpdateMethod and // md.update(salt) |
| 64 | + mua2.getQualifier() = mua.getQualifier().(VarAccess).getVariable().getAnAccess() and |
| 65 | + mua2 != mua |
| 66 | + ) |
| 67 | + ) |
| 68 | + or |
| 69 | + // invoke `md.digest(password)` without another call of `md.update(salt)` |
| 70 | + exists(MethodAccess mda | |
| 71 | + sink.asExpr() = mda and |
| 72 | + mda.getMethod() instanceof MDDigestMethod and // md.digest(password) |
| 73 | + mda.getNumArgument() = 1 and |
| 74 | + not exists(MethodAccess mua | |
| 75 | + mua.getMethod() instanceof MDUpdateMethod and // md.update(salt) |
| 76 | + mua.getQualifier() = mda.getQualifier().(VarAccess).getVariable().getAnAccess() |
| 77 | + ) |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + /** Holds for additional steps such as `passwordStr.getBytes()` */ |
| 82 | + override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 83 | + exists(MethodAccess ma | |
| 84 | + pred.asExpr() = ma.getAnArgument() and |
| 85 | + (succ.asExpr() = ma or succ.asExpr() = ma.getQualifier()) |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +from DataFlow::PathNode source, DataFlow::PathNode sink, HashWithoutSaltConfiguration c |
| 91 | +where c.hasFlowPath(source, sink) |
| 92 | +select sink.getNode(), source, sink, "$@ is hashed without a salt.", source.getNode(), |
| 93 | + "The password" |
0 commit comments