-
Notifications
You must be signed in to change notification settings - Fork 35
/
crackalack_lookup.c
2249 lines (1791 loc) · 83.2 KB
/
crackalack_lookup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Rainbow Crackalack: crackalack_lookup.c
* Copyright (C) 2018-2021 Joe Testa <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms version 3 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Performs GPU-accelerated password hash lookups on rainbow tables.
*/
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/sysinfo.h>
#define O_BINARY 0
#endif
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "opencl_setup.h"
#include "charset.h"
#include "clock.h"
#include "cpu_rt_functions.h"
#include "hash_validate.h"
#include "misc.h"
#include "rtc_decompress.h"
#include "shared.h"
#include "test_shared.h" /* TODO: move hex_to_bytes() elsewhere. */
#include "verify.h"
#include "version.h"
#define VERBOSE 1
#define PRECOMPUTE_KERNEL_PATH "precompute.cl"
#define PRECOMPUTE_NTLM8_KERNEL_PATH "precompute_ntlm8.cl"
#define PRECOMPUTE_NTLM9_KERNEL_PATH "precompute_ntlm9.cl"
#define FALSE_ALARM_KERNEL_PATH "false_alarm_check.cl"
#define FALSE_ALARM_NTLM8_KERNEL_PATH "false_alarm_check_ntlm8.cl"
#define FALSE_ALARM_NTLM9_KERNEL_PATH "false_alarm_check_ntlm9.cl"
#define HASH_FILE_FORMAT_PLAIN 1
#define HASH_FILE_FORMAT_PWDUMP 2
/* Struct to form a linked list of precomputed end indices, and potential start indices (which are usually false alarms). */
struct _precomputed_and_potential_indices {
char *username; /* Non-NULL if loaded file format is pwdump. */
char *hash;
cl_ulong *precomputed_end_indices;
cl_uint num_precomputed_end_indices;
cl_ulong *potential_start_indices;
unsigned int num_potential_start_indices;
unsigned int potential_start_indices_size;
unsigned int *potential_start_index_positions; /* Buffer size is always num_potential_start_indices. */
char *plaintext; /* Set if hash is cracked. */
char *index_filename; /* File path containing the ".index" file. */
struct _precomputed_and_potential_indices *next;
};
typedef struct _precomputed_and_potential_indices precomputed_and_potential_indices;
/* Struct to represent one GPU device. */
typedef struct {
cl_uint device_number;
cl_device_id device;
cl_context context;
cl_program program;
cl_kernel kernel;
cl_command_queue queue;
cl_uint num_work_units;
} gpu_dev;
/* Struct to pass arguments to a host thread. */
typedef struct {
unsigned int hash_type;
char *hash_name;
char *username; /* Non-NULL when pwdump format input file given. */
char *hash; /* In hex. */
char *charset;
char *charset_name;
unsigned int plaintext_len_min;
unsigned int plaintext_len_max;
unsigned int table_index;
unsigned int reduction_offset;
unsigned int chain_len;
unsigned int total_devices;
uint64_t *results;
unsigned int num_results;
cl_ulong *potential_start_indices;
unsigned int num_potential_start_indices;
/* Buffer size is always num_potential_start_indices. */
unsigned int *potential_start_index_positions;
/* Length is always num_potential_start_indices. */
cl_ulong *hash_base_indices;
gpu_dev gpu;
} thread_args;
/* Struct to pass to binary search threads. */
typedef struct {
cl_ulong *rainbow_table;
unsigned int num_chains;
precomputed_and_potential_indices *ppi_head;
unsigned int thread_number;
unsigned int total_threads;
} search_thread_args;
/* Struct to hold node in linked list of preloaded tables. */
struct _preloaded_table {
char *filepath;
cl_ulong *rainbow_table;
unsigned int num_chains;
struct _preloaded_table *next;
};
typedef struct _preloaded_table preloaded_table;
typedef struct {
char *rt_dir;
} preloading_thread_args;
unsigned int count_tables(char *dir);
void find_rt_params(char *dir, rt_parameters *rt_params);
void free_loaded_hashes(char **usernames, char **hashes);
void *host_thread_false_alarm(void *ptr);
void *preloading_thread(void *ptr);
void print_eta_precompute();
cl_ulong *search_precompute_cache(char *index_data, unsigned int *num_indices, char *filename, unsigned int filename_size);
void search_tables(unsigned int total_tables, precomputed_and_potential_indices *ppi, thread_args *args);
void save_cracked_hash(precomputed_and_potential_indices *ppi, unsigned int hash_type);
/* The path of the pot file to store cracked hashes in. This can be overridden by
* a command line arg. */
char jtr_pot_filename[128] = "rainbowcrackalack_jtr.pot";
char hashcat_pot_filename[128] = "rainbowcrackalack_hashcat.pot";
/* The number of seconds spent on precomputation, file I/O, searching, and false alarm
* checking. */
double time_precomp = 0, time_io = 0, time_searching = 0, time_falsealarms = 0;
/* The total number of false alarms, chains processed, respectively. */
uint64_t num_falsealarms = 0, num_chains_processed = 0;
/* The total number of hashes cracked in this invokation and number of tables
* processed, respectively. */
unsigned int num_cracked = 0, num_tables_processed = 0;
/* Mutex to protect the precomputed_and_potential_indices array. _*/
pthread_mutex_t ppi_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Barrier to ensure that kernels on multiple devices are all run at the same time.
* The closed-source AMD driver on Windows effectively blocks other devices while
* one kernel is running; this ensures parallelization in that environment, since
* all kernels will run at once. The open source AMD ROCm driver on Linux may or
* may not get a very slight performance bump with this enabled. */
pthread_barrier_t barrier = {0};
/* Set to 1 if AMD GPUs found. */
unsigned int is_amd_gpu = 0;
/* The global work size, as over-ridden by the user on the command line. */
size_t user_provided_gws = 0;
/* The platform number to disable (-1 to not disable any). */
int disable_platform = -1;
/* The total number of precomputed indices loaded into memory. Each one of these is
* a cl_ulong (8 bytes). */
uint64_t total_precomputed_indices_loaded = 0;
/* Set to 1 if the NTLM8/9 message was printed. This prevents console spam. */
unsigned int printed_precompute_optimized_message = 0;
unsigned int printed_false_alarm_optimized_message = 0;
/* The total number of tables in all subdirectories of the directory given
* by the user. */
unsigned int total_tables = 0;
/* Set to 1 by the preloading thread to indicate that no more tables exist for loading. */
unsigned int table_loading_complete = 0;
/* The current size of the preloaded tables list. */
unsigned int num_preloaded_tables_available = 0;
/* A linked list of preloaded tables. */
preloaded_table *preloaded_table_list = NULL;
/* Condition for the main thread to wait for more tables on. */
pthread_cond_t condition_wait_for_tables = PTHREAD_COND_INITIALIZER;
/* Condition for the preloading thread to wait on (when the MAX_PRELOAD_NUM is reached). */
pthread_cond_t condition_continue_loading_tables = PTHREAD_COND_INITIALIZER;
/* The lock for the preloaded tables system. */
pthread_mutex_t preloaded_tables_lock = PTHREAD_MUTEX_INITIALIZER;
/* The time at which precomputation begins. */
struct timespec precompute_start_time = {0};
/* The time at which table searching begins. */
struct timespec search_start_time = {0};
/* Number of uncracked hashes. */
unsigned int num_hashes = 0;
/* Number of hashes precomputed so far. */
unsigned int num_hashes_precomputed = 0;
/* Total number of hashes that will be precomputed. */
unsigned int num_hashes_precomputed_total = 0;
/* The total number of tables to preload in memory while binary searching and false
* alarm checking is done by the main thread. */
#define MAX_PRELOAD_NUM 2
#define LOCK_PPI() \
if (pthread_mutex_lock(&ppi_mutex)) { perror("Failed to lock mutex"); exit(-1); }
#define UNLOCK_PPI() \
if (pthread_mutex_unlock(&ppi_mutex)) { perror("Failed to unlock mutex"); exit(-1); }
/* Adds a potential start index (and position within the chain) to check for false
* alarms. */
void add_potential_start_index_and_position(precomputed_and_potential_indices *ppi, cl_ulong start, unsigned int position) {
#define POTENTIAL_START_INDICES_INITIAL_SIZE 16
LOCK_PPI();
/* Initialize the potential_start_indices buffer if it isn't already. */
if (ppi->potential_start_indices == NULL) {
ppi->potential_start_indices = calloc(POTENTIAL_START_INDICES_INITIAL_SIZE, sizeof(cl_ulong));
ppi->potential_start_index_positions = calloc(POTENTIAL_START_INDICES_INITIAL_SIZE, sizeof(cl_ulong));
if ((ppi->potential_start_indices == NULL) || (ppi->potential_start_index_positions == NULL)) {
fprintf(stderr, "Failed to initialize potential_start_indices / potential_start_index_positions buffer.\n");
exit(-1);
}
ppi->potential_start_indices_size = POTENTIAL_START_INDICES_INITIAL_SIZE;
}
/* If its time to re-size the array... */
if (ppi->num_potential_start_indices == ppi->potential_start_indices_size) {
unsigned int new_size_in_ulongs = ppi->potential_start_indices_size * 2;
/*printf("Resizing array from %u to %u.\n", ppi->potential_start_indices_size, new_size_in_ulongs);*/
ppi->potential_start_indices = recalloc(ppi->potential_start_indices, new_size_in_ulongs * sizeof(cl_ulong), ppi->potential_start_indices_size * sizeof(cl_ulong));
ppi->potential_start_index_positions = recalloc(ppi->potential_start_index_positions, new_size_in_ulongs * sizeof(cl_ulong), ppi->potential_start_indices_size * sizeof(cl_ulong));
if ((ppi->potential_start_indices == NULL) || (ppi->potential_start_index_positions == NULL)) {
fprintf(stderr, "Failed to re-allocate potential_start_indices/potential_start_index_positions buffer to %u.\n", new_size_in_ulongs);
exit(-1);
}
ppi->potential_start_indices_size = new_size_in_ulongs;
}
ppi->potential_start_indices[ppi->num_potential_start_indices] = start;
ppi->potential_start_index_positions[ppi->num_potential_start_indices] = position;
ppi->num_potential_start_indices++;
UNLOCK_PPI();
}
void check_false_alarms(precomputed_and_potential_indices *ppi, thread_args *args) {
pthread_t threads[MAX_NUM_DEVICES] = {0};
char time_str[128] = {0};
struct timespec start_time = {0};
cl_ulong plaintext_space_up_to_index[MAX_PLAINTEXT_LEN] = {0};
unsigned int num_potential_start_indices = 0, i = 0, j = 0;
unsigned int total_devices = args[0].total_devices;
cl_ulong plaintext_space_total = 0;
double time_delta = 0.0;
precomputed_and_potential_indices *ppi_cur = ppi;
cl_ulong *potential_start_indices = NULL, *hash_base_indices = NULL;
unsigned int *potential_start_index_positions = NULL;
precomputed_and_potential_indices **ppi_refs = NULL;
/* First count all the potential start indices. */
while(ppi_cur) {
num_potential_start_indices += ppi_cur->num_potential_start_indices;
ppi_cur = ppi_cur->next;
}
/* If no potential matches were found, there's nothing else to do. */
if (num_potential_start_indices == 0) {
printf("No matches found in table.\n");
return;
}
printf(" Checking %u potential matches...\n", num_potential_start_indices); fflush(stdout);
num_falsealarms += num_potential_start_indices;
/* Allocate a buffer to hold them all. */
potential_start_indices = calloc(num_potential_start_indices, sizeof(cl_ulong));
potential_start_index_positions = calloc(num_potential_start_indices, sizeof(cl_ulong));
hash_base_indices = calloc(num_potential_start_indices, sizeof(cl_ulong));
ppi_refs = calloc(num_potential_start_indices, sizeof(precomputed_and_potential_indices *));
if ((potential_start_indices == NULL) || (potential_start_index_positions == NULL) || (hash_base_indices == NULL) || (ppi_refs == NULL)) {
fprintf(stderr, "Error while creating buffer for potential start indices/positions/hash indices/ppi refs.\n");
exit(-1);
}
plaintext_space_total = fill_plaintext_space_table(strlen(args->charset), args->plaintext_len_min, args->plaintext_len_max, plaintext_space_up_to_index);
/* Collate all the start indices into one buffer. */
ppi_cur = ppi;
while(ppi_cur) {
unsigned char hash[MAX_HASH_OUTPUT_LEN] = {0};
unsigned int hash_len = hex_to_bytes(ppi_cur->hash, sizeof(hash), hash);
cl_ulong hash_base_index = hash_to_index(hash, hash_len, args->reduction_offset, plaintext_space_total, 0); /* We always use position 0 here. When the GPU code is comparing indices, it will add in the current position. */
if (ppi_cur->plaintext == NULL) {
for (i = 0; i < ppi_cur->num_potential_start_indices; i++, j++) {
potential_start_indices[j] = ppi_cur->potential_start_indices[i];
potential_start_index_positions[j] = ppi_cur->potential_start_index_positions[i];
hash_base_indices[j] = hash_base_index;
/* For this index, hold a reference to the ppi struct. This later lets us find
* the ppi, given a result index from the GPU. */
ppi_refs[j] = ppi_cur;
}
}
ppi_cur = ppi_cur->next;
}
/*for (i = 0; i < num_potential_start_indices; i++)
printf("Start point: %lu; Chain position: %u; hash base index: %lu\n", potential_start_indices[i], potential_start_index_positions[i], hash_base_indices[i]);*/
/* Start the timer false alarm checking. */
start_timer(&start_time);
/* Start one thread to control each GPU. */
for (i = 0; i < total_devices; i++) {
/* Each thread gets the same reference to the list of potential start indices. */
args[i].potential_start_indices = potential_start_indices;
args[i].num_potential_start_indices = num_potential_start_indices;
args[i].potential_start_index_positions = potential_start_index_positions;
args[i].hash_base_indices = hash_base_indices;
if (pthread_create(&(threads[i]), NULL, &host_thread_false_alarm, &(args[i]))) {
perror("Failed to create thread");
exit(-1);
}
}
/* Wait for all threads to finish. */
for (i = 0; i < total_devices; i++) {
if (pthread_join(threads[i], NULL) != 0) {
perror("Failed to join with thread");
exit(-1);
}
}
/* Search for valid results, and update the ppi with the plaintext. */
for (i = 0; i < total_devices; i++) {
for (j = 0; j < args[i].num_results; j++) {
if (args[i].results[j] != 0) {
char plaintext[MAX_PLAINTEXT_LEN] = {0};
unsigned int plaintext_len = 0;
index_to_plaintext(args[i].results[j], args[i].charset, strlen(args[i].charset), args[i].plaintext_len_min, args[i].plaintext_len_max, plaintext_space_up_to_index, plaintext, &plaintext_len);
/* Double check NTLM results to weed out super false alarms. */
if (args[i].hash_type == HASH_NTLM) {
unsigned char hash[16] = {0};
char hash_hex[(sizeof(hash) * 2) + 1] = {0};
ntlm_hash(plaintext, plaintext_len, hash);
if (!bytes_to_hex(hash, sizeof(hash), hash_hex, sizeof(hash_hex)) || \
(strcmp(hash_hex, ppi_refs[j]->hash) != 0)) {
/*printf("Found super false positive!: NTLM('%s') != %s\n", plaintext, ppi_refs[j]->hash);*/
continue;
}
} else
printf("WARNING: CPU code to double-check this cracked hash has not yet been added. There is a 60%% chance this is a false positive! A workaround is to use John The Ripper to validate this result(s).\n");
/* Its official: we cracked a hash! */
/* Save the plaintext, clear the precomputed end indices list (since its
* no longer useful, save the hash/plaintext combo into the pot file, and
* tell the user. */
ppi_refs[j]->plaintext = strdup(plaintext);
ppi_refs[j]->num_precomputed_end_indices = 0;
FREE(ppi_refs[j]->precomputed_end_indices);
save_cracked_hash(ppi_refs[j], args[i].hash_type);
printf("%sHASH CRACKED => %s:%s%s\n", GREENB, (ppi_refs[j]->username != NULL) ? ppi_refs[j]->username : ppi_refs[j]->hash, plaintext, CLR); fflush(stdout);
}
}
}
time_delta = get_elapsed(&start_time);
time_falsealarms += time_delta;
seconds_to_human_time(time_str, sizeof(time_str), (unsigned int)time_delta);
printf(" Completed false alarm checks in %s.\n", time_str); fflush(stdout);
FREE(potential_start_indices);
FREE(potential_start_index_positions);
FREE(hash_base_indices);
FREE(ppi_refs);
FREE(args->results);
args->num_results = 0;
}
/* Print a warning to the user if a lot of memory is used by the pre-computed indices. */
void check_memory_usage() {
uint64_t total_memory = get_total_memory(), num_precompute_bytes = 0;
double percent_memory_used = 0.0;
if (total_memory == 0)
return;
num_precompute_bytes = total_precomputed_indices_loaded * sizeof(cl_ulong);
percent_memory_used = ((double)num_precompute_bytes / (double)total_memory) * 100;
if (percent_memory_used > 65) {
printf("\n\n\n\t!! WARNING !!\n\n\tThe pre-computed indices take up more than 65%% of total RAM! This may result in strange failures from clFinish() and other OpenCL functions. If this happens, either run this lookup with a smaller number of hashes at a time, or do it on a machine with more memory.\n\n\tMemory used by pre-compute indices: %"QUOTE PRIu64"\n\tTotal RAM: %"QUOTE PRIu64"\n\tPercent used: %.1f%%\n\n\n\n", num_precompute_bytes, total_memory, percent_memory_used);
}
}
/* Free all the potential start indices. */
void clear_potential_start_indices(precomputed_and_potential_indices *ppi) {
precomputed_and_potential_indices *ppi_cur = ppi;
while(ppi_cur) {
FREE(ppi_cur->potential_start_indices);
FREE(ppi_cur->potential_start_index_positions);
ppi_cur->num_potential_start_indices = 0;
ppi_cur = ppi_cur->next;
}
}
/* Returns the total number of *.rt and *.rtc in all subdirectories of the
* specified directory. */
unsigned int count_tables(char *dir) {
DIR *d = NULL;
struct dirent *de = NULL;
unsigned int ret = 0, is_file = 0, is_dir = 0;
d = opendir(dir);
if (d == NULL) {
fprintf(stderr, "Failed to open directory %s: %s\n", dir, strerror(errno)); fflush(stderr);
return 0;
}
while ((de = readdir(d)) != NULL) {
#ifdef _WIN32
struct stat st = {0};
char path[256] = {0};
/* The d_type field of the dirent struct is not a POSIX standard, and Windows
* doesn't support it. So we fall back to using stat(). */
snprintf(path, sizeof(path) - 1, "%s\\%s", dir, de->d_name);
if (stat(path, &st) < 0) {
fprintf(stderr, "Error: failed to stat() %s: %s. Continuing anyway...\n", path, strerror(errno)); fflush(stderr);
is_file = 0;
is_dir = 0;
} else {
is_file = S_ISREG(st.st_mode);
is_dir = S_ISDIR(st.st_mode);
}
#else
/* Linux has the d_type field, which is much more efficient to use than doing
* another stat(). */
is_file = (de->d_type == DT_REG);
is_dir = (de->d_type == DT_DIR);
#endif
if (is_file && (str_ends_with(de->d_name, ".rt") || str_ends_with(de->d_name, ".rtc")))
ret++;
else if (is_dir && (strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0)) {
char subdir_path[1024] = {0};
filepath_join(subdir_path, sizeof(subdir_path) - 1, dir, de->d_name);
ret += count_tables(subdir_path);
}
}
closedir(d);
return ret;
}
/* Free the hashes we loaded from disk or command line. */
void free_loaded_hashes(char **usernames, char **hashes) {
unsigned int i = 0;
if (usernames != NULL) {
for (i = 0; i < num_hashes; i++) {
FREE(usernames[i]);
}
FREE(usernames);
}
if (hashes != NULL) {
for (i = 0; i < num_hashes; i++) {
FREE(hashes[i]);
}
FREE(hashes);
}
num_hashes = 0;
}
/* Recursively searches the target directory for the first rainbow table file, and uses its filename to infer
* the rainbow table parameters. */
void find_rt_params(char *dir_name, rt_parameters *rt_params) {
char filepath[512] = {0};
DIR *dir = NULL;
struct dirent *de = NULL;
struct stat st;
dir = opendir(dir_name);
if (dir == NULL) /* This directory may not allow the current process permission. */
return;
while ((de = readdir(dir)) != NULL) {
/* Create an absolute path to this entity. */
filepath_join(filepath, sizeof(filepath), dir_name, de->d_name);
/* If this is a directory, recurse into it. */
if ((strcmp(de->d_name, ".") != 0) && (strcmp(de->d_name, "..") != 0) && (stat(filepath, &st) == 0) && S_ISDIR(st.st_mode)) {
find_rt_params(filepath, rt_params);
/* If we're searching for rainbowtable parameters, and successfully parsed them
* in the recursive call, we're done. */
if ((rt_params != NULL) && rt_params->parsed) {
closedir(dir); dir = NULL;
return;
}
/* If this is a compressed or uncompressed rainbow table, process it! */
} else if (str_ends_with(de->d_name, ".rt") || str_ends_with(de->d_name, ".rtc")) {
/* Try to parse them from this file name. On success, return immediately
* (no further processing needed), otherwise continue searching until the
* first valid set of parameters is found. */
parse_rt_params(rt_params, de->d_name);
if (rt_params->parsed) {
closedir(dir); dir = NULL;
return;
}
}
}
closedir(dir); dir = NULL;
}
/* Free the precomputed_hashes linked list. */
void free_precomputed_and_potential_indices(precomputed_and_potential_indices **ppi_head) {
precomputed_and_potential_indices *ppi = *ppi_head, *ppi_next = NULL;
while (ppi) {
ppi_next = ppi->next;
FREE(ppi->precomputed_end_indices);
FREE(ppi->potential_start_indices);
FREE(ppi->potential_start_index_positions);
FREE(ppi->index_filename);
ppi->num_potential_start_indices = 0;
FREE(ppi->plaintext);
FREE(ppi);
ppi = ppi_next;
}
*ppi_head = NULL;
}
/* Returns the number of CPU cores on this machine. */
unsigned int get_num_cpu_cores() {
#ifdef _WIN32
SYSTEM_INFO sysinfo = {0};
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#else
return get_nprocs();
#endif
}
/* A host thread which controls each GPU for false alarm checks. */
void *host_thread_false_alarm(void *ptr) {
thread_args *args = (thread_args *)ptr;
gpu_dev *gpu = &(args->gpu);
cl_context context = NULL;
cl_command_queue queue = NULL;
cl_kernel kernel = NULL;
int err = 0;
char *kernel_path = FALSE_ALARM_KERNEL_PATH, *kernel_name = "false_alarm_check";
cl_mem hash_type_buffer = NULL, charset_buffer = NULL, plaintext_len_min_buffer = NULL, plaintext_len_max_buffer = NULL, reduction_offset_buffer = NULL, plaintext_space_total_buffer = NULL, plaintext_space_up_to_index_buffer = NULL, device_num_buffer = NULL, total_devices_buffer = NULL, num_start_indices_buffer = NULL, start_indices_buffer = NULL, start_index_positions_buffer = NULL, hash_base_indices_buffer = NULL, output_block_buffer = NULL, exec_block_scaler_buffer = NULL;
/*cl_mem debug_ulong_buffer = NULL;*/
cl_ulong *start_indices = NULL, *hash_base_indices = NULL, *plaintext_indices = NULL, *output_block = NULL;
unsigned int *start_index_positions = NULL;
unsigned int num_start_indices = 0, num_start_index_positions = 0, num_hash_base_indices = 0, num_plaintext_indices = 0, num_exec_blocks = 0, output_block_len = 0, exec_block = 0, output_block_index = 0, plaintext_indicies_index = 0;
uint64_t plaintext_space_total = 0;
cl_ulong plaintext_space_up_to_index[MAX_PLAINTEXT_LEN] = {0};
size_t gws = 0, kernel_work_group_size = 0, kernel_preferred_work_group_size_multiple = 0;
/*cl_ulong debug_ulong[128] = {0};*/
plaintext_space_total = fill_plaintext_space_table(strlen(args->charset), args->plaintext_len_min, args->plaintext_len_max, plaintext_space_up_to_index);
num_start_indices = num_start_index_positions = num_hash_base_indices = num_plaintext_indices = args->num_potential_start_indices;
start_indices = args->potential_start_indices;
start_index_positions = args->potential_start_index_positions;
hash_base_indices = args->hash_base_indices;
plaintext_indices = calloc(num_plaintext_indices, sizeof(cl_ulong));
if (plaintext_indices == NULL) {
fprintf(stderr, "Error while allocating buffers.\n");
exit(-1);
}
/* If we're generating the standard NTLM 8-character tables, use the special
* optimized kernel instead! */
if (is_ntlm8(args->hash_type, args->charset, args->plaintext_len_min, args->plaintext_len_max, args->reduction_offset, args->chain_len)) {
kernel_path = FALSE_ALARM_NTLM8_KERNEL_PATH;
kernel_name = "false_alarm_check_ntlm8";
if ((args->gpu.device_number == 0) && (printed_false_alarm_optimized_message == 0)) { /* Only the first thread prints this, and only prints it once. */
printf("\nNote: optimized NTLM8 kernel will be used for false alarm checks.\n\n"); fflush(stdout);
printed_false_alarm_optimized_message = 1;
}
} else if (is_ntlm9(args->hash_type, args->charset, args->plaintext_len_min, args->plaintext_len_max, args->reduction_offset, args->chain_len)) {
kernel_path = FALSE_ALARM_NTLM9_KERNEL_PATH;
kernel_name = "false_alarm_check_ntlm9";
if ((args->gpu.device_number == 0) && (printed_false_alarm_optimized_message == 0)) { /* Only the first thread prints this, and only prints it once. */
printf("\nNote: optimized NTLM9 kernel will be used for false alarm checks.\n\n"); fflush(stdout);
printed_false_alarm_optimized_message = 1;
}
}
/* Load the kernel. */
gpu->context = CLCREATECONTEXT(context_callback, &(gpu->device));
gpu->queue = CLCREATEQUEUE(gpu->context, gpu->device);
load_kernel(gpu->context, 1, &(gpu->device), kernel_path, kernel_name, &(gpu->program), &(gpu->kernel), args->hash_type);
/* These variables are set so the CLCREATEARG* macros work correctly. */
context = gpu->context;
queue = gpu->queue;
kernel = gpu->kernel;
if ((rc_clGetKernelWorkGroupInfo(kernel, gpu->device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &kernel_work_group_size, NULL) != CL_SUCCESS) || \
(rc_clGetKernelWorkGroupInfo(kernel, gpu->device, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, sizeof(size_t), &kernel_preferred_work_group_size_multiple, NULL) != CL_SUCCESS)) {
fprintf(stderr, "Failed to get preferred work group size!\n");
CLRELEASEKERNEL(gpu->kernel);
CLRELEASEPROGRAM(gpu->program);
CLRELEASEQUEUE(gpu->queue);
CLRELEASECONTEXT(gpu->context);
pthread_exit(NULL);
return NULL;
}
/* If the user provided a static GWS on the command line, use that. Otherwise,
* use the driver's work group size multiplied by the preferred multiple. */
if (user_provided_gws > 0) {
gws = user_provided_gws;
printf("GPU #%u is using user-provided GWS value of %"PRIu64"\n", gpu->device_number, gws);
} else {
/*gws = kernel_work_group_size * kernel_preferred_work_group_size_multiple;*/
/* TODO: fix this so that false alarm checking is done in partitions instead of
* all at once (this can improve speed). Currently, when GWS != num_start_indices,
* lookups don't succeed due to some bug. */
gws = num_start_indices;
/* Somehow, on AMD GPUs, the kernel crashes with a message like:
*
* Memory access fault by GPU node-2 (Agent handle: 0x1bb5e00) on address
* 0x7f4c80b27000. Reason: Page not present or supervisor privilege.
*
* A work-around is to set the GWS to the number of start indices and just do it in
* one pass. */
if (is_amd_gpu)
gws = num_start_indices;
/*printf("GPU #%u is using dynamic GWS: %"PRIu64" (work group) x %"PRIu64" (pref. multiple) = %"PRIu64"\n", gpu->device_number, kernel_work_group_size, kernel_preferred_work_group_size_multiple, gws);*/
}
fflush(stdout);
/* Count the number of times we need to run the kernel. */
num_exec_blocks = num_start_indices / gws;
if (num_start_indices % gws != 0)
num_exec_blocks++;
output_block_len = gws;
output_block = calloc(output_block_len, sizeof(cl_ulong));
if (output_block == NULL) {
fprintf(stderr, "Error while allocating output buffer(s).\n");
exit(-1);
}
CLCREATEARG(0, hash_type_buffer, CL_RO, args->hash_type, sizeof(cl_uint));
CLCREATEARG_ARRAY(1, charset_buffer, CL_RO, args->charset, strlen(args->charset) + 1);
CLCREATEARG(2, plaintext_len_min_buffer, CL_RO, args->plaintext_len_min, sizeof(cl_uint));
CLCREATEARG(3, plaintext_len_max_buffer, CL_RO, args->plaintext_len_max, sizeof(cl_uint));
CLCREATEARG(4, reduction_offset_buffer, CL_RO, args->reduction_offset, sizeof(cl_uint));
CLCREATEARG(5, plaintext_space_total_buffer, CL_RO, plaintext_space_total, sizeof(cl_ulong));
CLCREATEARG_ARRAY(6, plaintext_space_up_to_index_buffer, CL_RO, plaintext_space_up_to_index, MAX_PLAINTEXT_LEN * sizeof(cl_ulong));
CLCREATEARG(7, device_num_buffer, CL_RO, gpu->device_number, sizeof(cl_uint));
CLCREATEARG(8, total_devices_buffer, CL_RO, args->total_devices, sizeof(cl_uint));
CLCREATEARG(9, num_start_indices_buffer, CL_RO, num_start_indices, sizeof(cl_uint));
CLCREATEARG_ARRAY(10, start_indices_buffer, CL_RO, start_indices, num_start_indices * sizeof(cl_ulong));
CLCREATEARG_ARRAY(11, start_index_positions_buffer, CL_RO, start_index_positions, num_start_index_positions * sizeof(unsigned int));
CLCREATEARG_ARRAY(12, hash_base_indices_buffer, CL_RO, hash_base_indices, num_hash_base_indices * sizeof(cl_ulong));
CLCREATEARG_ARRAY(14, output_block_buffer, CL_WO, output_block, output_block_len * sizeof(cl_ulong));
for (exec_block = 0; exec_block < num_exec_blocks; exec_block++) {
unsigned int exec_block_scaler = exec_block * gws;
CLCREATEARG(13, exec_block_scaler_buffer, CL_RO, exec_block_scaler, sizeof(cl_uint));
if (is_amd_gpu) {
int barrier_ret = pthread_barrier_wait(&barrier);
if ((barrier_ret != 0) && (barrier_ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
fprintf(stderr, "pthread_barrier_wait() failed!\n"); fflush(stderr);
exit(-1);
}
}
/* Run the kernel and wait for it to finish. */
CLRUNKERNEL(gpu->queue, gpu->kernel, &gws);
CLFLUSH(gpu->queue);
CLWAIT(gpu->queue);
/* Read the results. */
CLREADBUFFER(output_block_buffer, output_block_len * sizeof(cl_ulong), output_block);
output_block_index = 0;
while ((plaintext_indicies_index < num_plaintext_indices) && (output_block_index < output_block_len))
plaintext_indices[plaintext_indicies_index++] = output_block[output_block_index++];
CLFREEBUFFER(exec_block_scaler_buffer);
}
/* Set the results so the main thread can access them. */
args->results = plaintext_indices;
args->num_results = num_plaintext_indices;
/*
{
unsigned int i = 0;
printf("results: ");
for (i = 0; i < args->num_results; i++)
printf("%lu ", args->results[i]);
printf("\n");
}
*/
FREE(output_block);
CLFREEBUFFER(hash_type_buffer);
CLFREEBUFFER(charset_buffer);
CLFREEBUFFER(plaintext_len_min_buffer);
CLFREEBUFFER(plaintext_len_max_buffer);
CLFREEBUFFER(reduction_offset_buffer);
CLFREEBUFFER(plaintext_space_total_buffer);
CLFREEBUFFER(plaintext_space_up_to_index_buffer);
CLFREEBUFFER(device_num_buffer);
CLFREEBUFFER(total_devices_buffer);
CLFREEBUFFER(num_start_indices_buffer);
CLFREEBUFFER(start_indices_buffer);
CLFREEBUFFER(start_index_positions_buffer);
CLFREEBUFFER(hash_base_indices_buffer);
CLFREEBUFFER(output_block_buffer);
CLRELEASEKERNEL(gpu->kernel);
CLRELEASEPROGRAM(gpu->program);
CLRELEASEQUEUE(gpu->queue);
CLRELEASECONTEXT(gpu->context);
pthread_exit(NULL);
return NULL;
}
/* A host thread which controls each GPU for hash pre-computation. */
void *host_thread_precompute(void *ptr) {
thread_args *args = (thread_args *)ptr;
gpu_dev *gpu = &(args->gpu);
cl_context context = NULL;
cl_command_queue queue = NULL;
cl_kernel kernel = NULL;
int err = 0;
char *kernel_path = PRECOMPUTE_KERNEL_PATH, *kernel_name = "precompute";
cl_mem hash_type_buffer = NULL, hash_buffer = NULL, hash_len_buffer = NULL, charset_buffer = NULL, plaintext_len_min_buffer = NULL, plaintext_len_max_buffer = NULL, table_index_buffer = NULL, chain_len_buffer = NULL, device_num_buffer = NULL, total_devices_buffer = NULL, exec_block_scaler_buffer = NULL, output_block_buffer = NULL/*, debug_buffer = NULL*/;
size_t gws = 0;
cl_ulong *output = NULL, *output_block = NULL;
unsigned int output_len = 0, output_block_len = 0, num_exec_blocks = 0, exec_block = 0, output_index = 0, output_block_index = 0;
/*unsigned int i = 0;*/
unsigned char hash_binary[32] = {0};
cl_uint hash_binary_len = 0;
/* Convert the hash from a hex string to bytes.*/
hash_binary_len = hex_to_bytes(args->hash, sizeof(hash_binary), hash_binary);
/* The work size is the chain length divided among the total number of GPUs. Round
* up if it doesn't divide evenly; this results in slightly more work being done in
* order to get complete coverage. */
output_len = args->chain_len / args->total_devices;
if ((args->chain_len % args->total_devices) != 0)
output_len++;
/* If we're generating the standard NTLM 8-character tables, use the special
* optimized kernel instead! */
if (is_ntlm8(args->hash_type, args->charset, args->plaintext_len_min, args->plaintext_len_max, args->reduction_offset, args->chain_len)) {
kernel_path = PRECOMPUTE_NTLM8_KERNEL_PATH;
kernel_name = "precompute_ntlm8";
if ((args->gpu.device_number == 0) && (printed_precompute_optimized_message == 0)) { /* Only the first thread prints this, and only prints it once. */
printf("\nNote: optimized NTLM8 kernel will be used for precomputation.\n\n"); fflush(stdout);
printed_precompute_optimized_message = 1;
}
} else if (is_ntlm9(args->hash_type, args->charset, args->plaintext_len_min, args->plaintext_len_max, args->reduction_offset, args->chain_len)) {
kernel_path = PRECOMPUTE_NTLM9_KERNEL_PATH;
kernel_name = "precompute_ntlm9";
if ((args->gpu.device_number == 0) && (printed_precompute_optimized_message == 0)) { /* Only the first thread prints this, and only prints it once. */
printf("\nNote: optimized NTLM9 kernel will be used for precomputation.\n\n"); fflush(stdout);
printed_precompute_optimized_message = 1;
}
}
/* Load the kernel. */
gpu->context = CLCREATECONTEXT(context_callback, &(gpu->device));
gpu->queue = CLCREATEQUEUE(gpu->context, gpu->device);
load_kernel(gpu->context, 1, &(gpu->device), kernel_path, kernel_name, &(gpu->program), &(gpu->kernel), args->hash_type);
/* These variables are set so the CLCREATEARG* macros work correctly. */
context = gpu->context;
queue = gpu->queue;
kernel = gpu->kernel;
if (rc_clGetKernelWorkGroupInfo(kernel, gpu->device, CL_KERNEL_WORK_GROUP_SIZE /*CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE*/, sizeof(size_t), &gws, NULL) != CL_SUCCESS) {
fprintf(stderr, "Failed to get preferred work group size!\n");
CLRELEASEKERNEL(gpu->kernel);
CLRELEASEPROGRAM(gpu->program);
CLRELEASEQUEUE(gpu->queue);
CLRELEASECONTEXT(gpu->context);
pthread_exit(NULL);
return NULL;
}
gws = gws * gpu->num_work_units;
/* In the event that the global work size is larger than the number of outputs we
* need, cap the GWS. */
if (gws > output_len) gws = output_len;
/* Count the number of times we need to run the kernel. */
num_exec_blocks = output_len / gws;
if (output_len % gws != 0)
num_exec_blocks++;
/*printf("Host thread #%u started; GWS: %zu.\n", gpu->device_number, gws);*/
/* This will hold the results from this one GPU. */
output = calloc(output_len, sizeof(cl_ulong));
/* Holds the results from one kernel exec. */
output_block_len = gws;
output_block = calloc(output_block_len, sizeof(cl_ulong));
if ((output == NULL) || (output_block == NULL)) {
fprintf(stderr, "Error while allocating output buffer(s).\n");
exit(-1);
}
/* Get the number of compute units in this device. */
/*get_device_uint(gpu->device, CL_DEVICE_MAX_COMPUTE_UNITS, &(gpu->num_work_units));*/
CLCREATEARG(0, hash_type_buffer, CL_RO, args->hash_type, sizeof(cl_uint));
CLCREATEARG_ARRAY(1, hash_buffer, CL_RO, hash_binary, hash_binary_len);
CLCREATEARG(2, hash_len_buffer, CL_RO, hash_binary_len, sizeof(cl_uint));
CLCREATEARG_ARRAY(3, charset_buffer, CL_RO, args->charset, strlen(args->charset) + 1);
CLCREATEARG(4, plaintext_len_min_buffer, CL_RO, args->plaintext_len_min, sizeof(cl_uint));
CLCREATEARG(5, plaintext_len_max_buffer, CL_RO, args->plaintext_len_max, sizeof(cl_uint));
CLCREATEARG(6, table_index_buffer, CL_RO, args->table_index, sizeof(cl_uint));
CLCREATEARG(7, chain_len_buffer, CL_RO, args->chain_len, sizeof(cl_ulong));
CLCREATEARG(8, device_num_buffer, CL_RO, gpu->device_number, sizeof(cl_uint));
CLCREATEARG(9, total_devices_buffer, CL_RO, args->total_devices, sizeof(cl_uint));
CLCREATEARG_ARRAY(11, output_block_buffer, CL_WO, output_block, output_block_len * sizeof(cl_ulong));
/*CLCREATEARG_DEBUG(9, debug_buffer, debug_ptr);*/
for (exec_block = 0; exec_block < num_exec_blocks; exec_block++) {
unsigned int exec_block_scaler = exec_block * gws;
CLCREATEARG(10, exec_block_scaler_buffer, CL_RO, exec_block_scaler, sizeof(cl_uint));
if (is_amd_gpu) {
int barrier_ret = pthread_barrier_wait(&barrier);
if ((barrier_ret != 0) && (barrier_ret != PTHREAD_BARRIER_SERIAL_THREAD)) {
fprintf(stderr, "pthread_barrier_wait() failed!\n"); fflush(stderr);
exit(-1);
}
}
/* Run the kernel and wait for it to finish. */
CLRUNKERNEL(gpu->queue, gpu->kernel, &gws);
CLFLUSH(gpu->queue);
CLWAIT(gpu->queue);
/* Read the results. */
CLREADBUFFER(output_block_buffer, output_block_len * sizeof(cl_ulong), output_block);
/* Append this block out output to the total output for this GPU. */
output_block_index = 0;
while ((output_index < output_len) && (output_block_index < output_block_len))
output[output_index++] = output_block[output_block_index++];
CLFREEBUFFER(exec_block_scaler_buffer);
}
/* Set the results so the main thread can access them. */
args->results = output;
args->num_results = output_len;
/*
printf("GPU %u: ", gpu->device_number);
for (i = 0; i < output_len; i++) {
printf("%"PRIu64" ", output[i]);
}
printf("\n");
*/
FREE(output_block);
CLFREEBUFFER(hash_type_buffer);
CLFREEBUFFER(hash_buffer);
CLFREEBUFFER(hash_len_buffer);
CLFREEBUFFER(charset_buffer);
CLFREEBUFFER(plaintext_len_min_buffer);
CLFREEBUFFER(plaintext_len_max_buffer);
CLFREEBUFFER(table_index_buffer);
CLFREEBUFFER(chain_len_buffer);
CLFREEBUFFER(device_num_buffer);
CLFREEBUFFER(total_devices_buffer);
CLFREEBUFFER(exec_block_scaler_buffer);
CLFREEBUFFER(output_block_buffer);
/*CLFREEBUFFER(debug_buffer);*/
CLRELEASEKERNEL(gpu->kernel);