-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAFS_fmt.c
477 lines (394 loc) · 10.1 KB
/
AFS_fmt.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2001,2012,2015,2017 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#include <stdint.h>
#include <string.h>
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "DES_std.h"
#include "common.h"
#include "formats.h"
#define FORMAT_LABEL "AFS"
#define FORMAT_NAME "Kerberos AFS"
#define BENCHMARK_COMMENT ""
#define BENCHMARK_LENGTH 0x208
#define PLAINTEXT_LENGTH 63
#define CIPHERTEXT_LENGTH 20
static struct fmt_tests tests[] = {
{"$K4$e35e9294ecef926d,0123", "U*U*U*U*"},
{"$K4$64c7c2aedccd70d6,0123456789", "U*U***U*"},
{"$K4$d9e985b36268f168,01234567", "U*U***U"},
{"$K4$b9615786dfb53297,longcellname", "longpassword"},
{"$K4$a8dc8aeaa2c48a97,", ""},
{"$K4$dfda85c7619183a2,XXXXXXXX", "XXXXXXXX"},
{"$K4$e3e59de6f1d5eaf4,cell", "password355"},
{"$K4$b02cc24aefbc865b,", "thisisaverylongpassword"},
{NULL}
};
#define ALGORITHM_NAME DES_STD_ALGORITHM_NAME
#define BINARY_SIZE (3 * ARCH_SIZE)
#define BINARY_ALIGN ARCH_SIZE
#define SALT_SIZE 40
#define SALT_ALIGN 1
#define MIN_KEYS_PER_CRYPT 0x80
#define MAX_KEYS_PER_CRYPT 0x100
#define AFS_SALT "#~..........." /* An invalid salt */
#define AFS_long_key "52912979" /* "kerberos" >> 1 */
#define AFS_long_IV "kerberos" /* :-) */
/*
* They're only using 8 characters of crypt(3) output, effectively reducing
* the hash size to 48 bits... We are emulating this behavior with bitmasks.
*/
#define AFS_MASK_16 DES_DO_SIZE_FIX(0xFFF9FFF9)
#if ARCH_BITS >= 64
#define AFS_BINARY_MASK \
(AFS_MASK_16 | ((unsigned ARCH_WORD)AFS_MASK_16 << 32))
#else
#define AFS_BINARY_MASK AFS_MASK_16
#endif
#define TOTAL_BINARY_MASK (DES_BINARY_MASK & AFS_BINARY_MASK)
#if ARCH_LITTLE_ENDIAN
#define AFS_swap(x, y) \
(y) = (x);
#else
#define AFS_swap(x, y) \
{ \
tmp = (x); \
tmp = (tmp << 16) | (tmp >> 16); \
(y) = ((tmp & 0x00FF00FF) << 8) | ((tmp >> 8) & 0x00FF00FF); \
}
#endif
static struct {
union {
double dummy;
DES_binary binary;
} aligned;
int is_long;
char key[PLAINTEXT_LENGTH + 1];
} buffer[MAX_KEYS_PER_CRYPT];
static char cell[SALT_SIZE + 8];
static int cell_length;
static ARCH_WORD AFS_salt_binary;
static union {
double dummy;
DES_KS data;
} AFS_long_KS;
static DES_binary AFS_long_IV_binary;
static void init(struct fmt_main *self)
{
uint32_t block[2];
#if !ARCH_LITTLE_ENDIAN
uint32_t tmp;
#endif
DES_std_init();
AFS_salt_binary = DES_std_get_salt(AFS_SALT);
DES_raw_set_key(AFS_long_key);
memcpy(AFS_long_KS.data, DES_KS_current, sizeof(DES_KS));
memcpy(block, AFS_long_IV, 8);
#if !ARCH_LITTLE_ENDIAN
AFS_swap(block[0], block[0]);
AFS_swap(block[1], block[1]);
#endif
DES_std_set_block(block[0], block[1]);
memcpy(AFS_long_IV_binary, DES_IV, sizeof(DES_binary));
}
static int valid(char *ciphertext, struct fmt_main *self)
{
char *pos;
int index, count;
unsigned int value;
if (strncmp(ciphertext, "$K4$", 4)) return 0;
for (pos = &ciphertext[4]; atoi16l[ARCH_INDEX(*pos)] != 0x7F; pos++);
if (*pos != ',' || pos - ciphertext != CIPHERTEXT_LENGTH) return 0;
for (index = 0; index < 16; index += 2) {
value = atoi16[ARCH_INDEX(ciphertext[index + 4])] << 4;
value |= atoi16[ARCH_INDEX(ciphertext[index + 5])];
count = 0;
if (value)
do {
count++;
} while ((value &= value - 1));
if (!(count & 1)) return 0;
}
return 1;
}
static void *get_binary(char *ciphertext)
{
static ARCH_WORD out[6];
char base64[14];
int known_long;
int index;
unsigned int value;
out[0] = out[1] = 0;
strcpy(base64, AFS_SALT);
known_long = 0;
for (index = 0; index < 16; index += 2) {
value = atoi16[ARCH_INDEX(ciphertext[index + 4])] << 4;
value |= atoi16[ARCH_INDEX(ciphertext[index + 5])];
out[index >> 3] |= (value | 1) << ((index << 2) & 0x18);
if (atoi64[value >>= 1] == 0x7F)
known_long = 1;
else
base64[(index >> 1) + 2] = value;
}
if (known_long)
out[2] = ~(ARCH_WORD)0;
else
memcpy(&out[2], DES_std_get_binary(base64), 16);
return out;
}
static void *salt(char *ciphertext)
{
static char out[SALT_SIZE + 1];
strncpy(out, &ciphertext[21], SALT_SIZE);
out[SALT_SIZE] = 0;
return strlwr(out);
}
static int binary_hash_0(void *binary)
{
if (((ARCH_WORD *)binary)[2] == ~(ARCH_WORD)0)
return *(ARCH_WORD *)binary & 0xFF;
return DES_STD_HASH_0(((ARCH_WORD *)binary)[2]);
}
static int binary_hash_1(void *binary)
{
if (((ARCH_WORD *)binary)[2] == ~(ARCH_WORD)0)
return *(ARCH_WORD *)binary & 0xFFF;
return DES_STD_HASH_1(((ARCH_WORD *)binary)[2]);
}
static ARCH_WORD to_short_hash(int index)
{
char base64[14];
char *ptr;
int pos, value;
strcpy(base64, AFS_SALT);
ptr = &base64[2];
for (pos = 0; pos < 8; pos++) {
value = buffer[index].aligned.binary[pos >> 2];
value >>= ((pos & 3) << 3) + 1;
value &= 0x7F;
if (atoi64[value] == 0x7F) return ~(ARCH_WORD)0;
*ptr++ = value;
}
return *DES_std_get_binary(base64);
}
static int get_hash_0(int index)
{
ARCH_WORD binary;
if (buffer[index].is_long) {
if ((binary = to_short_hash(index)) == ~(ARCH_WORD)0)
return buffer[index].aligned.binary[0] & 0xFF;
} else
binary = buffer[index].aligned.binary[0] & AFS_BINARY_MASK;
return DES_STD_HASH_0(binary);
}
static int get_hash_1(int index)
{
ARCH_WORD binary;
if (buffer[index].is_long) {
if ((binary = to_short_hash(index)) == ~(ARCH_WORD)0)
return buffer[index].aligned.binary[0] & 0xFFF;
} else
binary = buffer[index].aligned.binary[0] & AFS_BINARY_MASK;
return DES_STD_HASH_1(binary);
}
static void set_salt(void *salt)
{
strnzcpy(cell, salt, SALT_SIZE);
memset(&cell[cell_length = strlen(cell)], 0, 8);
}
static void set_key(char *key, int index)
{
strnzcpy(buffer[index].key, key, PLAINTEXT_LENGTH + 1);
}
static char *get_key(int index)
{
return buffer[index].key;
}
static int crypt_all(int *pcount, struct db_salt *salt)
{
int count = *pcount;
int index, pos, length;
char xor[8];
uint32_t space[(PLAINTEXT_LENGTH + SALT_SIZE + 8) / 4 + 1];
uint32_t *ptr;
ARCH_WORD space_binary[(PLAINTEXT_LENGTH + SALT_SIZE + 8) / 2 + 1];
ARCH_WORD *ptr_binary;
unsigned ARCH_WORD block[2];
union {
double dummy;
DES_binary data;
} binary;
uint32_t key[2];
#if !ARCH_LITTLE_ENDIAN
uint32_t tmp;
#endif
DES_std_set_salt(AFS_salt_binary);
memset(DES_IV, 0, sizeof(DES_IV));
DES_count = 25;
for (index = 0; index < count; index++)
if ((length = strlen(buffer[index].key)) > 8)
buffer[index].is_long = length;
else {
buffer[index].is_long = 0;
memcpy(xor, cell, 8);
for (pos = 0; pos < 8 && buffer[index].key[pos]; pos++)
xor[pos] ^= buffer[index].key[pos];
for (pos = 0; pos < 8; pos++)
if (!xor[pos]) xor[pos] = 'X';
DES_std_set_key(xor);
DES_std_crypt(DES_KS_current, buffer[index].aligned.binary);
}
DES_std_set_salt(0);
DES_count = 1;
for (index = 0; index < count; index++)
if ((length = buffer[index].is_long)) {
memcpy(space, buffer[index].key, length);
memcpy((char *)space + length, cell, cell_length + 8);
memcpy(binary.data, AFS_long_IV_binary, sizeof(binary.data));
length += cell_length;
ptr = space;
ptr_binary = space_binary;
do {
AFS_swap(*ptr++, block[0]);
AFS_swap(*ptr++, block[1]);
DES_std_set_block(block[0], block[1]);
*ptr_binary++ = DES_IV[0];
DES_IV[0] ^= binary.data[0];
*ptr_binary++ = DES_IV[1];
DES_IV[1] ^= binary.data[1];
#if ARCH_BITS < 64
*ptr_binary++ = DES_IV[2];
DES_IV[2] ^= binary.data[2];
*ptr_binary++ = DES_IV[3];
DES_IV[3] ^= binary.data[3];
#endif
DES_std_crypt(AFS_long_KS.data, binary.data);
length -= 8;
} while (length > 0);
DES_std_get_block(binary.data, block);
AFS_swap(block[0] >> 1, key[0]);
AFS_swap(block[1] >> 1, key[1]);
DES_raw_set_key((char *)key);
length = buffer[index].is_long + cell_length;
ptr_binary = space_binary;
do {
DES_IV[0] = binary.data[0] ^ *ptr_binary++;
DES_IV[1] = binary.data[1] ^ *ptr_binary++;
#if ARCH_BITS < 64
DES_IV[2] = binary.data[2] ^ *ptr_binary++;
DES_IV[3] = binary.data[3] ^ *ptr_binary++;
#endif
DES_std_crypt(DES_KS_current, binary.data);
length -= 8;
} while (length > 0);
DES_std_get_block(binary.data, block);
buffer[index].aligned.binary[0] = block[0] | 0x01010101;
buffer[index].aligned.binary[1] = block[1] | 0x01010101;
}
return count;
}
static int cmp_all(void *binary, int count)
{
int index;
for (index = 0; index < count; index++)
if (buffer[index].is_long) {
if (*(unsigned ARCH_WORD *)binary ==
buffer[index].aligned.binary[0])
return 1;
} else {
if (((unsigned ARCH_WORD *)binary)[2] ==
(buffer[index].aligned.binary[0] & TOTAL_BINARY_MASK))
return 1;
}
return 0;
}
static int cmp_one(void *binary, int index)
{
if (buffer[index].is_long)
return *(unsigned ARCH_WORD *)binary ==
buffer[index].aligned.binary[0];
return ((unsigned ARCH_WORD *)binary)[2] ==
(buffer[index].aligned.binary[0] & TOTAL_BINARY_MASK);
}
static int cmp_exact(char *source, int index)
{
ARCH_WORD *binary;
int word;
binary = get_binary(source);
if (buffer[index].is_long) {
if ((unsigned ARCH_WORD)binary[0] !=
buffer[index].aligned.binary[0] ||
(unsigned ARCH_WORD)binary[1] !=
buffer[index].aligned.binary[1])
return 0;
} else {
for (word = 0; word < 16 / DES_SIZE; word++)
if ((unsigned ARCH_WORD)binary[word + 2] !=
(buffer[index].aligned.binary[word] & TOTAL_BINARY_MASK))
return 0;
}
return 1;
}
struct fmt_main fmt_AFS = {
{
FORMAT_LABEL,
FORMAT_NAME,
ALGORITHM_NAME,
BENCHMARK_COMMENT,
BENCHMARK_LENGTH,
PLAINTEXT_LENGTH,
BINARY_SIZE,
BINARY_ALIGN,
SALT_SIZE,
SALT_ALIGN,
MIN_KEYS_PER_CRYPT,
MAX_KEYS_PER_CRYPT,
FMT_CASE | FMT_8_BIT,
tests
}, {
init,
fmt_default_done,
fmt_default_reset,
fmt_default_prepare,
valid,
fmt_default_split,
get_binary,
salt,
fmt_default_source,
{
binary_hash_0,
binary_hash_1,
NULL,
NULL,
NULL,
NULL,
NULL
},
fmt_default_salt_hash,
set_salt,
set_key,
get_key,
fmt_default_clear_keys,
crypt_all,
{
get_hash_0,
get_hash_1,
NULL,
NULL,
NULL,
NULL,
NULL
},
cmp_all,
cmp_one,
cmp_exact
}
};