-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathjohn.c
More file actions
2158 lines (1891 loc) · 59.8 KB
/
john.c
File metadata and controls
2158 lines (1891 loc) · 59.8 KB
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-2024 by Solar Designer
* Copyright (c) 2009-2026, magnum
* Copyright (c) 2021, Claudio
* Copyright (c) 2009-2018, JimF
*
* 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.
*/
#if AC_BUILT
#include "autoconfig.h"
#else
#define _GNU_SOURCE 1 /* for strcasestr */
#ifdef __SIZEOF_INT128__
#define HAVE___INT128 1
#endif
#endif
#define NEED_OS_FORK
#define NEED_OS_TIMER
#include "os.h"
#include <stdio.h>
#if HAVE_DIRENT_H && HAVE_SYS_TYPES_H
#include <dirent.h>
#include <sys/types.h>
#elif _MSC_VER || __MINGW32__
#include <windows.h>
char CPU_req_name[48];
#endif
#if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
#include <unistd.h>
#endif
#include <errno.h>
#if !AC_BUILT
#include <string.h>
#ifndef _MSC_VER
#include <strings.h>
#endif
#else
#if STRING_WITH_STRINGS
#include <string.h>
#include <strings.h>
#elif HAVE_STRING_H
#include <string.h>
#elif HAVE_STRINGS_H
#include <strings.h>
#endif
#endif
#include <stdlib.h>
#include <sys/stat.h>
#if OS_FORK
#include <sys/wait.h>
#include <signal.h>
#endif
#if !AC_BUILT || HAVE_LOCALE_H
#include <locale.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 "openssl_local_overrides.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 "dyna_salt.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "options.h"
#include "config.h"
#include "bench.h"
#ifdef HAVE_FUZZ
#include "fuzz.h"
#endif
#include "charset.h"
#include "single.h"
#include "wordlist.h"
#include "prince.h"
#include "inc.h"
#include "mask.h"
#include "mkv.h"
#include "subsets.h"
#include "external.h"
#include "batch.h"
#include "dynamic_compiler.h"
#include "fake_salts.h"
#include "listconf.h"
#include "crc32.h"
#include "john_mpi.h"
#include "regex.h"
#include "unicode.h"
#include "gpu_common.h"
#include "opencl_common.h"
#ifdef HAVE_ZTEX
#include "ztex_common.h"
#endif
#ifdef NO_JOHN_BLD
#define JOHN_BLD "unk-build-type"
#else
#include "john_build_rule.h"
#endif
#if HAVE_MPI
#ifdef _OPENMP
#define _MP_VERSION " MPI + OMP"
#else
#define _MP_VERSION " MPI"
#endif
#else
#ifdef _OPENMP
#define _MP_VERSION " OMP"
#else
#define _MP_VERSION ""
#endif
#endif
#include "omp_autotune.h"
#include "color.h"
extern int dynamic_Register_formats(struct fmt_main **ptr);
#if CPU_DETECT
extern int CPU_detect(void);
extern char CPU_req_name[];
#endif
extern struct fmt_main fmt_DES, fmt_BSDI, fmt_MD5, fmt_md5crypt_long, fmt_BF;
extern struct fmt_main fmt_scrypt;
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 struct fmt_main fmt_NT;
#ifdef HAVE_ZTEX
extern struct fmt_main fmt_ztex_descrypt;
extern struct fmt_main fmt_ztex_bcrypt;
extern struct fmt_main fmt_ztex_sha512crypt;
extern struct fmt_main fmt_ztex_drupal7;
extern struct fmt_main fmt_ztex_sha256crypt;
extern struct fmt_main fmt_ztex_md5crypt;
extern struct fmt_main fmt_ztex_phpass;
#endif
#include "fmt_externs.h"
extern int unshadow(int argc, char **argv);
extern int unafs(int argc, char **argv);
extern int unique(int argc, char **argv);
extern int undrop(int argc, char **argv);
extern int base64conv(int argc, char **argv);
extern int zip2john(int argc, char **argv);
extern int gpg2john(int argc, char **argv);
extern int rar2john(int argc, char **argv);
int john_main_process = 1;
#if OS_FORK
int john_child_count = 0;
int *john_child_pids = NULL;
#endif
char *john_terminal_locale = "C";
uint64_t john_max_cands;
char *john_session_name = "";
static int children_ok = 1;
static struct db_main database;
static int loaded_extra_pots;
static struct fmt_main dummy_format;
static int exit_status = 0;
static void john_register_one(struct fmt_main *format)
{
if (options.format) {
if (options.format[0] == '/' && options.format[1]) {
static int drop = 1;
if (drop) {
if (fmt_match(&options.format[1], format, 1))
drop = 0;
return;
}
} else if (options.format[0] == '-' && options.format[1]) {
if (fmt_match(&options.format[1], format, 1))
return;
} else if (options.format[0] == '+' && options.format[1]) {
if (!fmt_match(&options.format[1], format, 0))
return;
} else if (!fmt_match(options.format, format, 0))
return;
} else if (!options.format_list)
if (cfg_get_bool(SECTION_DISABLED, SUBSECTION_FORMATS, format->params.label, 0) &&
((options.flags & FLG_TEST_CHK) || options.listconf))
return;
fmt_register(format);
}
static void john_register_all(void)
{
#ifndef DYNAMIC_DISABLED
int i, cnt;
struct fmt_main *selfs;
#endif
if (options.format) {
/* Dynamic compiler format needs case intact and it can't be used with wildcard or lists */
if (strncasecmp(options.format, "dynamic=", 8)) {
strlwr(options.format);
if (options.format[0] != ',' && strchr(options.format, ',')) {
options.format_list = options.format;
options.format = NULL;
}
}
}
/* Let ZTEX formats appear before CPU formats */
#ifdef HAVE_ZTEX
john_register_one(&fmt_ztex_descrypt);
john_register_one(&fmt_ztex_bcrypt);
john_register_one(&fmt_ztex_sha512crypt);
john_register_one(&fmt_ztex_drupal7);
john_register_one(&fmt_ztex_sha256crypt);
john_register_one(&fmt_ztex_md5crypt);
john_register_one(&fmt_ztex_phpass);
#endif
john_register_one(&fmt_DES);
john_register_one(&fmt_BSDI);
john_register_one(&fmt_MD5);
john_register_one(&fmt_md5crypt_long);
john_register_one(&fmt_BF);
john_register_one(&fmt_scrypt);
john_register_one(&fmt_LM);
john_register_one(&fmt_AFS);
john_register_one(&fmt_trip);
/* Add all plug-in formats */
#include "fmt_registers.h"
#ifndef DYNAMIC_DISABLED
/* Add dynamic formats last so they never have precedence */
cnt = dynamic_Register_formats(&selfs);
for (i = 0; i < cnt; ++i)
john_register_one(&(selfs[i]));
#endif
john_register_one(&fmt_dummy);
#if HAVE_CRYPT
john_register_one(&fmt_crypt);
#endif
/* Do we have --format=LIST? If so, re-build fmt_list from it, in requested order. */
if (options.format_list && !fmt_check_custom_list())
error_msg("Could not parse format list '%s'\n", options.format_list);
if (!fmt_list) {
if (john_main_process) {
fprintf(stderr, "Error: No format matched requested %s '%s'\n", fmt_type(options.format), options.format);
}
error();
}
}
static void john_log_format(void)
{
int enc_len, utf8_len, cmp_len;
char max_len_s[128];
/* make sure the format is properly initialized */
#if HAVE_OPENCL
if (!(options.acc_devices->count && options.fork &&
strstr(database.format->params.label, "-opencl")))
#endif
fmt_init(database.format);
utf8_len = enc_len = cmp_len = database.format->params.plaintext_length;
if (options.target_enc == UTF_8)
utf8_len /= 3;
if (!(database.format->params.flags & FMT_8_BIT) ||
options.target_enc != UTF_8) {
/* Not using UTF-8 so length is not ambiguous */
snprintf(max_len_s, sizeof(max_len_s), "%d", enc_len);
} else if (!fmt_raw_len || fmt_raw_len == enc_len) {
/* Example: Office and thin dynamics */
snprintf(max_len_s, sizeof(max_len_s),
"%d [worst case UTF-8] to %d [ASCII]",
utf8_len, enc_len);
} else if (enc_len == 3 * fmt_raw_len) {
/* Example: NT */
snprintf(max_len_s, sizeof(max_len_s), "%d", utf8_len);
cmp_len = utf8_len;
} else {
/* Example: SybaseASE */
snprintf(max_len_s, sizeof(max_len_s),
"%d [worst case UTF-8] to %d [ASCII]",
utf8_len, fmt_raw_len);
cmp_len = fmt_raw_len;
}
log_event("- Hash type: %.100s%s%.100s (min-len %d, max-len %s%s)",
database.format->params.label,
database.format->params.format_name[0] ? ", " : "",
database.format->params.format_name,
database.format->params.plaintext_min_length,
max_len_s,
(database.format == &fmt_DES || database.format == &fmt_LM) ?
", longer passwords split" : "");
log_event("- Algorithm: %.100s",
database.format->params.algorithm_name);
if (cmp_len < MAX_PLAINTEXT_LENGTH && (!options.force_maxlength || options.force_maxlength > cmp_len) &&
(options.flags & (FLG_BATCH_CHK|FLG_SINGLE_CHK|FLG_WORDLIST_CHK|FLG_LOOPBACK_CHK|FLG_PRINCE_CHK|FLG_EXTERNAL_CHK)))
printf("Note: Passwords longer than %s %s%s\n", max_len_s,
(database.format->params.flags & FMT_TRUNC) ?
((database.options->flags & DB_SPLIT) ? "split" : "truncated") : "rejected",
(database.format->params.flags & FMT_TRUNC) ? " (property of the hash)" : "");
}
static void john_log_format2(void)
{
int min_chunk, chunk;
/* Messages require extra info not available in john_log_format().
These are delayed until mask_init(), fmt_reset() */
chunk = min_chunk = database.format->params.max_keys_per_crypt;
if (options.force_maxkeys && options.force_maxkeys < chunk)
chunk = min_chunk = options.force_maxkeys;
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 || defined(OMP_FALLBACK_BINARY)
#if defined(__DJGPP__)
#error OMP_FALLBACK is incompatible with the current DOS 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);
#ifdef JOHN_SYSTEMWIDE_EXEC
#define OMP_FALLBACK_PATHNAME JOHN_SYSTEMWIDE_EXEC "/" OMP_FALLBACK_BINARY
#else
#define OMP_FALLBACK_PATHNAME path_expand("$JOHN/" OMP_FALLBACK_BINARY)
#endif
execv(OMP_FALLBACK_PATHNAME, argv);
#ifdef JOHN_SYSTEMWIDE_EXEC
perror("execv: " OMP_FALLBACK_PATHNAME);
#else
perror("execv: $JOHN/" OMP_FALLBACK_BINARY);
#endif
}
}
#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.verbosity >= VERB_DEFAULT)
#if HAVE_MPI
if (mpi_p == 1)
#endif
if (database.format && database.format->params.label &&
!strstr(database.format->params.label, "-opencl") &&
!strstr(database.format->params.label, "-ztex"))
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
if (!(options.flags & (FLG_PIPE_CHK | FLG_STDIN_CHK)))
fprintf(stderr, "Warning: %s for this hash type, "
"consider --fork=%d\n",
msg, john_omp_threads_orig);
else
#endif
fprintf(stderr, "Warning: %s for this hash type\n",
msg);
}
}
/*
* 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 HAVE_MPI
/*
* If OMP_NUM_THREADS is set, we assume the user knows what
* he is doing. Here's how to pass it to remote hosts:
* mpirun -x OMP_NUM_THREADS=4 -np 4 -host ...
*/
if (mpi_p > 1) {
if (getenv("OMP_NUM_THREADS") == NULL &&
cfg_get_bool(SECTION_OPTIONS, SUBSECTION_MPI,
"MPIOMPmutex", 1)) {
if (cfg_get_bool(SECTION_OPTIONS, SUBSECTION_MPI,
"MPIOMPverbose", 1) && mpi_id == 0)
fprintf(stderr, "MPI in use, disabling OMP "
"(see doc/README.mpi)\n");
omp_set_num_threads(1);
john_omp_threads_orig = 0; /* Mute later warning */
} else if (john_omp_threads_orig > 1 &&
cfg_get_bool(SECTION_OPTIONS, SUBSECTION_MPI,
"MPIOMPverbose", 1) && mpi_id == 0)
fprintf(stderr, "Note: Running both MPI and OMP"
" (see doc/README.mpi)\n");
} else
#endif
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)
if (options.verbosity >= VERB_DEFAULT)
if (john_main_process) {
const char *format = database.format ?
database.format->params.label : options.format;
if (format && strstr(format, "-opencl"))
fputs("Warning: OpenMP is disabled; "
"GPU may be under-utilized\n", stderr);
else
fputs("Warning: OpenMP is disabled; "
"a non-OpenMP build may be faster\n", stderr);
}
}
#endif
static void john_set_tristates(void)
{
/* Config CrackStatus may be overridden by --crack-status tri-state */
if (options.crack_status == -1)
options.crack_status = cfg_get_bool(SECTION_OPTIONS, NULL, "CrackStatus", 0);
}
#if OS_FORK
static void john_fork(void)
{
int i, pid;
int *pids;
fflush(stdout);
fflush(stderr);
#if HAVE_MPI
/*
* We already initialized MPI before knowing this is actually a fork session.
* So now we need to tear that "1-node MPI session" down before forking, or
* all sorts of funny things might happen.
*/
mpi_teardown();
#endif
/*
* 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));
unsigned int range = options.node_max - options.node_min + 1;
unsigned int npf = range / options.fork;
for (i = 1; i < options.fork; i++) {
switch ((pid = fork())) {
case -1:
pexit("fork");
case 0:
sig_preinit();
if (rec_restoring_now) {
unsigned int save_min = options.node_min;
unsigned int save_max = options.node_max;
unsigned int save_count = options.node_count;
unsigned int save_fork = options.fork;
options.node_min += i * npf;
options.node_max = options.node_min + npf - 1;
rec_done(-2);
rec_restore_args(1);
john_set_tristates();
if (options.node_min != save_min ||
options.node_max != save_max ||
options.node_count != save_count ||
options.fork != save_fork)
error_msg("Inconsistent crash recovery file: %s\n", rec_name);
}
options.node_min += i * npf;
options.node_max = options.node_min + npf - 1;
#if HAVE_OPENCL
/* Poor man's multi-device support */
if (options.acc_devices->count &&
strstr(database.format->params.label, "-opencl")) {
/* Postponed format init in forked process */
fmt_init(database.format);
}
#endif
#if HAVE_ZTEX
if (strstr(database.format->params.label, "-ztex")) {
list_init(&ztex_use_list);
list_extract_list(ztex_use_list, ztex_detected_list,
i * ztex_devices_per_fork, ztex_devices_per_fork);
ztex_fork_num = i;
usleep(i * 100000);
}
#endif
sig_init_child();
return;
default:
pids[i - 1] = pid;
}
}
options.node_max = options.node_min + npf - 1;
#if HAVE_OPENCL
/* Poor man's multi-device support */
if (options.acc_devices->count &&
strstr(database.format->params.label, "-opencl")) {
/* Postponed format init in mother process */
fmt_init(database.format);
}
#endif
#if HAVE_ZTEX
if (strstr(database.format->params.label, "-ztex")) {
list_init(&ztex_use_list);
list_extract_list(ztex_use_list, ztex_detected_list,
0, ztex_devices_per_fork);
}
#endif
john_main_process = 1;
john_child_pids = pids;
john_child_count = options.fork - 1;
}
/*
* This is the "equivalent" of john_fork() for MPI runs. We are mostly
* mimicing a -fork run, especially for resuming a session.
*/
#if HAVE_MPI
static void john_set_mpi(void)
{
unsigned int range = options.node_max - options.node_min + 1;
unsigned int npf = range / mpi_p;
if (mpi_p > 1) {
if (!john_main_process) {
if (rec_restoring_now) {
unsigned int save_min = options.node_min;
unsigned int save_max = options.node_max;
unsigned int save_count = options.node_count;
unsigned int save_fork = options.fork;
options.node_min += mpi_id * npf;
options.node_max = options.node_min + npf - 1;
rec_done(-2);
rec_restore_args(1);
john_set_tristates();
if (options.node_min != save_min ||
options.node_max != save_max ||
options.node_count != save_count ||
options.fork != save_fork)
error_msg("Inconsistent crash recovery file: %s\n", rec_name);
}
}
}
options.node_min += mpi_id * npf;
options.node_max = options.node_min + npf - 1;
fflush(stdout);
fflush(stderr);
}
#endif
static void john_wait(void)
{
log_flush();
/* Tell our friends there is nothing more to crack! */
if (!database.password_count && !options.reload_at_crack &&
cfg_get_bool(SECTION_OPTIONS, NULL, "ReloadAtDone", 1))
raise(SIGUSR2);
if (!john_main_process)
return;
int waiting_for = john_child_count;
log_event("Waiting for %d child%s to terminate",
waiting_for, waiting_for == 1 ? "" : "ren");
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
#if HAVE_MPI
static void john_mpi_wait(void)
{
if (!database.password_count && !options.reload_at_crack) {
int i;
for (i = 0; i < mpi_p; i++) {
if (i == mpi_id)
continue;
if (mpi_req[i] == NULL)
mpi_req[i] = mem_alloc_tiny(sizeof(MPI_Request),
MEM_ALIGN_WORD);
else
if (*mpi_req[i] != MPI_REQUEST_NULL)
continue;
MPI_Isend("r", 1, MPI_CHAR, i, JOHN_MPI_RELOAD,
MPI_COMM_WORLD, mpi_req[i]);
}
}
if (john_main_process) {
log_event("Waiting for other node%s to terminate",
mpi_p > 2 ? "s" : "");
fprintf(stderr, "Waiting for other node%s to terminate session%s\n",
mpi_p > 2 ? "s" : "", john_session_name);
mpi_teardown();
}
/* Close and possibly remove our .rec file now */
rec_done(!event_abort ? -1 : -2);
}
#endif
char *john_loaded_counts(struct db_main *db, char *prelude)
{
static char buf[128];
char nbuf[24];
if (db->password_count == 0)
return "No remaining hashes";
if (db->password_count == 1 && !options.regen_lost_salts) {
sprintf(buf, "%s 1 password hash", prelude);
return buf;
}
int salt_count = db->salt_count;
/* At the time we say "Loaded xx hashes", the regen code hasn't yet updated the salt_count */
if (options.regen_lost_salts && salt_count == 1)
salt_count = regen_salts_count;
int p = sprintf(buf, "%s %d password hash%s with %s %s salts", prelude, db->password_count,
db->password_count > 1 ? "es" : "",
salt_count > 1 ? jtr_itoa(salt_count, nbuf, 24, 10) : "no",
(salt_count > 1 && options.regen_lost_salts) ? "possible" : "different");
if (p >= 0) {
if (options.regen_lost_salts && db->password_count < salt_count) {
int bf_penalty = 10 * salt_count / db->password_count;
if (bf_penalty > 10)
sprintf(buf + p, " (%d.%dx salt BF penalty)", bf_penalty / 10, bf_penalty % 10);
} else {
int bf_penalty = options.regen_lost_salts ? regen_salts_count : 1;
int boost = (10 * bf_penalty * db->password_count + (salt_count / 2)) / salt_count;
if (salt_count > 1 && boost > 10)
sprintf(buf + p, " (%d.%dx same-salt boost)", boost / 10, boost % 10);
}
}
return buf;
}
static void john_load_conf(void)
{
int internal, target;
if (!(options.flags & FLG_VERBOSITY)) {
options.verbosity = cfg_get_int(SECTION_OPTIONS, NULL,
"Verbosity");
/* If it doesn't exist in john.conf it ends up as -1 */
if (options.verbosity == -1)
options.verbosity = VERB_DEFAULT;
if (options.verbosity < 1 || options.verbosity > VERB_DEBUG) {
if (john_main_process)
fprintf(stderr, "Invalid verbosity level in "
"config file, use 1-%u (default %u)"
" or %u for debug\n",
VERB_MAX, VERB_DEFAULT, VERB_DEBUG);
error();
}
}
if (options.activepot == NULL) {
if (options.secure)
options.activepot = str_alloc_copy(SEC_POT_NAME);
else
options.activepot = str_alloc_copy(POT_NAME);
}
if (options.activewordlistrules == NULL)
if (!(options.activewordlistrules =
cfg_get_param(SECTION_OPTIONS, NULL,
"BatchModeWordlistRules")))
options.activewordlistrules =
str_alloc_copy(SUBSECTION_WORDLIST);
if (options.activesinglerules == NULL)
if (!(options.activesinglerules =
cfg_get_param(SECTION_OPTIONS, NULL,
"SingleRules")))
options.activesinglerules =
str_alloc_copy(SUBSECTION_SINGLE);
if ((options.flags & FLG_LOOPBACK_CHK) &&
!(options.flags & FLG_RULES_CHK)) {
if ((options.activewordlistrules =
cfg_get_param(SECTION_OPTIONS, NULL,
"LoopbackRules")))
options.flags |= FLG_RULES_CHK;
}
if ((options.flags & FLG_WORDLIST_CHK) &&
!(options.flags & FLG_RULES_CHK)) {
if ((options.activewordlistrules =
cfg_get_param(SECTION_OPTIONS, NULL,
"WordlistRules")))
{
if (strlen(options.activewordlistrules) == 0)
options.activewordlistrules = NULL;
else
options.flags |= FLG_RULES_CHK;
}
}
/* EmulateBrokenEncoding feature */
options.replacement_character = 0;
if (cfg_get_bool(SECTION_OPTIONS, NULL, "EmulateBrokenEncoding", 0)) {
const char *value;
value = cfg_get_param(SECTION_OPTIONS, NULL, "ReplacementCharacter");
if (value != NULL)
options.replacement_character = value[0];
}
options.secure = cfg_get_bool(SECTION_OPTIONS, NULL, "SecureMode", 0);
options.show_uid_in_cracks = cfg_get_bool(SECTION_OPTIONS, NULL, "ShowUIDinCracks", 0);
options.reload_at_crack =
cfg_get_bool(SECTION_OPTIONS, NULL, "ReloadAtCrack", 0);
options.reload_at_save =
cfg_get_bool(SECTION_OPTIONS, NULL, "ReloadAtSave", 1);
options.abort_file = cfg_get_param(SECTION_OPTIONS, NULL, "AbortFile");
options.pause_file = cfg_get_param(SECTION_OPTIONS, NULL, "PauseFile");
#if HAVE_OPENCL
if (cfg_get_bool(SECTION_OPTIONS, SUBSECTION_OPENCL, "ForceScalar", 0))
options.flags |= FLG_SCALAR;
#endif
options.loader.log_passwords = options.secure ||
cfg_get_bool(SECTION_OPTIONS, NULL, "LogCrackedPasswords", 0);
if (!options.input_enc && !(options.flags & FLG_TEST_CHK)) {
if ((options.flags & FLG_LOOPBACK_CHK) &&
cfg_get_bool(SECTION_OPTIONS, NULL, "UnicodeStoreUTF8", 0))
options.input_enc = UTF_8;
else {
options.input_enc =
cp_name2id(cfg_get_param(SECTION_OPTIONS, NULL, "DefaultEncoding"), 1);
}
options.default_enc = options.input_enc;
}
/* Pre-init in case some format's prepare() needs it */
internal = options.internal_cp;
target = options.target_enc;
initUnicode(UNICODE_UNICODE);
options.internal_cp = internal;
options.target_enc = target;
options.unicode_cp = CP_UNDEF;
}
static void john_load_conf_db(void)
{
if (options.flags & FLG_STDOUT) {
/* john.conf alternative for --internal-codepage */
if (!options.internal_cp && options.target_enc == UTF_8 &&
(options.flags & (FLG_RULES_IN_USE | FLG_BATCH_CHK | FLG_MASK_CHK)))
if (!(options.internal_cp =
cp_name2id(cfg_get_param(SECTION_OPTIONS, NULL, "DefaultInternalCodepage"), 1)))
options.internal_cp =
cp_name2id(cfg_get_param(SECTION_OPTIONS, NULL, "DefaultInternalEncoding"), 1);
}
if (!options.unicode_cp)
initUnicode(UNICODE_UNICODE);
options.report_utf8 = cfg_get_bool(SECTION_OPTIONS,
NULL, "AlwaysReportUTF8", 0);
/* Unicode (UTF-16) formats may lack encoding support. We
must stop the user from trying to use it because it will
just result in false negatives. */
if (database.format && options.target_enc != ENC_RAW && options.target_enc != ISO_8859_1 &&
database.format->params.flags & FMT_UNICODE && !(database.format->params.flags & FMT_ENC)) {
if (john_main_process)
fprintf(stderr, "This format does not yet support"
" other encodings than ISO-8859-1\n");
error();
}
if (database.format && database.format->params.flags & FMT_UNICODE)
options.store_utf8 = cfg_get_bool(SECTION_OPTIONS,
NULL, "UnicodeStoreUTF8", 0);
else
options.store_utf8 = options.target_enc != ENC_RAW && cfg_get_bool(SECTION_OPTIONS, NULL, "CPstoreUTF8", 0);
if (options.target_enc != options.input_enc &&
options.input_enc != UTF_8) {
if (john_main_process)
fprintf(stderr, "Target encoding can only be specified"
" if input encoding is UTF-8\n");
error();
}
if (john_main_process)
if (!(options.flags & FLG_SHOW_CHK) && !options.loader.showuncracked) {
if (options.flags & (FLG_PASSWD | FLG_WORDLIST_CHK |
FLG_STDIN_CHK | FLG_PIPE_CHK))
if (options.default_enc && options.input_enc != ENC_RAW)
fprintf(stderr, "Using default input encoding: %s\n",
cp_id2name(options.input_enc));
if (options.target_enc != options.input_enc &&
(!database.format ||
!(database.format->params.flags & FMT_UNICODE))) {
if (options.default_target_enc)
fprintf(stderr, "Using default target "
"encoding: %s\n",
cp_id2name(options.target_enc));
else
fprintf(stderr, "Target encoding: %s\n",
cp_id2name(options.target_enc));
}
if (options.input_enc != options.internal_cp)
if (database.format &&
(database.format->params.flags & FMT_UNICODE))
fprintf(stderr, "Rules/masks using %s\n",
cp_id2name(options.internal_cp));
}
}
static void load_extra_pots(struct db_main *db, void (*process_file)(struct db_main *db, char *name))
{
struct cfg_list *list;
struct cfg_line *line;
if ((list = cfg_get_list("List.Extra:", "Potfiles")))
if ((line = list->head))
do {
struct stat s;
char *name = (char*)path_expand(line->data);
loaded_extra_pots = 1;
if (!stat(name, &s) && s.st_mode & S_IFREG)
process_file(db, name);
#if HAVE_DIRENT_H && HAVE_SYS_TYPES_H
else if (s.st_mode & S_IFDIR) {
DIR *dp;
dp = opendir(name);
if (dp != NULL) {
struct dirent *ep;
while ((ep = readdir(dp))) {
char dname[2 * PATH_BUFFER_SIZE];