|
| 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 error |
| 6 | + * @id cs/hash-without-salt |
| 7 | + * @tags security |
| 8 | + * external/cwe-759 |
| 9 | + */ |
| 10 | + |
| 11 | +import csharp |
| 12 | +import semmle.code.csharp.dataflow.DataFlow2 |
| 13 | +import semmle.code.csharp.dataflow.TaintTracking |
| 14 | +import semmle.code.csharp.dataflow.TaintTracking2 |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** The C# class `Windows.Security.Cryptography.Core.HashAlgorithmProvider`. */ |
| 18 | +class HashAlgorithmProvider extends RefType { |
| 19 | + HashAlgorithmProvider() { |
| 20 | + this.hasQualifiedName("Windows.Security.Cryptography.Core", "HashAlgorithmProvider") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** The C# class `System.Security.Cryptography.HashAlgorithm`. */ |
| 25 | +class HashAlgorithm extends RefType { |
| 26 | + HashAlgorithm() { this.hasQualifiedName("System.Security.Cryptography", "HashAlgorithm") } |
| 27 | +} |
| 28 | + |
| 29 | +/** The C# class `System.Security.Cryptography.KeyedHashAlgorithm`. */ |
| 30 | +class KeyedHashAlgorithm extends RefType { |
| 31 | + KeyedHashAlgorithm() { |
| 32 | + this.hasQualifiedName("System.Security.Cryptography", "KeyedHashAlgorithm") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * The method `ComputeHash()`, `ComputeHashAsync`, `TryComputeHash`, `HashData`, or |
| 38 | + * `TryHashData` declared in `System.Security.Cryptography.HashAlgorithm` and the method |
| 39 | + * `HashData()` declared in `Windows.Security.Cryptography.Core.HashAlgorithmProvider`. |
| 40 | + */ |
| 41 | +class HashMethod extends Method { |
| 42 | + HashMethod() { |
| 43 | + this.getDeclaringType().getABaseType*() instanceof HashAlgorithm and |
| 44 | + this.getName().matches(["%ComputeHash%", "%HashData"]) |
| 45 | + or |
| 46 | + this.getDeclaringType().getABaseType*() instanceof HashAlgorithmProvider and |
| 47 | + this.hasName("HashData") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Gets a regular expression for matching common names of variables that indicate the |
| 53 | + * value being held is a password. |
| 54 | + */ |
| 55 | +string getPasswordRegex() { result = "(?i)pass(wd|word|code|phrase)" } |
| 56 | + |
| 57 | +/** Finds variables that hold password information judging by their names. */ |
| 58 | +class PasswordVarExpr extends Expr { |
| 59 | + PasswordVarExpr() { |
| 60 | + exists(Variable v | this = v.getAnAccess() | v.getName().regexpMatch(getPasswordRegex())) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Holds if `mc` is a hashing method call or invokes a hashing method call |
| 66 | + * directly or indirectly. |
| 67 | + */ |
| 68 | +predicate isHashCall(MethodCall mc) { |
| 69 | + mc.getTarget() instanceof HashMethod |
| 70 | + or |
| 71 | + exists(MethodCall mcc | |
| 72 | + mc.getTarget().calls(mcc.getTarget()) and |
| 73 | + isHashCall(mcc) and |
| 74 | + DataFlow::localExprFlow(mc.getTarget().getAParameter().getAnAccess(), mcc.getAnArgument()) |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +/** Holds if there is another hashing method call. */ |
| 79 | +predicate hasAnotherHashCall(MethodCall mc) { |
| 80 | + exists(MethodCall mc2, DataFlow2::Node src, DataFlow2::Node sink | |
| 81 | + isHashCall(mc2) and |
| 82 | + mc2 != mc and |
| 83 | + ( |
| 84 | + src.asExpr() = mc.getQualifier() or |
| 85 | + src.asExpr() = mc.getAnArgument() or |
| 86 | + src.asExpr() = mc |
| 87 | + ) and |
| 88 | + ( |
| 89 | + sink.asExpr() = mc2.getQualifier() or |
| 90 | + sink.asExpr() = mc2.getAnArgument() |
| 91 | + ) and |
| 92 | + DataFlow::localFlow(src, sink) |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +/** Holds if a password hash without salt is further processed in another method call. */ |
| 97 | +predicate hasFurtherProcessing(MethodCall mc) { |
| 98 | + mc.getTarget().fromLibrary() and |
| 99 | + ( |
| 100 | + mc.getTarget().hasQualifiedName("System.Array", "Copy") or // Array.Copy(passwordHash, 0, password.Length), 0, key, 0, keyLen); |
| 101 | + mc.getTarget().hasQualifiedName("System.String", "Concat") or // string.Concat(passwordHash, saltkey) |
| 102 | + mc.getTarget().hasQualifiedName("System.Buffer", "BlockCopy") or // Buffer.BlockCopy(passwordHash, 0, allBytes, 0, 20) |
| 103 | + mc.getTarget().hasQualifiedName("System.String", "Format") // String.Format("{0}:{1}:{2}", username, salt, password) |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Holds if `mc` is part of a call graph that satisfies `isHashCall` but is not at the |
| 109 | + * top of the call hierarchy. |
| 110 | + */ |
| 111 | +predicate hasHashAncestor(MethodCall mc) { |
| 112 | + exists(MethodCall mpc | |
| 113 | + mpc.getTarget().calls(mc.getTarget()) and |
| 114 | + isHashCall(mpc) and |
| 115 | + DataFlow::localExprFlow(mpc.getTarget().getAParameter().getAnAccess(), mc.getAnArgument()) |
| 116 | + ) |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Taint configuration tracking flow from an expression whose name suggests it holds |
| 121 | + * password data to a method call that generates a hash without a salt. |
| 122 | + */ |
| 123 | +class HashWithoutSaltConfiguration extends TaintTracking::Configuration { |
| 124 | + HashWithoutSaltConfiguration() { this = "HashWithoutSaltConfiguration" } |
| 125 | + |
| 126 | + override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof PasswordVarExpr } |
| 127 | + |
| 128 | + override predicate isSink(DataFlow::Node sink) { |
| 129 | + exists(MethodCall mc | |
| 130 | + sink.asExpr() = mc.getArgument(0) and |
| 131 | + isHashCall(mc) and |
| 132 | + not hasAnotherHashCall(mc) and |
| 133 | + not hasHashAncestor(mc) and |
| 134 | + not exists(MethodCall mmc | |
| 135 | + hasFurtherProcessing(mmc) and |
| 136 | + DataFlow::localExprFlow(mc, mmc.getAnArgument()) |
| 137 | + ) and |
| 138 | + not exists(Call c | |
| 139 | + ( |
| 140 | + c.getTarget().getDeclaringType().getABaseType*() instanceof HashAlgorithm or |
| 141 | + c.getTarget() |
| 142 | + .getDeclaringType() |
| 143 | + .getABaseType*() |
| 144 | + .hasQualifiedName("System.Security.Cryptography", "DeriveBytes") |
| 145 | + ) and |
| 146 | + DataFlow::localExprFlow(mc, c.getAnArgument()) |
| 147 | + ) |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 152 | + exists(MethodCall mc | |
| 153 | + mc.getTarget() |
| 154 | + .hasQualifiedName("Windows.Security.Cryptography.CryptographicBuffer", |
| 155 | + "ConvertStringToBinary") and |
| 156 | + mc.getArgument(0) = node1.asExpr() and |
| 157 | + mc = node2.asExpr() |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Holds if a password is concatenated with a salt then hashed together through calls such as `System.Array.CopyTo()`, for example, |
| 163 | + * `byte[] rawSalted = new byte[passBytes.Length + salt.Length];` |
| 164 | + * `passBytes.CopyTo(rawSalted, 0);` |
| 165 | + * `salt.CopyTo(rawSalted, passBytes.Length);` |
| 166 | + * `byte[] saltedPassword = sha256.ComputeHash(rawSalted);` |
| 167 | + * Or the password is concatenated with a salt as a string. |
| 168 | + */ |
| 169 | + override predicate isSanitizer(DataFlow::Node node) { |
| 170 | + exists(MethodCall mc | |
| 171 | + hasFurtherProcessing(mc) and |
| 172 | + mc.getAnArgument() = node.asExpr() |
| 173 | + ) |
| 174 | + or |
| 175 | + exists(AddExpr e | node.asExpr() = e.getAnOperand()) // password+salt |
| 176 | + or |
| 177 | + exists(InterpolatedStringExpr e | node.asExpr() = e.getAnInsert()) |
| 178 | + or |
| 179 | + exists(Call c | |
| 180 | + c.getTarget() |
| 181 | + .getDeclaringType() |
| 182 | + .getABaseType*() |
| 183 | + .hasQualifiedName("System.Security.Cryptography", "DeriveBytes") |
| 184 | + ) |
| 185 | + or |
| 186 | + // a salt or key is included in subclasses of `KeyedHashAlgorithm` |
| 187 | + exists(MethodCall mc, Assignment a, ObjectCreation oc | |
| 188 | + a.getRValue() = oc and |
| 189 | + oc.getObjectType().getABaseType+() instanceof KeyedHashAlgorithm and |
| 190 | + mc.getTarget() instanceof HashMethod and |
| 191 | + a.getLValue() = mc.getQualifier().(VariableAccess).getTarget().getAnAccess() and |
| 192 | + mc.getArgument(0) = node.asExpr() |
| 193 | + ) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +from DataFlow::PathNode source, DataFlow::PathNode sink, HashWithoutSaltConfiguration c |
| 198 | +where c.hasFlowPath(source, sink) |
| 199 | +select sink.getNode(), source, sink, "$@ is hashed without a salt.", source.getNode(), |
| 200 | + "The password" |
0 commit comments