| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 72 |
_InitHive( &h ); |
| 73 |
|
| 74 |
|
| 75 |
if( _RegOpenHive( system, &h ) ) { |
| 76 |
sprintf(error, "Error opening hive file %s\n", system ); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 163 |
pkey[i] = key[p[i]]; |
| 164 |
} |
| 165 |
|
| 166 |
free(n); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|