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
/
mpsp.c
1815 lines (1326 loc) · 46.3 KB
/
mpsp.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
/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.h"
#include "event.h"
#include "bitops.h"
#include "logfile.h"
#include "convert.h"
#include "filehandling.h"
#include "backend.h"
#include "shared.h"
#include "ext_lzma.h"
#include "mpsp.h"
static const char *DEF_MASK = "?1?2?2?2?2?2?2?3?3?3?3?d?d?d?d";
#define MAX_MFS 5 // 4*charset, 1*mask
static int sp_comp_val (const void *p1, const void *p2)
{
const hcstat_table_t *b1 = (const hcstat_table_t *) p1;
const hcstat_table_t *b2 = (const hcstat_table_t *) p2;
const u64 v1 = b1->val;
const u64 v2 = b2->val;
if (v1 < v2) return 1;
if (v1 > v2) return -1;
return 0;
}
static void mp_css_split_cnt (hashcat_ctx_t *hashcat_ctx, const u32 css_cnt_orig, u32 css_cnt_lr[2])
{
const mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
const user_options_t *user_options = hashcat_ctx->user_options;
u32 css_cnt_l = mask_ctx->css_cnt;
u32 css_cnt_r;
if (user_options->slow_candidates == true)
{
css_cnt_r = 0;
}
else
{
if (hashconfig->attack_exec == ATTACK_EXEC_INSIDE_KERNEL)
{
if (css_cnt_orig < 6)
{
css_cnt_r = 1;
}
else if (css_cnt_orig == 6)
{
css_cnt_r = 2;
}
else
{
if ((hashconfig->opts_type & OPTS_TYPE_PT_UTF16LE) || (hashconfig->opts_type & OPTS_TYPE_PT_UTF16BE))
{
if (css_cnt_orig == 8 || css_cnt_orig == 10)
{
css_cnt_r = 2;
}
else
{
css_cnt_r = 4;
}
}
else
{
if ((mask_ctx->css_buf[0].cs_len * mask_ctx->css_buf[1].cs_len * mask_ctx->css_buf[2].cs_len) > 256)
{
css_cnt_r = 3;
}
else
{
css_cnt_r = 4;
}
}
}
}
else
{
css_cnt_r = 1;
/* unfinished code?
int sum = css_buf[css_cnt_r - 1].cs_len;
for (u32 i = 1; i < 4 && i < css_cnt; i++)
{
if (sum > 1) break; // we really don't need alot of amplifier them for slow hashes
css_cnt_r++;
sum *= css_buf[css_cnt_r - 1].cs_len;
}
*/
}
}
css_cnt_l -= css_cnt_r;
css_cnt_lr[0] = css_cnt_l;
css_cnt_lr[1] = css_cnt_r;
}
static int mp_css_append_salt (hashcat_ctx_t *hashcat_ctx, salt_t *salt_buf)
{
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
u32 salt_len = salt_buf->salt_len;
u8 *salt_buf_ptr = (u8 *) salt_buf->salt_buf;
if ((mask_ctx->css_cnt + salt_len) > 256) return -1;
for (u32 i = 0, j = mask_ctx->css_cnt; i < salt_len; i++, j++)
{
mask_ctx->css_buf[j].cs_buf[0] = salt_buf_ptr[i];
mask_ctx->css_buf[j].cs_len = 1;
mask_ctx->css_cnt++;
}
return 0;
}
static int mp_css_utf16le_expand (hashcat_ctx_t *hashcat_ctx)
{
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
u32 css_cnt_utf16le = mask_ctx->css_cnt * 2;
if (css_cnt_utf16le > 256) return -1;
cs_t *css_buf_utf16le = (cs_t *) hccalloc (css_cnt_utf16le, sizeof (cs_t));
for (u32 i = 0, j = 0; i < mask_ctx->css_cnt; i += 1, j += 2)
{
memcpy (&css_buf_utf16le[j + 0], &mask_ctx->css_buf[i], sizeof (cs_t));
css_buf_utf16le[j + 1].cs_buf[0] = 0;
css_buf_utf16le[j + 1].cs_len = 1;
}
memcpy (mask_ctx->css_buf, css_buf_utf16le, css_cnt_utf16le * sizeof (cs_t));
mask_ctx->css_cnt = css_cnt_utf16le;
hcfree (css_buf_utf16le);
return 0;
}
static int mp_css_utf16be_expand (hashcat_ctx_t *hashcat_ctx)
{
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
u32 css_cnt_utf16be = mask_ctx->css_cnt * 2;
if (css_cnt_utf16be > 256) return -1;
cs_t *css_buf_utf16be = (cs_t *) hccalloc (css_cnt_utf16be, sizeof (cs_t));
for (u32 i = 0, j = 0; i < mask_ctx->css_cnt; i += 1, j += 2)
{
css_buf_utf16be[j + 0].cs_buf[0] = 0;
css_buf_utf16be[j + 0].cs_len = 1;
memcpy (&css_buf_utf16be[j + 1], &mask_ctx->css_buf[i], sizeof (cs_t));
}
memcpy (mask_ctx->css_buf, css_buf_utf16be, css_cnt_utf16be * sizeof (cs_t));
mask_ctx->css_cnt = css_cnt_utf16be;
hcfree (css_buf_utf16be);
return 0;
}
static int mp_css_to_uniq_tbl (hashcat_ctx_t *hashcat_ctx, u32 css_cnt, cs_t *css, u32 uniq_tbls[SP_PW_MAX][CHARSIZ])
{
/* generates a lookup table where key is the char itself for fastest possible lookup performance */
if (css_cnt > SP_PW_MAX)
{
event_log_error (hashcat_ctx, "Mask length is too long.");
return -1;
}
for (u32 css_pos = 0; css_pos < css_cnt; css_pos++)
{
u32 *uniq_tbl = uniq_tbls[css_pos];
u32 *cs_buf = css[css_pos].cs_buf;
u32 cs_len = css[css_pos].cs_len;
for (u32 cs_pos = 0; cs_pos < cs_len; cs_pos++)
{
u32 c = cs_buf[cs_pos] & 0xff;
uniq_tbl[c] = 1;
}
}
return 0;
}
static int mp_add_cs_buf (hashcat_ctx_t *hashcat_ctx, const u32 *in_buf, size_t in_len, cs_t *css, u32 css_cnt)
{
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
if (css_cnt == 256)
{
event_log_error (hashcat_ctx, "Invalid mask length.");
return -1;
}
cs_t *cs = &css[css_cnt];
size_t css_uniq_sz = CHARSIZ * sizeof (u32);
u32 *css_uniq = (u32 *) hcmalloc (css_uniq_sz);
size_t i;
for (i = 0; i < cs->cs_len; i++)
{
const u32 u = cs->cs_buf[i];
css_uniq[u] = 1;
}
for (i = 0; i < in_len; i++)
{
u32 u = in_buf[i] & 0xff;
if (hashconfig->opts_type & OPTS_TYPE_PT_UPPER) u = (u32) toupper (u);
if (css_uniq[u] == 1) continue;
css_uniq[u] = 1;
cs->cs_buf[cs->cs_len] = u;
cs->cs_len++;
}
hcfree (css_uniq);
return 0;
}
static int mp_expand (hashcat_ctx_t *hashcat_ctx, const char *in_buf, size_t in_len, cs_t *mp_sys, cs_t *mp_usr, u32 mp_usr_offset, int interpret)
{
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
size_t in_pos;
for (in_pos = 0; in_pos < in_len; in_pos++)
{
u32 p0 = in_buf[in_pos] & 0xff;
if (interpret == 1 && p0 == '?')
{
in_pos++;
if (in_pos == in_len)
{
event_log_error (hashcat_ctx, "Syntax error in mask: %s", in_buf);
return -1;
}
u32 p1 = in_buf[in_pos] & 0xff;
int rc = 0;
switch (p1)
{
case 'l': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[0].cs_buf, mp_sys[0].cs_len, mp_usr, mp_usr_offset);
break;
case 'u': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[1].cs_buf, mp_sys[1].cs_len, mp_usr, mp_usr_offset);
break;
case 'd': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[2].cs_buf, mp_sys[2].cs_len, mp_usr, mp_usr_offset);
break;
case 's': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[3].cs_buf, mp_sys[3].cs_len, mp_usr, mp_usr_offset);
break;
case 'a': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[4].cs_buf, mp_sys[4].cs_len, mp_usr, mp_usr_offset);
break;
case 'b': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[5].cs_buf, mp_sys[5].cs_len, mp_usr, mp_usr_offset);
break;
case 'h': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[6].cs_buf, mp_sys[6].cs_len, mp_usr, mp_usr_offset);
break;
case 'H': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[7].cs_buf, mp_sys[7].cs_len, mp_usr, mp_usr_offset);
break;
case '1': if (mp_usr[0].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 1 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[0].cs_buf, mp_usr[0].cs_len, mp_usr, mp_usr_offset);
break;
case '2': if (mp_usr[1].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 2 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[1].cs_buf, mp_usr[1].cs_len, mp_usr, mp_usr_offset);
break;
case '3': if (mp_usr[2].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 3 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[2].cs_buf, mp_usr[2].cs_len, mp_usr, mp_usr_offset);
break;
case '4': if (mp_usr[3].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 4 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[3].cs_buf, mp_usr[3].cs_len, mp_usr, mp_usr_offset);
break;
case '?': rc = mp_add_cs_buf (hashcat_ctx, &p0, 1, mp_usr, mp_usr_offset);
break;
default: event_log_error (hashcat_ctx, "Syntax error in mask: %s", in_buf);
return -1;
}
if (rc == -1) return -1;
}
else
{
if (hashconfig->opts_type & OPTS_TYPE_PT_HEX)
{
in_pos++;
if (in_pos == in_len)
{
event_log_error (hashcat_ctx, "The hex-charset option expects exactly 2 hexadecimal chars. Failed mask: %s", in_buf);
return -1;
}
u32 p1 = in_buf[in_pos] & 0xff;
if ((is_valid_hex_char ((u8) p0) == false) || (is_valid_hex_char ((u8) p1) == false))
{
event_log_error (hashcat_ctx, "Invalid hex character detected in mask %s", in_buf);
return -1;
}
u32 chr = 0;
chr = (u32) hex_convert ((u8) p1) << 0;
chr |= (u32) hex_convert ((u8) p0) << 4;
const int rc = mp_add_cs_buf (hashcat_ctx, &chr, 1, mp_usr, mp_usr_offset);
if (rc == -1) return -1;
}
else
{
u32 chr = p0;
const int rc = mp_add_cs_buf (hashcat_ctx, &chr, 1, mp_usr, mp_usr_offset);
if (rc == -1) return -1;
}
}
}
return 0;
}
static int mp_gen_css (hashcat_ctx_t *hashcat_ctx, char *mask_buf, size_t mask_len, cs_t *mp_sys, cs_t *mp_usr, cs_t *css_buf, u32 *css_cnt)
{
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
memset (css_buf, 0, 256 * sizeof (cs_t));
size_t mask_pos;
size_t css_pos;
for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
{
char p0 = mask_buf[mask_pos];
if (p0 == '?')
{
mask_pos++;
if (mask_pos == mask_len)
{
event_log_error (hashcat_ctx, "Syntax error in mask: %s", mask_buf);
return -1;
}
char p1 = mask_buf[mask_pos];
u32 chr = (u32) p1;
int rc = 0;
switch (p1)
{
case 'l': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[0].cs_buf, mp_sys[0].cs_len, css_buf, css_pos);
break;
case 'u': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[1].cs_buf, mp_sys[1].cs_len, css_buf, css_pos);
break;
case 'd': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[2].cs_buf, mp_sys[2].cs_len, css_buf, css_pos);
break;
case 's': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[3].cs_buf, mp_sys[3].cs_len, css_buf, css_pos);
break;
case 'a': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[4].cs_buf, mp_sys[4].cs_len, css_buf, css_pos);
break;
case 'b': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[5].cs_buf, mp_sys[5].cs_len, css_buf, css_pos);
break;
case 'h': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[6].cs_buf, mp_sys[6].cs_len, css_buf, css_pos);
break;
case 'H': rc = mp_add_cs_buf (hashcat_ctx, mp_sys[7].cs_buf, mp_sys[7].cs_len, css_buf, css_pos);
break;
case '1': if (mp_usr[0].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 1 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[0].cs_buf, mp_usr[0].cs_len, css_buf, css_pos);
break;
case '2': if (mp_usr[1].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 2 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[1].cs_buf, mp_usr[1].cs_len, css_buf, css_pos);
break;
case '3': if (mp_usr[2].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 3 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[2].cs_buf, mp_usr[2].cs_len, css_buf, css_pos);
break;
case '4': if (mp_usr[3].cs_len == 0) { event_log_error (hashcat_ctx, "Custom-charset 4 is undefined."); return -1; }
rc = mp_add_cs_buf (hashcat_ctx, mp_usr[3].cs_buf, mp_usr[3].cs_len, css_buf, css_pos);
break;
case '?': rc = mp_add_cs_buf (hashcat_ctx, &chr, 1, css_buf, css_pos);
break;
default: event_log_error (hashcat_ctx, "Syntax error in mask: %s", mask_buf);
return -1;
}
if (rc == -1) return -1;
}
else
{
if (hashconfig->opts_type & OPTS_TYPE_PT_HEX)
{
mask_pos++;
// if there is no 2nd hex character, show an error:
if (mask_pos == mask_len)
{
event_log_error (hashcat_ctx, "The hex-charset option expects exactly 2 hexadecimal chars. Failed mask: %s", mask_buf);
return -1;
}
char p1 = mask_buf[mask_pos];
// if they are not valid hex character, show an error:
if ((is_valid_hex_char ((u8) p0) == false) || (is_valid_hex_char ((u8) p1) == false))
{
event_log_error (hashcat_ctx, "Invalid hex character detected in mask %s", mask_buf);
return -1;
}
u32 chr = 0;
chr |= (u32) hex_convert ((u8) p1) << 0;
chr |= (u32) hex_convert ((u8) p0) << 4;
const int rc = mp_add_cs_buf (hashcat_ctx, &chr, 1, css_buf, css_pos);
if (rc == -1) return -1;
}
else
{
u32 chr = (u32) p0;
const int rc = mp_add_cs_buf (hashcat_ctx, &chr, 1, css_buf, css_pos);
if (rc == -1) return -1;
}
}
}
if (css_pos == 0)
{
event_log_error (hashcat_ctx, "Invalid mask length (0).");
return -1;
}
*css_cnt = css_pos;
return 0;
}
static int mp_get_truncated_mask (hashcat_ctx_t *hashcat_ctx, const char *mask_buf, const size_t mask_len, const u32 len, char *new_mask_buf)
{
const hashconfig_t *hashconfig = hashcat_ctx->hashconfig;
u32 mask_pos;
u32 css_pos;
for (mask_pos = 0, css_pos = 0; mask_pos < mask_len; mask_pos++, css_pos++)
{
if (css_pos == len) break;
char p0 = mask_buf[mask_pos];
new_mask_buf[mask_pos] = p0;
if (p0 == '?')
{
mask_pos++;
if (mask_pos == mask_len) break;
new_mask_buf[mask_pos] = mask_buf[mask_pos];
}
else
{
if (hashconfig->opts_type & OPTS_TYPE_PT_HEX)
{
mask_pos++;
if (mask_pos == mask_len)
{
event_log_error (hashcat_ctx, "The hex-charset option expects exactly 2 hexadecimal chars. Failed mask: %s", mask_buf);
return -1;
}
char p1 = mask_buf[mask_pos];
// if they are not valid hex character, show an error:
if ((is_valid_hex_char ((u8) p0) == false) || (is_valid_hex_char ((u8) p1) == false))
{
event_log_error (hashcat_ctx, "Invalid hex character detected in mask: %s", mask_buf);
return -1;
}
new_mask_buf[mask_pos] = p1;
}
}
}
return 0;
}
static void mp_setup_sys (cs_t *mp_sys)
{
u32 pos;
u32 chr;
u32 donec[CHARSIZ] = { 0 };
for (pos = 0, chr = 'a'; chr <= 'z'; chr++) { donec[chr] = 1;
mp_sys[0].cs_buf[pos++] = chr;
mp_sys[0].cs_len = pos; }
for (pos = 0, chr = 'A'; chr <= 'Z'; chr++) { donec[chr] = 1;
mp_sys[1].cs_buf[pos++] = chr;
mp_sys[1].cs_len = pos; }
for (pos = 0, chr = '0'; chr <= '9'; chr++) { donec[chr] = 1;
mp_sys[2].cs_buf[pos++] = chr;
mp_sys[2].cs_len = pos; }
for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { if (donec[chr]) continue;
mp_sys[3].cs_buf[pos++] = chr;
mp_sys[3].cs_len = pos; }
for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { mp_sys[4].cs_buf[pos++] = chr;
mp_sys[4].cs_len = pos; }
for (pos = 0, chr = 0x00; chr <= 0xff; chr++) { mp_sys[5].cs_buf[pos++] = chr;
mp_sys[5].cs_len = pos; }
for (pos = 0, chr = 0x30; chr <= 0x39; chr++) { mp_sys[6].cs_buf[pos++] = chr;
mp_sys[6].cs_len = pos; }
for (pos = 10, chr = 0x61; chr <= 0x66; chr++) { mp_sys[6].cs_buf[pos++] = chr;
mp_sys[6].cs_len = pos; }
for (pos = 0, chr = 0x30; chr <= 0x39; chr++) { mp_sys[7].cs_buf[pos++] = chr;
mp_sys[7].cs_len = pos; }
for (pos = 10, chr = 0x41; chr <= 0x46; chr++) { mp_sys[7].cs_buf[pos++] = chr;
mp_sys[7].cs_len = pos; }
}
static int mp_setup_usr (hashcat_ctx_t *hashcat_ctx, cs_t *mp_sys, cs_t *mp_usr, const char *buf, const u32 userindex)
{
HCFILE fp;
if (hc_fopen (&fp, buf, "rb") == false)
{
const int rc = mp_expand (hashcat_ctx, buf, strlen (buf), mp_sys, mp_usr, userindex, 1);
if (rc == -1) return -1;
}
else
{
char mp_file[1024];
const size_t nread = hc_fread (mp_file, 1, sizeof (mp_file) - 1, &fp);
if (!hc_feof (&fp))
{
event_log_error (hashcat_ctx, "%s: Custom charset file is too large.", buf);
hc_fclose (&fp);
return -1;
}
hc_fclose (&fp);
if (nread == 0)
{
event_log_error (hashcat_ctx, "%s: Custom charset file is empty.", buf);
return -1;
}
mp_file[nread] = 0;
const size_t len = superchop_with_length (mp_file, nread);
if (len == 0)
{
event_log_error (hashcat_ctx, "%s: Custom charset file is corrupted.", buf);
return -1;
}
const int rc = mp_expand (hashcat_ctx, mp_file, len, mp_sys, mp_usr, userindex, 0);
if (rc == -1) return -1;
}
return 0;
}
static void mp_reset_usr (cs_t *mp_usr, const u32 userindex)
{
mp_usr[userindex].cs_len = 0;
memset (mp_usr[userindex].cs_buf, 0, sizeof (mp_usr[userindex].cs_buf));
}
static int sp_setup_tbl (hashcat_ctx_t *hashcat_ctx)
{
folder_config_t *folder_config = hashcat_ctx->folder_config;
mask_ctx_t *mask_ctx = hashcat_ctx->mask_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
char *shared_dir = folder_config->shared_dir;
char *hcstat = user_options->markov_hcstat2;
u32 disable = user_options->markov_disable;
u32 classic = user_options->markov_classic;
hcstat_table_t *root_table_buf = mask_ctx->root_table_buf;
hcstat_table_t *markov_table_buf = mask_ctx->markov_table_buf;
/**
* Initialize hcstats
*/
u64 *root_stats_buf = (u64 *) hccalloc (SP_ROOT_CNT, sizeof (u64));
u64 *root_stats_ptr = root_stats_buf;
u64 *root_stats_buf_by_pos[SP_PW_MAX];
for (int i = 0; i < SP_PW_MAX; i++)
{
root_stats_buf_by_pos[i] = root_stats_ptr;
root_stats_ptr += CHARSIZ;
}
u64 *markov_stats_buf = (u64 *) hccalloc (SP_MARKOV_CNT, sizeof (u64));
u64 *markov_stats_ptr = markov_stats_buf;
u64 *markov_stats_buf_by_key[SP_PW_MAX][CHARSIZ];
for (int i = 0; i < SP_PW_MAX; i++)
{
for (int j = 0; j < CHARSIZ; j++)
{
markov_stats_buf_by_key[i][j] = markov_stats_ptr;
markov_stats_ptr += CHARSIZ;
}
}
/**
* Load hcstats File
*/
char hcstat_tmp[256];
if (hcstat == NULL)
{
snprintf (hcstat_tmp, sizeof (hcstat_tmp), "%s/%s", shared_dir, SP_HCSTAT);
hcstat = hcstat_tmp;
}
struct stat s;
if (stat (hcstat, &s) == -1)
{
event_log_error (hashcat_ctx, "%s: %s", hcstat, strerror (errno));
return -1;
}
HCFILE fp;
if (hc_fopen (&fp, hcstat, "rb") == false)
{
event_log_error (hashcat_ctx, "%s: %s", hcstat, strerror (errno));
return -1;
}
u8 *inbuf = (u8 *) hcmalloc (s.st_size);
SizeT inlen = (SizeT) hc_fread (inbuf, 1, s.st_size, &fp);
if (inlen != (SizeT) s.st_size)
{
event_log_error (hashcat_ctx, "%s: Could not read data.", hcstat);
hc_fclose (&fp);
hcfree (inbuf);
return -1;
}
hc_fclose (&fp);
u8 *outbuf = (u8 *) hcmalloc (SP_FILESZ);
SizeT outlen = SP_FILESZ;
const char props = 0x1c; // lzma properties constant, retrieved with 7z2hashcat
const SRes res = hc_lzma2_decompress (inbuf, &inlen, outbuf, &outlen, &props);
if (res != SZ_OK)
{
event_log_error (hashcat_ctx, "%s: Could not uncompress data.", hcstat);
hcfree (inbuf);
hcfree (outbuf);
return -1;
}
if (outlen != SP_FILESZ)
{
event_log_error (hashcat_ctx, "%s: Could not uncompress data.", hcstat);
hcfree (inbuf);
hcfree (outbuf);
return -1;
}
u64 *ptr = (u64 *) outbuf;
u64 v = *ptr++;
u64 z = *ptr++;
memcpy (root_stats_buf, ptr, sizeof (u64) * SP_ROOT_CNT); ptr += SP_ROOT_CNT;
memcpy (markov_stats_buf, ptr, sizeof (u64) * SP_MARKOV_CNT); // ptr += SP_MARKOV_CNT;
hcfree (inbuf);
hcfree (outbuf);
/**
* switch endianess
*/
v = byte_swap_64 (v);
z = byte_swap_64 (z);
for (int i = 0; i < SP_ROOT_CNT; i++) root_stats_buf[i] = byte_swap_64 (root_stats_buf[i]);
for (int i = 0; i < SP_MARKOV_CNT; i++) markov_stats_buf[i] = byte_swap_64 (markov_stats_buf[i]);
/**
* verify header
*/
if (v != SP_VERSION)
{
event_log_error (hashcat_ctx, "%s: Invalid header", hcstat);
return -1;
}
if (z != 0)
{
event_log_error (hashcat_ctx, "%s: Invalid header", hcstat);
return -1;
}
/**
* Markov modifier of hcstat_table on user request
*/
if (disable)
{
memset (root_stats_buf, 0, SP_ROOT_CNT * sizeof (u64));
memset (markov_stats_buf, 0, SP_MARKOV_CNT * sizeof (u64));
}
if (classic)
{
/* Add all stats to first position */
for (int i = 1; i < SP_PW_MAX; i++)
{
u64 *out = root_stats_buf_by_pos[0];
u64 *in = root_stats_buf_by_pos[i];
for (int j = 0; j < CHARSIZ; j++)
{
*out++ += *in++;
}
}
for (int i = 1; i < SP_PW_MAX; i++)
{
u64 *out = markov_stats_buf_by_key[0][0];
u64 *in = markov_stats_buf_by_key[i][0];
for (int j = 0; j < CHARSIZ; j++)
{
for (int k = 0; k < CHARSIZ; k++)
{
*out++ += *in++;
}
}
}
/* copy them to all pw_positions */
for (int i = 1; i < SP_PW_MAX; i++)
{
memcpy (root_stats_buf_by_pos[i], root_stats_buf_by_pos[0], CHARSIZ * sizeof (u64));
}
for (int i = 1; i < SP_PW_MAX; i++)
{
memcpy (markov_stats_buf_by_key[i][0], markov_stats_buf_by_key[0][0], CHARSIZ * CHARSIZ * sizeof (u64));
}
}
/**
* Initialize tables
*/
hcstat_table_t *root_table_ptr = root_table_buf;
hcstat_table_t *root_table_buf_by_pos[SP_PW_MAX];
for (int i = 0; i < SP_PW_MAX; i++)
{
root_table_buf_by_pos[i] = root_table_ptr;
root_table_ptr += CHARSIZ;
}
hcstat_table_t *markov_table_ptr = markov_table_buf;
hcstat_table_t *markov_table_buf_by_key[SP_PW_MAX][CHARSIZ];
for (int i = 0; i < SP_PW_MAX; i++)
{
for (int j = 0; j < CHARSIZ; j++)
{
markov_table_buf_by_key[i][j] = markov_table_ptr;
markov_table_ptr += CHARSIZ;
}
}
/**
* Convert hcstat to tables
*/
for (int i = 0; i < SP_ROOT_CNT; i++)
{
u32 key = i % CHARSIZ;
root_table_buf[i].key = key;
root_table_buf[i].val = root_stats_buf[i];
}
for (int i = 0; i < SP_MARKOV_CNT; i++)
{
u32 key = i % CHARSIZ;
markov_table_buf[i].key = key;
markov_table_buf[i].val = markov_stats_buf[i];
}
hcfree (root_stats_buf);
hcfree (markov_stats_buf);
/**
* Finally sort them
*/
for (int i = 0; i < SP_PW_MAX; i++)
{
qsort (root_table_buf_by_pos[i], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
}
for (int i = 0; i < SP_PW_MAX; i++)
{
for (int j = 0; j < CHARSIZ; j++)
{
qsort (markov_table_buf_by_key[i][j], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val);
}
}
return 0;
}
static int sp_get_sum (u32 start, u32 stop, cs_t *root_css_buf, u64 *result)
{
u64 sum = 1;
u32 i;
for (i = start; i < stop; i++)
{
if (overflow_check_u64_mul (sum, root_css_buf[i].cs_len) == false) return -1;
sum *= root_css_buf[i].cs_len;
}
*result = sum;
return 0;
}
static void sp_tbl_to_css (hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf, cs_t *root_css_buf, cs_t *markov_css_buf, u32 threshold, u32 uniq_tbls[SP_PW_MAX][CHARSIZ])
{
memset (root_css_buf, 0, SP_PW_MAX * sizeof (cs_t));
memset (markov_css_buf, 0, SP_PW_MAX * CHARSIZ * sizeof (cs_t));
/**
* Convert tables to css
*/
for (u32 i = 0; i < SP_ROOT_CNT; i++)
{
u32 pw_pos = i / CHARSIZ;
cs_t *cs = &root_css_buf[pw_pos];
if (cs->cs_len == threshold) continue;
u32 key = root_table_buf[i].key;
if (uniq_tbls[pw_pos][key] == 0) continue;
cs->cs_buf[cs->cs_len] = key;
cs->cs_len++;
}
/**
* Convert table to css
*/
for (u32 i = 0; i < SP_MARKOV_CNT; i++)
{
u32 c = i / CHARSIZ;
cs_t *cs = &markov_css_buf[c];
if (cs->cs_len == threshold) continue;
u32 pw_pos = c / CHARSIZ;
u32 key = markov_table_buf[i].key;
if ((pw_pos + 1) < SP_PW_MAX) if (uniq_tbls[pw_pos + 1][key] == 0) continue;
cs->cs_buf[cs->cs_len] = key;