-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.c
1103 lines (932 loc) · 26.5 KB
/
loader.c
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2000,2003,2005,2010-2012,2015 by Solar Designer
*/
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "path.h"
#include "memory.h"
#include "common.h"
#include "list.h"
#include "signals.h"
#include "formats.h"
#include "loader.h"
#ifdef HAVE_CRYPT
extern struct fmt_main fmt_crypt;
int ldr_in_pot = 0;
#endif
/*
* Flags for read_file().
*/
#define RF_ALLOW_MISSING 1
#define RF_ALLOW_DIR 2
/*
* Word separator characters for ldr_split_words(), used on GECOS fields.
*/
#define issep \
"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\177"
static char issep_map[0x100];
static int issep_initialized = 0;
static char *no_username = "?";
static void read_file(struct db_main *db, char *name, int flags,
void (*process_line)(struct db_main *db, char *line))
{
struct stat file_stat;
FILE *file;
char line[LINE_BUFFER_SIZE];
if (flags & RF_ALLOW_DIR) {
if (stat(name, &file_stat)) {
if (flags & RF_ALLOW_MISSING)
if (errno == ENOENT) return;
pexit("stat: %s", path_expand(name));
} else
if (S_ISDIR(file_stat.st_mode)) return;
}
if (!(file = fopen(path_expand(name), "r"))) {
if ((flags & RF_ALLOW_MISSING) && errno == ENOENT) return;
pexit("fopen: %s", path_expand(name));
}
while (fgets(line, sizeof(line), file)) {
process_line(db, line);
check_abort(0);
}
if (ferror(file)) pexit("fgets");
if (fclose(file)) pexit("fclose");
}
static void ldr_init_issep(void)
{
char *pos;
if (issep_initialized) return;
memset(issep_map, 0, sizeof(issep_map));
memset(issep_map, 1, 33);
for (pos = issep; *pos; pos++)
issep_map[ARCH_INDEX(*pos)] = 1;
issep_initialized = 1;
}
void ldr_init_database(struct db_main *db, struct db_options *db_options)
{
db->loaded = 0;
db->pw_size = sizeof(struct db_password);
db->salt_size = sizeof(struct db_salt);
if (!(db_options->flags & DB_WORDS)) {
db->pw_size -= sizeof(struct list_main *);
if (!(db_options->flags & DB_LOGIN))
db->pw_size -= sizeof(char *);
db->salt_size -= sizeof(struct db_keys *);
}
db->options = mem_alloc_copy(db_options,
sizeof(struct db_options), MEM_ALIGN_WORD);
if (db->options->flags & DB_WORDS) {
db->options->flags |= DB_LOGIN;
ldr_init_issep();
}
db->salts = NULL;
db->password_hash = NULL;
db->password_hash_func = NULL;
if (db_options->flags & DB_CRACKED) {
db->salt_hash = NULL;
db->cracked_hash = mem_calloc(
CRACKED_HASH_SIZE, sizeof(struct db_cracked *));
} else {
db->salt_hash = mem_calloc(
SALT_HASH_SIZE, sizeof(struct db_salt *));
db->cracked_hash = NULL;
}
list_init(&db->plaintexts);
db->salt_count = db->password_count = db->guess_count = 0;
db->format = NULL;
}
/*
* Allocate a hash table for use by the loader itself. We use this for two
* purposes: to detect and avoid loading of duplicate hashes when DB_WORDS is
* not set, and to remove previously-cracked hashes (found in john.pot). We
* allocate, use, and free this hash table prior to deciding on the sizes of
* and allocating the per-salt hash tables to be used while cracking.
*/
static void ldr_init_password_hash(struct db_main *db)
{
int (*func)(void *binary);
int size_num = PASSWORD_HASH_SIZE_FOR_LDR;
size_t size;
if (size_num >= 2 && mem_saving_level >= 2) {
size_num--;
if (mem_saving_level >= 3)
size_num--;
}
do {
func = db->format->methods.binary_hash[size_num];
if (func && func != fmt_default_binary_hash)
break;
} while (--size_num >= 0);
if (size_num < 0)
size_num = 0;
db->password_hash_func = func;
size = (size_t)password_hash_sizes[size_num] *
sizeof(struct db_password *);
db->password_hash = mem_alloc(size);
memset(db->password_hash, 0, size);
}
static char *ldr_get_field(char **ptr)
{
char *res, *pos;
if (!*ptr) return "";
if ((pos = strchr(res = *ptr, ':'))) {
*pos++ = 0; *ptr = pos;
} else {
pos = res;
do {
if (*pos == '\r' || *pos == '\n') *pos = 0;
} while (*pos++);
*ptr = NULL;
}
return res;
}
static int ldr_check_list(struct list_main *list, char *s1, char *s2)
{
struct list_entry *current;
char *data;
if (!(current = list->head)) return 0;
if (*current->data == '-') {
data = current->data + 1;
do {
if (!strcmp(s1, data) || !strcmp(s2, data)) return 1;
if ((current = current->next)) data = current->data;
} while (current);
} else {
do {
data = current->data;
if (!strcmp(s1, data) || !strcmp(s2, data)) return 0;
} while ((current = current->next));
return 1;
}
return 0;
}
static MAYBE_INLINE int ldr_check_shells(struct list_main *list, char *shell)
{
char *name;
if (list->head) {
if ((name = strrchr(shell, '/'))) name++; else name = shell;
return ldr_check_list(list, shell, name);
}
return 0;
}
static int ldr_split_line(char **login, char **ciphertext,
char **gecos, char **home,
char *source, struct fmt_main **format,
struct db_options *options, char *line)
{
struct fmt_main *alt;
char *fields[10], *uid, *gid, *shell;
int i, retval;
fields[0] = *login = ldr_get_field(&line);
fields[1] = *ciphertext = ldr_get_field(&line);
/* Check for NIS stuff */
if (((*login)[0] == '+' && (!(*login)[1] || (*login)[1] == '@')) &&
strlen(*ciphertext) < 10 && strncmp(*ciphertext, "$dummy$", 7))
return 0;
if (!**ciphertext && !line) {
/* Possible hash on a line on its own (no colons) */
char *p = *login;
/* Skip leading and trailing whitespace */
while (*p == ' ' || *p == '\t') p++;
*ciphertext = p;
p += strlen(p) - 1;
while (p > *ciphertext && (*p == ' ' || *p == '\t')) p--;
p++;
/* Some valid dummy hashes may be shorter than 10 characters, so don't subject
* them to the length checks. */
if (((*ciphertext)[0] != '$' ||
strncmp(*ciphertext, "$dummy$", 7)) &&
p - *ciphertext != 10 /* not tripcode */) {
/* Check for a special case: possibly a traditional crypt(3) hash with
* whitespace in its invalid salt. Only support such hashes at the very start
* of a line (no leading whitespace other than the invalid salt). */
if (p - *ciphertext == 11 && *ciphertext - *login == 2)
(*ciphertext)--;
if (p - *ciphertext == 12 && *ciphertext - *login == 1)
(*ciphertext)--;
if (p - *ciphertext < 13)
return 0;
}
*p = 0;
fields[0] = *login = no_username;
fields[1] = *ciphertext;
}
if (source)
strcpy(source, line ? line : "");
/*
* This check is just a loader performance optimization, so that we can parse
* fewer fields when we know we won't need the rest. It should be revised or
* removed when there are formats that use higher-numbered fields in prepare().
*/
if ((options->flags & DB_WORDS) || options->shells->head) {
/* Parse all fields */
for (i = 2; i < 10; i++)
fields[i] = ldr_get_field(&line);
} else {
/* Parse some fields only */
for (i = 2; i < 4; i++)
fields[i] = ldr_get_field(&line);
for (; i < 10; i++)
fields[i] = "/";
}
/* /etc/passwd */
*gecos = fields[4];
*home = fields[5];
if (fields[0] == no_username)
goto find_format;
uid = fields[2];
gid = fields[3];
shell = fields[6];
if (fields[5][0] != '/' &&
((!strcmp(fields[5], "0") && !strcmp(fields[6], "0")) ||
fields[8][0] == '/' ||
fields[9][0] == '/')) {
/* /etc/master.passwd */
*gecos = fields[7];
*home = fields[8];
shell = fields[9];
} else if (fields[3] - fields[2] == 32 + 1) {
/* PWDUMP */
uid = fields[1];
*ciphertext = fields[2];
if (!strncmp(*ciphertext, "NO PASSWORD", 11))
*ciphertext = "";
gid = shell = "";
*gecos = fields[4];
*home = fields[5];
/* Re-introduce the previously removed uid field */
if (source) {
int shift = strlen(uid);
memmove(source + shift + 1, source, strlen(source) + 1);
memcpy(source, uid, shift);
source[shift] = ':';
}
}
if (ldr_check_list(options->users, *login, uid)) return 0;
if (ldr_check_list(options->groups, gid, gid)) return 0;
if (ldr_check_shells(options->shells, shell)) return 0;
find_format:
if (*format) {
char *prepared;
int valid;
prepared = (*format)->methods.prepare(fields, *format);
if (prepared)
valid = (*format)->methods.valid(prepared, *format);
else
valid = 0;
if (valid) {
*ciphertext = prepared;
return valid;
}
alt = fmt_list;
do {
if (alt == *format)
continue;
if (alt->params.flags & FMT_WARNED)
continue;
#ifdef HAVE_CRYPT
if (alt == &fmt_crypt &&
#ifdef __sun
strncmp(*ciphertext, "$md5$", 5) &&
strncmp(*ciphertext, "$md5,", 5) &&
#endif
strncmp(*ciphertext, "$5$", 3) &&
strncmp(*ciphertext, "$6$", 3))
continue;
#endif
prepared = alt->methods.prepare(fields, alt);
if (alt->methods.valid(prepared, alt)) {
alt->params.flags |= FMT_WARNED;
fprintf(stderr,
"Warning: only loading hashes of type "
"\"%s\", but also saw type \"%s\"\n"
"Use the \"--format=%s\" option to force "
"loading hashes of that type instead\n",
(*format)->params.label,
alt->params.label,
alt->params.label);
break;
}
} while ((alt = alt->next));
return 0;
}
retval = -1;
if ((alt = fmt_list))
do {
char *prepared;
int valid;
#ifdef HAVE_CRYPT
/*
* Only probe for support by the current system's crypt(3) if this is forced
* from the command-line or/and if the hash encoding string looks like one of
* those that are only supported in that way. Avoid the probe in other cases
* because it may be slow and undesirable (false detection is possible).
*/
if (alt == &fmt_crypt &&
fmt_list != &fmt_crypt /* not forced */ &&
#ifdef __sun
strncmp(*ciphertext, "$md5$", 5) &&
strncmp(*ciphertext, "$md5,", 5) &&
#endif
strncmp(*ciphertext, "$5$", 3) &&
strncmp(*ciphertext, "$6$", 3))
continue;
#endif
prepared = alt->methods.prepare(fields, alt);
if (!prepared)
continue;
valid = alt->methods.valid(prepared, alt);
if (!valid)
continue;
if (retval < 0) {
retval = valid;
*ciphertext = prepared;
fmt_init(*format = alt);
#ifdef LDR_WARN_AMBIGUOUS
if (!source) /* not --show */
continue;
#endif
break;
}
#ifdef LDR_WARN_AMBIGUOUS
fprintf(stderr,
"Warning: detected hash type \"%s\", but the string is "
"also recognized as \"%s\"\n"
"Use the \"--format=%s\" option to force loading these "
"as that type instead\n",
(*format)->params.label, alt->params.label,
alt->params.label);
#endif
} while ((alt = alt->next));
return retval;
}
static void ldr_split_string(struct list_main *dst, char *src)
{
char *word, *pos;
char c;
pos = src;
do {
word = pos;
while (*word && issep_map[ARCH_INDEX(*word)]) word++;
if (!*word) break;
pos = word;
while (!issep_map[ARCH_INDEX(*pos)]) pos++;
c = *pos;
*pos = 0;
list_add_unique(dst, word);
*pos++ = c;
} while (c && dst->count < LDR_WORDS_MAX);
}
static struct list_main *ldr_init_words(char *login, char *gecos, char *home)
{
struct list_main *words;
char *pos;
list_init(&words);
if (*login && login != no_username)
list_add(words, login);
ldr_split_string(words, gecos);
if (login != no_username)
ldr_split_string(words, login);
if ((pos = strrchr(home, '/')) && pos[1])
list_add_unique(words, pos + 1);
return words;
}
static void ldr_load_pw_line(struct db_main *db, char *line)
{
static int skip_dupe_checking = 0;
struct fmt_main *format;
int index, count;
char *login, *ciphertext, *gecos, *home;
char *piece;
void *binary, *salt;
int salt_hash, pw_hash;
struct db_salt *current_salt, *last_salt;
struct db_password *current_pw, *last_pw;
struct list_main *words;
size_t pw_size;
count = ldr_split_line(&login, &ciphertext, &gecos, &home,
NULL, &db->format, db->options, line);
if (count <= 0) return;
if (count >= 2) db->options->flags |= DB_SPLIT;
format = db->format;
words = NULL;
if (!db->password_hash)
ldr_init_password_hash(db);
for (index = 0; index < count; index++) {
piece = format->methods.split(ciphertext, index, format);
binary = format->methods.binary(piece);
pw_hash = db->password_hash_func(binary);
if (!(db->options->flags & DB_WORDS) && !skip_dupe_checking) {
int collisions = 0;
if ((current_pw = db->password_hash[pw_hash]))
do {
if (!memcmp(binary, current_pw->binary,
format->params.binary_size) &&
!strcmp(piece, format->methods.source(
current_pw->source, current_pw->binary))) {
db->options->flags |= DB_NODUP;
break;
}
if (++collisions <= LDR_HASH_COLLISIONS_MAX)
continue;
if (format->params.binary_size)
fprintf(stderr, "Warning: "
"excessive partial hash "
"collisions detected\n%s",
db->password_hash_func !=
fmt_default_binary_hash ? "" :
"(cause: the \"format\" lacks "
"proper binary_hash() function "
"definitions)\n");
else
fprintf(stderr, "Warning: "
"check for duplicates partially "
"bypassed to speedup loading\n");
skip_dupe_checking = 1;
current_pw = NULL; /* no match */
break;
} while ((current_pw = current_pw->next_hash));
if (current_pw) continue;
}
salt = format->methods.salt(piece);
salt_hash = format->methods.salt_hash(salt);
if ((current_salt = db->salt_hash[salt_hash]))
do {
if (!memcmp(current_salt->salt, salt,
format->params.salt_size))
break;
} while ((current_salt = current_salt->next));
if (!current_salt) {
last_salt = db->salt_hash[salt_hash];
current_salt = db->salt_hash[salt_hash] =
mem_alloc_tiny(db->salt_size, MEM_ALIGN_WORD);
current_salt->next = last_salt;
current_salt->salt = mem_alloc_copy(salt,
format->params.salt_size,
format->params.salt_align);
current_salt->index = fmt_dummy_hash;
current_salt->bitmap = NULL;
current_salt->list = NULL;
current_salt->hash = ¤t_salt->list;
current_salt->hash_size = -1;
current_salt->count = 0;
if (db->options->flags & DB_WORDS)
current_salt->keys = NULL;
db->salt_count++;
}
current_salt->count++;
db->password_count++;
/* If we're not allocating memory for the "login" field, we may as well not
* allocate it for the "source" field if the format doesn't need it. */
pw_size = db->pw_size;
if (!(db->options->flags & DB_LOGIN) &&
format->methods.source != fmt_default_source)
pw_size -= sizeof(char *);
last_pw = current_salt->list;
current_pw = current_salt->list = mem_alloc_tiny(
pw_size, MEM_ALIGN_WORD);
current_pw->next = last_pw;
last_pw = db->password_hash[pw_hash];
db->password_hash[pw_hash] = current_pw;
current_pw->next_hash = last_pw;
/* If we're not going to use the source field for its usual purpose yet we had
* to allocate memory for it (because we need at least one field after it), see
* if we can pack the binary value in it. */
if ((db->options->flags & DB_LOGIN) &&
format->methods.source != fmt_default_source &&
sizeof(current_pw->source) >= format->params.binary_size)
current_pw->binary = memcpy(¤t_pw->source,
binary, format->params.binary_size);
else
current_pw->binary = mem_alloc_copy(binary,
format->params.binary_size,
format->params.binary_align);
if (format->methods.source == fmt_default_source)
current_pw->source = str_alloc_copy(piece);
if (db->options->flags & DB_WORDS) {
if (!words)
words = ldr_init_words(login, gecos, home);
current_pw->words = words;
}
if (db->options->flags & DB_LOGIN) {
if (count >= 2 && count <= 9) {
current_pw->login = mem_alloc_tiny(
strlen(login) + 3, MEM_ALIGN_NONE);
sprintf(current_pw->login, "%s:%d",
login, index + 1);
} else
if (login == no_username)
current_pw->login = login;
else
if (words && *login)
current_pw->login = words->head->data;
else
current_pw->login = str_alloc_copy(login);
}
}
}
void ldr_load_pw_file(struct db_main *db, char *name)
{
read_file(db, name, RF_ALLOW_DIR, ldr_load_pw_line);
}
static void ldr_load_pot_line(struct db_main *db, char *line)
{
struct fmt_main *format = db->format;
char *ciphertext;
void *binary;
int hash;
int need_removal;
struct db_password *current;
ciphertext = ldr_get_field(&line);
if (format->methods.valid(ciphertext, format) != 1) return;
ciphertext = format->methods.split(ciphertext, 0, format);
binary = format->methods.binary(ciphertext);
hash = db->password_hash_func(binary);
need_removal = 0;
if ((current = db->password_hash[hash]))
do {
if (!current->binary) /* already marked for removal */
continue;
if (memcmp(binary, current->binary, format->params.binary_size))
continue;
if (strcmp(ciphertext,
format->methods.source(current->source, current->binary)))
continue;
current->binary = NULL; /* mark for removal */
need_removal = 1;
} while ((current = current->next_hash));
if (need_removal)
db->options->flags |= DB_NEED_REMOVAL;
}
void ldr_load_pot_file(struct db_main *db, char *name)
{
if (db->format) {
#ifdef HAVE_CRYPT
ldr_in_pot = 1;
#endif
read_file(db, name, RF_ALLOW_MISSING, ldr_load_pot_line);
#ifdef HAVE_CRYPT
ldr_in_pot = 0;
#endif
}
}
/*
* The following are several functions called by ldr_fix_database().
* They assume that the per-salt hash tables have not yet been initialized.
*/
/*
* Glue the salt_hash[] buckets together and into the salts list. The loader
* needs the hash table, but then we free it and the cracker uses the list.
*/
static void ldr_init_salts(struct db_main *db)
{
struct db_salt **tail, *current;
int hash;
for (hash = 0, tail = &db->salts; hash < SALT_HASH_SIZE; hash++)
if ((current = db->salt_hash[hash])) {
*tail = current;
do {
tail = ¤t->next;
} while ((current = current->next));
}
}
/*
* Remove the previously-cracked hashes marked with "binary = NULL" by
* ldr_load_pot_line().
*/
static void ldr_remove_marked(struct db_main *db)
{
struct db_salt *current_salt, *last_salt;
struct db_password *current_pw, *last_pw;
if (!(db->options->flags & DB_NEED_REMOVAL))
return;
last_salt = NULL;
if ((current_salt = db->salts))
do {
last_pw = NULL;
if ((current_pw = current_salt->list))
do {
if (!current_pw->binary) {
db->password_count--;
current_salt->count--;
if (last_pw)
last_pw->next = current_pw->next;
else
current_salt->list = current_pw->next;
} else
last_pw = current_pw;
} while ((current_pw = current_pw->next));
if (!current_salt->list) {
db->salt_count--;
if (last_salt)
last_salt->next = current_salt->next;
else
db->salts = current_salt->next;
} else
last_salt = current_salt;
} while ((current_salt = current_salt->next));
db->options->flags &= ~DB_NEED_REMOVAL;
}
/*
* Remove salts with too few or too many password hashes.
*/
static void ldr_filter_salts(struct db_main *db)
{
struct db_salt *current, *last;
int min = db->options->min_pps;
int max = db->options->max_pps;
if (!max) {
if (!min) return;
max = ~(unsigned int)0 >> 1;
}
last = NULL;
if ((current = db->salts))
do {
if (current->count < min || current->count > max) {
if (last)
last->next = current->next;
else
db->salts = current->next;
db->salt_count--;
db->password_count -= current->count;
} else
last = current;
} while ((current = current->next));
}
/*
* Allocate memory for and initialize the hash table for this salt if needed.
* Also initialize salt->count (the number of password hashes for this salt).
*/
static void ldr_init_hash_for_salt(struct db_main *db, struct db_salt *salt)
{
struct db_password *current;
int (*hash_func)(void *binary);
size_t bitmap_size, hash_size;
int hash;
if (salt->hash_size < 0) {
salt->count = 0;
if ((current = salt->list))
do {
current->next_hash = NULL; /* unused */
salt->count++;
} while ((current = current->next));
return;
}
bitmap_size = password_hash_sizes[salt->hash_size];
{
size_t size = (bitmap_size +
sizeof(*salt->bitmap) * 8 - 1) /
(sizeof(*salt->bitmap) * 8) * sizeof(*salt->bitmap);
salt->bitmap = mem_alloc_tiny(size, sizeof(*salt->bitmap));
memset(salt->bitmap, 0, size);
}
hash_size = bitmap_size >> PASSWORD_HASH_SHR;
if (hash_size > 1) {
size_t size = hash_size * sizeof(struct db_password *);
salt->hash = mem_alloc_tiny(size, MEM_ALIGN_WORD);
memset(salt->hash, 0, size);
}
salt->index = db->format->methods.get_hash[salt->hash_size];
hash_func = db->format->methods.binary_hash[salt->hash_size];
salt->count = 0;
if ((current = salt->list))
do {
hash = hash_func(current->binary);
salt->bitmap[hash / (sizeof(*salt->bitmap) * 8)] |=
1U << (hash % (sizeof(*salt->bitmap) * 8));
if (hash_size > 1) {
hash >>= PASSWORD_HASH_SHR;
current->next_hash = salt->hash[hash];
salt->hash[hash] = current;
} else
current->next_hash = current->next;
salt->count++;
} while ((current = current->next));
}
/*
* Decide on whether to use a hash table and on its size for each salt, call
* ldr_init_hash_for_salt() to allocate and initialize the hash tables.
*/
static void ldr_init_hash(struct db_main *db)
{
struct db_salt *current;
int threshold, size;
threshold = password_hash_thresholds[0];
if (db->format && (db->format->params.flags & FMT_BS)) {
/*
* Estimate the complexity of DES_bs_get_hash() for each computed hash (but
* comparing it against less than 1 loaded hash on average due to the use of a
* hash table) vs. the complexity of DES_bs_cmp_all() for all computed hashes
* at once (but calling it for each loaded hash individually).
*/
threshold = 5 * ARCH_BITS / ARCH_BITS_LOG + 1;
}
if ((current = db->salts))
do {
size = -1;
if (current->count >= threshold && mem_saving_level < 3)
for (size = PASSWORD_HASH_SIZES - 1; size >= 0; size--)
if (current->count >=
password_hash_thresholds[size] &&
db->format->methods.binary_hash[size] &&
db->format->methods.binary_hash[size] !=
fmt_default_binary_hash)
break;
if (mem_saving_level >= 2)
size--;
current->hash_size = size;
ldr_init_hash_for_salt(db, current);
} while ((current = current->next));
}
void ldr_fix_database(struct db_main *db)
{
ldr_init_salts(db);
MEM_FREE(db->password_hash);
MEM_FREE(db->salt_hash);
ldr_filter_salts(db);
ldr_remove_marked(db);
ldr_init_hash(db);
db->loaded = 1;
}
static int ldr_cracked_hash(char *ciphertext)
{
unsigned int hash, extra;
unsigned char *p = (unsigned char *)ciphertext;
hash = p[0] | 0x20; /* ASCII case insensitive */
if (!hash)
goto out;
extra = p[1] | 0x20;
if (!extra)
#if CRACKED_HASH_SIZE >= 0x100
goto out;
#else
goto out_and;
#endif
p += 2;
while (*p) {
hash <<= 3; extra <<= 2;
hash += p[0] | 0x20;
if (!p[1]) break;
extra += p[1] | 0x20;
p += 2;
if (hash & 0xe0000000) {
hash ^= hash >> CRACKED_HASH_LOG;
extra ^= extra >> CRACKED_HASH_LOG;
hash &= CRACKED_HASH_SIZE - 1;
}
}
hash -= extra;
hash ^= extra << (CRACKED_HASH_LOG / 2);
hash ^= hash >> CRACKED_HASH_LOG;
#if CRACKED_HASH_LOG <= 15
hash ^= hash >> (2 * CRACKED_HASH_LOG);
#endif
#if CRACKED_HASH_LOG <= 10
hash ^= hash >> (3 * CRACKED_HASH_LOG);
#endif
#if CRACKED_HASH_SIZE < 0x100
out_and:
#endif
hash &= CRACKED_HASH_SIZE - 1;
out:
return hash;
}
static void ldr_show_pot_line(struct db_main *db, char *line)
{
char *ciphertext, *pos;
int hash;
struct db_cracked *current, *last;
ciphertext = ldr_get_field(&line);
if (line) {
/* If just one format was forced on the command line, insist on it */
if (!fmt_list->next &&
!fmt_list->methods.valid(ciphertext, fmt_list))
return;
pos = line;
do {
if (*pos == '\r' || *pos == '\n') *pos = 0;
} while (*pos++);
if (db->options->flags & DB_PLAINTEXTS) {
list_add(db->plaintexts, line);
return;
}
hash = ldr_cracked_hash(ciphertext);
last = db->cracked_hash[hash];
current = db->cracked_hash[hash] =
mem_alloc_tiny(sizeof(struct db_cracked),
MEM_ALIGN_WORD);
current->next = last;
current->ciphertext = str_alloc_copy(ciphertext);
current->plaintext = str_alloc_copy(line);
}
}
void ldr_show_pot_file(struct db_main *db, char *name)
{
#ifdef HAVE_CRYPT
ldr_in_pot = 1;
#endif
read_file(db, name, RF_ALLOW_MISSING, ldr_show_pot_line);
#ifdef HAVE_CRYPT
ldr_in_pot = 0;
#endif
}
static void ldr_show_pw_line(struct db_main *db, char *line)
{
int show;
char source[LINE_BUFFER_SIZE];
struct fmt_main *format;