-
Notifications
You must be signed in to change notification settings - Fork 0
/
yhashcrack.c
453 lines (345 loc) · 9.21 KB
/
yhashcrack.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
//
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
//
#include "types.h"
//
#include "yhash/yhash.h"
//
typedef struct dictionary_s {
//List of passwords
ascii **list;
//Number of entries
u64 n;
//
u64 max_n;
//
u64 size;
//
u64 rounds;
} dictionary_t;
//
typedef struct thread_task_s {
//Pointer to the thread block of the dictionary
ascii **list;
//Number of passwords in the block
u64 n;
//Thread ID
pthread_t tid;
//The hash to crack
u8 *target_hash;
//The hash length
u64 hash_len;
//The hash function
void (*hash_function)(const u8 *, const u64, u8 *);
//
u8 found;
//The password (if found)
ascii *password;
} thread_task_t;
//
ascii yhashcrack_logo[] =
" _____ _ _____ _ \n"
" _ _| | |___ ___| |_| |___ ___ ___| |_ \n"
"| | | | .'|_ -| | --| _| .'| _| '_|\n"
"|_ |__|__|__,|___|_|_|_____|_| |__,|___|_,_|\n"
"|___| \n"
;
//
dictionary_t *create_dictionary(u64 max_n, u64 max_len)
{
dictionary_t *d = malloc(sizeof(dictionary_t));
if (!d)
return printf("Error: cannot allocate dictionary\n"), NULL;
d->list = malloc(sizeof(ascii *) * max_n);
if (!d->list)
return printf("Error: cannot allocate dictionary list\n"), NULL;
d->n = 0;
d->max_n = max_n;
d->size = 0;
d->rounds = 0;
for (u64 i = 0; i < max_n; i++)
{
d->list[i] = malloc(sizeof(ascii) * max_len);
if (!d->list[i])
{
printf("Error: cannot allocate password entry '%llu' in dictionary\n", i);
exit(7);
}
}
return d;
}
//
void destroy_dictionary(dictionary_t *d)
{
if (d)
{
for (u64 i = 0; i < d->max_n; i++)
free(d->list[i]);
free(d->list);
d->n = 0;
d->max_n = 0;
d->size = 0;
d->rounds = 0;
}
else
printf("Error: dictionary pointer is NULL\n"), exit(5);
}
//
ascii load_dictionary(FILE *fp, dictionary_t *d)
{
u64 i = 0;
ascii done = 1;
f64 elapsed = 0.0;
struct timespec after, before;
if (fp)
{
printf("Loading dictionary block");
fflush(stdout);
clock_gettime(CLOCK_MONOTONIC_RAW, &before);
while (i < d->max_n && done != EOF)
{
done = fscanf(fp, "%s\n", d->list[i]);
d->size += strlen(d->list[i]);
i++;
}
d->n = i;
clock_gettime(CLOCK_MONOTONIC_RAW, &after);
elapsed = (f64)(after.tv_sec - before.tv_sec);
printf(" (%llu MiB) in %.3lf s\n", d->size >> 20, elapsed);
}
return (done == EOF) ? 0 : 1;
}
//Convert a string to a hash
void str_to_hash(ascii *str, u8 *hash, u64 str_len)
{
u8 b;
static u8 cvt_tab[6] = { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
for (u64 i = 0, j = 0; i < str_len; i += 2, j++)
{
b = 0x00;
//High 4 bits
if (str[i] >= '0' && str[i] <= '9')
b = (str[i] - '0') << 4;
else
if (str[i] >= 'A' && str[i] <= 'F')
b = cvt_tab[str[i] - 'A'] << 4;
else
if (str[i] >= 'a' && str[i] <= 'f')
b = cvt_tab[str[i] - 'a'] << 4;
//Low 4 bits
if (str[i + 1] >= '0' && str[i + 1] <= '9')
b |= (str[i + 1] - '0') & 0x0F;
else
if (str[i + 1] >= 'A' && str[i + 1] <= 'F')
b |= cvt_tab[str[i + 1] - 'A'] & 0x0F;
else
if (str[i + 1] >= 'a' && str[i + 1] <= 'f')
b |= cvt_tab[str[i + 1] - 'a'] & 0x0F;
//Store byte
hash[j] = b;
}
}
//
void print_hash(const u8 *hash, u64 hash_len)
{
for (u64 i = 0; i < hash_len; i++)
printf("%02x", hash[i]);
printf("\n");
}
//
u8 compare(const u8 *hash1, const u8 *hash2, u64 hash_len)
{
u64 d = 0;
for (u64 i = 0; i < hash_len; i++)
d += (hash1[i] ^ hash2[i]);
return (d) ? 0 : 1;
}
//Currently unused!
u8 lookup_hash_sequential(dictionary_t *d, u8 *target_hash, void (*hash_function)(const u8 *, const u64, u8 *), const u64 hash_len, ascii **password)
{
u8 found = 0;
u8 hash[hash_len];
for (u64 i = 0; i < d->n; i++)
{
hash_function((u8 *)d->list[i], strlen(d->list[i]), hash);
found = compare(target_hash, hash, hash_len);
if (found)
{
(*password) = d->list[i];
break;
}
}
return found;
}
//
void *thread_task(void *arg)
{
thread_task_t *tt = (thread_task_t *)arg;
u8 found = 0;
ascii **list = tt->list;
u8 *target_hash = tt->target_hash;
u64 hash_len = tt->hash_len;
u8 hash[hash_len];
void (*hash_function)(const u8 *, const u64, u8 *) = tt->hash_function;
for (u64 i = 0; i < tt->n; i++)
{
hash_function((u8 *)list[i], strlen(list[i]), hash);
found = compare(target_hash, hash, hash_len);
if (found)
{
tt->found = 1;
tt->password = list[i];
break;
}
}
if (!found)
tt->password = NULL;
return NULL;
}
//
u8 lookup_hash_parallel(u64 nt, dictionary_t *d, u8 *target_hash, void (*hash_function)(const u8 *, const u64, u8 *), const u64 hash_len, ascii **password)
{
u8 found = 0;
u64 thread_n = d->n / nt;
u64 thread_m = d->n % nt;
thread_task_t *tt = malloc(sizeof(thread_task_t) * nt);
if (!tt)
{
printf("Error: cannot allocate memory for threads\n");
exit(6);
}
for (u64 i = 0; i < nt; i++)
{
tt[i].n = (thread_n + ((i == nt - 1) ? thread_m : 0));
tt[i].list = &d->list[i * thread_n];
tt[i].hash_function = hash_function;
tt[i].target_hash = target_hash;
tt[i].hash_len = hash_len;
tt[i].found = 0;
tt[i].password = NULL;
pthread_create(&tt[i].tid, NULL, thread_task, &tt[i]);
}
for (u64 i = 0; i < nt; i++)
{
pthread_join(tt[i].tid, NULL);
if (tt[i].found)
{
(*password) = tt[i].password;
found = 1;
}
}
free(tt);
return found;
}
//
int main(int argc, char **argv)
{
//Print logo
printf("%s\n", yhashcrack_logo);
//Handle command line parameters
if (argc < 4)
return printf("Usage: %s [hashing algorithm] [number of threads] [dictionary path] [hash]\n", argv[0]), 1;
//Set hash length and hash function pointer
u64 hash_len = 0;
void (*hash_function)(const u8 *, const u64, u8 *) = NULL;;
if (!strcmp(argv[1], "md5"))
{
hash_len = MD5_HASH_SIZE;
hash_function = md5hash;
}
else
if (!strcmp(argv[1], "sha1"))
{
hash_len = SHA1_HASH_SIZE;
hash_function = sha1hash;
}
else
if (!strcmp(argv[1], "sha224"))
{
hash_len = SHA224_HASH_SIZE;
hash_function = sha224hash;
}
else
if (!strcmp(argv[1], "sha256"))
{
hash_len = SHA256_HASH_SIZE;
hash_function = sha256hash;
}
else
if (!strcmp(argv[1], "sha512"))
{
hash_len = SHA512_HASH_SIZE;
hash_function = sha512hash;
}
else
{
printf("Error: unknown hashing algorithm '%s'\n", argv[1]);
exit(8);
}
//Get number of threads
u64 nt = atoll(argv[2]);
if (nt < 1)
return printf("Error: invalid number of threads '%llu'\n", nt), 4;
//Printing info
printf("Number of threads : %llu\n"
"Hashing algorithm : %s\n"
"Dictionary file : %s\n"
"Target hash : %s\n", nt, argv[1], argv[3], argv[4]);
//Get file size
u64 size = 0;
struct stat sb;
u64 dictionary_size = 0;
if (stat(argv[3], &sb) < 0)
return printf("Error: cannot open file '%s'\n", argv[3]), 3;
dictionary_size = sb.st_size;
//Open dictionary file
FILE *fp = fopen(argv[3], "rb");
if (!fp)
return printf("Error: cannot open file '%s'\n", argv[3]), 3;
u8 found = 0;
ascii *password = NULL;
u8 target_hash[hash_len];
struct timespec lu_after, lu_before;
struct timespec all_after, all_before;
//Convert parameter hash (string) into number form
str_to_hash(argv[4], target_hash, strlen(argv[4]));
dictionary_t *d = create_dictionary(100000000, 32);
if (!d)
return printf("Error: cannot create dictionary\n"), 4;
printf("Dictionary file size: %llu MiB; %llu GiB\n\n", dictionary_size >> 20, dictionary_size >> 30);
clock_gettime(CLOCK_MONOTONIC_RAW, &all_before);
while (load_dictionary(fp, d) != EOF)
{
clock_gettime(CLOCK_MONOTONIC_RAW, &lu_before);
found = lookup_hash_parallel(nt, d, target_hash, hash_function, hash_len, &password);
clock_gettime(CLOCK_MONOTONIC_RAW, &lu_after);
f64 lu_elapsed = (f64)(lu_after.tv_sec - lu_before.tv_sec);
//Total searched memory size
size += d->size;
printf("Hashed and compared %llu passwords (%llu MiB), in %.2lf seconds\n\n", d->n, d->size >> 20, lu_elapsed);
if (found)
break;
d->size = 0;
d->rounds++;
}
clock_gettime(CLOCK_MONOTONIC_RAW, &all_after);
if (found)
printf("### Cracked :] password: '%s'\n\n", password);
else
printf("### Sorry! No password matched the given %s hash\n\n", argv[1]);
f64 all_elapsed = (f64)(all_after.tv_sec - all_before.tv_sec);
printf("Cracking run time: %.2lf seconds, %.2lf minutes; Number of rounds: %llu; Searched memory: ", all_elapsed, all_elapsed / 60.0, d->rounds);
if ((size >> 20) > 1024)
printf("%.2lf GiB\n", (f64)size / (1024 * 1024 *1024));
else
printf("%.2lf MiB\n", (f64)size / (1024 * 1024));
destroy_dictionary(d);
free(d);
fclose(fp);
return 0;
}