-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjohn.c
712 lines (617 loc) · 17.2 KB
/
john.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
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2004,2006,2009-2013,2015 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*
* Please note that although this main john.c file is under the cut-down BSD
* license above (so that you may reuse sufficiently generic pieces of code
* from this file under these relaxed terms), some other source files that it
* uses are under GPLv2. For licensing terms for John the Ripper as a whole,
* see doc/LICENSE.
*/
#define NEED_OS_FORK
#include "os.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/stat.h>
#if OS_FORK
#include <sys/wait.h>
#endif
#include "params.h"
#ifdef _OPENMP
#include <omp.h>
static int john_omp_threads_orig = 0;
static int john_omp_threads_new;
#endif
#include "arch.h"
#include "misc.h"
#include "path.h"
#include "memory.h"
#include "list.h"
#include "tty.h"
#include "signals.h"
#include "common.h"
#include "idle.h"
#include "formats.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "config.h"
#include "bench.h"
#include "charset.h"
#include "single.h"
#include "wordlist.h"
#include "inc.h"
#include "external.h"
#include "batch.h"
#if CPU_DETECT
extern int CPU_detect(void);
#endif
extern struct fmt_main fmt_DES, fmt_BSDI, fmt_MD5, fmt_BF;
extern struct fmt_main fmt_AFS, fmt_LM;
#ifdef HAVE_CRYPT
extern struct fmt_main fmt_crypt;
#endif
extern struct fmt_main fmt_trip;
extern struct fmt_main fmt_dummy;
extern int unshadow(int argc, char **argv);
extern int unafs(int argc, char **argv);
extern int unique(int argc, char **argv);
int john_main_process = 1;
#if OS_FORK
int john_child_count = 0;
int *john_child_pids = NULL;
#endif
static int children_ok = 1;
static struct db_main database;
static struct fmt_main dummy_format;
static int exit_status = 0;
static void john_register_one(struct fmt_main *format)
{
if (options.format && strcasecmp(options.format, format->params.label))
return;
fmt_register(format);
}
static void john_register_all(void)
{
john_register_one(&fmt_DES);
john_register_one(&fmt_BSDI);
john_register_one(&fmt_MD5);
john_register_one(&fmt_BF);
john_register_one(&fmt_LM);
john_register_one(&fmt_AFS);
john_register_one(&fmt_trip);
john_register_one(&fmt_dummy);
#ifdef HAVE_CRYPT
john_register_one(&fmt_crypt);
#endif
if (!fmt_list) {
fprintf(stderr, "Unknown ciphertext format name requested\n");
error();
}
}
static void john_log_format(void)
{
int min_chunk, chunk;
log_event("- Hash type: %.100s%s%.100s (lengths up to %d%s)",
database.format->params.label,
database.format->params.format_name[0] ? ", " : "",
database.format->params.format_name,
database.format->params.plaintext_length,
database.format->methods.split != fmt_default_split ?
", longer passwords split" : "");
log_event("- Algorithm: %.100s",
database.format->params.algorithm_name);
chunk = min_chunk = database.format->params.max_keys_per_crypt;
if (options.flags & (FLG_SINGLE_CHK | FLG_BATCH_CHK) &&
chunk < SINGLE_HASH_MIN)
chunk = SINGLE_HASH_MIN;
if (chunk > 1)
log_event("- Candidate passwords %s be buffered and "
"tried in chunks of %d",
min_chunk > 1 ? "will" : "may",
chunk);
}
#ifdef _OPENMP
static void john_omp_init(void)
{
john_omp_threads_new = omp_get_max_threads();
if (!john_omp_threads_orig)
john_omp_threads_orig = john_omp_threads_new;
}
#if OMP_FALLBACK
#if defined(__DJGPP__) || defined(__CYGWIN__)
#error OMP_FALLBACK is incompatible with the current DOS and Windows code
#endif
#define HAVE_JOHN_OMP_FALLBACK
static void john_omp_fallback(char **argv) {
if (!getenv("JOHN_NO_OMP_FALLBACK") && john_omp_threads_new <= 1) {
rec_done(-2);
#define OMP_FALLBACK_PATHNAME JOHN_SYSTEMWIDE_EXEC "/" OMP_FALLBACK_BINARY
execv(OMP_FALLBACK_PATHNAME, argv);
perror("execv: " OMP_FALLBACK_PATHNAME);
}
}
#endif
static void john_omp_maybe_adjust_or_fallback(char **argv)
{
if (options.fork && !getenv("OMP_NUM_THREADS")) {
john_omp_threads_new /= options.fork;
if (john_omp_threads_new < 1)
john_omp_threads_new = 1;
omp_set_num_threads(john_omp_threads_new);
john_omp_init();
#ifdef HAVE_JOHN_OMP_FALLBACK
john_omp_fallback(argv);
#endif
}
}
static void john_omp_show_info(void)
{
if (!options.fork && john_omp_threads_orig > 1 &&
database.format && database.format != &dummy_format &&
!rec_restoring_now) {
const char *msg = NULL;
if (!(database.format->params.flags & FMT_OMP))
msg = "no OpenMP support";
else if ((database.format->params.flags & FMT_OMP_BAD))
msg = "poor OpenMP scalability";
if (msg)
#if OS_FORK
fprintf(stderr, "Warning: %s for this hash type, "
"consider --fork=%d\n",
msg, john_omp_threads_orig);
#else
fprintf(stderr, "Warning: %s for this hash type\n",
msg);
#endif
}
/*
* Only show OpenMP info if one of the following is true:
* - we have a format detected for the loaded hashes and it is OpenMP-enabled;
* - we're doing --test and no format is specified (so we will test all,
* including some that are presumably OpenMP-enabled);
* - we're doing --test and the specified format is OpenMP-enabled.
*/
{
int show = 0;
if (database.format &&
(database.format->params.flags & FMT_OMP))
show = 1;
else if ((options.flags & (FLG_TEST_CHK | FLG_FORMAT)) ==
FLG_TEST_CHK)
show = 1;
else if ((options.flags & FLG_TEST_CHK) &&
(fmt_list->params.flags & FMT_OMP))
show = 1;
if (!show)
return;
}
if (options.fork) {
#if OS_FORK
if (john_omp_threads_new > 1)
fprintf(stderr,
"Will run %d OpenMP threads per process "
"(%u total across %u processes)\n",
john_omp_threads_new,
john_omp_threads_new * options.fork, options.fork);
else if (john_omp_threads_orig > 1)
fputs("Warning: OpenMP was disabled due to --fork; "
"a non-OpenMP build may be faster\n", stderr);
#endif
} else {
if (john_omp_threads_new > 1)
fprintf(stderr,
"Will run %d OpenMP threads\n",
john_omp_threads_new);
}
if (john_omp_threads_orig == 1)
fputs("Warning: OpenMP is disabled; "
"a non-OpenMP build may be faster\n", stderr);
}
#endif
#if OS_FORK
static void john_fork(void)
{
int i, pid;
int *pids;
fflush(stdout);
fflush(stderr);
/*
* It may cost less memory to reset john_main_process to 0 before fork()'ing
* the children than to do it in every child process individually (triggering
* copy-on-write of the entire page). We then reset john_main_process back to
* 1 in the parent, but this only costs one page, not one page per child.
*/
john_main_process = 0;
pids = mem_alloc_tiny((options.fork - 1) * sizeof(*pids),
sizeof(*pids));
for (i = 1; i < options.fork; i++) {
switch ((pid = fork())) {
case -1:
pexit("fork");
case 0:
options.node_min += i;
options.node_max = options.node_min;
if (rec_restoring_now) {
unsigned int node_id = options.node_min;
rec_done(-2);
rec_restore_args(1);
if (node_id != options.node_min + i)
fprintf(stderr,
"Inconsistent crash recovery file:"
" %s\n", rec_name);
options.node_min = options.node_max = node_id;
}
sig_init_child();
return;
default:
pids[i - 1] = pid;
}
}
john_main_process = 1;
john_child_pids = pids;
john_child_count = options.fork - 1;
options.node_max = options.node_min;
}
static void john_wait(void)
{
int waiting_for = john_child_count;
log_event("Waiting for %d child%s to terminate",
waiting_for, waiting_for == 1 ? "" : "ren");
log_flush();
fprintf(stderr, "Waiting for %d child%s to terminate\n",
waiting_for, waiting_for == 1 ? "" : "ren");
/*
* Although we may block on wait(2), we still have signal handlers and a timer
* in place, so we're relaying keypresses to child processes via signals.
*/
while (waiting_for) {
int i, status;
int pid = wait(&status);
if (pid == -1) {
if (errno != EINTR)
perror("wait");
} else
for (i = 0; i < john_child_count; i++) {
if (john_child_pids[i] == pid) {
john_child_pids[i] = 0;
waiting_for--;
children_ok = children_ok &&
WIFEXITED(status) && !WEXITSTATUS(status);
break;
}
}
}
/* Close and possibly remove our .rec file now */
rec_done((children_ok && !event_abort) ? -1 : -2);
}
#endif
static char *john_loaded_counts(void)
{
static char s_loaded_counts[80];
if (database.password_count == 1)
return "1 password hash";
sprintf(s_loaded_counts,
database.salt_count > 1 ?
"%d password hashes with %d different salts" :
"%d password hashes with no different salts",
database.password_count,
database.salt_count);
return s_loaded_counts;
}
static void john_load(void)
{
struct list_entry *current;
umask(077);
if (options.flags & FLG_EXTERNAL_CHK)
ext_init(options.external);
if (options.flags & FLG_MAKECHR_CHK) {
options.loader.flags |= DB_CRACKED;
ldr_init_database(&database, &options.loader);
if (options.flags & FLG_PASSWD) {
ldr_show_pot_file(&database, POT_NAME);
database.options->flags |= DB_PLAINTEXTS;
if ((current = options.passwd->head))
do {
ldr_show_pw_file(&database, current->data);
} while ((current = current->next));
} else {
database.options->flags |= DB_PLAINTEXTS;
ldr_show_pot_file(&database, POT_NAME);
}
return;
}
if (options.flags & FLG_STDOUT) {
ldr_init_database(&database, &options.loader);
database.format = &dummy_format;
memset(&dummy_format, 0, sizeof(dummy_format));
dummy_format.params.plaintext_length = options.length;
dummy_format.params.flags = FMT_CASE | FMT_8_BIT;
}
if (options.flags & FLG_PASSWD) {
int total;
if (options.flags & FLG_SHOW_CHK) {
options.loader.flags |= DB_CRACKED;
ldr_init_database(&database, &options.loader);
ldr_show_pot_file(&database, POT_NAME);
if ((current = options.passwd->head))
do {
ldr_show_pw_file(&database, current->data);
} while ((current = current->next));
printf("%s%d password hash%s cracked, %d left\n",
database.guess_count ? "\n" : "",
database.guess_count,
database.guess_count != 1 ? "es" : "",
database.password_count -
database.guess_count);
return;
}
if (options.flags & (FLG_SINGLE_CHK | FLG_BATCH_CHK) &&
status.pass <= 1)
options.loader.flags |= DB_WORDS;
else
if (mem_saving_level)
options.loader.flags &= ~DB_LOGIN;
ldr_init_database(&database, &options.loader);
if ((current = options.passwd->head))
do {
ldr_load_pw_file(&database, current->data);
} while ((current = current->next));
if ((options.flags & FLG_CRACKING_CHK) &&
database.password_count) {
log_init(LOG_NAME, NULL, options.session);
if (status_restored_time)
log_event("Continuing an interrupted session");
else
log_event("Starting a new session");
log_event("Loaded a total of %s", john_loaded_counts());
printf("Loaded %s (%s%s%s [%s])\n",
john_loaded_counts(),
database.format->params.label,
database.format->params.format_name[0] ? ", " : "",
database.format->params.format_name,
database.format->params.algorithm_name);
}
total = database.password_count;
ldr_load_pot_file(&database, POT_NAME);
ldr_fix_database(&database);
if (!database.password_count) {
log_discard();
printf("No password hashes %s (see FAQ)\n",
total ? "left to crack" : "loaded");
} else
if (database.password_count < total) {
log_event("Remaining %s", john_loaded_counts());
printf("Remaining %s\n", john_loaded_counts());
}
if ((options.flags & FLG_PWD_REQ) && !database.salts) exit(0);
}
#ifdef _OPENMP
john_omp_show_info();
#endif
if (options.node_count) {
if (options.node_min != options.node_max) {
log_event("- Node numbers %u-%u of %u%s",
options.node_min, options.node_max,
options.node_count, options.fork ? " (fork)" : "");
fprintf(stderr, "Node numbers %u-%u of %u%s\n",
options.node_min, options.node_max,
options.node_count, options.fork ? " (fork)" : "");
} else {
log_event("- Node number %u of %u",
options.node_min, options.node_count);
fprintf(stderr, "Node number %u of %u\n",
options.node_min, options.node_count);
}
#if OS_FORK
if (options.fork)
john_fork();
#endif
}
}
#if CPU_DETECT
static void CPU_detect_or_fallback(char **argv, int make_check)
{
if (!CPU_detect()) {
#if CPU_REQ
#if CPU_FALLBACK
#if defined(__DJGPP__) || defined(__CYGWIN__)
#error CPU_FALLBACK is incompatible with the current DOS and Windows code
#endif
if (!make_check) {
#define CPU_FALLBACK_PATHNAME JOHN_SYSTEMWIDE_EXEC "/" CPU_FALLBACK_BINARY
execv(CPU_FALLBACK_PATHNAME, argv);
perror("execv: " CPU_FALLBACK_PATHNAME);
}
#endif
fprintf(stderr, "Sorry, %s is required for this build\n",
CPU_NAME);
if (make_check)
exit(0);
error();
#endif
}
}
#else
#define CPU_detect_or_fallback(argv, make_check)
#endif
static void john_init(char *name, int argc, char **argv)
{
int make_check = (argc == 2 && !strcmp(argv[1], "--make_check"));
if (make_check)
argv[1] = "--test=0";
CPU_detect_or_fallback(argv, make_check);
#ifdef _OPENMP
john_omp_init();
#endif
if (!make_check) {
#ifdef HAVE_JOHN_OMP_FALLBACK
john_omp_fallback(argv);
#endif
path_init(argv);
#if JOHN_SYSTEMWIDE
cfg_init(CFG_PRIVATE_FULL_NAME, 1);
#endif
cfg_init(CFG_FULL_NAME, 0);
}
status_init(NULL, 1);
if (argc < 2)
john_register_all(); /* for printing by opt_init() */
opt_init(name, argc, argv);
#ifdef _OPENMP
john_omp_maybe_adjust_or_fallback(argv);
#endif
john_register_all(); /* maybe restricted to one format by options */
common_init();
sig_init();
if (!make_check && !(options.flags & (FLG_SHOW_CHK | FLG_STDOUT))) {
fflush(stdout);
setvbuf(stdout, NULL, _IOLBF, 0);
}
john_load();
}
static void john_run(void)
{
if (options.flags & FLG_TEST_CHK)
exit_status = benchmark_all() ? 1 : 0;
else
if (options.flags & FLG_MAKECHR_CHK)
do_makechars(&database, options.charset);
else
if (options.flags & FLG_CRACKING_CHK) {
int remaining = database.password_count;
if (!(options.flags & FLG_STDOUT)) {
char *where = fmt_self_test(database.format);
if (where) {
fprintf(stderr, "Self test failed (%s)\n",
where);
error();
}
database.format->methods.reset(&database);
log_init(LOG_NAME, POT_NAME, options.session);
status_init(NULL, 1);
john_log_format();
if (idle_requested(database.format))
log_event("- Configured to use otherwise idle "
"processor cycles only");
}
tty_init(options.flags & FLG_STDIN_CHK);
if (options.flags & FLG_SINGLE_CHK)
do_single_crack(&database);
else
if (options.flags & FLG_WORDLIST_CHK)
do_wordlist_crack(&database, options.wordlist,
(options.flags & FLG_RULES) != 0);
else
if (options.flags & FLG_INC_CHK)
do_incremental_crack(&database, options.charset);
else
if (options.flags & FLG_EXTERNAL_CHK)
do_external_crack(&database);
else
if (options.flags & FLG_BATCH_CHK)
do_batch_crack(&database);
status_print();
#if OS_FORK
if (options.fork && john_main_process)
john_wait();
#endif
tty_done();
if (john_main_process && database.password_count < remaining) {
char *might = "Warning: passwords printed above might";
char *partial = " be partial";
char *not_all = " not be all those cracked";
switch (database.options->flags &
(DB_SPLIT | DB_NODUP)) {
case DB_SPLIT:
fprintf(stderr, "%s%s\n", might, partial);
break;
case DB_NODUP:
fprintf(stderr, "%s%s\n", might, not_all);
break;
case (DB_SPLIT | DB_NODUP):
fprintf(stderr, "%s%s and%s\n",
might, partial, not_all);
}
fputs("Use the \"--show\" option to display all of "
"the cracked passwords reliably\n", stderr);
}
}
}
static void john_done(void)
{
if ((options.flags & (FLG_CRACKING_CHK | FLG_STDOUT)) ==
FLG_CRACKING_CHK) {
if (event_abort) {
log_event("Session aborted");
/* We have already printed to stderr from signals.c */
} else if (children_ok) {
log_event("Session completed");
if (john_main_process)
fprintf(stderr, "Session completed\n");
} else {
const char *msg =
"Main process session completed, "
"but some child processes failed";
log_event("%s", msg);
fprintf(stderr, "%s\n", msg);
exit_status = 1;
}
fmt_done(database.format);
}
log_done();
path_done();
check_abort(0);
}
int main(int argc, char **argv)
{
char *name;
#ifdef __DJGPP__
if (--argc <= 0) return 1;
if ((name = strrchr(argv[0], '/')))
strcpy(name + 1, argv[1]);
name = argv[1];
argv[1] = argv[0];
argv++;
#else
if (!argv[0])
name = "john";
else
if ((name = strrchr(argv[0], '/')))
name++;
else
name = argv[0];
#endif
#ifdef __CYGWIN__
strlwr(name);
if (strlen(name) > 4 && !strcmp(name + strlen(name) - 4, ".exe"))
name[strlen(name) - 4] = 0;
#endif
if (!strcmp(name, "unshadow")) {
CPU_detect_or_fallback(argv, 0);
return unshadow(argc, argv);
}
if (!strcmp(name, "unafs")) {
CPU_detect_or_fallback(argv, 0);
return unafs(argc, argv);
}
if (!strcmp(name, "unique")) {
CPU_detect_or_fallback(argv, 0);
return unique(argc, argv);
}
john_init(name, argc, argv);
john_run();
john_done();
return exit_status;
}