/
DBScorer.m
427 lines (373 loc) · 14.3 KB
/
DBScorer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// DBScorer.m
// Zxcvbn
//
// Created by Leah Culver on 2/9/14.
// Copyright (c) 2014 Dropbox. All rights reserved.
//
#import "DBScorer.h"
#import "DBMatcher.h"
@implementation DBScorer
- (DBResult *)minimumEntropyMatchSequence:(NSString *)password matches:(NSArray *)matches
{
/* minimum entropy search
takes a list of overlapping matches, returns the non-overlapping sublist with
minimum entropy. O(nm) dp alg for length-n password with m candidate matches.
*/
float bruteforceCardinality = [self calcBruteforceCardinality:password]; // e.g. 26 for lowercase
NSMutableArray *upToK = [[NSMutableArray alloc] init]; // minimum entropy up to k.
NSMutableArray *backpointers = [[NSMutableArray alloc] init]; // for the optimal sequence of matches up to k, holds the final match (match.j == k). null means the sequence ends w/ a brute-force character.
for (int k = 0; k < [password length]; k++) {
// starting scenario to try and beat: adding a brute-force character to the minimum entropy sequence at k-1.
[upToK insertObject:[NSNumber numberWithFloat:[get(upToK, k-1) floatValue] + lg(bruteforceCardinality)] atIndex:k];
[backpointers insertObject:[NSNull null] atIndex:k];
for (DBMatch *match in matches) {
NSUInteger i = match.i;
NSUInteger j = match.j;
if (j != k) {
continue;
}
// see if best entropy up to i-1 + entropy of this match is less than the current minimum at j.
float candidateEntropy = [get(upToK, (int)i-1) floatValue] + [self calcEntropy:match];
if (candidateEntropy < [[upToK objectAtIndex:j] floatValue]) {
[upToK insertObject:[NSNumber numberWithFloat:candidateEntropy] atIndex:j];
[backpointers insertObject:match atIndex:j];
}
}
}
// walk backwards and decode the best sequence
NSMutableArray *matchSequence = [[NSMutableArray alloc] init];
NSInteger k = [password length] - 1;
while (k >= 0) {
DBMatch *match = [backpointers objectAtIndex:k];
if (![match isEqual:[NSNull null]]) {
[matchSequence addObject:match];
k = match.i - 1;
} else {
k -= 1;
}
}
matchSequence = [[NSMutableArray alloc] initWithArray:[[matchSequence reverseObjectEnumerator] allObjects]];
// fill in the blanks between pattern matches with bruteforce "matches"
// that way the match sequence fully covers the password: match1.j == match2.i - 1 for every adjacent match1, match2.
DBMatch* (^makeBruteforceMatch)(NSUInteger i, NSUInteger j) = ^ DBMatch* (NSUInteger i, NSUInteger j) {
DBMatch *match = [[DBMatch alloc] init];
match.pattern = @"bruteforce";
match.i = i;
match.j = j;
match.token = [password substringWithRange:NSMakeRange(i, j - i + 1)];
match.entropy = lg(pow(bruteforceCardinality, j - i + 1));
match.cardinality = bruteforceCardinality;
return match;
};
k = 0;
NSMutableArray *matchSequenceCopy = [[NSMutableArray alloc] init];
for (DBMatch *match in matchSequence) {
NSUInteger i = match.i;
NSUInteger j = match.j;
if (i - k > 0) {
[matchSequenceCopy addObject:makeBruteforceMatch(k, i-1)];
}
k = j + 1;
[matchSequenceCopy addObject:match];
}
if (k < [password length]) {
[matchSequenceCopy addObject:makeBruteforceMatch(k, [password length] - 1)];
matchSequence = matchSequenceCopy;
}
float minEntropy = 0.0;
if ([password length] > 0) { // corner case is for an empty password ''
minEntropy = [[upToK objectAtIndex:[password length] - 1] floatValue];
}
float crackTime = [self entropyToCrackTime:minEntropy];
// final result object
DBResult *result = [[DBResult alloc] init];
result.password = password;
result.entropy = roundToXDigits(minEntropy, 3);
result.matchSequence = matchSequence;
result.crackTime = roundToXDigits(crackTime, 3);
result.crackTimeDisplay = [self displayTime:crackTime];
result.score = [self crackTimeToScore:crackTime];
return result;
}
- (float)entropyToCrackTime:(float)entropy
{
/*
threat model -- stolen hash catastrophe scenario
assumes:
* passwords are stored as salted hashes, different random salt per user.
(making rainbow attacks infeasable.)
* hashes and salts were stolen. attacker is guessing passwords at max rate.
* attacker has several CPUs at their disposal.
* for a hash function like bcrypt/scrypt/PBKDF2, 10ms per guess is a safe lower bound.
* (usually a guess would take longer -- this assumes fast hardware and a small work factor.)
* adjust for your site accordingly if you use another hash function, possibly by
* several orders of magnitude!
*/
float singleGuess = .010;
float numAttackers = 100; // number of cores guessing in parallel.
float secondsPerGuess = singleGuess / numAttackers;
return .5 * pow(2, entropy) * secondsPerGuess; // average, not total
}
- (int)crackTimeToScore:(float)seconds
{
if (seconds < pow(10, 2)) {
return 0;
}
if (seconds < pow(10, 4)) {
return 1;
}
if (seconds < pow(10, 6)) {
return 2;
}
if (seconds < pow(10, 8)) {
return 3;
}
return 4;
}
#pragma mark - entropy calcs -- one function per match pattern
- (float)calcEntropy:(DBMatch *)match
{
if (match.entropy > 0) {
// a match's entropy doesn't change. cache it.
return match.entropy;
}
if ([match.pattern isEqualToString:@"repeat"]) {
match.entropy = [self repeatEntropy:match];
} else if ([match.pattern isEqualToString:@"sequence"]) {
match.entropy = [self sequenceEntropy:match];
} else if ([match.pattern isEqualToString:@"digits"]) {
match.entropy = [self digitsEntropy:match];
} else if ([match.pattern isEqualToString:@"year"]) {
match.entropy = [self yearEntropy:match];
} else if ([match.pattern isEqualToString:@"date"]) {
match.entropy = [self dateEntropy:match];
} else if ([match.pattern isEqualToString:@"spatial"]) {
match.entropy = [self spatialEntropy:match];
} else if ([match.pattern isEqualToString:@"dictionary"]) {
match.entropy = [self dictionaryEntropy:match];
}
return match.entropy;
}
- (float)repeatEntropy:(DBMatch *)match
{
float cardinality = [self calcBruteforceCardinality:match.token];
return lg(cardinality * [match.token length]);
}
- (float)sequenceEntropy:(DBMatch *)match
{
NSString *firstChr = [match.token substringToIndex:1];
float baseEntropy = 0;
if ([@[@"a", @"1"] containsObject:firstChr]) {
baseEntropy = 1;
} else {
unichar chr = [firstChr characterAtIndex:0];
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:chr]) {
baseEntropy = lg(10); // digits
} else if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:chr]) {
baseEntropy = lg(26); // lower
} else {
baseEntropy = lg(26) + 1; // extra bit for uppercase
}
}
if (!match.ascending) {
baseEntropy += 1; // extra bit for descending instead of ascending
}
return baseEntropy + lg([match.token length]);
}
- (float)digitsEntropy:(DBMatch *)match
{
return lg(pow(10, [match.token length]));
}
static int kNumYears = 119; // years match against 1900 - 2019
static int kNumMonths = 12;
static int kNumDays = 31;
- (float)yearEntropy:(DBMatch *)match
{
return lg(kNumYears);
}
- (float)dateEntropy:(DBMatch *)match
{
float entropy = 0.0;
if (match.year < 100) {
entropy = lg(kNumDays * kNumMonths * 100); // two-digit year
} else {
entropy = lg(kNumDays * kNumMonths * kNumYears); // four-digit year
}
if ([match.separator length]) {
entropy += 2; // add two bits for separator selection [/,-,.,etc]
}
return entropy;
}
- (float)spatialEntropy:(DBMatch *)match
{
DBMatcher *matcher = [[DBMatcher alloc] init];
NSUInteger s;
NSUInteger d;
if ([@[@"qwerty", @"dvorak"] containsObject:match.graph]) {
s = matcher.keyboardStartingPositions;
d = matcher.keyboardAverageDegree;
} else {
s = matcher.keypadStartingPositions;
d = matcher.keypadAverageDegree;
}
int possibilities = 0;
NSUInteger L = [match.token length];
int t = match.turns;
// estimate the number of possible patterns w/ length L or less with t turns or less.
for (int i = 2; i <= L; i++) {
int possibleTurns = MIN(t, i - 1);
for (int j = 1; j <= possibleTurns; j++) {
possibilities += binom(i - 1, j - 1) * s * pow(d, j);
}
}
float entropy = lg(possibilities);
// add extra entropy for shifted keys. (% instead of 5, A instead of a.)
// math is similar to extra entropy from uppercase letters in dictionary matches.
if (match.shiftedCount) {
int S = match.shiftedCount;
NSUInteger U = [match.token length] - match.shiftedCount; // unshifted count
NSUInteger possibilities = 0;
for (int i = 0; i <= MIN(S, U); i++) {
possibilities += binom(S + U, i);
}
entropy += lg(possibilities);
}
return entropy;
}
- (float)dictionaryEntropy:(DBMatch *)match
{
match.baseEntropy = lg(match.rank); // keep these as properties for display purposes
match.upperCaseEntropy = [self extraUppercaseEntropy:match];
match.l33tEntropy = [self extraL33tEntropy:match];
return match.baseEntropy + match.upperCaseEntropy + match.l33tEntropy;
}
- (float)extraUppercaseEntropy:(DBMatch *)match
{
NSString *word = match.token;
if ([word rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]].location == NSNotFound) {
return 0; // all lower
}
// a capitalized word is the most common capitalization scheme,
// so it only doubles the search space (uncapitalized + capitalized): 1 extra bit of entropy.
// allcaps and end-capitalized are common enough too, underestimate as 1 extra bit to be safe.
NSString *startUpper = @"^[A-Z][^A-Z]+$";
NSString *endUpper = @"^[^A-Z]+[A-Z]$";
NSString *allUpper = @"^[A-Z]+$";
for (NSString *regex in @[startUpper, endUpper, allUpper]) {
if ([[NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex] evaluateWithObject:word]) {
return 1;
}
}
// otherwise calculate the number of ways to capitalize U+L uppercase+lowercase letters with U uppercase letters or less.
// or, if there's more uppercase than lower (for e.g. PASSwORD), the number of ways to lowercase U+L letters with L lowercase letters or less.
int uppercaseLength = 0;
int lowercaseLength = 0;
for (int i = 0; i < [word length]; i++) {
unichar chr = [word characterAtIndex:i];
if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:chr]) {
uppercaseLength++;
} else if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:chr]) {
lowercaseLength++;
}
}
float possibilities = 0.0;
for (int i = 0; i <= MIN(uppercaseLength, lowercaseLength); i++) {
possibilities += binom(uppercaseLength + lowercaseLength, i);
}
return lg(possibilities);
}
- (int)extraL33tEntropy:(DBMatch *)match
{
if (!match.l33t) {
return 0;
}
int possibilities = 0;
for (NSString *subbed in [match.sub allKeys]) {
NSString *unsubbed = [match.sub objectForKey:subbed];
NSUInteger subLength = [[match.token componentsSeparatedByString:subbed] count] - 1;
NSUInteger unsubLength = [[match.token componentsSeparatedByString:unsubbed] count] - 1;
for (int i = 0; i <= MIN(unsubLength, subLength); i++) {
possibilities += binom(unsubLength + subLength, i);
}
}
// corner: return 1 bit for single-letter subs, like 4pple -> apple, instead of 0.
return possibilities <= 1 ? 1 : lg(possibilities);
}
#pragma mark - utilities
- (float)calcBruteforceCardinality:(NSString *)password
{
int digits = 0;
int upper = 0;
int lower = 0;
int symbols = 0;
for (int i = 0; i < [password length]; i++) {
unichar chr = [password characterAtIndex:i];
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:chr]) {
digits = 10;
} else if ([[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:chr]) {
upper = 26;
} else if ([[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:chr]) {
lower = 26;
} else {
symbols = 33;
}
}
return digits + upper + lower + symbols;
}
- (NSString *)displayTime:(int)seconds
{
int minute = 60;
int hour = minute * 60;
int day = hour * 24;
int month = day * 31;
int year = month * 12;
int century = year * 100;
if (seconds < minute)
return @"instant";
if (seconds < hour)
return [NSString stringWithFormat:@"%d minutes", 1 + (int)ceil(seconds / minute)];
if (seconds < day)
return [NSString stringWithFormat:@"%d hours", 1 + (int)ceil(seconds / hour)];
if (seconds < month)
return [NSString stringWithFormat:@"%d days", 1 + (int)ceil(seconds / day)];
if (seconds < year)
return [NSString stringWithFormat:@"%d months", 1 + (int)ceil(seconds / month)];
if (seconds < century)
return [NSString stringWithFormat:@"%d years", 1 + (int)ceil(seconds / year)];
return @"centuries";
}
#pragma mark - functions
float binom(NSUInteger n, NSUInteger k)
{
// Returns binomial coefficient (n choose k).
// http://blog.plover.com/math/choose.html
if (k > n) { return 0; }
if (k == 0) { return 1; }
float result = 1;
for (int denom = 1; denom <= k; denom++) {
result *= n;
result /= denom;
n -= 1;
}
return result;
}
float lg(float n)
{
return log2f(n);
}
NSString* roundToXDigits(float number, int digits)
{
//return round(number * pow(10, digits)) / pow(10, digits);
return [NSString stringWithFormat:@"%.*f", digits, number];
}
id get(NSArray *a, int i)
{
if (i < 0 || i >= [a count]) {
return 0;
}
return [a objectAtIndex:i];
}
@end
@implementation DBResult
@end