-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve-part3.c
380 lines (305 loc) · 8.56 KB
/
solve-part3.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
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <omp.h>
#include "md5.h"
#ifndef MD5_DIGEST_LENGTH
#define MD5_DIGEST_LENGTH 16
#endif
#define OMP_THREAD_LIMIT 32
#define DATA_COUNT 77
#define DATA_LEN 32
#define DATA_BUFFER_SIZE (64 * 1024)
#define EOS_MARK "cafebabe"
char atad[DATA_BUFFER_SIZE+1];
/* store pointers to I1, I2, I3, I4 */
uint8_t *I[4];
size_t I_size[4];
uint32_t I1[DATA_COUNT][DATA_LEN];
uint8_t *target_md5[6];
struct bf_ctx {
uint32_t **data;
uint8_t a;
uint8_t b;
};
static uint8_t ascii2byte(uint8_t *val) {
uint8_t temp = *val;
if(temp > 0x60) temp -= 39; /* convert chars a-f */
temp -= 48; /* convert chars 0-9 */
temp *= 16;
temp += *(val+1);
if(*(val+1) > 0x60) temp -= 39; /* convert chars a-f */
temp -= 48; /* convert chars 0-9 */
return temp;
}
struct bf_ctx *new_bf_ctx(void) {
int i;
struct bf_ctx *ctx;
ctx = malloc(sizeof(struct bf_ctx));
ctx->data = (uint32_t **) malloc(sizeof(uint32_t *) * DATA_COUNT);
for (i = 0; i < DATA_COUNT; i++) {
ctx->data[i] = malloc(sizeof(uint32_t) * DATA_LEN);
}
ctx->a = 0; ctx->b = 0;
return ctx;
}
void free_bf_ctx(struct bf_ctx *ctx) {
int i;
if (!ctx)
return;
if (ctx->data) {
for (i = 0; i < DATA_COUNT; i++)
if (ctx->data[i])
free(ctx->data[i]);
}
free(ctx);
}
void copy_bf_ctx(struct bf_ctx *dest, struct bf_ctx *src) {
int i;
for (i = 0; i < DATA_COUNT; i++) {
memcpy(dest->data[i], src->data[i], sizeof(uint32_t) * DATA_LEN);
}
dest->a = src->a;
dest->b = src->b;
}
void load_data(FILE *stream) {
int i = 0, j;
char *prev_token, *token;
size_t count;
uint8_t *p;
uint32_t t;
if (fgets(atad, DATA_BUFFER_SIZE, stream) == NULL) {
fprintf(stderr, "error during fgets\n");
exit(EXIT_FAILURE);
}
prev_token = strtok(atad, " ");
while ((token = strtok(NULL, " "))) {
if (!strcmp(token, EOS_MARK)) {
count = strlen(prev_token) / 2;
I[i] = malloc(sizeof(uint8_t) * count);
I_size[i] = count;
for (j = 0; j < count; j++) {
I[i][j] = ascii2byte( (uint8_t *) prev_token + 2 * j );
}
i++;
}
prev_token = token;
}
/* Convert I1 data to uint32_t */
for (i = 0; i < DATA_COUNT; i++) {
for (j = 0; j < DATA_LEN; j++) {
p = I[0] + (DATA_LEN * sizeof(uint32_t)) * i + j * sizeof(uint32_t);
t = (*p) | (*(p+1) << 8) | (*(p+2) << 16) | (*(p+3) << 24);
I1[i][j] = t;
}
}
/* Set the pointer to each MD5 target value */
for (i = 0; i < 6; i++) {
target_md5[i] = I[2] + i * MD5_DIGEST_LENGTH;
}
}
int decrypt_main(uint8_t b0, uint8_t b1, const uint32_t *data, size_t data_len, uint32_t *tmp) {
uint32_t k;
int i;
void *p;
k = b0 | b1 << 8 | b0 << 16 | b1 << 24;
for (i = 0; i < data_len; i++) {
k = k ^ data[i];
tmp[i] = k;
}
p = memmem(tmp, data_len * sizeof(uint32_t), "roll", 4);
return (p != NULL);
}
int bf_main_I(int idx, uint8_t *key) {
int b0, b1;
uint8_t *p;
uint32_t *tmp, *data;
size_t count;
int i, ret;
count = I_size[idx] / 4;
data = malloc(sizeof(uint32_t) * count);
tmp = malloc(sizeof(uint32_t) * count);
for (i = 0; i < count; i++) {
p = I[idx] + i * sizeof(uint32_t);
data[i] = (*p) | (*(p+1) << 8) | (*(p+2) << 16) | (*(p+3) << 24);
}
for (b0 = 0; b0 < 256; b0++) {
for (b1 = 0; b1 < 256; b1++) {
ret = decrypt_main(b0, b1, data, count, tmp);
if (ret != 0) {
key[0] = b0;
key[1] = b1;
goto cleanup;
}
}
}
cleanup:
free(data); free(tmp);
return ret;
}
void bf_main(uint8_t *key) {
int ret;
ret = bf_main_I(1, key + 2);
if (ret == 0) {
fprintf(stderr, "cannot find key for I2\n");
exit(EXIT_FAILURE);
}
ret = bf_main_I(3, key);
if (ret == 0) {
fprintf(stderr, "cannot find key for I4\n");
exit(EXIT_FAILURE);
}
}
static inline uint32_t lfsr(uint32_t v) {
uint8_t bit;
bit = ( (v >> 0) ^ (v >> 2) ^ (v >> 3) ^ (v >> 7) ) & 1;
return (v >> 1) | (bit << 31);
}
static inline int cmp(uint32_t t) {
if (t < 0x55555555)
return 1;
else {
if (t < 0xaaaaaaaa)
return -1;
else
return 0;
}
}
void decrypt_I4(struct bf_ctx *ctx, uint8_t *key, uint8_t *md5sum) {
int i;
uint32_t t, tmp;
MD5_CTX md5_ctx;
uint32_t **data;
uint8_t a, b, old_a, old_b;
t = key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3];
data = ctx->data;
a = ctx->a;
b = ctx->b;
for (i = 0; i < 10240; i++) {
old_a = a;
old_b = b;
t = lfsr(t);
a = (a + cmp(t) + DATA_COUNT) % DATA_COUNT;
t = lfsr(t);
b = (b + cmp(t) + DATA_LEN) % DATA_LEN;
t = lfsr(t);
/* Swapping data[c][d] and data[a][b] */
tmp = data[a][b];
data[a][b] = data[old_a][old_b];
data[old_a][old_b] = tmp ^ htonl(t);
}
ctx->a = a; ctx->b = b;
MD5_Init(&md5_ctx);
for (i = 0; i < DATA_COUNT; i++) {
MD5_Update(&md5_ctx, ctx->data[i], sizeof(uint32_t) * DATA_LEN);
}
MD5_Final(md5sum, &md5_ctx);
}
void bf_I4(uint8_t *key) {
#ifdef HOLLYWOOD
int l, hcount = 0;
uint8_t hchars[4] = { '-', '\\', '|', '/' };
#endif
int i, j, keyfound;
struct bf_ctx *ref_ctx;
struct bf_ctx **threads_ctx;
uint8_t *current_key;
ref_ctx = new_bf_ctx();
for (i = 0; i < DATA_COUNT; i++) {
memcpy(ref_ctx->data[i], I1[i], sizeof(uint32_t) * DATA_LEN);
}
threads_ctx = malloc(OMP_THREAD_LIMIT * sizeof(struct bf_ctx *));
for (i = 0; i < OMP_THREAD_LIMIT; i++) {
threads_ctx[i] = new_bf_ctx();
}
#ifdef HOLLYWOOD
fprintf(stderr, "[+] hollywood mode engaged\n");
#endif
#pragma omp parallel
{
#pragma omp barrier
if (omp_get_thread_num() == 0) {
fprintf(stderr, "[+] starting %d threads\n", omp_get_num_threads());
}
}
for (i = 0; i < 6; i++) {
keyfound = 0;
current_key = key + 2 + 2 * i;
#pragma omp parallel for
for (j = 0; j < 256; j++) {
int k, thread_id;
struct bf_ctx *tmp_ctx;
uint8_t tmpkey[4];
uint8_t md5sum[MD5_DIGEST_LENGTH];
if (keyfound == 1) {
j = 256;
continue;
}
memcpy(tmpkey, current_key, 2);
tmpkey[2] = j;
thread_id = omp_get_thread_num();
tmp_ctx = threads_ctx[thread_id];
for (k = 0; k < 256; k++) {
tmpkey[3] = k;
#ifdef HOLLYWOOD
if (thread_id == 0 && k == 0) {
fprintf(stderr, "\r[%c] key = ", hchars[hcount++ % 4]);
for (l = 0; l < 16; l++)
fprintf(stderr, "%2.2x", key[l]);
fflush(stderr);
}
#endif
copy_bf_ctx(tmp_ctx, ref_ctx);
decrypt_I4(tmp_ctx, tmpkey, md5sum);
if (!memcmp(md5sum, target_md5[i], MD5_DIGEST_LENGTH)) {
#pragma omp critical
{
keyfound = 1;
copy_bf_ctx(ref_ctx, tmp_ctx);
memcpy(current_key, tmpkey, 4);
}
}
}
}
if (keyfound == 0) {
fprintf(stderr, "key not found!\n");
exit(0);
}
}
fprintf(stderr, "\r[!] key = ");
for (i = 0; i < 16; i++) {
fprintf(stderr, "%2.2x", key[i]);
}
fprintf(stderr, "\n");
for (i = 0; i < DATA_COUNT; i++) {
fwrite(ref_ctx->data[i], sizeof(uint32_t) * DATA_LEN, 1, stdout);
}
free_bf_ctx(ref_ctx);
for (i = 0; i < OMP_THREAD_LIMIT; i++)
free_bf_ctx(threads_ctx[i]);
}
int main(int argc, char **argv) {
FILE *f;
uint8_t key[16];
if (argc != 2) {
fprintf(stderr, "usage: %s input (- for stdin)\n", argv[0]);
exit(EXIT_FAILURE);
}
if (!strcmp(argv[1], "-")) {
f = stdin;
} else {
f = fopen(argv[1], "r");
}
load_data(f);
fprintf(stderr, "[*] solving part 3\n");
memset(key, 0, 16);
bf_main(key);
bf_I4(key);
exit(EXIT_SUCCESS);}