-
Notifications
You must be signed in to change notification settings - Fork 29
/
crypto.c
454 lines (366 loc) · 11.5 KB
/
crypto.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
/* crypto.c
* By Ron
* Created August, 2008
*
* (See LICENSE.txt)
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/des.h>
#include <openssl/hmac.h>
#include <openssl/md4.h>
#include <openssl/md5.h>
#include "memory.h"
#include "types.h"
#include "crypto.h"
static void password_to_key(const uint8_t password[7], uint8_t key[8])
{
/* make room for parity bits */
key[0] = (password[0] >> 0);
key[1] = ((password[0]) << 7) | (password[1] >> 1);
key[2] = ((password[1]) << 6) | (password[2] >> 2);
key[3] = ((password[2]) << 5) | (password[3] >> 3);
key[4] = ((password[3]) << 4) | (password[4] >> 4);
key[5] = ((password[4]) << 3) | (password[5] >> 5);
key[6] = ((password[5]) << 2) | (password[6] >> 6);
key[7] = ((password[6]) << 1);
}
static void des(const uint8_t password[7], const uint8_t data[8], uint8_t result[])
{
DES_cblock key;
DES_key_schedule schedule;
password_to_key(password, key);
DES_set_odd_parity(&key);
DES_set_key_unchecked(&key, &schedule);
DES_ecb_encrypt((DES_cblock*)data, (DES_cblock*)result, &schedule, DES_ENCRYPT);
}
void lm_create_hash(const char *password, uint8_t result[16])
{
size_t i;
uint8_t password1[7];
uint8_t password2[7];
uint8_t kgs[] = "KGS!@#$%";
uint8_t hash1[8];
uint8_t hash2[8];
/* Initialize passwords to NULLs. */
memset(password1, 0, 7);
memset(password2, 0, 7);
/* Copy passwords over, convert to uppercase, they're automatically padded with NULLs. */
for(i = 0; i < 7; i++)
{
if(i < strlen(password))
password1[i] = toupper(password[i]);
if(i + 7 < strlen(password))
password2[i] = toupper(password[i + 7]);
}
/* Do the encryption. */
des(password1, kgs, hash1);
des(password2, kgs, hash2);
/* Copy the result to the return parameter. */
memcpy(result + 0, hash1, 8);
memcpy(result + 8, hash2, 8);
}
void lm_create_response(const uint8_t lanman[16], const uint8_t challenge[8], uint8_t result[24])
{
size_t i;
uint8_t password1[7];
uint8_t password2[7];
uint8_t password3[7];
uint8_t hash1[8];
uint8_t hash2[8];
uint8_t hash3[8];
/* Initialize passwords. */
memset(password1, 0, 7);
memset(password2, 0, 7);
memset(password3, 0, 7);
/* Copy data over. */
for(i = 0; i < 7; i++)
{
password1[i] = lanman[i];
password2[i] = lanman[i + 7];
password3[i] = (i + 14 < 16) ? lanman[i + 14] : 0;
}
/* do the encryption. */
des(password1, challenge, hash1);
des(password2, challenge, hash2);
des(password3, challenge, hash3);
/* Copy the result to the return parameter. */
memcpy(result + 0, hash1, 8);
memcpy(result + 8, hash2, 8);
memcpy(result + 16, hash3, 8);
}
void ntlm_create_hash(const char *password, uint8_t result[16])
{
char *unicode = unicode_alloc(password);
MD4_CTX ntlm;
if(!unicode)
DIE_MEM();
MD4_Init(&ntlm);
MD4_Update(&ntlm, unicode, strlen(password) * 2);
MD4_Final(result, &ntlm);
}
void ntlm_create_response(const uint8_t ntlm[16], const uint8_t challenge[8], uint8_t result[24])
{
lm_create_response(ntlm, challenge, result);
}
void ntlmv2_create_hash(const uint8_t ntlm[16], const char *username, const char *domain, uint8_t hash[16])
{
/* Convert username to unicode. */
size_t username_length = strlen(username);
size_t domain_length = strlen(domain);
char *combined;
uint8_t *combined_unicode;
/* Probably shouldn't do this, but this is all prototype so eh? */
if(username_length > 256 || domain_length > 256)
DIE("username or domain too long.");
/* Combine the username and domain into one string. */
combined = safe_malloc(username_length + domain_length + 1);
memset(combined, 0, username_length + domain_length + 1);
memcpy(combined, username, username_length);
memcpy(combined + username_length, domain, domain_length);
/* Convert to Unicode. */
combined_unicode = (uint8_t*)unicode_alloc_upper(combined);
if(!combined_unicode)
DIE_MEM();
/* Perform the Hmac-MD5. */
HMAC(EVP_md5(), ntlm, 16, combined_unicode, (username_length + domain_length) * 2, hash, NULL);
safe_free(combined_unicode);
safe_free(combined);
}
void lmv2_create_response(const uint8_t ntlm[16], const char *username, const char *domain, const uint8_t challenge[8], uint8_t *result, uint8_t *result_size)
{
ntlmv2_create_response(ntlm, username, domain, challenge, result, result_size);
}
void ntlmv2_create_response(const uint8_t ntlm[16], const char *username, const char *domain, const uint8_t challenge[8], uint8_t *result, uint8_t *result_size)
{
size_t i;
uint8_t v2hash[16];
uint8_t *data;
uint8_t blip[8];
uint8_t *blob;
uint8_t blob_length;
/* Create the 'blip'. TODO: Do I care if this is random? */
for(i = 0; i < 8; i++)
blip[i] = i;
if(*result_size < 24)
{
/* Result can't be less than 24 bytes. */
DIE("Result size is too low!");
}
else if(*result_size == 24)
{
/* If they're looking for 24 bytes, then it's just the raw blob. */
blob = safe_malloc(8);
memcpy(blob, blip, 8);
blob_length = 8;
}
else
{
blob = safe_malloc(24);
for(i = 0; i < 24; i++)
blob[i] = i;
blob_length = 24;
}
/* Allocate room enough for the server challenge and the client blob. */
data = safe_malloc(8 + blob_length);
/* Copy the challenge into the memory. */
memcpy(data, challenge, 8);
/* Copy the blob into the memory. */
memcpy(data + 8, blob, blob_length);
/* Get the v2 hash. */
ntlmv2_create_hash(ntlm, username, domain, v2hash);
/* Generate the v2 response. */
HMAC(EVP_md5(), v2hash, 16, data, 8 + blob_length, result, NULL);
/* Copy the blob onto the end of the v2 response. */
memcpy(result + 16, blob, blob_length);
/* Store the result size. */
*result_size = blob_length + 16;
/* Finally, free up some memory. */
safe_free(data);
safe_free(blob);
}
void lm_create_session_key(const uint8_t lanman[16], uint8_t result[16])
{
/* LM key is the first 8 bytes of the LM hash, followed by 8 NULL bytes. Seriously. */
memcpy(result, lanman, 8);
memset(result + 8, 0, 9);
}
void ntlm_create_session_key(const uint8_t ntlm[16], uint8_t result[16])
{
/* NTLM key is an MD4 of the ntlm hash. */
MD4_CTX session_key;
MD4_Init(&session_key);
MD4_Update(&session_key, ntlm, 16);
MD4_Final(result, &session_key);
}
void lmv2_create_session_key(const uint8_t ntlm[16], const char *username, const char *domain, const uint8_t lmv2_response[16], uint8_t result[16])
{
uint8_t v2hash[16];
ntlmv2_create_hash(ntlm, username, domain, v2hash);
HMAC(EVP_md5(), v2hash, 16, lmv2_response, 16, result, NULL);
DIE("This function doesn't work.");
}
void ntlmv2_create_session_key(const uint8_t ntlm[16], const char *username, const char *domain, const uint8_t ntlmv2_response[16], uint8_t result[16])
{
lmv2_create_session_key(ntlm, username, domain, ntlmv2_response, result);
DIE("This function doesn't work.");
}
/* mac_key = concat(session_key, response_hash);
* signature = first 8 bytes of MD5(mac_key, smb_data);
*/
void calculate_signature(const uint8_t *data, size_t length, const uint8_t mac_key[40], uint8_t signature[8])
{
MD5_CTX md5;
uint8_t full_result[16];
MD5_Init(&md5);
MD5_Update(&md5, mac_key, 40);
MD5_Update(&md5, data, length);
MD5_Final(full_result, &md5);
memcpy(signature, full_result, 8);
}
/* Converts a single character ('0' = '9', 'a' - 'f', and 'A' - 'F') to its equivalent.
* Returns -1 for error. */
static int character_to_hex(char ch)
{
if(ch >= '0' && ch <= '9')
return ch - '0';
if(ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if(ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return -1;
}
/* String has to be 32 bytes, hash has to be 16.
*
* Returns TRUE on success. */
static NBBOOL single_string_to_hash(const char *string, uint8_t hash[16])
{
size_t i;
for(i = 0; i < 32; i += 2)
{
int upper = character_to_hex(string[i]);
int lower = character_to_hex(string[i + 1]);
if(upper < 0 || lower < 0)
return FALSE;
hash[i / 2] = (upper << 4) | lower;
}
return TRUE;
}
int string_to_hash(const char *string, uint8_t first[16], uint8_t second[16])
{
memset(first, 0, 16);
memset(second, 0, 16);
/* Single hash. */
if(strlen(string) == 32)
{
if(single_string_to_hash(string, first))
return 1;
return 0;
}
/* Two hashes, with no separators. */
if(strlen(string) == 64)
{
if(single_string_to_hash(string, first) && single_string_to_hash(string + 32, second))
return 2;
return 0;
}
/* Two hashes, with a separator. */
if(strlen(string) == 65)
{
if(single_string_to_hash(string, first) && single_string_to_hash(string + 33, second))
return 2;
return 0;
}
return 0;
}
#if 0
/* Test data:
* Challenge: 7b9cb5458f2631cd
* Password: iagotest1
* LM: d702a1d01b6bc24127bcbf149915a329
* NTLM: bce0ce40de0d1d9cd08a4e6066a14b92
* LM c/r: 5c8b5515cd84996359cef9c7c40832390831aa88933d2198
* NTLMv1 c/r: 07648d36df458762fa4178f548254c6dd337607cc92e8e19
*/
static void print_hex(char *prefix, uint8_t *hex, int length)
{
size_t i;
printf("%s", prefix);
for(i = 0; i < length; i++)
printf("%02x", hex[i]);
printf("\n");
}
int main(int argc, char *argv[])
{
uint8_t lanman[16];
uint8_t ntlm[16];
uint8_t ntlmv2[16];
uint8_t lanman_response[24];
uint8_t ntlm_response[24];
uint8_t ntlmv2_response[24];
uint8_t ntlmv2_length = 24;
uint8_t *challenge = (uint8_t *)"\x7b\x9c\xb5\x45\x8f\x26\x31\xcd";
char *password = "iagotest1";
/* for(i = 1; i < argc; i++)
{
lm_create_hash(argv[i], lanman);
ntlm_create_hash(argv[i], ntlm);
printf("test%d:%d:", i, 1000+i);
for(j = 0; j < 16; j++)
printf("%02x", lanman[j]);
printf(":");
for(j = 0; j < 16; j++)
printf("%02x", ntlm[j]);
printf(":::\n");
}
*/
lm_create_hash(password, lanman);
lm_create_response(lanman, challenge, lanman_response);
ntlm_create_hash("iagotest1", ntlm);
ntlm_create_response(ntlm, challenge, ntlm_response);
ntlmv2_create_hash(ntlm, "ron", "", ntlmv2);
ntlmv2_create_response(ntlm, "ron", "", challenge, ntlmv2_response, &ntlmv2_length);
printf("+LM: d702a1d01b6bc24127bcbf149915a329\n");
print_hex("LM: ", lanman, 16);
printf("\n");
printf("=NTLM: bce0ce40de0d1d9cd08a4e6066a14b92\n");
print_hex("NTLM: ", ntlm, 16);
printf("\n");
print_hex("NTLMv2: ", ntlmv2, 16);
printf("\n");
printf("+LM c/r: 5c8b5515cd84996359cef9c7c40832390831aa88933d2198\n");
print_hex("LM c/r: ", lanman_response, 24);
printf("\n");
printf("+NTLMv1 c/r: 07648d36df458762fa4178f548254c6dd337607cc92e8e19\n");
print_hex("NTLMv1 c/r: ", ntlm_response, 24);
printf("\n");
print_hex("NTLMv2 c/r: ", ntlmv2_response, 24);
printf("\n");
printf("\n");
#if 0
printf("testing string_to_hash...\n");
printf("%d hashes:\n", string_to_hash("d702a1d01b6bc24127bcbf149915a329", test1, test2));
print_hex("Test1: ", test1, 16);
print_hex("Test2: ", test2, 16);
printf("\n");
printf("testing string_to_hash...\n");
printf("%d hashes:\n", string_to_hash("bce0ce40de0d1d9cd08a4e6066a14b92", test1, test2));
print_hex("Test1: ", test1, 16);
print_hex("Test2: ", test2, 16);
printf("\n");
printf("testing string_to_hash...\n");
printf("%d hashes:\n", string_to_hash("d702a1d01b6bc24127bcbf149915a329bce0ce40de0d1d9cd08a4e6066a14b92", test1, test2));
print_hex("Test1: ", test1, 16);
print_hex("Test2: ", test2, 16);
printf("\n");
printf("testing string_to_hash...\n");
printf("%d hashes:\n", string_to_hash("d702a1d01b6bc24127bcbf149915a329:bce0ce40de0d1d9cd08a4e6066a14b92", test1, test2));
print_hex("Test1: ", test1, 16);
print_hex("Test2: ", test2, 16);
printf("\n");
#endif
return 0;
}
#endif