-
Notifications
You must be signed in to change notification settings - Fork 5
/
rule.cpp
705 lines (635 loc) · 25.4 KB
/
rule.cpp
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
//
// Created by Vavaldi on 14-8-2021.
//
#include "rule.h"
#include <cctype>
#include <iostream>
Rule::Rule(const char input_rule, const std::string& input_rule_value_1, const std::string& input_rule_value_2) {
rule_value_1 = input_rule_value_1;
rule_value_2 = input_rule_value_2;
rule = input_rule;
rule_processor = build_rule_processor();
invalid_rule = false;
if(!validate_rule()) {
invalid_rule = true;
};
}
bool Rule::operator==(const Rule& rhs) const
{
return (rule == rhs.rule) && (rule_value_1 == rhs.rule_value_1) && (rule_value_2 == rhs.rule_value_2);
// or, in C++11 (must #include <tuple>)
// return std::tie(apple, banana) == std::tie(rhs.apple, rhs.banana);
}
int Rule::rule_identify(char rule_char) {
switch(rule_char) {
case ':':
case 'l':
case 'u':
case 'c':
case 'C':
case 't':
case 'r':
case 'd':
case 'f':
case '{':
case '}':
case '[':
case ']':
case 'k':
case 'K':
case 'q':
case 'E':
return 1;
case '@':
case 'T':
case 'p':
case 'D':
case 'Z':
case 'z':
case '$':
case '^':
case '<':
case '>':
case '_':
case '\'':
case '!':
case '/':
case 'y':
case 'Y':
case '-':
case '+':
case 'e':
case '.':
case ',':
case 'L':
case 'R':
return 2;
case 's':
case 'S':
case 'x':
case 'O':
case 'o':
case 'i':
case '3':
case '*':
return 3;
default:
return -1;
}
}
bool Rule::validate_rule() const {
switch(rule) {
case ':': return true;
case 'l':
case 'u':
case 'c':
case 'C':
case 't':
case 'r':
case 'd':
case 'f':
case '{':
case '}':
case '[':
case ']':
case 'k':
case 'K':
case 'q':
case 'E':
if(!(rule_value_1.empty() && rule_value_2.empty())) { // Unary operations should not have rule values.
return false;
}
return true;
case '@':
case 'T':
case 'p':
case 'D':
case 'Z':
case 'z':
case '$':
case '^':
case '<':
case '>':
case '_':
case '\'':
case '!':
case '/':
case 'y':
case 'Y':
case '-':
case '+':
case 'e':
case '.':
case ',':
case 'L':
case 'R':
if(rule_value_1.empty() || !rule_value_2.empty()) { // Binary operations should not have 2 rule values.
return false;
}
return true;
case 's':
case 'S':
case 'x':
case 'O':
case 'o':
case 'i':
case '3':
case '*':
if(rule_value_1.empty() || rule_value_2.empty()) {
return false;
}
return true;
default:
return false;
}
}
std::function<void(std::string&)> Rule::build_rule_processor() {
int int_value_1, int_value_2;
bool value_1_is_numeric = (rule_value_1.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos);
bool value_2_is_numeric = (rule_value_2.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos);
auto get_number_value = [](const std::string& value)
{
std::string alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
size_t index = alphabet.find(value[0]);
return static_cast<int>(index);
};
switch(rule) {
case ':':
return [](std::string& plaintext){};
case 'l':
return [](std::string& plaintext){
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::tolower);
};
case 'u':
return [](std::string& plaintext){
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::toupper);
};
case 'c':
return [](std::string& plaintext){
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::tolower);
plaintext[0] = char(toupper(plaintext[0]));
};
case 'C':
return [](std::string& plaintext){
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::toupper);
plaintext[0] = char(tolower(plaintext[0]));
};
case 't':
return [](std::string& plaintext){
for (char& i : plaintext) {
if(islower(static_cast<unsigned char>(i))) {
i = char(toupper(i));
} else if (isupper(static_cast<unsigned char>(i))) {
i = char(tolower(i));
}
}
};
case 'q':
return [](std::string& plaintext){
for(std::string::size_type i = 0; i < plaintext.size(); ++i) {
plaintext.insert(i+1, 1, plaintext[i]);
i++;
}
};
case 'r':
return [](std::string& plaintext){
reverse(plaintext.begin(), plaintext.end());
};
case 'k':
return [](std::string& plaintext){
if(plaintext.length() < 2) return;
char i = plaintext[0];
plaintext[0] = plaintext[1];
plaintext[1] = i;
};
case 'K':
return [](std::string& plaintext){
if(plaintext.length() < 2) return;
std::swap(plaintext[plaintext.length()-1], plaintext[plaintext.length()-2]);
};
case 'd':
return [](std::string& plaintext){
plaintext += plaintext;
};
case 'f':
return [](std::string& plaintext){
std::string copy = plaintext; // Extra copy for operations
reverse(copy.begin(), copy.end());
plaintext += copy;
};
case '{':
return [](std::string& plaintext){
if(plaintext.empty()) return;
plaintext += plaintext[0];
plaintext.erase(0,1);
};
case '}':
return [](std::string& plaintext){
if(plaintext.empty()) return;
plaintext.insert(0, 1, plaintext.back());
plaintext.pop_back();
};
case '[':
return [](std::string& plaintext){
plaintext.erase(0, 1);
};
case ']':
return [](std::string& plaintext){
if(plaintext.empty()) return;
plaintext.erase(plaintext.size()-1, 1);
};
case 'E':
return [](std::string& plaintext) {
if(plaintext.empty()) return;
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::tolower);
plaintext[0] = char(toupper(plaintext[0]));
for (int i=0; i < plaintext.size(); i++) {
if(i-1 < plaintext.size() && plaintext[i] == ' ') {
plaintext[i + 1] = char(toupper(plaintext[i + 1]));
}
}
};
case 'T':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // character location
return [char_location = int_value_1](std::string& plaintext){
if(plaintext.empty() || char_location > plaintext.size()-1) {
return;
}
if(islower(static_cast<unsigned char>(plaintext[char_location]))) {
plaintext[char_location] = char(toupper(plaintext[char_location]));
} else if (isupper(static_cast<unsigned char>(plaintext[char_location]))) {
plaintext[char_location] = char(tolower(plaintext[char_location]));
}
};
case 'p':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1);
return [duplicate_count=int_value_1](std::string& plaintext){
std::string copy = plaintext;
for(int i=0; i < duplicate_count; i++) {
plaintext += copy;
}
};
case 'D':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // character location
if(int_value_1 < 0) {
break;
}
return [char_location=int_value_1](std::string& plaintext){
if(plaintext.empty() || char_location > plaintext.size()-1) {
return;
}
plaintext.erase(char_location, 1);
};
case 'z':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // duplicate amount
return [duplicate_count=int_value_1](std::string& plaintext){
for(int i=0; i < duplicate_count; i++) {
plaintext.insert(0, plaintext.substr(0,1));
}
};
case 'Z':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // duplicate amount
return [duplicate_count=int_value_1](std::string& plaintext){
if(plaintext.empty()) return;
for(int i=0; i < duplicate_count; i++) {
plaintext += plaintext.substr(plaintext.size()-1, 1);
}
};
case '\'':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // duplicate amount
if(int_value_1 < 0) {
break;
}
return [duplicate_count=int_value_1](std::string& plaintext){
if(plaintext.empty() || duplicate_count > plaintext.size()-1) {
return;
}
plaintext = plaintext.substr(0, duplicate_count);
};
case 's':
if(rule_value_1.size() == 1 && rule_value_2.size() == 1) {
return [rule_value_1=rule_value_1[0], rule_value_2=rule_value_2[0]](std::string& plaintext){
std::replace(plaintext.begin(), plaintext.end(), rule_value_1, rule_value_2);
};
} else {
return [rule_value_1=rule_value_1, rule_value_2=rule_value_2](std::string& plaintext){
size_t index = 0;
while(true) {
index = plaintext.find(rule_value_1, index);
if(index == std::string::npos) break;
plaintext = plaintext.replace(index, rule_value_1.size(), rule_value_2);
index += rule_value_2.size();
}
};
}
case 'S':
return [rule_value_1=rule_value_1, rule_value_2=rule_value_2](std::string& plaintext){
if(plaintext.find(rule_value_1) != std::string::npos) {
plaintext = plaintext.replace(plaintext.find(rule_value_1), rule_value_1.size(), rule_value_2);
}
};
case '$':
return [rule_value_1=rule_value_1](std::string& plaintext){
plaintext.append(rule_value_1);
};
case '^':
return [rule_value_1=rule_value_1](std::string& plaintext){
plaintext.insert(0, rule_value_1);
};
case 'y':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
if(int_value_1 == 0) break;
return [rule_value_1=int_value_1](std::string& plaintext){
plaintext.insert(0, plaintext.substr(0, rule_value_1));
};
case 'Y':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
if(int_value_1 == 0) break;
return [rule_value_1=int_value_1](std::string& plaintext){
if(rule_value_1 > plaintext.size()) {
plaintext += plaintext;
} else {
plaintext += plaintext.substr(plaintext.size() - rule_value_1);
}
};
case 'L':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
return [rule_value_1=int_value_1](std::string& plaintext){
if(rule_value_1 >= plaintext.size()) return;
plaintext[rule_value_1] = static_cast<char>(static_cast<unsigned char>(plaintext[rule_value_1]) << 1);
};
case 'R':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
return [rule_value_1=int_value_1](std::string& plaintext){
if(rule_value_1 >= plaintext.size()) return;
plaintext[rule_value_1] = static_cast<char>(static_cast<unsigned char>(plaintext[rule_value_1]) >> 1);
};
case '-':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
return [rule_value_1=int_value_1](std::string& plaintext) {
if(rule_value_1 >= plaintext.size()) return;
plaintext[rule_value_1] = --plaintext[rule_value_1];
};
case '+':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
return [rule_value_1=int_value_1](std::string& plaintext) {
if(rule_value_1 >= plaintext.size()) return;
plaintext[rule_value_1] = ++plaintext[rule_value_1];
};
case '@':
return [rule_value_1=rule_value_1](std::string& plaintext){
plaintext.erase(std::remove(plaintext.begin(), plaintext.end(), rule_value_1[0]), plaintext.end());
};
case '.':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // target location
return [rule_value_1=int_value_1](std::string& plaintext){
if(rule_value_1+1 >= plaintext.size()) return;
plaintext[rule_value_1] = plaintext[rule_value_1+1];
};
case ',':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // target location
return [rule_value_1=int_value_1](std::string& plaintext){
if(rule_value_1 < plaintext.size() && rule_value_1 > 0) {
plaintext[rule_value_1] = plaintext[rule_value_1-1];
}
};
case 'e':
return [rule_value_1=rule_value_1](std::string& plaintext){
if(plaintext.empty()) return;
transform(plaintext.begin(), plaintext.end(), plaintext.begin(), ::tolower);
plaintext[0] = char(toupper(plaintext[0]));
for (int i=0; i < plaintext.size(); i++) {
if(i+1 < plaintext.size() && plaintext[i] == rule_value_1[0]) {
plaintext[i + 1] = char(toupper(plaintext[i + 1]));
}
}
};
case 'i':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // insert location
return [insert_location=int_value_1, rule_value_2=rule_value_2](std::string& plaintext) {
if(plaintext.empty() || insert_location > plaintext.size()) return;
plaintext.insert(insert_location, rule_value_2);
};
case 'O':
if(!value_1_is_numeric || !value_2_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // start location
int_value_2 = get_number_value(rule_value_2); // delete amount
return [start_loc=int_value_1, delete_amount=int_value_2](std::string& plaintext) {
if(start_loc > plaintext.size()-1 || plaintext.empty()) return;
if(start_loc + delete_amount < plaintext.size()) {
plaintext.erase(start_loc, delete_amount);
} else {
// plaintext.erase(start_loc, plaintext.size()-start_loc); // Delete until end. // Hashcat behaviour does not delete if it goes out of bound.
}
};
case 'o':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // overwrite location
if(int_value_1 < 0) break;
return [offset=int_value_1, replace_value=rule_value_2](std::string& plaintext){
if((offset+replace_value.size()-1) > plaintext.size()-1 || plaintext.empty()) return;
for(int i=0; i < replace_value.size(); i++) {
plaintext[offset + i] = replace_value[i];
}
};
case '*':
if(!value_1_is_numeric || !value_2_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // swap location 1
int_value_2 = get_number_value(rule_value_2); // swap location 2
return [swap_1=int_value_1, swap_2=int_value_2](std::string& plaintext){
if(plaintext.empty() || swap_1 >= plaintext.size() || swap_2 >= plaintext.size()) return;
std::swap(plaintext[swap_1], plaintext[swap_2]);
};
case 'x':
if(!value_1_is_numeric || !value_2_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // start location
int_value_2 = get_number_value(rule_value_2); // delete amount
if(int_value_1 < 0 || int_value_2 < 0) break;
return [start_loc=int_value_1, keep_amount=int_value_2](std::string& plaintext){
if(plaintext.empty() || start_loc >= plaintext.size()) return;
if((start_loc + keep_amount) <= plaintext.size()) {
plaintext.erase(start_loc + keep_amount, plaintext.size()-(start_loc+keep_amount));
plaintext.erase(0, start_loc); // Delete from start to start of rule
}
};
case '<':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1);
return [reject_count=int_value_1](std::string& plaintext){
if(reject_count < plaintext.size()) return;
plaintext = "";
};
case '>':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1);
return [reject_count=int_value_1](std::string& plaintext){
if(reject_count > plaintext.size()) return;
plaintext = "";
};
case '_':
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1);
return [reject_count=int_value_1](std::string& plaintext){
if(reject_count == plaintext.size()) {
plaintext = "";
}
};
case '!': // Reject plains which contain rule_value_1
return [rule_value_1=rule_value_1](std::string& plaintext){
if (plaintext.find(rule_value_1) != std::string::npos) {
plaintext = "";
}
};
case '/': // Reject plains which do not contain char X
return [rule_value_1=rule_value_1](std::string& plaintext){
if (plaintext.find(rule_value_1) == std::string::npos) {
plaintext = "";
}
};
case '3': // Toggle letter after nth instance of a char
if(!value_1_is_numeric) break;
int_value_1 = get_number_value(rule_value_1); // character location
return [rule_instance_of_char = int_value_1, char_value_rule = rule_value_2](std::string& plaintext) {
if(plaintext.empty()) return;
int n_th_instance_of_char = -1;
for(char &char_value : plaintext) {
if(n_th_instance_of_char == rule_instance_of_char) {
if (islower(static_cast<unsigned char>(char_value))) {
char_value = char(toupper(char_value));
} else if (isupper(static_cast<unsigned char>(char_value))) {
char_value = char(tolower(char_value));
}
return;
}
if(char_value == char_value_rule[0]) n_th_instance_of_char++;
}
};
}
invalid_rule = true;
return [](std::string& plaintext){};
}
void Rule::process(std::string& plaintext) {
return rule_processor(plaintext);
}
std::string Rule::print(int output_channel, bool hashcat_output) { // 0 = stdout, 1 = error, 2 = return
std::string rule_1_copy = rule_value_1;
std::string rule_2_copy = rule_value_2;
std::string debug_string;
// Replace tabs with hex encoded tabs
size_t start_pos = rule_1_copy.find('\t');
if(start_pos != std::string::npos) {
rule_1_copy.replace(start_pos, 1, "\\x09");
}
start_pos = rule_2_copy.find('\t');
if(start_pos != std::string::npos) {
rule_2_copy.replace(start_pos, 1, "\\x09");
}
if(hashcat_output) {
start_pos = rule_1_copy.find(' ');
if(start_pos != std::string::npos) {
rule_1_copy.replace(start_pos, 1, "\\x20");
}
start_pos = rule_2_copy.find(' ');
if(start_pos != std::string::npos) {
rule_2_copy.replace(start_pos, 1, "\\x20");
}
}
switch(Rule::rule_identify(rule)) {
case 1:
if (output_channel == 0) {
std::cout << rule;
} else if (output_channel == 1) {
std::cerr << rule;
} else if (output_channel == 2) {
debug_string += rule;
}
break;
case 2:
if (output_channel == 0) {
std::cout << rule << rule_1_copy;
} else if (output_channel == 1) {
std::cerr << rule << rule_1_copy;
} else if (output_channel == 2) {
debug_string += rule;
debug_string += rule_1_copy;
}
break;
case 3:
if (rule_value_1.size() > 1) { // if the first rule is multi-byte
start_pos = rule_1_copy.find('/');
if (start_pos != std::string::npos) {
rule_1_copy.replace(start_pos, 1, "\\/");
}
start_pos = rule_2_copy.find('/');
if (start_pos != std::string::npos) {
rule_2_copy.replace(start_pos, 1, "\\/");
}
if (output_channel == 0) {
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
} else if (output_channel == 1) {
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
} else if (output_channel == 2) {
debug_string += rule;
debug_string += '/';
debug_string += rule_1_copy;
debug_string += '/';
debug_string += rule_2_copy;
}
} else if (rule_value_2.size() > 1) { // if the second rule is multi-byte
start_pos = rule_1_copy.find('/');
if (start_pos != std::string::npos) {
rule_1_copy.replace(start_pos, 1, "\\/");
}
start_pos = rule_2_copy.find('/');
if (start_pos != std::string::npos) {
rule_2_copy.replace(start_pos, 1, "\\/");
}
if (output_channel == 0) {
if (hashcat_output) {
std::cout << rule << rule_1_copy << rule_2_copy;
} else {
std::cout << rule << '/' << rule_1_copy << '/' << rule_2_copy;
}
} else if (output_channel == 1) {
if (hashcat_output) {
std::cerr << rule << rule_1_copy << rule_2_copy;
} else {
std::cerr << rule << '/' << rule_1_copy << '/' << rule_2_copy;
}
} else if (output_channel == 2) {
debug_string += rule;
if (!hashcat_output) debug_string += '/';
debug_string += rule_1_copy;
if (!hashcat_output) debug_string += '/';
debug_string += rule_2_copy;
}
} else { // rule 1 is not multi-byte
if (output_channel == 0) {
std::cout << rule << rule_1_copy << rule_2_copy;
} else if (output_channel == 1) {
std::cerr << rule << rule_1_copy << rule_2_copy;
} else if (output_channel == 2) {
debug_string += rule;
debug_string += rule_1_copy;
debug_string += rule_2_copy;
}
}
break;
}
return debug_string;
}