forked from openwall/john
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcracker.c
More file actions
1365 lines (1152 loc) · 32.8 KB
/
cracker.c
File metadata and controls
1365 lines (1152 loc) · 32.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-2003,2006,2010-2013,2015,2017 by Solar Designer
* Copyright (c) 2009-2018 by JimF
* Copyright (c) 2011-2025 by magnum
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*/
#define NEED_OS_TIMER
#include "os.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#if (!AC_BUILT || HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif
#include <time.h>
#if (!AC_BUILT || HAVE_SYS_TIMES_H)
#include <sys/times.h>
#endif
#include <errno.h>
#if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
#include <unistd.h>
#endif
#if _MSC_VER || HAVE_IO_H
#include <io.h> // open()
#endif
#include "arch.h"
#include "params.h"
#include "base64_convert.h"
#if CRK_PREFETCH && defined(__SSE__)
#include <xmmintrin.h>
#endif
#include "misc.h"
#include "memory.h"
#include "signals.h"
#include "idle.h"
#include "formats.h"
#include "dyna_salt.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "external.h"
#include "options.h"
#include "config.h"
#include "mask_ext.h"
#include "mask.h"
#include "unicode.h"
#include "cracker.h"
#include "john.h"
#include "fake_salts.h"
#include "sha.h"
#include "john_mpi.h"
#include "path.h"
#include "jumbo.h"
#include "opencl_common.h"
#if HAVE_LIBDL && defined(HAVE_OPENCL)
#include "gpu_common.h"
#endif
#include "rules.h"
#include "tty.h"
#include "color.h"
#ifdef index
#undef index
#endif
extern long clk_tck;
static int crk_process_key_max_keys;
static struct db_main *crk_db;
static struct fmt_params *crk_params;
static struct fmt_methods crk_methods;
#if CRK_PREFETCH
#if 1
static unsigned int crk_prefetch;
#else
#define crk_prefetch CRK_PREFETCH
#endif
#endif
static int crk_key_index, crk_last_key;
static void *crk_last_salt;
static struct db_keys *crk_guesses;
static uint64_t *crk_timestamps;
static char crk_stdout_key[PLAINTEXT_BUFFER_SIZE];
static int kpc_warn, kpc_warn_limit, single_running;
static fix_state_fp hybrid_fix_state;
int crk_stacked_rule_count = 1;
rule_stack crk_rule_stack;
int64_t crk_pot_pos;
void (*crk_fix_state)(void);
int (*crk_process_key)(char *key);
static int process_key_stack_rules(char *key);
/* Expose max_keys_per_crypt to the world (needed in recovery.c) */
int crk_max_keys_per_crypt(void)
{
return options.force_maxkeys ? options.force_maxkeys : crk_params->max_keys_per_crypt;
}
static void crk_dummy_set_salt(void *salt)
{
/* Refresh salt every 30 seconds in case it was thrashed */
if (event_refresh_salt > 30) {
crk_db->format->methods.set_salt(salt);
event_refresh_salt = 0;
}
}
static void crk_dummy_fix_state(void)
{
}
static void crk_init_salt(void)
{
if (!crk_db->salts->next) {
crk_methods.set_salt(crk_db->salts->salt);
crk_methods.set_salt = crk_dummy_set_salt;
}
}
static void crk_help(void)
{
static int printed = 0;
if (!john_main_process || printed)
return;
if ((options.flags & FLG_STDOUT) && isatty(fileno(stdout)))
return;
#ifdef HAVE_MPI
if (mpi_p > 1 || getenv("OMPI_COMM_WORLD_SIZE"))
#ifdef SIGUSR1
fprintf(stderr, "Send SIGUSR1 to mpirun for status\n");
#else
fprintf(stderr, "Send SIGHUP to john process for status\n");
#endif
else
#endif
if (tty_has_keyboard())
fprintf(stderr, "Press 'q' or Ctrl-C to abort, 'h' for help, almost any other key for status\n");
else
fprintf(stderr, "Press Ctrl-C to abort, "
#ifdef SIGUSR1
"or send SIGUSR1 to john process for status\n");
#else
"or send SIGHUP to john process for status\n");
#endif
if (event_delayed_status)
fprintf(stderr, "Delayed status pending...\r");
printed = 1;
}
void crk_init(struct db_main *db, void (*fix_state)(void),
struct db_keys *guesses)
{
char *where;
size_t size;
/*
* We should have already called fmt_self_test() from john.c. This redundant
* self-test is only to catch some more obscure bugs in debugging builds (it
* is a no-op in normal builds). Additionally, we skip it even in debugging
* builds if we're running in --stdout mode (there's no format involved then)
* or if the format has a custom reset() method (we've already called reset(db)
* from john.c, and we don't want to mess with the format's state).
*/
if (db->loaded && db->format->methods.reset == fmt_default_reset && !(options.flags & FLG_NOTESTS)) {
struct db_main *test_db = ldr_init_test_db(db->format, db);
if ((where = fmt_self_test(db->format, test_db))) {
log_event("! Self test failed (%s)", where);
fprintf(stderr, "Self test failed (%s)\n", where);
error();
}
ldr_free_db(test_db, 1);
}
#if HAVE_OPENCL
/* This erases the 'spinning wheel' cursor from self-test */
if (john_main_process && isatty(fileno(stderr)))
fprintf(stderr, " \b");
#endif
status.salt_count = db->salt_count;
status.password_count = db->password_count;
crk_db = db;
crk_params = &db->format->params;
memcpy(&crk_methods, &db->format->methods, sizeof(struct fmt_methods));
#if CRK_PREFETCH && !defined(crk_prefetch)
{
unsigned int m = crk_params->max_keys_per_crypt;
if (m > CRK_PREFETCH) {
unsigned int n = (m + CRK_PREFETCH - 1) / CRK_PREFETCH;
crk_prefetch = (m + n - 1) / n;
/* CRK_PREFETCH / 2 < crk_prefetch <= CRK_PREFETCH */
} else {
/* Actual prefetch will be capped to crypt_all() return value anyway, so let's
* not cap it to max_keys_per_crypt here in case crypt_all() generates more
* candidates on its own. */
crk_prefetch = CRK_PREFETCH;
}
}
#endif
if (db->loaded) crk_init_salt();
crk_process_key_max_keys = 0; /* use slow path at first */
crk_last_key = crk_key_index = 0;
crk_last_salt = NULL;
if (fix_state)
(crk_fix_state = fix_state)();
else
crk_fix_state = crk_dummy_fix_state;
if (options.flags & FLG_MASK_STACKED)
mask_fix_state();
crk_guesses = guesses;
kpc_warn = crk_params->min_keys_per_crypt;
if (db->loaded) {
size = crk_params->max_keys_per_crypt * sizeof(uint64_t);
memset(crk_timestamps = mem_alloc(size), -1, size);
} else
crk_stdout_key[0] = 0;
rec_save();
crk_help();
idle_init(db->format);
if (options.verbosity < VERB_DEFAULT)
kpc_warn_limit = 0;
else
if ((kpc_warn_limit =
cfg_get_int(SECTION_OPTIONS, NULL, "MaxKPCWarnings")) == -1)
kpc_warn_limit = CRK_KPC_WARN;
if (!(options.flags & FLG_SINGLE_CHK))
rules_stacked_after = !!(crk_stacked_rule_count = rules_init_stack(options.rule_stack, &crk_rule_stack, db));
else
crk_stacked_rule_count = 0;
if (crk_stacked_rule_count == 0)
crk_stacked_rule_count = 1;
if (rules_stacked_after)
crk_process_key = process_key_stack_rules;
else
crk_process_key = crk_direct_process_key;
/*
* Resetting crk_process_key above disables the suppressor, but it can
* possibly be re-enabled by a cracking mode.
*/
if (status.suppressor_start && !status.suppressor_end) {
status.suppressor_end = status.cands;
status.suppressor_end_time = status_get_time();
}
}
/*
* crk_remove_salt() is called by crk_remove_hash() when it happens to remove
* the last password hash for a salt.
*/
static void crk_remove_salt(struct db_salt *salt)
{
struct db_salt **current;
crk_db->salt_count--;
status.salt_count = crk_db->salt_count;
current = &crk_db->salts;
while (*current != salt)
current = &(*current)->next;
*current = salt->next;
/* If we kept the salt_hash table, update it */
if (crk_db->salt_hash) {
int hash = crk_methods.salt_hash(salt->salt);
if (crk_db->salt_hash[hash] == salt) {
if (options.verbosity >= VERB_DEBUG) {
fprintf(stderr, "Got rid of %s, %s\n", strncasecmp(crk_params->label, "wpapsk", 6) ? "a salt" : (char*)(salt->salt) + 4, crk_loaded_counts());
status_update_counts();
}
if (salt->next &&
crk_methods.salt_hash(salt->next->salt) == hash)
crk_db->salt_hash[hash] = salt->next;
else
crk_db->salt_hash[hash] = NULL;
}
}
dyna_salt_remove(salt->salt);
}
/*
* Updates the database after a password has been cracked.
*/
static void crk_remove_hash(struct db_salt *salt, struct db_password *pw)
{
struct db_password **start, **current;
int hash, count;
assert(salt->count >= 1);
crk_db->password_count--;
status.password_count = crk_db->password_count;
BLOB_FREE(crk_db->format, pw->binary);
if (!--salt->count) {
salt->list = NULL; /* "single crack" mode might care */
crk_remove_salt(salt);
if (!salt->bitmap)
return;
}
/*
* If there's no bitmap for this salt, assume that next_hash fields are unused
* and don't need to be updated. Only bother with the list.
*/
if (!salt->bitmap) {
current = &salt->list;
while (*current != pw)
current = &(*current)->next;
*current = pw->next;
pw->binary = NULL;
return;
}
hash = crk_db->format->methods.binary_hash[salt->hash_size](pw->binary);
count = 0;
start = current = &salt->hash[hash >> PASSWORD_HASH_SHR];
do {
if (crk_db->format->methods.binary_hash[salt->hash_size]
((*current)->binary) == hash)
count++;
if (*current == pw) {
/*
* If we can, skip the write to hash table to avoid unnecessary page
* copy-on-write when running with "--fork". We can do this when we're about
* to remove this entry from the bitmap, which we'd be checking first.
*/
if (count == 1 && current == start && !pw->next_hash)
break;
*current = pw->next_hash;
} else {
current = &(*current)->next_hash;
}
} while (*current);
assert(count >= 1);
/*
* If we have removed the last entry with the exact hash value from this hash
* bucket (which could also contain entries with nearby hash values in case
* PASSWORD_HASH_SHR is non-zero), we must also reset the corresponding bit.
*/
if (count == 1)
salt->bitmap[hash / (sizeof(*salt->bitmap) * 8)] &=
~(1U << (hash % (sizeof(*salt->bitmap) * 8)));
/*
* If there's a hash table for this salt, assume that the list is only used by
* "single crack" mode, so mark the entry for removal by "single crack" mode
* code if that's what we're running, instead of traversing the list here.
*
* Or, if FMT_REMOVE, the format explicitly intends to traverse the list
* during cracking, and will remove entries at that point.
*/
if (crk_guesses || (crk_params->flags & FMT_REMOVE))
pw->binary = NULL;
}
/* Negative index is not counted/reported (got it from pot sync) */
static int crk_process_guess(struct db_salt *salt, struct db_password *pw, int index)
{
char utf8buf_key[PLAINTEXT_BUFFER_SIZE + 1];
char utf8login[PLAINTEXT_BUFFER_SIZE + 1];
char tmp8[PLAINTEXT_BUFFER_SIZE + 1];
int dupe;
char *key, *utf8key, *repkey, *replogin, *repuid;
if (index >= 0 && index < crk_params->max_keys_per_crypt) {
dupe = crk_timestamps[index] == status.crypts;
crk_timestamps[index] = status.crypts;
} else
dupe = 0;
repkey = key = index < 0 ? "" : crk_methods.get_key(index);
if (crk_db->options->flags & DB_LOGIN) {
replogin = pw->login;
if (options.show_uid_in_cracks)
repuid = pw->uid;
else
repuid = "";
} else
replogin = repuid = "";
if (index >= 0 && (options.store_utf8 || options.report_utf8)) {
if (options.target_enc == UTF_8)
utf8key = key;
else {
utf8key = cp_to_utf8_r(key, utf8buf_key,
PLAINTEXT_BUFFER_SIZE);
// Double-check that the conversion was correct. Our
// fallback is to log, warn and use the original key
// instead. If you see it, we have a bug.
utf8_to_cp_r(utf8key, tmp8,
PLAINTEXT_BUFFER_SIZE);
if (strcmp(tmp8, key)) {
fprintf_color(color_warning, stderr, "Warning, conversion failed %s"
" -> %s -> %s - fallback to codepage\n",
key, utf8key, tmp8);
log_event("Warning, conversion failed %s -> %s"
" -> %s - fallback to codepage", key,
utf8key, tmp8);
utf8key = key;
}
}
if (options.report_utf8) {
repkey = utf8key;
if (options.internal_cp != UTF_8)
replogin = cp_to_utf8_r(replogin,
utf8login, PLAINTEXT_BUFFER_SIZE);
}
if (options.store_utf8)
key = utf8key;
}
// Ok, FIX the salt ONLY if -regen-lost-salts=X was used.
if (options.regen_lost_salts && (crk_params->flags & FMT_DYNAMIC) == FMT_DYNAMIC)
crk_guess_fixup_salt(pw->source, *(char**)(salt->salt));
if (options.max_run_time < 0) {
#if OS_TIMER
timer_abort = 0 - options.max_run_time;
#else
timer_abort = status_get_time() - options.max_run_time;
#endif
}
if (options.max_cands < 0)
john_max_cands = status.cands - options.max_cands + crk_params->max_keys_per_crypt;
/* If we got this crack from a pot sync, don't report or count */
if (index >= 0) {
const char *ct;
char buffer[LINE_BUFFER_SIZE + 1];
if (dupe && !(crk_params->flags & FMT_BLOB))
ct = NULL;
else
ct = ldr_pot_source(
crk_methods.source(pw->source, pw->binary),
buffer);
log_guess(crk_db->options->flags & DB_LOGIN ? replogin : "?",
crk_db->options->flags & DB_LOGIN ? repuid : "",
(char*)ct,
repkey, key, crk_db->options->field_sep_char, index);
if (options.crack_status)
event_pending = event_status = 1;
crk_db->guess_count++;
status.guess_count++;
if (crk_guesses && !dupe) {
strnfcpy(crk_guesses->ptr, key,
crk_params->plaintext_length);
crk_guesses->ptr += crk_params->plaintext_length;
crk_guesses->count++;
}
}
if (!(crk_params->flags & FMT_NOT_EXACT))
crk_remove_hash(salt, pw);
if (options.regen_lost_salts) {
/*
* salt->list pointer was copied to all salts so if the first
* entry was removed, we need to fixup all other salts. If OTOH
* the last hash was removed, we need to drop all salts.
*/
struct db_salt *s = crk_db->salts;
do {
if (!crk_db->password_count)
crk_remove_salt(s);
else if (s->list && s->list->binary == NULL)
s->list = s->list->next;
} while ((s = s->next));
}
if (!crk_db->salts)
return 1;
crk_init_salt();
return 0;
}
char *crk_loaded_counts(void)
{
return john_loaded_counts(crk_db, "Remaining");
}
static int crk_remove_pot_entry(char *ciphertext)
{
struct db_salt *salt;
struct db_password *pw;
char argcopy[LINE_BUFFER_SIZE];
void *pot_salt;
/*
* If the pot entry is truncated from a huge ciphertext, we have
* this alternate code path that's slower but aware of the magic.
*/
if (ldr_isa_pot_source(ciphertext)) {
if ((salt = crk_db->salts))
do {
if ((pw = salt->list))
do {
char *source;
source = crk_methods.source(pw->source,
pw->binary);
if (!ldr_pot_source_cmp(ciphertext, source)) {
if (crk_process_guess(salt, pw, -1))
return 1;
if (!(crk_db->options->flags & DB_WORDS))
break;
}
} while ((pw = pw->next));
} while ((salt = salt->next));
return 0;
}
/*
* We need to copy ciphertext, because the one we got actually
* points to a static buffer in split() and we are going to call
* that function again and compare the results. Thanks to
* Christien Rioux for pointing this out.
*/
ciphertext = strnzcpy(argcopy, ciphertext, sizeof(argcopy));
pot_salt = crk_methods.salt(ciphertext);
dyna_salt_create(pot_salt);
/* Do we still have a hash table for salts? */
if (crk_db->salt_hash) {
salt = crk_db->salt_hash[crk_methods.salt_hash(pot_salt)];
if (!salt)
return 0;
} else
salt = crk_db->salts;
do {
if (!dyna_salt_cmp(pot_salt, salt->salt, crk_params->salt_size))
break;
} while ((salt = salt->next));
dyna_salt_remove(pot_salt);
if (!salt)
return 0;
if (!salt->bitmap) {
if ((pw = salt->list))
do {
char *source;
source = crk_methods.source(pw->source, pw->binary);
//assert(source != ciphertext);
if (!strcmp(source, ciphertext)) {
if (crk_process_guess(salt, pw, -1))
return 1;
if (!(crk_db->options->flags & DB_WORDS))
break;
}
} while ((pw = pw->next));
}
else {
int hash;
char *binary = crk_methods.binary(ciphertext);
hash = crk_methods.binary_hash[salt->hash_size](binary);
BLOB_FREE(crk_db->format, binary);
if (!(salt->bitmap[hash / (sizeof(*salt->bitmap) * 8)] &
(1U << (hash % (sizeof(*salt->bitmap) * 8)))))
return 0;
if ((pw = salt->hash[hash >> PASSWORD_HASH_SHR]))
do {
char *source;
source = crk_methods.source(pw->source, pw->binary);
//assert(source != ciphertext);
if (!strcmp(source, ciphertext)) {
if (crk_process_guess(salt, pw, -1))
return 1;
if (!(crk_db->options->flags & DB_WORDS))
break;
}
} while ((pw = pw->next_hash));
}
return 0;
}
int crk_reload_pot(void)
{
char line[LINE_BUFFER_SIZE];
FILE *pot_file;
int passwords = crk_db->password_count;
int salts = crk_db->salt_count;
event_reload = 0;
if (event_abort)
return 0;
if (crk_params->flags & FMT_NOT_EXACT)
return 0;
if (!(pot_file = fopen(path_expand(options.activepot), "rb")))
pexit("fopen: %s", path_expand(options.activepot));
if (crk_pot_pos) {
if (jtr_fseek64(pot_file, 0, SEEK_END) == -1)
pexit("fseek to end of pot file");
if (crk_pot_pos == jtr_ftell64(pot_file)) {
if (fclose(pot_file))
pexit("fclose");
return 0;
}
if (crk_pot_pos > jtr_ftell64(pot_file)) {
if (john_main_process) {
fprintf(stderr,
"Note: pot file shrunk. Recovering.\n");
}
log_event("Note: pot file shrunk. Recovering.");
rewind(pot_file);
crk_pot_pos = 0;
}
if (jtr_fseek64(pot_file, crk_pot_pos, SEEK_SET) == -1) {
perror("fseek to sync pos. of pot file");
log_event("fseek to sync pos. of pot file: %s",
strerror(errno));
crk_pot_pos = 0;
if (fclose(pot_file))
pexit("fclose");
return 0;
}
}
ldr_in_pot = 1; /* Mutes some warnings from valid() et al */
while (fgetl(line, sizeof(line), pot_file)) {
char *p, *ciphertext = line;
char *fields[10] = { NULL };
if (!(p = strchr(ciphertext, options.loader.field_sep_char)))
continue;
*p = 0;
fields[0] = "";
fields[1] = ciphertext;
ciphertext = crk_methods.prepare(fields, crk_db->format);
if (ldr_trunc_valid(ciphertext, crk_db->format)) {
ciphertext = crk_methods.split(ciphertext, 0,
crk_db->format);
if (crk_remove_pot_entry(ciphertext))
break;
}
}
ldr_in_pot = 0;
crk_pot_pos = jtr_ftell64(pot_file);
if (fclose(pot_file))
pexit("fclose");
passwords -= crk_db->password_count;
salts -= crk_db->salt_count;
if (john_main_process && passwords) {
log_event("+ pot sync removed %d hashes/%d salts; %s",
passwords, salts, crk_loaded_counts());
if (salts && cfg_get_bool(SECTION_OPTIONS, NULL,
"ShowSaltProgress", 0)) {
fprintf(stderr, "%s after pot sync\n", crk_loaded_counts());
status_update_counts();
}
}
return (!crk_db->salts);
}
#ifdef HAVE_MPI
static void crk_mpi_probe(void)
{
static MPI_Status s;
int flag;
MPI_Iprobe(MPI_ANY_SOURCE, JOHN_MPI_RELOAD, MPI_COMM_WORLD, &flag, &s);
if (flag) {
static MPI_Request r;
char buf[16];
event_reload = 1;
MPI_Irecv(buf, 1, MPI_CHAR, MPI_ANY_SOURCE,
JOHN_MPI_RELOAD, MPI_COMM_WORLD, &r);
}
}
#endif
static void crk_poll_files(void)
{
struct stat trigger_stat;
if (options.abort_file &&
stat(path_expand(options.abort_file), &trigger_stat) == 0) {
if (!event_abort && john_main_process)
fprintf(stderr, "Abort file seen\n");
log_event("Abort file seen");
event_pending = event_abort = 1;
}
else if (options.pause_file && stat(path_expand(options.pause_file), &trigger_stat) == 0) {
#if !HAVE_SYS_TIMES_H
clock_t end, start = clock();
#else
struct tms buf;
clock_t end, start = times(&buf);
#endif
status_print(0);
if (john_main_process)
fprintf(stderr, "Pause file seen, going to sleep (session saved)\n");
log_event("Pause file seen, going to sleep");
/* Better save stuff before going to sleep */
rec_save();
do {
int s = 3;
do {
s = sleep(s);
} while (s);
} while (stat(path_expand(options.pause_file), &trigger_stat) == 0);
/* Disregard pause time for stats */
#if !HAVE_SYS_TIMES_H
end = clock();
#else
end = times(&buf);
#endif
status.start_time += (end - start);
int pause_time = (end - start) / clk_tck;
log_event("Pause file removed after %d seconds, continuing", pause_time);
if (john_main_process)
fprintf(stderr, "Pause file removed after %d seconds, continuing\n", pause_time);
}
}
static int crk_process_event(void)
{
static int hugepage_reported;
if (!hugepage_reported) {
const char *msg = hugepage_report();
if (msg) {
log_event("%s", msg);
if (john_main_process)
fprintf(stderr, "%s\n", msg);
}
hugepage_reported = 1;
}
#ifdef HAVE_MPI
if (event_mpiprobe) {
event_mpiprobe = 0;
crk_mpi_probe();
}
#endif
if (event_save) {
event_save = 0;
rec_save();
}
if (event_help)
sig_help();
if (event_status)
status_print(0);
if (event_ticksafety) {
event_ticksafety = 0;
status_ticks_overflow_safety();
}
if (event_poll_files) {
event_poll_files = 0;
#if HAVE_LIBDL && defined(HAVE_OPENCL)
gpu_check_temp();
#endif
crk_poll_files();
}
event_pending = event_reload;
return event_abort;
}
void crk_set_hybrid_fix_state_func_ptr(fix_state_fp fp)
{
hybrid_fix_state = fp;
}
/*
* Called from crk_salt_loop for every salt or, when in Single mode, from
* crk_process_salt with just a specific salt.
*/
static int crk_password_loop(struct db_salt *salt)
{
int count;
unsigned int match, index;
#if CRK_PREFETCH
unsigned int target;
#endif
#if !OS_TIMER
sig_timer_emu_tick();
#endif
idle_yield();
if (event_pending && crk_process_event())
return -1;
/*
* magnum, December 2020:
* I can't fathom how/why this would be the correct place for this, but
* it seems to work and just moving it to the others does break resume
* for hybrid external so here it stays, until further research.
*/
if (hybrid_fix_state)
hybrid_fix_state();
if (kpc_warn_limit && crk_key_index < kpc_warn) {
static int last_warn_kpc, initial_value;
int s;
uint64_t ps = status.cands;
if ((s = status_get_time()))
ps /= s;
if (single_running && crk_db->salt_count)
ps /= crk_db->salt_count;
if (!initial_value)
initial_value = kpc_warn_limit;
if (kpc_warn > crk_params->min_keys_per_crypt)
kpc_warn = crk_params->min_keys_per_crypt;
if (crk_key_index < kpc_warn &&
ps <= (kpc_warn - crk_key_index) &&
last_warn_kpc != crk_key_index) {
last_warn_kpc = crk_key_index;
if (options.node_count)
fprintf(stderr, "%u: ", NODE);
fprintf_color(color_warning, stderr, "Warning: Only %d%s candidate%s buffered%s, "
"minimum %d needed for performance.\n",
crk_key_index,
mask_int_cand.num_int_cand > 1 ? " base" : "",
crk_key_index > 1 ? "s" : "",
single_running ? " for the current salt" : "",
crk_params->min_keys_per_crypt);
if (!--kpc_warn_limit) {
if (options.node_count)
fprintf(stderr, "%u: ", NODE);
fprintf_color(color_warning, stderr,
"Further messages of this type will be suppressed.\n");
log_event(
"- Saw %d calls to crypt_all() with sub-optimal batch size (stopped counting)",
initial_value);
}
}
}
count = crk_key_index;
match = crk_methods.crypt_all(&count, salt);
crk_last_key = count;
status_update_crypts((uint64_t)salt->count * count, count);
if (!match)
return 0;
if (!salt->bitmap) {
struct db_password *pw = salt->list;
do {
if (crk_methods.cmp_all(pw->binary, match))
for (index = 0; index < match; index++)
if (crk_methods.cmp_one(pw->binary, index))
if (crk_methods.cmp_exact(crk_methods.source(pw->source, pw->binary), index)) {
if (crk_process_guess(salt, pw, index))
return 1;
else {
if (!(crk_params->flags & FMT_NOT_EXACT))
break;
}
}
} while ((pw = pw->next));
return 0;
}
#if CRK_PREFETCH
for (index = 0; index < match; index = target) {
unsigned int slot, ahead, lucky;
struct {
unsigned int i;
union {
unsigned int *b;
struct db_password **p;
} u;
} a[CRK_PREFETCH];
target = index + crk_prefetch;
if (target > match)
target = match;
for (slot = 0, ahead = index; ahead < target; slot++, ahead++) {
unsigned int h = salt->index(ahead);
unsigned int *b = &salt->bitmap[h / (sizeof(*salt->bitmap) * 8)];
a[slot].i = h;
a[slot].u.b = b;
#ifdef __SSE__
_mm_prefetch((const char *)b, _MM_HINT_NTA);
#else
*(volatile unsigned int *)b;
#endif
}
lucky = 0;
for (slot = 0, ahead = index; ahead < target; slot++, ahead++) {
unsigned int h = a[slot].i;
if (*a[slot].u.b & (1U << (h % (sizeof(*salt->bitmap) * 8)))) {
struct db_password **pwp = &salt->hash[h >> PASSWORD_HASH_SHR];
#ifdef __SSE__
_mm_prefetch((const char *)pwp, _MM_HINT_NTA);
#else
*(void * volatile *)pwp;
#endif
a[lucky].i = ahead;
a[lucky++].u.p = pwp;
}
}
#if 1
if (!lucky)
continue;
for (slot = 0; slot < lucky; slot++) {
struct db_password *pw = *a[slot].u.p;
/*
* Chances are this will also prefetch the next_hash field and the actual