|
| 1 | +/** |
| 2 | + * @name Use of a hash function without a salt |
| 3 | + * @description Hashed passwords without a salt are vulnerable to dictionary attacks. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity warning |
| 6 | + * @precision low |
| 7 | + * @id java/hash-without-salt |
| 8 | + * @tags security |
| 9 | + * external/cwe-759 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.dataflow.TaintTracking |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/** |
| 17 | + * Gets a regular expression for matching common names of variables |
| 18 | + * that indicate the value being held is a password. |
| 19 | + */ |
| 20 | +string getPasswordRegex() { result = "(?i).*pass(wd|word|code|phrase).*" } |
| 21 | + |
| 22 | +/** Finds variables that hold password information judging by their names. */ |
| 23 | +class PasswordVarExpr extends VarAccess { |
| 24 | + PasswordVarExpr() { |
| 25 | + exists(string name | name = this.getVariable().getName().toLowerCase() | |
| 26 | + name.regexpMatch(getPasswordRegex()) and not name.matches("%hash%") // Exclude variable names such as `passwordHash` since their values were already hashed |
| 27 | + ) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** Holds if `Expr` e is a direct or indirect operand of `ae`. */ |
| 32 | +predicate hasAddExprAncestor(AddExpr ae, Expr e) { ae.getAnOperand+() = e } |
| 33 | + |
| 34 | +/** The Java class `java.security.MessageDigest`. */ |
| 35 | +class MessageDigest extends RefType { |
| 36 | + MessageDigest() { this.hasQualifiedName("java.security", "MessageDigest") } |
| 37 | +} |
| 38 | + |
| 39 | +/** The method call `MessageDigest.getInstance(...)` */ |
| 40 | +class MDConstructor extends StaticMethodAccess { |
| 41 | + MDConstructor() { |
| 42 | + exists(Method m | m = this.getMethod() | |
| 43 | + m.getDeclaringType() instanceof MessageDigest and |
| 44 | + m.hasName("getInstance") |
| 45 | + ) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** The method `digest()` declared in `java.security.MessageDigest`. */ |
| 50 | +class MDDigestMethod extends Method { |
| 51 | + MDDigestMethod() { |
| 52 | + this.getDeclaringType() instanceof MessageDigest and |
| 53 | + this.hasName("digest") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** The method `update()` declared in `java.security.MessageDigest`. */ |
| 58 | +class MDUpdateMethod extends Method { |
| 59 | + MDUpdateMethod() { |
| 60 | + this.getDeclaringType() instanceof MessageDigest and |
| 61 | + this.hasName("update") |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** The hashing method that could taint the input. */ |
| 66 | +class MDHashMethodAccess extends MethodAccess { |
| 67 | + MDHashMethodAccess() { |
| 68 | + ( |
| 69 | + this.getMethod() instanceof MDDigestMethod or |
| 70 | + this.getMethod() instanceof MDUpdateMethod |
| 71 | + ) and |
| 72 | + this.getNumArgument() != 0 |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Holds if `MethodAccess` ma is a method access of `MDHashMethodAccess` or |
| 78 | + * invokes a method access of `MDHashMethodAccess` directly or indirectly. |
| 79 | + */ |
| 80 | +predicate isHashAccess(MethodAccess ma) { |
| 81 | + ma instanceof MDHashMethodAccess |
| 82 | + or |
| 83 | + exists(MethodAccess mca | |
| 84 | + ma.getMethod().calls(mca.getMethod()) and |
| 85 | + isHashAccess(mca) and |
| 86 | + DataFlow::localExprFlow(ma.getMethod().getAParameter().getAnAccess(), mca.getAnArgument()) |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Holds if there is a second method access that satisfies `isHashAccess` whose qualifier or argument |
| 92 | + * is the same as the method call `ma` that satisfies `isHashAccess`. |
| 93 | + */ |
| 94 | +predicate hasAnotherHashCall(MethodAccess ma) { |
| 95 | + isHashAccess(ma) and |
| 96 | + exists(MethodAccess ma2, VarAccess va | |
| 97 | + ma2 != ma and |
| 98 | + isHashAccess(ma2) and |
| 99 | + not va.getVariable().getType() instanceof PrimitiveType and |
| 100 | + ( |
| 101 | + ma.getQualifier() = va and |
| 102 | + ma2.getQualifier() = va.getVariable().getAnAccess() |
| 103 | + or |
| 104 | + ma.getQualifier() = va and |
| 105 | + ma2.getAnArgument() = va.getVariable().getAnAccess() |
| 106 | + or |
| 107 | + ma.getAnArgument() = va and |
| 108 | + ma2.getQualifier() = va.getVariable().getAnAccess() |
| 109 | + or |
| 110 | + ma.getAnArgument() = va and |
| 111 | + ma2.getAnArgument() = va.getVariable().getAnAccess() |
| 112 | + ) |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Holds if `MethodAccess` ma is part of a call graph that satisfies `isHashAccess` |
| 118 | + * but is not at the top of the call hierarchy. |
| 119 | + */ |
| 120 | +predicate hasHashAncestor(MethodAccess ma) { |
| 121 | + exists(MethodAccess mpa | |
| 122 | + mpa.getMethod().calls(ma.getMethod()) and |
| 123 | + isHashAccess(mpa) and |
| 124 | + DataFlow::localExprFlow(mpa.getMethod().getAParameter().getAnAccess(), ma.getAnArgument()) |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +/** Holds if `MethodAccess` ma is a hashing call without a sibling node making another hashing call. */ |
| 129 | +predicate isSingleHashMethodCall(MethodAccess ma) { |
| 130 | + isHashAccess(ma) and not hasAnotherHashCall(ma) |
| 131 | +} |
| 132 | + |
| 133 | +/** Holds if `MethodAccess` ma is a single hashing call that is not invoked by a wrapper method. */ |
| 134 | +predicate isSink(MethodAccess ma) { isSingleHashMethodCall(ma) and not hasHashAncestor(ma) } |
| 135 | + |
| 136 | +/** Sink of hashing calls. */ |
| 137 | +class HashWithoutSaltSink extends DataFlow::ExprNode { |
| 138 | + HashWithoutSaltSink() { |
| 139 | + exists(MethodAccess ma | |
| 140 | + this.asExpr() = ma.getAnArgument() and |
| 141 | + isSink(ma) |
| 142 | + ) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Taint configuration tracking flow from an expression whose name suggests it holds password data |
| 148 | + * to a method call that generates a hash without a salt. |
| 149 | + */ |
| 150 | +class HashWithoutSaltConfiguration extends TaintTracking::Configuration { |
| 151 | + HashWithoutSaltConfiguration() { this = "HashWithoutSaltConfiguration" } |
| 152 | + |
| 153 | + override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof PasswordVarExpr } |
| 154 | + |
| 155 | + override predicate isSink(DataFlow::Node sink) { sink instanceof HashWithoutSaltSink } |
| 156 | + |
| 157 | + /** |
| 158 | + * Holds if a password is concatenated with a salt then hashed together through the call `System.arraycopy(password.getBytes(), ...)`, for example, |
| 159 | + * `System.arraycopy(password.getBytes(), 0, allBytes, 0, password.getBytes().length);` |
| 160 | + * `System.arraycopy(salt, 0, allBytes, password.getBytes().length, salt.length);` |
| 161 | + * `byte[] messageDigest = md.digest(allBytes);` |
| 162 | + * Or the password is concatenated with a salt as a string. |
| 163 | + */ |
| 164 | + override predicate isSanitizer(DataFlow::Node node) { |
| 165 | + exists(MethodAccess ma | |
| 166 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.lang", "System") and |
| 167 | + ma.getMethod().hasName("arraycopy") and |
| 168 | + ma.getArgument(0) = node.asExpr() |
| 169 | + ) // System.arraycopy(password.getBytes(), ...) |
| 170 | + or |
| 171 | + exists(AddExpr e | hasAddExprAncestor(e, node.asExpr())) // password+salt |
| 172 | + or |
| 173 | + exists(ConditionalExpr ce | ce.getAChildExpr() = node.asExpr()) // useSalt?password+":"+salt:password |
| 174 | + or |
| 175 | + exists(MethodAccess ma | |
| 176 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.lang", "StringBuilder") and |
| 177 | + ma.getMethod().hasName("append") and |
| 178 | + ma.getArgument(0) = node.asExpr() // stringBuilder.append(password).append(salt) |
| 179 | + ) |
| 180 | + or |
| 181 | + exists(MethodAccess ma | |
| 182 | + ma.getQualifier().(VarAccess).getVariable().getType() instanceof Interface and |
| 183 | + ma.getAnArgument() = node.asExpr() // Method access of interface type variables requires runtime determination thus not handled |
| 184 | + ) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +from DataFlow::PathNode source, DataFlow::PathNode sink, HashWithoutSaltConfiguration cc |
| 189 | +where cc.hasFlowPath(source, sink) |
| 190 | +select sink, source, sink, "$@ is hashed without a salt.", source, "The password" |
0 commit comments