Debian Codegraph @

samdump2.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* Samdump2
2 Dump nt/lanman password hashes from a sam hive with Syskey enabled
3
4 Thank to Dmitry Andrianov for the program name ^_^
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 This program is released under the GPL with the additional exemption
21 that compiling, linking, and/or using OpenSSL is allowed.
22
23 Copyright (C) 2004-2006 Nicola Cuomo <[email protected]>
24 Improvments and some bugs fixes by Objectif Securité
25 <http://www.objectif-securite.ch>
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <openssl/rc4.h>
31 #include <openssl/md5.h>
32 #include <openssl/des.h>
33 #if defined(__linux__)
34 #include <endian.h>
35 #elif defined(__FreeBSD__)
36 #include <machine/endian.h>
37 #endif
38 #include "hive.h"
39 #include "list.h"
40
41 #ifdef BYTE_ORDER
42 #if BYTE_ORDER == LITTLE_ENDIAN
43 #elif BYTE_ORDER == BIG_ENDIAN
44 #include <byteswap.h>
45 #else
46 #warning "Doesn't define a standard ENDIAN type"
47 #endif
48 #else
49 #warning "Doesn't define BYTE_ORDER"
50 #endif
51
52
53 /* Cut&Paste from pwdump source code */
54
55 /*
56 * Convert a 7 byte array into an 8 byte des key with odd parity.
57 */
58
59 void str_to_key(unsigned char *str,unsigned char *key)
60 {
61 // void des_set_odd_parity(des_cblock *);
62 int i;
63
64 key[0] = str[0]>>1;
65 key[1] = ((str[0]&0x01)<<6) | (str[1]>>2);
66 key[2] = ((str[1]&0x03)<<5) | (str[2]>>3);
67 key[3] = ((str[2]&0x07)<<4) | (str[3]>>4);
68 key[4] = ((str[3]&0x0F)<<3) | (str[4]>>5);
69 key[5] = ((str[4]&0x1F)<<2) | (str[5]>>6);
70 key[6] = ((str[5]&0x3F)<<1) | (str[6]>>7);
71 key[7] = str[6]&0x7F;
72 for (i=0;i<8;i++) {
73 key[i] = (key[i]<<1);
74 }
75 des_set_odd_parity((des_cblock *)key);
76 }
77
78 /*
79 * Function to convert the RID to the first decrypt key.
80 */
81
82 void sid_to_key1(unsigned long sid,unsigned char deskey[8])
83 {
84 unsigned char s[7];
85
86 s[0] = (unsigned char)(sid & 0xFF);
87 s[1] = (unsigned char)((sid>>8) & 0xFF);
88 s[2] = (unsigned char)((sid>>16) & 0xFF);
89 s[3] = (unsigned char)((sid>>24) & 0xFF);
90 s[4] = s[0];
91 s[5] = s[1];
92 s[6] = s[2];
93
94 str_to_key(s,deskey);
95 }
96
97 /*
98 * Function to convert the RID to the second decrypt key.
99 */
100
101 void sid_to_key2(unsigned long sid,unsigned char deskey[8])
102 {
103 unsigned char s[7];
104
105 s[0] = (unsigned char)((sid>>24) & 0xFF);
106 s[1] = (unsigned char)(sid & 0xFF);
107 s[2] = (unsigned char)((sid>>8) & 0xFF);
108 s[3] = (unsigned char)((sid>>16) & 0xFF);
109 s[4] = s[0];
110 s[5] = s[1];
111 s[6] = s[2];
112
113 str_to_key(s,deskey);
114 }
115
116
117 /*
118 * Convert UTF-16 to UTF-8
119 */
120
121 unsigned char* utf16_to_utf8 (unsigned char *dest, unsigned short int *src, size_t size) {
122 unsigned int code_high = 0;
123
124 while (size--)
125 {
126 unsigned int code = *src++;
127
128 if (code_high)
129 {
130 if (code >= 0xDC00 && code <= 0xDFFF)
131 {
132 /* Surrogate pair. */
133 code = ((code_high - 0xD800) << 12) + (code - 0xDC00) + 0x10000;
134
135 *dest++ = (code >> 18) | 0xF0;
136 *dest++ = ((code >> 12) & 0x3F) | 0x80;
137 *dest++ = ((code >> 6) & 0x3F) | 0x80;
138 *dest++ = (code & 0x3F) | 0x80;
139 }
140 else
141 {
142 /* Error... */
143 *dest++ = '?';
144 }
145
146 code_high = 0;
147 }
148 else
149 {
150 if (code <= 0x007F)
151 *dest++ = code;
152 else if (code <= 0x07FF)
153 {
154 *dest++ = (code >> 6) | 0xC0;
155 *dest++ = (code & 0x3F) | 0x80;
156 }
157 else if (code >= 0xD800 && code <= 0xDBFF)
158 {
159 code_high = code;
160 continue;
161 }
162 else if (code >= 0xDC00 && code <= 0xDFFF)
163 {
164 /* Error... */
165 *dest++ = '?';
166 }
167 else
168 {
169 *dest++ = (code >> 12) | 0xE0;
170 *dest++ = ((code >> 6) & 0x3F) | 0x80;
171 *dest++ = (code & 0x3F) | 0x80;
172 }
173 }
174 }
175
176 return dest;
177 }
178
179
180 //---
181
182 int samdump2(unsigned char *sam, list_t *list, unsigned char *bootkey, char *error, int debug, int live, unsigned int size) {
183
184 /* const */
185 char *regaccountkey, *reguserskey;
186 unsigned char aqwerty[] = "!@#$%^&*()qwertyUIOPAzxcvbnmQQQQQQQQQQQQ)(*@&%";
187 unsigned char anum[] = "0123456789012345678901234567890123456789";
188 unsigned char antpassword[] = "NTPASSWORD";
189 unsigned char almpassword[] = "LMPASSWORD";
190
191 char *buff; int buff_len;
192
193 char *root_key;
194
195 /* hive */
196 struct hive h;
197 nk_hdr *n = NULL;
198
199 /* hive buffer */
200 unsigned char *b = NULL;
201 int blen;
202 unsigned char regkeyname[50];
203 int regkeynamelen;
204 char *keyname;
205
206 /* md5 contex, hash, rc4 key, hashed bootkey */
207 MD5_CTX md5c;
208 unsigned char md5hash[0x10];
209 RC4_KEY rc4k;
210 unsigned char hbootkey[0x20];
211
212 /* Des */
213 des_key_schedule ks1, ks2;
214 des_cblock deskey1, deskey2;
215
216 int i, j;
217
218 char *username_utf8;
219 int rid;
220 unsigned short int disabled = 0;
221 int usernameoffset, usernamelen;
222 int lm_hashesoffset, nt_hashesoffset;
223 int lm_size, nt_size;
224
225 unsigned char obfkey[0x10];
226 unsigned char fb[0x10];
227
228 /* Initialize registry access function */
229 _InitHive( &h );
230
231 if (!live) {
232 /* Open sam hive */
233 if( _RegOpenHive(sam, &h ) ) {
234 sprintf( error, "Error opening sam hive or not valid file(\"%s\")\n", sam );
235 return -1;
236 }
237 } else {
238 if( _RegOpenHiveBuffer(sam, (unsigned long) size, &h ) ) {
239 sprintf( error, "Error opening sam hive, hive not valid\n");
240 return -1;
241 }
242 }
243
244 /* Get Root key name
245 SAM for 2k/XP,
246 CMI-CreateHive{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx} for Vista */
247 if( _RegGetRootKey( &h, &root_key)) {
248 sprintf( error, "Error reading hive root key\n");
249 return -1;
250 }
251 if (debug)
252 printf("Root Key : %s\n", root_key);
253
254 regaccountkey = (char *) malloc(strlen(root_key)+30);
255 reguserskey = (char *) malloc(strlen(root_key)+30);
256
257 sprintf(regaccountkey, "%s\\SAM\\Domains\\Account", root_key);
258 sprintf(reguserskey, "%s\\SAM\\Domains\\Account\\Users", root_key);
259
260 n = (nk_hdr*) malloc(sizeof(nk_hdr));
261
262 /* Open SAM\\SAM\\Domains\\Account key*/
263 if( _RegOpenKey( &h, regaccountkey, &n ) ) {
264 _RegCloseHive( &h );
265 sprintf( error, "%s key!\n", regaccountkey );
266 return -1;
267 }
268
269 if( _RegQueryValue( &h, "F", n, &b, &blen ) ) {
270 _RegCloseHive( &h );
271 sprintf( error, "No F!\n" );
272 return -1;
273 }
274
275 /* hash the bootkey */
276 MD5_Init( &md5c );
277 MD5_Update( &md5c, &b[0x70], 0x10 );
278 MD5_Update( &md5c, aqwerty, 0x2f );
279 MD5_Update( &md5c, bootkey, 0x10 );
280 MD5_Update( &md5c, anum, 0x29 );
281 MD5_Final( md5hash, &md5c );
282 RC4_set_key( &rc4k, 0x10, md5hash );
283 RC4( &rc4k, 0x20, &b[0x80], hbootkey );
284
285 j = 0;
286
287 /* Enumerate user */
288 while( j != -1 ) {
289 /* Open SAM\\SAM\\Domains\\Account\\Users */
290 if( _RegOpenKey( &h, reguserskey, &n ) ) {
291 _RegCloseHive( &h );
292 sprintf( error, "No Users key!\n" );
293 return -1;
294 }
295
296 regkeynamelen = sizeof( regkeyname );
297
298 j = _RegEnumKey( &h, n, j, (char*)regkeyname, &regkeynamelen );
299
300 if (debug)
301 printf("******************** %d ********************\n", j);
302
303 /* Skip Names key */
304 if( !memcmp( regkeyname, "Names", regkeynamelen ) )
305 continue;
306
307 keyname = (char*) malloc( strlen( reguserskey ) + regkeynamelen + 2 );
308
309 /* Open SAM\\SAM\\Domains\\Account\\Users\\userrid */
310 strcpy( keyname, reguserskey );
311 strcat( keyname, "\\" ) ;
312 strcat( keyname, (char*)regkeyname ) ;
313
314 if (debug)
315 printf("keyname = %s\n", keyname);
316
317 if( _RegOpenKey( &h, keyname, &n ) ) {
318 _RegCloseHive( &h );
319
320 sprintf( error, "Asd -_- _RegEnumKey fail!\n" );
321 return -1;
322 }
323
324 if( _RegQueryValue( &h, "F", n, &b, &blen ) ) {
325 _RegCloseHive( &h );
326
327 sprintf( error, "No F value!\n" );
328 return -1;
329 }
330
331 disabled = (unsigned short) *(b+56) & 0x0001;
332 if (debug)
333 printf("disabled = %d\n", disabled);
334
335
336 if( _RegQueryValue( &h, "V", n, &b, &blen ) ) {
337 _RegCloseHive( &h );
338
339 sprintf( error, "No V value!\n" );
340 return -1;
341 }
342
343 /* rid */
344 rid = strtoul( (char*)regkeyname, NULL, 16 );
345
346 /* get the username */
347 /* 0x10 username size 0xc username offset */
348 #if BYTE_ORDER == LITTLE_ENDIAN
349 usernamelen = *(int*)(b + 0x10) >> 1;
350 #elif BYTE_ORDER == BIG_ENDIAN
351 usernamelen = __bswap_32(*(int*)(b + 0x10) >> 1);
352 #endif
353 usernameoffset = b[0xc] + 0xcc;
354
355 if (debug)
356 printf("\nusername len=%d, off=%x\n", usernamelen, usernameoffset);
357
358 username_utf8 = (char *) malloc (usernamelen*2 + 1);
359 memset(username_utf8, 0, usernamelen*2 + 1);
360 utf16_to_utf8 ((unsigned char *)username_utf8, (unsigned short int*) &b[usernameoffset], usernamelen);
361
362 #if BYTE_ORDER == LITTLE_ENDIAN
363 lm_hashesoffset = *(int *)(b + 0x9c ) + 0xcc;
364 lm_size = *(int *)(b + 0xa0 );
365 nt_hashesoffset = *(int *)(b + 0xa8 ) + 0xcc;
366 nt_size = *(int *)(b + 0xac );
367 #elif BYTE_ORDER == BIG_ENDIAN
368 lm_hashesoffset = __bswap_32(*(int *)(b + 0x9c )) + 0xcc;
369 lm_size = __bswap_32(*(int *)(b + 0xa0 ));
370 nt_hashesoffset = __bswap_32(*(int *)(b + 0xa8 )) + 0xcc;
371 nt_size = __bswap_32(*(int *)(b + 0xac ));
372 #endif
373
374 if (debug) {
375 printf("lm_hashoffset = %x, lm_size = %x\n", lm_hashesoffset, lm_size);
376 printf("nt_hashoffset = %x, nt_size = %x\n", nt_hashesoffset, nt_size);
377 }
378
379 buff = (char*)malloc(512);
380 /* Print the user hash */
381 if (disabled)
382 buff_len = sprintf(buff, "*disabled* %s:%d:", username_utf8, rid );
383 else
384 buff_len = sprintf(buff, "%s:%d:", username_utf8, rid );
385
386
387 if( lm_size == 0x14 ) {
388 if (debug) {
389 printf("\n");
390 for( i = 0; i < 0x10; i++ )
391 printf( "%.2x", b[lm_hashesoffset+4+i] );
392 }
393
394 /* LANMAN */
395 /* hash the hbootkey and decode lanman password hashes */
396 MD5_Init( &md5c );
397 MD5_Update( &md5c, hbootkey, 0x10 );
398 #if BYTE_ORDER == LITTLE_ENDIAN
399 MD5_Update( &md5c, &rid, 0x4 );
400 #elif BYTE_ORDER == BIG_ENDIAN
401 rid = __bswap_32(rid);
402 MD5_Update( &md5c, &rid, 0x4 );
403 rid = __bswap_32(rid);
404 #endif
405 MD5_Update( &md5c, almpassword, 0xb );
406 MD5_Final( md5hash, &md5c );
407
408 RC4_set_key( &rc4k, 0x10, md5hash );
409 RC4( &rc4k, 0x10, &b[ lm_hashesoffset + 4 ], obfkey );
410 if (debug) {
411 printf("\nobfkey: ");
412 for( i = 0; i < 0x10; i++ )
413 printf( "%.2x", (unsigned char)obfkey[i] );
414 printf("\n");
415 }
416
417
418 /* From Pwdump */
419
420 /* Get the two decrpt keys. */
421 sid_to_key1(rid,(unsigned char *)deskey1);
422 des_set_key_checked((des_cblock *)deskey1,ks1);
423 sid_to_key2(rid,(unsigned char *)deskey2);
424 des_set_key_unchecked((des_cblock *)deskey2,ks2);
425
426 /* Decrypt the lanman password hash as two 8 byte blocks. */
427 des_ecb_encrypt((des_cblock *)obfkey,
428 (des_cblock *)fb, ks1, DES_DECRYPT);
429 des_ecb_encrypt((des_cblock *)(obfkey + 8),
430 (des_cblock *)&fb[8], ks2, DES_DECRYPT);
431
432
433
434 // sf25( obfkey, (int*)&rid, fb );
435
436 for( i = 0; i < 0x10; i++ ) {
437 sprintf(buff+buff_len, "%.2x", fb[i] );
438 buff_len +=2;
439 }
440 } else {
441 sprintf(buff+buff_len, "aad3b435b51404eeaad3b435b51404ee" );
442 buff_len+=32;
443 }
444 sprintf(buff+buff_len, ":" );
445 buff_len++;
446
447 if( nt_size == 0x14 ) {
448 if (debug) {
449 printf("\n");
450 for( i = 0; i < 0x10; i++ )
451 printf( "%.2x", b[nt_hashesoffset+4+i] );
452 printf("\n");
453 }
454
455 /* NT */
456 /* hash the hbootkey and decode the nt password hashes */
457 MD5_Init( &md5c );
458 MD5_Update( &md5c, hbootkey, 0x10 );
459 #if BYTE_ORDER == LITTLE_ENDIAN
460 MD5_Update( &md5c, &rid, 0x4 );
461 #elif BYTE_ORDER == BIG_ENDIAN
462 rid = __bswap_32(rid);
463 MD5_Update( &md5c, &rid, 0x4 );
464 rid = __bswap_32(rid);
465 #endif
466 MD5_Update( &md5c, antpassword, 0xb );
467 MD5_Final( md5hash, &md5c );
468
469 RC4_set_key( &rc4k, 0x10, md5hash );
470 RC4( &rc4k, 0x10, &b[ nt_hashesoffset + 4], obfkey );
471
472 if (lm_size != 0x14) {
473 /* Get the two decrpt keys. */
474 sid_to_key1(rid,(unsigned char *)deskey1);
475 des_set_key((des_cblock *)deskey1,ks1);
476 sid_to_key2(rid,(unsigned char *)deskey2);
477 des_set_key((des_cblock *)deskey2,ks2);
478 }
479
480 /* Decrypt the NT md4 password hash as two 8 byte blocks. */
481 des_ecb_encrypt((des_cblock *)obfkey,
482 (des_cblock *)fb, ks1, DES_DECRYPT);
483 des_ecb_encrypt((des_cblock *)(obfkey + 8),
484 (des_cblock *)&fb[8], ks2, DES_DECRYPT);
485
486 /* sf27 wrap to sf25 */
487 //sf27( obfkey, (int*)&rid, fb );
488
489 for( i = 0; i < 0x10; i++ ) {
490 sprintf(buff+buff_len, "%.2x", fb[i] );
491 buff_len +=2;
492 }
493 } else {
494 sprintf(buff+buff_len, "31d6cfe0d16ae931b73c59d7e0c089c0");
495 buff_len+=32;
496 }
497 sprintf(buff+buff_len, ":::" );
498 buff_len+=3;
499
500 /* add the hash to the list */
501 list_add_tail(list, buff);
502
503 free( username_utf8 );
504 free( keyname );
505 }
506
507 _RegCloseHive( &h );
508 free(n);
509 free(b);
510
511 return 0;
512 }