Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gteissier
GitHub Repository: gteissier/erl-matter
Path: blob/master/crack-hash.c
271 views
1
#include <stdio.h>
2
#include <stdint.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <pthread.h>
6
#include <inttypes.h>
7
#include <assert.h>
8
9
#include "erldp.h"
10
11
#include <nettle/md5.h>
12
#include <nettle/base64.h>
13
14
volatile int quit = 0;
15
/* raw md5 of cookie, to be uncovered */
16
uint8_t raw_hash[MD5_DIGEST_SIZE];
17
18
char found_cookie[20];
19
uint64_t found_seed;
20
21
struct worker {
22
pthread_t tid;
23
uint64_t start;
24
uint64_t end;
25
uint64_t incr;
26
};
27
28
static void *run(void *arg) {
29
struct worker *w = arg;
30
uint64_t seed;
31
char cookie[20];
32
struct md5_ctx hash_ctx;
33
uint8_t digest[MD5_DIGEST_SIZE];
34
35
for (seed = w->start; seed <= w->end && !quit; seed += w->incr) {
36
create_cookie(seed, cookie, sizeof(cookie));
37
38
md5_init(&hash_ctx);
39
md5_update(&hash_ctx, sizeof(cookie), (uint8_t *) cookie);
40
md5_digest(&hash_ctx, MD5_DIGEST_SIZE, digest);
41
42
if (raw_hash[0] == digest[0] && memcmp(raw_hash, digest, MD5_DIGEST_SIZE) == 0) {
43
memcpy(found_cookie, cookie, sizeof(cookie));
44
found_seed = seed;
45
46
quit = 1;
47
return NULL;
48
}
49
}
50
51
return NULL;
52
}
53
54
55
int main(int argc, char **argv) {
56
int n_workers = 8;
57
struct worker *workers;
58
int i;
59
int ret;
60
struct base64_decode_ctx base64_ctx;
61
size_t dst_size = sizeof(raw_hash);
62
const char *hash;
63
64
if (argc != 2) {
65
fprintf(stderr, "usage: %s <base64-encoded md5 of cookie>\n", argv[0]);
66
exit(1);
67
}
68
69
hash = argv[1];
70
71
base64_decode_init(&base64_ctx);
72
base64_decode_update(&base64_ctx, &dst_size, raw_hash, strlen(hash), (const uint8_t *) hash);
73
ret = base64_decode_final(&base64_ctx);
74
assert(ret == 1);
75
assert(dst_size == MD5_DIGEST_SIZE);
76
77
workers = calloc(sizeof(*workers), n_workers);
78
assert(workers);
79
80
for (i = 0; i < n_workers; i++) {
81
workers[i].start = 0 + i;
82
workers[i].end = (1ULL<<36)-1;
83
workers[i].incr = n_workers;
84
85
ret = pthread_create(&workers[i].tid, NULL, run, &workers[i]);
86
assert(ret == 0);
87
}
88
89
for (i = 0; i < n_workers; i++) {
90
pthread_join(workers[i].tid, NULL);
91
}
92
93
if (quit) {
94
printf("%.*s\n", 20, found_cookie);
95
fprintf(stderr, " seed used to generate it = %" PRIu64 "\n", found_seed);
96
}
97
else {
98
fprintf(stderr, "cookie hash did not reveal a generated cookie\n");
99
}
100
101
return 0;
102
}
103
104
105