-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinc.c
705 lines (600 loc) · 16.5 KB
/
inc.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
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2006,2010-2013,2015 by Solar Designer
*/
#include <stdio.h>
#include <string.h>
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "path.h"
#include "memory.h"
#include "signals.h"
#include "formats.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "config.h"
#include "charset.h"
#include "external.h"
#include "cracker.h"
#include "john.h"
extern struct fmt_main fmt_LM;
typedef char (*char2_table)
[CHARSET_SIZE + 1][CHARSET_SIZE + 1];
typedef char (*chars_table)
[CHARSET_SIZE + 1][CHARSET_SIZE + 1][CHARSET_SIZE + 1];
static unsigned int rec_entry, rec_length;
static unsigned char rec_numbers[CHARSET_LENGTH];
static unsigned int entry, length;
static unsigned char numbers[CHARSET_LENGTH];
static int counts[CHARSET_LENGTH][CHARSET_LENGTH];
static unsigned int real_count, real_minc, real_min, real_max, real_size;
static unsigned char real_chars[CHARSET_SIZE];
static void save_state(FILE *file)
{
unsigned int pos;
fprintf(file, "%u\n2\n%u\n", rec_entry, rec_length + 1);
for (pos = 0; pos <= rec_length; pos++)
fprintf(file, "%u\n", (unsigned int)rec_numbers[pos]);
}
static int restore_state(FILE *file)
{
unsigned int compat, pos;
if (rec_version < 2)
return 1;
if (fscanf(file, "%u\n%u\n%u\n", &rec_entry, &compat, &rec_length) != 3)
return 1;
rec_length--; /* zero-based */
if (compat != 2 || rec_length >= CHARSET_LENGTH)
return 1;
for (pos = 0; pos <= rec_length; pos++) {
unsigned int number;
if (fscanf(file, "%u\n", &number) != 1)
return 1;
if (number >= CHARSET_SIZE)
return 1;
rec_numbers[pos] = number;
}
return 0;
}
static void fix_state(void)
{
rec_entry = entry;
rec_length = length;
memcpy(rec_numbers, numbers, length);
}
static void inc_format_error(char *charset)
{
log_event("! Incorrect charset file format: %.100s", charset);
if (john_main_process)
fprintf(stderr, "Incorrect charset file format: %s\n", charset);
error();
}
static int is_mixedcase(char *chars)
{
char present[0x100];
char *ptr, c;
memset(present, 0, sizeof(present));
ptr = chars;
while ((c = *ptr++))
present[ARCH_INDEX(c)] = 1;
ptr = chars;
while ((c = *ptr++)) {
/* assume ASCII */
if (c >= 'A' && c <= 'Z' && present[ARCH_INDEX(c) | 0x20])
return 1;
}
return 0;
}
static void inc_new_length(unsigned int length,
struct charset_header *header, FILE *file, char *charset,
char *char1, char2_table char2, chars_table *chars)
{
long offset;
int value, pos, i, j;
char *buffer;
int count;
log_event("- Switching to length %d", length + 1);
char1[0] = 0;
if (length) {
for (i = real_min; i <= real_max; i++)
(*char2)[i][0] = 0;
(*char2)[CHARSET_SIZE][0] = 0;
}
for (pos = 0; pos <= (int)length - 2; pos++) {
for (i = real_min; i <= real_max; i++)
for (j = real_min; j <= real_max; j++)
(*chars[pos])[i][j][0] = 0;
for (j = real_min; j <= real_max; j++)
(*chars[pos])[CHARSET_SIZE][j][0] = 0;
(*chars[pos])[CHARSET_SIZE][CHARSET_SIZE][0] = 0;
}
offset =
(long)header->offsets[length][0] |
((long)header->offsets[length][1] << 8) |
((long)header->offsets[length][2] << 16) |
((long)header->offsets[length][3] << 24);
if (fseek(file, offset, SEEK_SET))
pexit("fseek");
i = j = pos = -1;
if ((value = getc(file)) != EOF)
do {
if (value != CHARSET_ESC) {
switch (pos) {
case -1:
inc_format_error(charset);
case 0:
buffer = char1;
break;
case 1:
if (j < 0)
inc_format_error(charset);
buffer = (*char2)[j];
break;
default:
if (i < 0 || j < 0)
inc_format_error(charset);
buffer = (*chars[pos - 2])[i][j];
}
buffer[count = 0] = value;
while ((value = getc(file)) != EOF) {
buffer[++count] = value;
if (value == CHARSET_ESC)
break;
if (count >= CHARSET_SIZE)
inc_format_error(charset);
}
buffer[count] = 0;
continue;
}
if ((value = getc(file)) == EOF)
break;
else
if (value == CHARSET_NEW) {
if ((value = getc(file)) != (int)length)
break;
if ((value = getc(file)) == EOF)
break;
if (value < 0 || value > (int)length)
inc_format_error(charset);
pos = value;
} else
if (value == CHARSET_LINE) {
if (pos < 0)
inc_format_error(charset);
if ((value = getc(file)) == EOF)
break;
i = value;
if (i < 0 || i > CHARSET_SIZE)
inc_format_error(charset);
if ((value = getc(file)) == EOF)
break;
j = value;
if (j < 0 || j > CHARSET_SIZE)
inc_format_error(charset);
} else
inc_format_error(charset);
value = getc(file);
} while (value != EOF);
if (value == EOF) {
if (ferror(file))
pexit("getc");
else
inc_format_error(charset);
}
}
static int expand(char *dst, char *src, int size)
{
char present[CHARSET_SIZE];
char *dptr = dst, *sptr = src;
int count = size;
unsigned int i;
memset(present, 0, real_size);
while (*dptr) {
if (--count <= 1)
return 0;
i = ARCH_INDEX(*dptr++) - real_minc;
if (i >= real_size)
return -1;
present[i] = 1;
}
while (*sptr) {
i = ARCH_INDEX(*sptr) - real_minc;
if (i >= real_size)
return -1;
if (!present[i]) {
*dptr++ = *sptr++;
if (--count <= 1)
break;
} else
sptr++;
}
*dptr = 0;
return 0;
}
static void inc_new_count(unsigned int length, int count, char *charset,
char *allchars, char *char1, char2_table char2, chars_table *chars)
{
int pos, ci;
int size;
int error;
log_event("- Expanding tables for length %d to character count %d",
length + 1, count + 1);
size = count + 2;
error = expand(char1, allchars, size);
if (length)
error |= expand((*char2)[CHARSET_SIZE], allchars, size);
for (pos = 0; pos <= (int)length - 2; pos++)
error |= expand((*chars[pos])[CHARSET_SIZE][CHARSET_SIZE],
allchars, size);
for (ci = 0; ci < real_count; ci++) {
int i = real_chars[ci];
int cj;
if (length)
error |=
expand((*char2)[i], (*char2)[CHARSET_SIZE], size);
for (cj = 0; cj < real_count; cj++) {
int j = real_chars[cj];
for (pos = 0; pos <= (int)length - 2; pos++) {
error |= expand((*chars[pos])[i][j],
(*chars[pos])[CHARSET_SIZE][j], size);
error |= expand((*chars[pos])[i][j],
(*chars[pos])[CHARSET_SIZE][CHARSET_SIZE],
size);
}
}
}
if (error)
inc_format_error(charset);
}
static int inc_key_loop(int length, int fixed, int count,
char *char1, char2_table char2, chars_table *chars)
{
char key_i[PLAINTEXT_BUFFER_SIZE];
char key_e[PLAINTEXT_BUFFER_SIZE];
char *key;
char *chars_cache;
int *counts_length;
int counts_cache;
int numbers_cache;
int pos;
key_i[length + 1] = 0;
numbers[fixed] = count;
chars_cache = NULL;
counts_length = counts[length];
counts_cache = counts_length[length];
pos = 0;
update_ending:
if (pos < 2) {
if (pos == 0)
key_i[0] = char1[numbers[0]];
if (length)
key_i[1] = (*char2)[ARCH_INDEX(key_i[0]) - CHARSET_MIN]
[numbers[1]];
pos = 2;
}
while (pos < length) {
key_i[pos] = (*chars[pos - 2])
[ARCH_INDEX(key_i[pos - 2]) - CHARSET_MIN]
[ARCH_INDEX(key_i[pos - 1]) - CHARSET_MIN]
[numbers[pos]];
pos++;
}
numbers_cache = numbers[length];
if (pos == length) {
chars_cache = (*chars[pos - 2])
[ARCH_INDEX(key_i[pos - 2]) - CHARSET_MIN]
[ARCH_INDEX(key_i[pos - 1]) - CHARSET_MIN];
update_last:
key_i[length] = chars_cache[numbers_cache];
}
key = key_i;
if (!f_filter || ext_filter_body(key_i, key = key_e))
if (crk_process_key(key))
return 1;
pos = length;
if (fixed < length) {
if (++numbers_cache <= counts_cache) {
if (length >= 2)
goto update_last;
numbers[length] = numbers_cache;
goto update_ending;
}
numbers[pos--] = 0;
while (pos > fixed) {
if (++numbers[pos] <= counts_length[pos])
goto update_ending;
numbers[pos--] = 0;
}
}
while (pos-- > 0) {
if (++numbers[pos] <= counts_length[pos])
goto update_ending;
numbers[pos] = 0;
}
return 0;
}
void do_incremental_crack(struct db_main *db, char *mode)
{
char *charset;
int min_length, max_length, max_count;
char *extra;
FILE *file;
struct charset_header *header;
unsigned int check;
char allchars[CHARSET_SIZE + 1];
char char1[CHARSET_SIZE + 1];
char2_table char2;
chars_table chars[CHARSET_LENGTH - 2];
unsigned char *ptr;
unsigned int fixed, count;
int last_length, last_count;
int pos;
if (!mode) {
if (db->format == &fmt_LM)
mode = "LM_ASCII";
else
mode = "ASCII";
}
log_event("Proceeding with \"incremental\" mode: %.100s", mode);
if (!(charset = cfg_get_param(SECTION_INC, mode, "File"))) {
log_event("! No charset defined");
if (john_main_process)
fprintf(stderr, "No charset defined for mode: %s\n",
mode);
error();
}
extra = cfg_get_param(SECTION_INC, mode, "Extra");
if ((min_length = cfg_get_int(SECTION_INC, mode, "MinLen")) < 0)
min_length = 0;
if ((max_length = cfg_get_int(SECTION_INC, mode, "MaxLen")) < 0)
max_length = CHARSET_LENGTH;
max_count = cfg_get_int(SECTION_INC, mode, "CharCount");
if (min_length > max_length) {
log_event("! MinLen = %d exceeds MaxLen = %d",
min_length, max_length);
if (john_main_process)
fprintf(stderr, "MinLen = %d exceeds MaxLen = %d\n",
min_length, max_length);
error();
}
if (min_length > db->format->params.plaintext_length) {
log_event("! MinLen = %d is too large for this hash type",
min_length);
if (john_main_process)
fprintf(stderr,
"MinLen = %d exceeds the maximum possible "
"length for the current hash type (%d)\n",
min_length, db->format->params.plaintext_length);
error();
}
if (max_length > db->format->params.plaintext_length) {
log_event("! MaxLen = %d is too large for this hash type",
max_length);
if (john_main_process)
fprintf(stderr, "Warning: MaxLen = %d is too large "
"for the current hash type, reduced to %d\n",
max_length, db->format->params.plaintext_length);
max_length = db->format->params.plaintext_length;
}
if (max_length > CHARSET_LENGTH) {
log_event("! MaxLen = %d exceeds the compile-time limit of %d",
max_length, CHARSET_LENGTH);
if (john_main_process)
fprintf(stderr, "MaxLen = %d exceeds the compile-time "
"limit of %d\n", max_length, CHARSET_LENGTH);
error();
}
if (!(file = fopen(path_expand(charset), "rb")))
pexit("fopen: %s", path_expand(charset));
header = (struct charset_header *)mem_alloc(sizeof(*header));
if (charset_read_header(file, header) && !ferror(file))
inc_format_error(charset);
if (ferror(file))
pexit("fread");
if (feof(file) ||
memcmp(header->version, CHARSET_V, sizeof(header->version)) ||
!header->count)
inc_format_error(charset);
if (header->min != CHARSET_MIN || header->max != CHARSET_MAX ||
header->length != CHARSET_LENGTH) {
log_event("! Incompatible charset file: %.100s", charset);
if (john_main_process)
fprintf(stderr, "Incompatible charset file: %s\n",
charset);
error();
}
#if CHARSET_SIZE < 0xff
if (header->count > CHARSET_SIZE)
inc_format_error(charset);
#endif
check =
(unsigned int)header->check[0] |
((unsigned int)header->check[1] << 8) |
((unsigned int)header->check[2] << 16) |
((unsigned int)header->check[3] << 24);
if (!rec_restoring_now)
rec_check = check;
if (rec_check != check) {
log_event("! Charset file has changed: %.100s", charset);
if (john_main_process)
fprintf(stderr, "Charset file has changed: %s\n",
charset);
error();
}
if (fread(allchars, header->count, 1, file) != 1) {
if (ferror(file))
pexit("fread");
inc_format_error(charset);
}
/* Sanity-check and expand allchars */
real_minc = CHARSET_MIN; real_size = CHARSET_SIZE;
allchars[header->count] = 0;
if (expand(allchars, "", sizeof(allchars)))
inc_format_error(charset);
if (extra && expand(allchars, extra, sizeof(allchars))) {
log_event("! Extra characters not in compile-time "
"specified range ('\\x%02x' to '\\x%02x')",
CHARSET_MIN, CHARSET_MAX);
if (john_main_process)
fprintf(stderr, "Extra characters not in compile-time "
"specified range ('\\x%02x' to '\\x%02x')\n",
CHARSET_MIN, CHARSET_MAX);
error();
}
/* Calculate the actual real_* based on sanitized and expanded allchars */
{
unsigned char c;
real_min = 0xff;
real_count = real_max = 0;
while ((c = allchars[real_count])) {
c -= CHARSET_MIN;
if (c < real_min)
real_min = c;
if (c > real_max)
real_max = c;
real_chars[real_count++] = c;
}
real_minc = CHARSET_MIN + real_min;
real_size = real_max - real_min + 1;
if (real_size < real_count)
inc_format_error(charset);
}
if (max_count < 0)
max_count = CHARSET_SIZE;
if (min_length != max_length)
log_event("- Lengths %d to %d, up to %d different characters",
min_length, max_length, max_count);
else
log_event("- Length %d, up to %d different characters",
min_length, max_count);
if ((unsigned int)max_count > real_count) {
log_event("! Only %u characters available", real_count);
if (john_main_process)
fprintf(stderr,
"Warning: only %u characters available\n",
real_count);
}
if (!(db->format->params.flags & FMT_CASE) && is_mixedcase(allchars)) {
log_event("! Mixed-case charset, "
"but the hash type is case-insensitive");
if (john_main_process)
fprintf(stderr, "Warning: mixed-case charset, "
"but the current hash type is case-insensitive;\n"
"some candidate passwords may be unnecessarily "
"tried more than once.\n");
}
char2 = NULL;
for (pos = 0; pos < CHARSET_LENGTH - 2; pos++)
chars[pos] = NULL;
if (max_length >= 2) {
char2 = (char2_table)mem_alloc(sizeof(*char2));
for (pos = 0; pos < max_length - 2; pos++)
chars[pos] = (chars_table)mem_alloc(sizeof(*chars[0]));
}
rec_entry = 0;
memset(rec_numbers, 0, sizeof(rec_numbers));
status_init(NULL, 0);
rec_restore_mode(restore_state);
rec_init(db, save_state);
ptr = header->order;
entry = 0;
while (entry < rec_entry &&
ptr < &header->order[sizeof(header->order) - 1]) {
entry++;
length = *ptr++; fixed = *ptr++; count = *ptr++;
if (length >= CHARSET_LENGTH ||
fixed > length ||
count >= CHARSET_SIZE)
inc_format_error(charset);
if (count >= real_count || (fixed && !count))
continue;
if ((int)length + 1 < min_length ||
(int)length >= max_length ||
(int)count >= max_count)
continue;
if (count)
counts[length][fixed]++;
if (counts[length][fixed] != count) {
log_event("! Unexpected count: %d != %d",
counts[length][fixed] + 1, count + 1);
fprintf(stderr, "Unexpected count: %d != %d\n",
counts[length][fixed] + 1, count + 1);
error();
}
}
memcpy(numbers, rec_numbers, sizeof(numbers));
crk_init(db, fix_state, NULL);
last_count = last_length = -1;
entry--;
while (ptr < &header->order[sizeof(header->order) - 1]) {
int skip = 0;
if (options.node_count) {
int for_node = entry % options.node_count + 1;
skip = for_node < options.node_min ||
for_node > options.node_max;
}
entry++;
length = *ptr++; fixed = *ptr++; count = *ptr++;
if (length >= CHARSET_LENGTH ||
fixed > length ||
count >= CHARSET_SIZE)
inc_format_error(charset);
if (entry != rec_entry)
memset(numbers, 0, sizeof(numbers));
if (count >= real_count || (fixed && !count))
continue;
if ((int)length + 1 < min_length ||
(int)length >= max_length ||
(int)count >= max_count)
continue;
if (!skip) {
int i, max_count = 0;
if ((int)length != last_length) {
inc_new_length(last_length = length,
header, file, charset, char1, char2, chars);
last_count = -1;
}
for (i = 0; i <= length; i++)
if (counts[length][i] > max_count)
max_count = counts[length][i];
if (count > max_count)
max_count = count;
if (max_count > last_count) {
last_count = max_count;
inc_new_count(length, max_count, charset,
allchars, char1, char2, chars);
}
}
if (!length && !min_length) {
min_length = 1;
if (!skip && crk_process_key(fmt_null_key))
break;
}
if (count)
counts[length][fixed]++;
if (counts[length][fixed] != count) {
log_event("! Unexpected count: %d != %d",
counts[length][fixed] + 1, count + 1);
fprintf(stderr, "Unexpected count: %d != %d\n",
counts[length][fixed] + 1, count + 1);
error();
}
if (skip)
continue;
log_event("- Trying length %d, fixed @%d, character count %d",
length + 1, fixed + 1, counts[length][fixed] + 1);
if (inc_key_loop(length, fixed, count, char1, char2, chars))
break;
}
crk_done();
rec_done(event_abort);
for (pos = 0; pos < max_length - 2; pos++)
MEM_FREE(chars[pos]);
MEM_FREE(char2);
MEM_FREE(header);
fclose(file);
}