-
Notifications
You must be signed in to change notification settings - Fork 28
/
hashcobra.cpp
executable file
·699 lines (641 loc) · 20.8 KB
/
hashcobra.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
/*******************************************************************************
* hashcobra - Optimized Dictionary Attack *
* *
* *
* DESCRIPTION *
* hashcobra - Hash Cracking tool using Optimized dictionary attack *
* *
* *
* AUTHOR *
* sepehrdad *
* *
* *
* LICENSE *
* This software is distributed under the GNU General Public License version 3 *
* *
* *
* LEGAL NOTICE *
* THIS SOFTWARE IS PROVIDED FOR EDUCATIONAL USE ONLY! IF YOU ENGAGE IN ANY *
* ILLEGAL ACTIVITY THE AUTHOR DOES NOT TAKE ANY RESPONSIBILITY FOR IT. *
* BY USING THIS SOFTWARE YOU AGREE WITH THESE TERMS. *
* *
******************************************************************************/
#include <filesystem>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <rocksdb/db.h>
#define LTC_NO_TEST
#define LTC_NO_CIPHERS
#define LTC_NO_MODES
#define LTC_NO_MACS
#define LTC_NO_PRNGS
#define LTC_NO_PK
#define LTC_NO_PKCS
#define LTC_NO_MISC
#include <tomcrypt.h>
#define VERSION "v2.0.0-beta"
#define ERR(str) std::cerr << "ERROR: " << str << '\n'
static void banner() { std::cout << "--==[ hashcobra by sepehrdad ]==--\n\n"; }
static void version() { std::cout << "hashcobra " << VERSION << '\n'; }
static void hashing_algorithms() {
static const char *h_algs[] = {
"blake2b-160", "blake2b-256", "blake2b-384", "blake2b-512", "blake2s-128",
"blake2s-160", "blake2s-224", "blake2s-256", "md2", "md4",
"md5", "sha1", "sha224", "sha256", "sha384",
"sha512", "sha3-224", "sha3-256", "sha3-384", "sha3-512",
"keccak-224", "keccak-256", "keccak-384", "keccak-512", "ripemd-128",
"ripemd-160", "ripemd-256", "ripemd-320", "whirlpool", "tiger"};
std::cout << "Supported hashing algorithms:\n\n";
for (const auto &alg : h_algs)
std::cout << " > " << alg << '\n';
}
static void compression_algorithms() {
static const char *c_algs[] = {"zstd", "snappy", "zlib", "bzip2",
"lz4", "lz4hc", "none"};
std::cout << "Supported compression algorithms:\n\n";
for (const auto &alg : c_algs)
std::cout << " > " << alg << '\n';
}
static void operations() {
static const char *oprs[] = {"create", "crack"};
std::cout << "Supported operations:\n\n";
for (const auto &opr : oprs)
std::cout << " > " << opr << '\n';
}
static void help() {
std::cout
<< "usage:\n\n"
<< " hashcobra -o <opr> [options] | [misc]\n\n"
<< "options:\n\n"
<< " -a <alg> - hashing algorithm [default: md5]\n"
<< " - ? to list available algorithms\n"
<< " -c <alg> - compression algorithm [default: zstd]\n"
<< " - ? to list available algorithms\n"
<< " -h <hash> - hash to crack\n"
<< " -r <path> - rainbow table path [default: hashcobra.db]\n"
<< " -d <path> - dictionary file path\n"
<< " -o <opr> - operation to do\n"
<< " - ? to list available operations\n"
<< "misc:\n\n"
<< " -V - show version\n"
<< " -H - show help\n\n"
<< "example:\n\n"
<< " # Create md5 rainbow table with zstd compression from rockyou.txt\n"
<< " $ hashcobra -o create -d rockyou.txt \n\n"
<< " # Create sha512 rainbow table with no compression from "
"darkc0de.lst\n"
<< " $ hashcobra -o create -a sha512 -c none -r rt.db -d "
"darkc0de.lst\n\n"
<< " # Crack 1a1dc91c907325c69271ddf0c944bc72 using rt.db\n"
<< " $ hashcobra -o crack -h 1a1dc91c907325c69271ddf0c944bc72 -r "
"rt.db\n\n";
}
class database {
rocksdb::DB *db{};
rocksdb::Options options{};
rocksdb::Status status{};
public:
database(std::string path,
rocksdb::CompressionType compression = rocksdb::kZSTD) {
options.create_if_missing = true;
options.compression = compression;
status = rocksdb::DB::Open(options, path, &db);
if (!status.ok()) {
ERR(status.ToString());
exit(EXIT_FAILURE);
}
}
void put(const std::string &key, const std::string &value) {
status = db->Put(rocksdb::WriteOptions(), key, value);
if (!status.ok()) {
ERR(status.ToString());
exit(EXIT_FAILURE);
}
}
void get(const std::string &key, std::string &value) {
status = db->Get(rocksdb::ReadOptions(), key, &value);
if (!status.ok()) {
if (status.IsNotFound()) {
ERR("Not Found: " + key);
value.clear();
} else {
ERR(status.ToString());
exit(EXIT_FAILURE);
}
}
}
~database() { delete db; }
};
static std::string str2hex(const unsigned char *digest,
unsigned int digest_len) {
char hex_digest[digest_len * 2];
static const char Hextable[] = "0123456789abcdef";
for (unsigned i = 0; i < digest_len; ++i) {
const uint8_t b = digest[i];
hex_digest[i * 2] = Hextable[b >> 4];
hex_digest[i * 2 + 1] = Hextable[b & 0xf];
}
return std::string(hex_digest, digest_len * 2);
}
static constexpr unsigned int str2int(const char *str, int h = 0) {
return !str[h] ? 5381 : (str2int(str, h + 1) * 33) ^ str[h];
}
static unsigned char *RIPEMD128(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
rmd128_init(&md);
rmd128_process(&md, input, ilen);
rmd128_done(&md, output);
return output;
}
static unsigned char *RIPEMD160(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
rmd160_init(&md);
rmd160_process(&md, input, ilen);
rmd160_done(&md, output);
return output;
}
static unsigned char *RIPEMD256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
rmd256_init(&md);
rmd256_process(&md, input, ilen);
rmd256_done(&md, output);
return output;
}
static unsigned char *RIPEMD320(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
rmd320_init(&md);
rmd320_process(&md, input, ilen);
rmd320_done(&md, output);
return output;
}
static unsigned char *MD2(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
md2_init(&md);
md2_process(&md, input, ilen);
md2_done(&md, output);
return output;
}
static unsigned char *MD4(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
md4_init(&md);
md4_process(&md, input, ilen);
md4_done(&md, output);
return output;
}
static unsigned char *MD5(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
md5_init(&md);
md5_process(&md, input, ilen);
md5_done(&md, output);
return output;
}
static unsigned char *SHA1(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha1_init(&md);
sha1_process(&md, input, ilen);
sha1_done(&md, output);
return output;
}
static unsigned char *SHA224(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha224_init(&md);
sha224_process(&md, input, ilen);
sha224_done(&md, output);
return output;
}
static unsigned char *SHA256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha256_init(&md);
sha256_process(&md, input, ilen);
sha256_done(&md, output);
return output;
}
static unsigned char *SHA384(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha384_init(&md);
sha384_process(&md, input, ilen);
sha384_done(&md, output);
return output;
}
static unsigned char *SHA512(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha512_init(&md);
sha512_process(&md, input, ilen);
sha512_done(&md, output);
return output;
}
static unsigned char *SHA3_224(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_224_init(&md);
sha3_process(&md, input, ilen);
sha3_done(&md, output);
return output;
}
static unsigned char *SHA3_256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_256_init(&md);
sha3_process(&md, input, ilen);
sha3_done(&md, output);
return output;
}
static unsigned char *SHA3_384(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_384_init(&md);
sha3_process(&md, input, ilen);
sha3_done(&md, output);
return output;
}
static unsigned char *SHA3_512(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_512_init(&md);
sha3_process(&md, input, ilen);
sha3_done(&md, output);
return output;
}
static unsigned char *KECCAK_224(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_224_init(&md);
sha3_process(&md, input, ilen);
keccak_done(&md, output);
return output;
}
static unsigned char *KECCAK_256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_256_init(&md);
sha3_process(&md, input, ilen);
keccak_done(&md, output);
return output;
}
static unsigned char *KECCAK_384(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_384_init(&md);
sha3_process(&md, input, ilen);
keccak_done(&md, output);
return output;
}
static unsigned char *KECCAK_512(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
sha3_512_init(&md);
sha3_process(&md, input, ilen);
keccak_done(&md, output);
return output;
}
static unsigned char *WHIRLPOOL(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
whirlpool_init(&md);
whirlpool_process(&md, input, ilen);
whirlpool_done(&md, output);
return output;
}
static unsigned char *TIGER(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
tiger_init(&md);
tiger_process(&md, input, ilen);
tiger_done(&md, output);
return output;
}
static unsigned char *BLAKE2B_160(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2b_160_init(&md);
blake2b_process(&md, input, ilen);
blake2b_done(&md, output);
return output;
}
static unsigned char *BLAKE2B_256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2b_256_init(&md);
blake2b_process(&md, input, ilen);
blake2b_done(&md, output);
return output;
}
static unsigned char *BLAKE2B_384(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2b_384_init(&md);
blake2b_process(&md, input, ilen);
blake2b_done(&md, output);
return output;
}
static unsigned char *BLAKE2B_512(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2b_512_init(&md);
blake2b_process(&md, input, ilen);
blake2b_done(&md, output);
return output;
}
static unsigned char *BLAKE2S_128(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2s_128_init(&md);
blake2s_process(&md, input, ilen);
blake2s_done(&md, output);
return output;
}
static unsigned char *BLAKE2S_160(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2s_160_init(&md);
blake2s_process(&md, input, ilen);
blake2s_done(&md, output);
return output;
}
static unsigned char *BLAKE2S_224(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2s_224_init(&md);
blake2s_process(&md, input, ilen);
blake2s_done(&md, output);
return output;
}
static unsigned char *BLAKE2S_256(const unsigned char *input, size_t ilen,
unsigned char *output) {
hash_state md;
blake2s_256_init(&md);
blake2s_process(&md, input, ilen);
blake2s_done(&md, output);
return output;
}
typedef unsigned char *(*hash_func_t)(const unsigned char *, size_t,
unsigned char *);
static void create(std::string &rpath, std::string &dpath,
hash_func_t hash_func, unsigned digest_len,
rocksdb::CompressionType compression_type) {
if (!std::filesystem::exists(dpath)) {
ERR("Dictionary path doesn't exist: " + dpath);
exit(EXIT_FAILURE);
}
database db{rpath, compression_type};
std::string line{};
unsigned char digest[digest_len];
std::ifstream filestream(dpath);
if (filestream.good() && filestream.is_open()) {
while (getline(filestream, line)) {
hash_func((unsigned char *)line.c_str(), line.size(), digest);
db.put(str2hex(digest, digest_len), line);
}
filestream.close();
} else {
ERR("Unable to open dictionary file: " + dpath);
exit(EXIT_FAILURE);
}
}
static void crack(std::string &hash, std::string &rpath) {
if (!std::filesystem::exists(rpath)) {
ERR("Database not found:" + rpath);
exit(EXIT_FAILURE);
}
std::string string{};
database db{rpath};
db.get(hash, string);
if (!string.empty())
std::cout << hash << ':' << string << '\n';
}
int main(int argc, char *argv[]) {
banner();
if (argc < 2) {
ERR("use -H for help");
return EXIT_FAILURE;
}
int c{0};
hash_func_t hash_func{MD5};
unsigned long digest_len{md5_desc.hashsize};
rocksdb::CompressionType compression_type{rocksdb::kZSTD};
std::string hash{}, rpath{"hashcobra.db"}, dpath{}, opr{};
while ((c = getopt(argc, argv, "VHa:c:h:r:d:o:")) != -1) {
switch (c) {
case 'a':
switch (str2int(optarg)) {
case str2int("md2"):
hash_func = MD2;
digest_len = md2_desc.hashsize;
break;
case str2int("md4"):
hash_func = MD4;
digest_len = md4_desc.hashsize;
break;
case str2int("md5"):
hash_func = MD5;
digest_len = md5_desc.hashsize;
break;
case str2int("sha1"):
hash_func = SHA1;
digest_len = sha1_desc.hashsize;
break;
case str2int("sha224"):
hash_func = SHA224;
digest_len = sha224_desc.hashsize;
break;
case str2int("sha256"):
hash_func = SHA256;
digest_len = sha256_desc.hashsize;
break;
case str2int("sha384"):
hash_func = SHA384;
digest_len = sha384_desc.hashsize;
break;
case str2int("sha512"):
hash_func = SHA512;
digest_len = sha512_desc.hashsize;
break;
case str2int("sha3-224"):
hash_func = SHA3_224;
digest_len = sha3_224_desc.hashsize;
break;
case str2int("sha3-256"):
hash_func = SHA3_256;
digest_len = sha3_256_desc.hashsize;
break;
case str2int("sha3-384"):
hash_func = SHA3_384;
digest_len = sha3_384_desc.hashsize;
break;
case str2int("sha3-512"):
hash_func = SHA3_512;
digest_len = sha3_512_desc.hashsize;
break;
case str2int("keccak-224"):
hash_func = KECCAK_224;
digest_len = keccak_224_desc.hashsize;
break;
case str2int("keccak-256"):
hash_func = KECCAK_256;
digest_len = keccak_256_desc.hashsize;
break;
case str2int("keccak-384"):
hash_func = KECCAK_384;
digest_len = keccak_384_desc.hashsize;
break;
case str2int("keccak-512"):
hash_func = KECCAK_512;
digest_len = keccak_512_desc.hashsize;
break;
case str2int("ripemd-128"):
hash_func = RIPEMD128;
digest_len = rmd128_desc.hashsize;
break;
case str2int("ripemd-160"):
hash_func = RIPEMD160;
digest_len = rmd160_desc.hashsize;
break;
case str2int("ripemd-256"):
hash_func = RIPEMD256;
digest_len = rmd256_desc.hashsize;
break;
case str2int("ripemd-320"):
hash_func = RIPEMD320;
digest_len = rmd320_desc.hashsize;
break;
case str2int("whirlpool"):
hash_func = WHIRLPOOL;
digest_len = whirlpool_desc.hashsize;
break;
case str2int("tiger"):
hash_func = TIGER;
digest_len = tiger_desc.hashsize;
break;
case str2int("blake2b-160"):
hash_func = BLAKE2B_160;
digest_len = blake2b_160_desc.hashsize;
break;
case str2int("blake2b-256"):
hash_func = BLAKE2B_256;
digest_len = blake2b_256_desc.hashsize;
break;
case str2int("blake2b-384"):
hash_func = BLAKE2B_384;
digest_len = blake2b_384_desc.hashsize;
break;
case str2int("blake2b-512"):
hash_func = BLAKE2B_512;
digest_len = blake2b_512_desc.hashsize;
break;
case str2int("blake2s-128"):
hash_func = BLAKE2S_128;
digest_len = blake2s_128_desc.hashsize;
break;
case str2int("blake2s-160"):
hash_func = BLAKE2S_160;
digest_len = blake2s_160_desc.hashsize;
break;
case str2int("blake2s-224"):
hash_func = BLAKE2S_224;
digest_len = blake2s_224_desc.hashsize;
break;
case str2int("blake2s-256"):
hash_func = BLAKE2S_256;
digest_len = blake2s_256_desc.hashsize;
break;
case str2int("?"):
hashing_algorithms();
return EXIT_SUCCESS;
default:
ERR("Hashing algorithm not supported: " + std::string(optarg));
return EXIT_FAILURE;
}
break;
case 'c':
switch (str2int(optarg)) {
case str2int("zstd"):
compression_type = rocksdb::kZSTD;
break;
case str2int("snappy"):
compression_type = rocksdb::kSnappyCompression;
break;
case str2int("zlib"):
compression_type = rocksdb::kZlibCompression;
break;
case str2int("bzip2"):
compression_type = rocksdb::kBZip2Compression;
break;
case str2int("lz4"):
compression_type = rocksdb::kLZ4Compression;
break;
case str2int("lz4hc"):
compression_type = rocksdb::kLZ4HCCompression;
break;
case str2int("none"):
compression_type = rocksdb::kNoCompression;
break;
case str2int("?"):
compression_algorithms();
return EXIT_SUCCESS;
default:
ERR("Compression algorithm not supported: " + std::string(optarg));
return EXIT_FAILURE;
}
break;
case 'h':
hash = optarg;
break;
case 'r':
rpath = optarg;
break;
case 'd':
dpath = optarg;
break;
case 'o':
opr = optarg;
break;
case 'V':
version();
return EXIT_SUCCESS;
case 'H':
help();
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
switch (str2int(opr.c_str())) {
case str2int("create"):
if (dpath.empty()) {
ERR("No dictionary selected");
return EXIT_FAILURE;
}
create(rpath, dpath, hash_func, digest_len, compression_type);
break;
case str2int("crack"):
if (hash.empty()) {
ERR("No hash selected");
return EXIT_FAILURE;
}
crack(hash, rpath);
break;
case str2int("?"):
operations();
return EXIT_SUCCESS;
case str2int(""):
ERR("No operation selected");
return EXIT_FAILURE;
default:
ERR("Operation not supported: " + opr);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}