| 1 | /* Bkhive |
| 2 | Extract Syskey bootkey from the system hive file |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License |
| 6 | as published by the Free Software Foundation; either version 2 |
| 7 | of the License, or (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | Copyright (C) 2004-2006 Nicola Cuomo <[email protected]> |
| 19 | Improvments and some bugs fixes by Objectif Securite |
| 20 | <http://www.objectif-securite.ch> |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #if defined(__linux__) |
| 27 | #include <endian.h> |
| 28 | #elif defined(__FreeBSD__) |
| 29 | #include <machine/endian.h> |
| 30 | #endif |
| 31 | |
| 32 | #include "hive.h" |
| 33 | |
| 34 | #ifdef BYTE_ORDER |
| 35 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 36 | #elif BYTE_ORDER == BIG_ENDIAN |
| 37 | #include <byteswap.h> |
| 38 | #else |
| 39 | #warning "Doesn't define a standard ENDIAN type" |
| 40 | #endif |
| 41 | #else |
| 42 | #warning "Doesn't define BYTE_ORDER" |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | int bkhive(unsigned char *system, unsigned char *pkey, char *error, int debug) { |
| 47 | |
| 48 | /* hive */ |
| 49 | struct hive h; |
| 50 | nk_hdr *n = NULL; |
| 51 | unsigned char *b; |
| 52 | |
| 53 | int i, j, buf_len, control; |
| 54 | |
| 55 | char *kn[] = { "JD", "Skew1", "GBG", "Data" }; |
| 56 | char kv[9]; |
| 57 | unsigned char *buf = NULL; |
| 58 | char *keyname; |
| 59 | char *root_key, *regselect, *reglsa; |
| 60 | // System\ControlSet001\Control\Lsa\ on some nt4 box |
| 61 | |
| 62 | unsigned char key[0x10]; |
| 63 | |
| 64 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 65 | int p[] = { 0xb, 0x6, 0x7, 0x1, 0x8, 0xa, 0xe, 0x0, 0x3, 0x5, 0x2, 0xf, 0xd, 0x9, 0xc, 0x4 }; |
| 66 | #elif BYTE_ORDER == BIG_ENDIAN |
| 67 | int p[] = { 0x8, 0x5, 0x4, 0x2, 0xb, 0x9, 0xd, 0x3, 0x0, 0x6, 0x1, 0xc, 0xe, 0xa, 0xf, 0x7 }; |
| 68 | #endif |
| 69 | |
| 70 | |
| 71 | /* Initialize hive access */ |
| 72 | _InitHive( &h ); |
| 73 | |
| 74 | /* Open the system hive file */ |
| 75 | if( _RegOpenHive( system, &h ) ) { |
| 76 | sprintf(error, "Error opening hive file %s\n", system ); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | /* Get Root key name |
| 81 | $$$PROTO.HIV for 2k/XP, |
| 82 | CMI-CreateHive{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx} for Vista */ |
| 83 | if( _RegGetRootKey( &h, &root_key)) { |
| 84 | sprintf(error, "Error reading hive root key\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | if (debug) |
| 89 | printf("Root Key : %s\n", root_key); |
| 90 | |
| 91 | regselect = (char *) malloc(strlen(root_key)+10); |
| 92 | reglsa = (char *) malloc(strlen(root_key)+33); |
| 93 | |
| 94 | sprintf(regselect, "%s\\Select", root_key); |
| 95 | |
| 96 | /* Find the Default ControlSet */ |
| 97 | n = (nk_hdr*) malloc(sizeof(nk_hdr)); |
| 98 | |
| 99 | if (!_RegOpenKey(&h, regselect, &n)) |
| 100 | if (!_RegQueryValue( &h, "Default", n, &buf, &buf_len)) { |
| 101 | if (buf_len == 4) |
| 102 | control = *(int*)(buf); |
| 103 | else |
| 104 | control = 1; |
| 105 | |
| 106 | sprintf(reglsa,"%s\\ControlSet%03d\\Control\\Lsa\\", root_key, control); |
| 107 | if (debug) |
| 108 | printf("Default ControlSet: %03d\n", control); |
| 109 | } |
| 110 | else { |
| 111 | sprintf(error, "Error reading ControlSet: _RegQueryValue\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | else { |
| 115 | sprintf(error, "Error reading ControlSet: _RegOpenKey\n"); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* foreach keys */ |
| 121 | for( i = 0; i < 4; i++ ) { |
| 122 | keyname = (char *) malloc( strlen( reglsa ) + strlen( kn[i] ) + 1 ); |
| 123 | sprintf( keyname, "%s%s", reglsa, kn[i] ); |
| 124 | if (debug) |
| 125 | printf("********* %s *********\n", keyname); |
| 126 | |
| 127 | /* Access lsa subkey */ |
| 128 | if( _RegOpenKey( &h, keyname, &n ) ) { |
| 129 | _RegCloseHive( &h ); |
| 130 | |
| 131 | sprintf(error, "Error accessing key %s\nWrong/corrupted hive??\n", kn[i] ); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | /* Access the data */ |
| 136 | b = read_data( &h, n->classname_off + 0x1000 ); |
| 137 | if (debug) |
| 138 | printf("n->classname_len = %d b = %x\n", |
| 139 | n->classname_len, |
| 140 | (unsigned int)(b-(h.base))); |
| 141 | |
| 142 | // Quick hack for unicode -> ascii translation |
| 143 | for( j = 0; j < n->classname_len && j<9; j++) |
| 144 | kv[j] = b[j*2]; |
| 145 | kv[8] = 0; |
| 146 | sscanf( kv, "%x", (unsigned int*)( &key[i*4] ) ); |
| 147 | |
| 148 | free( keyname ); |
| 149 | } |
| 150 | |
| 151 | _RegCloseHive( &h ); |
| 152 | |
| 153 | if (debug) { |
| 154 | printf("Bootkey unsorted: "); |
| 155 | for( i = 0; i < 0x10; i++ ) { |
| 156 | printf("%.2x", key[i]); |
| 157 | } |
| 158 | printf("\n"); |
| 159 | } |
| 160 | |
| 161 | for( i = 0; i < 0x10; i++ ) { |
| 162 | /* Permute the class name */ |
| 163 | pkey[i] = key[p[i]]; |
| 164 | } |
| 165 | |
| 166 | free(n); |
| 167 | return 0; |
| 168 | } |
| 169 |