This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
forked from hashcat/hashcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordlist.c
657 lines (456 loc) · 14.8 KB
/
wordlist.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
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.h"
#include "event.h"
#include "convert.h"
#include "dictstat.h"
#include "rp.h"
#include "rp_cpu.h"
#include "shared.h"
#include "wordlist.h"
#include "emu_inc_hash_sha1.h"
size_t convert_from_hex (hashcat_ctx_t *hashcat_ctx, char *line_buf, const size_t line_len)
{
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
const user_options_t *user_options = hashcat_ctx->user_options;
if (line_len & 1) return (line_len); // not in hex
if (hashconfig->opts_type & OPTS_TYPE_PT_HEX)
{
size_t i, j;
for (i = 0, j = 0; j < line_len; i += 1, j += 2)
{
line_buf[i] = hex_to_u8 ((const u8 *) &line_buf[j]);
}
memset (line_buf + i, 0, line_len - i);
return (i);
}
if (user_options->wordlist_autohex_disable == false)
{
if (is_hexify ((const u8 *) line_buf, line_len) == true)
{
const size_t new_len = exec_unhexify ((const u8 *) line_buf, line_len, (u8 *) line_buf, line_len);
return new_len;
}
}
return (line_len);
}
int load_segment (hashcat_ctx_t *hashcat_ctx, HCFILE *fp)
{
wl_data_t *wl_data = hashcat_ctx->wl_data;
// NOTE: use (never changing) ->incr here instead of ->avail otherwise the buffer gets bigger and bigger
wl_data->pos = 0;
wl_data->cnt = hc_fread (wl_data->buf, 1, wl_data->incr - 1000, fp);
wl_data->buf[wl_data->cnt] = 0;
if (wl_data->cnt == 0) return 0;
if (wl_data->buf[wl_data->cnt - 1] == '\n') return 0;
while (!hc_feof (fp))
{
if (wl_data->cnt == wl_data->avail)
{
wl_data->buf = (char *) hcrealloc (wl_data->buf, wl_data->avail, wl_data->incr);
wl_data->avail += wl_data->incr;
}
const int c = hc_fgetc (fp);
if (c == EOF) break;
wl_data->buf[wl_data->cnt] = (char) c;
wl_data->cnt++;
if (c == '\n') break;
}
// ensure stream ends with a newline
if (wl_data->buf[wl_data->cnt - 1] != '\n')
{
wl_data->cnt++;
wl_data->buf[wl_data->cnt - 1] = '\n';
}
return 0;
}
void get_next_word_lm (char *buf, u64 sz, u64 *len, u64 *off)
{
char *ptr = buf;
for (u64 i = 0; i < sz; i++, ptr++)
{
if (*ptr >= 'a' && *ptr <= 'z') *ptr -= 0x20;
if (i == 7)
{
*off = i;
*len = i;
return;
}
if (*ptr != '\n') continue;
*off = i + 1;
if ((i > 0) && (buf[i - 1] == '\r')) i--;
*len = i;
return;
}
*off = sz;
*len = sz;
}
void get_next_word_uc (char *buf, u64 sz, u64 *len, u64 *off)
{
char *ptr = buf;
for (u64 i = 0; i < sz; i++, ptr++)
{
if (*ptr >= 'a' && *ptr <= 'z') *ptr -= 0x20;
if (*ptr != '\n') continue;
*off = i + 1;
if ((i > 0) && (buf[i - 1] == '\r')) i--;
*len = i;
return;
}
*off = sz;
*len = sz;
}
void get_next_word_std (char *buf, u64 sz, u64 *len, u64 *off)
{
char *ptr = buf;
for (u64 i = 0; i < sz; i++, ptr++)
{
if (*ptr != '\n') continue;
*off = i + 1;
if ((i > 0) && (buf[i - 1] == '\r')) i--;
*len = i;
return;
}
*off = sz;
*len = sz;
}
void get_next_word (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, char **out_buf, u32 *out_len)
{
user_options_t *user_options = hashcat_ctx->user_options;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
wl_data_t *wl_data = hashcat_ctx->wl_data;
while (wl_data->pos < wl_data->cnt)
{
u64 off;
u64 len;
char *ptr = wl_data->buf + wl_data->pos;
wl_data->func (ptr, wl_data->cnt - wl_data->pos, &len, &off);
wl_data->pos += off;
// do the on-the-fly hex decode using original buffer
// this is safe as length only decreases in size
len = (u32) convert_from_hex (hashcat_ctx, ptr, len);
// do the on-the-fly encoding
// needs to write into new buffer because size case both decrease and increase
if (wl_data->iconv_enabled == true)
{
char *iconv_ptr = wl_data->iconv_tmp;
size_t iconv_sz = HCBUFSIZ_TINY;
size_t ptr_len = len;
const size_t iconv_rc = iconv (wl_data->iconv_ctx, &ptr, &ptr_len, &iconv_ptr, &iconv_sz);
if (iconv_rc == (size_t) -1) continue;
ptr = wl_data->iconv_tmp;
len = HCBUFSIZ_TINY - iconv_sz;
}
// this is only a test for length, not writing into output buffer
if (run_rule_engine (user_options_extra->rule_len_l, user_options->rule_buf_l))
{
if (len >= RP_PASSWORD_SIZE) continue;
char rule_buf_out[RP_PASSWORD_SIZE];
memset (rule_buf_out, 0, sizeof (rule_buf_out));
const int rule_len_out = _old_apply_rule (user_options->rule_buf_l, user_options_extra->rule_len_l, ptr, (u32) len, rule_buf_out);
if (rule_len_out < 0) continue;
}
if (len > PW_MAX) continue;
*out_buf = ptr;
*out_len = (u32) len;
return;
}
if (hc_feof (fp))
{
fprintf (stderr, "BUG feof()!!\n");
return;
}
load_segment (hashcat_ctx, fp);
get_next_word (hashcat_ctx, fp, out_buf, out_len);
}
void pw_pre_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len, const u8 *base_buf, const int base_len, const int rule_idx)
{
if (device_param->pws_pre_cnt < device_param->kernel_power)
{
pw_pre_t *pw_pre = device_param->pws_pre_buf + device_param->pws_pre_cnt;
memcpy (pw_pre->pw_buf, pw_buf, pw_len);
pw_pre->pw_len = pw_len;
if (base_buf != NULL)
{
memcpy (pw_pre->base_buf, base_buf, base_len);
pw_pre->base_len = base_len;
}
pw_pre->rule_idx = rule_idx;
device_param->pws_pre_cnt++;
}
else
{
fprintf (stdout, "BUG pw_pre_add()!!\n");
return;
}
}
void pw_base_add (hc_device_param_t *device_param, pw_pre_t *pw_pre)
{
if (device_param->pws_base_cnt < device_param->kernel_power)
{
memcpy (device_param->pws_base_buf + device_param->pws_base_cnt, pw_pre, sizeof (pw_pre_t));
device_param->pws_base_cnt++;
}
else
{
fprintf (stderr, "BUG pw_base_add()!!\n");
return;
}
}
void pw_add (hc_device_param_t *device_param, const u8 *pw_buf, const int pw_len)
{
if (device_param->pws_cnt < device_param->kernel_power)
{
pw_idx_t *pw_idx = device_param->pws_idx + device_param->pws_cnt;
const u32 pw_len4 = (pw_len + 3) & ~3; // round up to multiple of 4
const u32 pw_len4_cnt = pw_len4 / 4;
pw_idx->cnt = pw_len4_cnt;
pw_idx->len = pw_len;
u8 *dst = (u8 *) (device_param->pws_comp + pw_idx->off);
memcpy (dst, pw_buf, pw_len);
memset (dst + pw_len, 0, pw_len4 - pw_len);
// prepare next element
pw_idx_t *pw_idx_next = pw_idx + 1;
pw_idx_next->off = pw_idx->off + pw_idx->cnt;
device_param->pws_cnt++;
}
else
{
fprintf (stderr, "BUG pw_add()!!\n");
return;
}
}
int count_words (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, const char *dictfile, u64 *result)
{
combinator_ctx_t *combinator_ctx = hashcat_ctx->combinator_ctx;
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
straight_ctx_t *straight_ctx = hashcat_ctx->straight_ctx;
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
user_options_extra_t *user_options_extra = hashcat_ctx->user_options_extra;
user_options_t *user_options = hashcat_ctx->user_options;
wl_data_t *wl_data = hashcat_ctx->wl_data;
//hc_signal (NULL);
dictstat_t d;
memset (&d, 0, sizeof (d));
if (fstat (hc_fileno (fp), &d.stat))
{
*result = 0;
return 0;
}
d.stat.st_mode = 0;
d.stat.st_nlink = 0;
d.stat.st_uid = 0;
d.stat.st_gid = 0;
d.stat.st_rdev = 0;
d.stat.st_atime = 0;
#if defined (STAT_NANOSECONDS_ACCESS_TIME)
d.stat.STAT_NANOSECONDS_ACCESS_TIME = 0;
#endif
#if defined (_POSIX)
d.stat.st_blksize = 0;
d.stat.st_blocks = 0;
#endif
memset (d.encoding_from, 0, sizeof (d.encoding_from));
memset (d.encoding_to, 0, sizeof (d.encoding_to));
strncpy (d.encoding_from, user_options->encoding_from, sizeof (d.encoding_from) - 1);
strncpy (d.encoding_to, user_options->encoding_to, sizeof (d.encoding_to) - 1);
if (d.stat.st_size == 0)
{
*result = 0;
return 0;
}
const size_t dictfile_len = strlen (dictfile);
u32 *dictfile_padded = (u32 *) hcmalloc (dictfile_len + 64); // padding required for sha1_update()
memcpy (dictfile_padded, dictfile, dictfile_len);
sha1_ctx_t sha1_ctx;
sha1_init (&sha1_ctx);
sha1_update (&sha1_ctx, dictfile_padded, dictfile_len);
sha1_final (&sha1_ctx);
hcfree (dictfile_padded);
memcpy (d.hash_filename, sha1_ctx.h, 16);
const u64 cached_cnt = dictstat_find (hashcat_ctx, &d);
if (run_rule_engine (user_options_extra->rule_len_l, user_options->rule_buf_l) == 0)
{
if (cached_cnt)
{
u64 keyspace = cached_cnt;
if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT)
{
if (overflow_check_u64_mul (keyspace, straight_ctx->kernel_rules_cnt) == false) return -1;
keyspace *= straight_ctx->kernel_rules_cnt;
}
else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI)
{
if (((hashconfig->opti_type & OPTI_TYPE_OPTIMIZED_KERNEL) == 0) && (user_options->attack_mode == ATTACK_MODE_HYBRID2))
{
if (overflow_check_u64_mul (keyspace, mask_ctx->bfs_cnt) == false) return -1;
keyspace *= mask_ctx->bfs_cnt;
}
else
{
if (overflow_check_u64_mul (keyspace, combinator_ctx->combs_cnt) == false) return -1;
keyspace *= combinator_ctx->combs_cnt;
}
}
cache_hit_t cache_hit;
cache_hit.dictfile = dictfile;
cache_hit.stat.st_size = d.stat.st_size;
cache_hit.cached_cnt = cached_cnt;
cache_hit.keyspace = keyspace;
EVENT_DATA (EVENT_WORDLIST_CACHE_HIT, &cache_hit, sizeof (cache_hit));
*result = keyspace;
return 0;
}
}
time_t rt_start;
time (&rt_start);
time_t now = 0;
time_t prev = 0;
u64 comp = 0;
u64 cnt = 0;
u64 cnt2 = 0;
while (!hc_feof (fp))
{
load_segment (hashcat_ctx, fp);
comp += wl_data->cnt;
u64 i = 0;
while (i < wl_data->cnt)
{
u64 len;
u64 off;
char *ptr = wl_data->buf + i;
wl_data->func (ptr, wl_data->cnt - i, &len, &off);
i += off;
// do the on-the-fly hex decode using original buffer
// this is safe as length only decreases in size
len = (u32) convert_from_hex (hashcat_ctx, ptr, len);
// do the on-the-fly encoding
if (wl_data->iconv_enabled == true)
{
char *iconv_ptr = wl_data->iconv_tmp;
size_t iconv_sz = HCBUFSIZ_TINY;
size_t ptr_len = len;
const size_t iconv_rc = iconv (wl_data->iconv_ctx, &ptr, &ptr_len, &iconv_ptr, &iconv_sz);
if (iconv_rc == (size_t) -1) continue;
ptr = wl_data->iconv_tmp;
len = HCBUFSIZ_TINY - iconv_sz;
}
if (run_rule_engine (user_options_extra->rule_len_l, user_options->rule_buf_l))
{
if (len >= RP_PASSWORD_SIZE) continue;
char rule_buf_out[RP_PASSWORD_SIZE];
memset (rule_buf_out, 0, sizeof (rule_buf_out));
const int rule_len_out = _old_apply_rule (user_options->rule_buf_l, user_options_extra->rule_len_l, ptr, (u32) len, rule_buf_out);
if (rule_len_out < 0) continue;
}
cnt2++;
if (len > PW_MAX) continue;
d.cnt++;
if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT)
{
if (overflow_check_u64_add (cnt, straight_ctx->kernel_rules_cnt) == false) return -1;
cnt += straight_ctx->kernel_rules_cnt;
}
else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI)
{
if (((hashconfig->opti_type & OPTI_TYPE_OPTIMIZED_KERNEL) == 0) && (user_options->attack_mode == ATTACK_MODE_HYBRID2))
{
if (overflow_check_u64_add (cnt, mask_ctx->bfs_cnt) == false) return -1;
cnt += mask_ctx->bfs_cnt;
}
else
{
if (overflow_check_u64_add (cnt, combinator_ctx->combs_cnt) == false) return -1;
cnt += combinator_ctx->combs_cnt;
}
}
}
time (&now);
if ((now - prev) == 0) continue;
time (&prev);
double percent = ((double) comp / (double) d.stat.st_size) * 100;
if (percent < 100)
{
cache_generate_t cache_generate;
cache_generate.dictfile = dictfile;
cache_generate.comp = comp;
cache_generate.percent = percent;
cache_generate.cnt = cnt;
cache_generate.cnt2 = cnt2;
EVENT_DATA (EVENT_WORDLIST_CACHE_GENERATE, &cache_generate, sizeof (cache_generate));
}
}
time_t rt_stop;
time (&rt_stop);
cache_generate_t cache_generate;
cache_generate.dictfile = dictfile;
cache_generate.comp = comp;
cache_generate.percent = 100;
cache_generate.cnt = cnt;
cache_generate.cnt2 = cnt2;
cache_generate.runtime = rt_stop - rt_start;
EVENT_DATA (EVENT_WORDLIST_CACHE_GENERATE, &cache_generate, sizeof (cache_generate));
dictstat_append (hashcat_ctx, &d);
//hc_signal (sigHandler_default);
*result = cnt;
return 0;
}
int wl_data_init (hashcat_ctx_t *hashcat_ctx)
{
hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
user_options_t *user_options = hashcat_ctx->user_options;
wl_data_t *wl_data = hashcat_ctx->wl_data;
wl_data->enabled = false;
if (user_options->benchmark == true) return 0;
if (user_options->hash_info == true) return 0;
if (user_options->left == true) return 0;
if (user_options->backend_info == true) return 0;
if (user_options->usage == true) return 0;
if (user_options->version == true) return 0;
wl_data->enabled = true;
wl_data->buf = (char *) hcmalloc (user_options->segment_size);
wl_data->avail = user_options->segment_size;
wl_data->incr = user_options->segment_size;
wl_data->cnt = 0;
wl_data->pos = 0;
/**
* choose dictionary parser
*/
wl_data->func = get_next_word_std;
if (hashconfig->opts_type & OPTS_TYPE_PT_UPPER)
{
wl_data->func = get_next_word_uc;
}
if (hashconfig->opts_type & OPTS_TYPE_PT_LM)
{
wl_data->func = get_next_word_lm;
}
/**
* iconv
*/
if (strcmp (user_options->encoding_from, user_options->encoding_to) != 0)
{
wl_data->iconv_enabled = true;
wl_data->iconv_ctx = iconv_open (user_options->encoding_to, user_options->encoding_from);
if (wl_data->iconv_ctx == (iconv_t) -1) return -1;
wl_data->iconv_tmp = (char *) hcmalloc (HCBUFSIZ_TINY);
}
return 0;
}
void wl_data_destroy (hashcat_ctx_t *hashcat_ctx)
{
wl_data_t *wl_data = hashcat_ctx->wl_data;
if (wl_data->enabled == false) return;
hcfree (wl_data->buf);
if (wl_data->iconv_enabled == true)
{
iconv_close (wl_data->iconv_ctx);
wl_data->iconv_enabled = false;
hcfree (wl_data->iconv_tmp);
}
memset (wl_data, 0, sizeof (wl_data_t));
}