| 1 | /* Hive.c |
| 2 | Hive file access, pretty lame and bugged but do the work O_o |
| 3 | Thanks to B.D. for file structure info |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License |
| 7 | as published by the Free Software Foundation; either version 2 |
| 8 | of the License, or (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | This program is released under the GPL with the additional exemption |
| 20 | that compiling, linking, and/or using OpenSSL is allowed. |
| 21 | |
| 22 | Copyright (C) 2004-2006 Nicola Cuomo <[email protected]> |
| 23 | Improvments and some bugs fixes by Objectif Securité |
| 24 | <http://www.objectif-securite.ch> |
| 25 | */ |
| 26 | |
| 27 | #include <sys/param.h> |
| 28 | #include "hive.h" |
| 29 | |
| 30 | #ifdef BYTE_ORDER |
| 31 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 32 | #elif BYTE_ORDER == BIG_ENDIAN |
| 33 | #include <byteswap.h> |
| 34 | #else |
| 35 | #warning "Doesn't define a standard ENDIAN type" |
| 36 | #endif |
| 37 | #else |
| 38 | #warning "Doesn't define BYTE_ORDER" |
| 39 | #endif |
| 40 | |
| 41 | |
| 42 | nk_hdr* read_nk(nk_hdr *nk, struct hive *h, int offset ) { |
| 43 | memcpy(nk, h->base+offset+4, sizeof(nk_hdr)); |
| 44 | nk->key_name = (h->base+offset+4+76); |
| 45 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 46 | #elif BYTE_ORDER == BIG_ENDIAN |
| 47 | nk->id = __bswap_16(nk->id); |
| 48 | nk->type = __bswap_16(nk->type); |
| 49 | nk->name_len = __bswap_16(nk->name_len); |
| 50 | nk->classname_len = __bswap_16(nk->classname_len); |
| 51 | nk->classname_off = __bswap_32(nk->classname_off); |
| 52 | nk->unk2 = __bswap_32(nk->unk2); |
| 53 | nk->lf_off = __bswap_32(nk->lf_off); |
| 54 | nk->value_cnt = __bswap_32(nk->value_cnt); |
| 55 | nk->value_off = __bswap_32(nk->value_off); |
| 56 | nk->subkey_num = __bswap_32(nk->subkey_num); |
| 57 | #endif |
| 58 | return nk; |
| 59 | } |
| 60 | |
| 61 | lf_hdr* read_lf(lf_hdr *lf, struct hive *h, int offset ) { |
| 62 | memcpy(lf, h->base+offset+4, sizeof(lf_hdr)); |
| 63 | lf->hr = (h->base+offset+4+4); |
| 64 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 65 | #elif BYTE_ORDER == BIG_ENDIAN |
| 66 | lf->id = __bswap_16(lf->id); |
| 67 | lf->key_num = __bswap_16(lf->key_num); |
| 68 | #endif |
| 69 | return lf; |
| 70 | } |
| 71 | |
| 72 | vk_hdr* read_vk(vk_hdr *vk, struct hive *h, int offset ) { |
| 73 | memcpy(vk, h->base+offset+4, sizeof(vk_hdr)); |
| 74 | vk->value_name = (h->base+offset+4+20); |
| 75 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 76 | #elif BYTE_ORDER == BIG_ENDIAN |
| 77 | vk->id = __bswap_16(vk->id); |
| 78 | vk->name_len = __bswap_16(vk->name_len); |
| 79 | vk->flag = __bswap_16(vk->flag); |
| 80 | vk->data_len = __bswap_32(vk->data_len); |
| 81 | vk->data_off = __bswap_32(vk->data_off); |
| 82 | vk->data_type = __bswap_32(vk->data_type); |
| 83 | #endif |
| 84 | return vk; |
| 85 | } |
| 86 | |
| 87 | int* read_valuelist(int *value, struct hive *h, int offset, int size ){ |
| 88 | memcpy(value, h->base+offset+4, size*sizeof(int)); |
| 89 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 90 | #elif BYTE_ORDER == BIG_ENDIAN |
| 91 | int i; |
| 92 | for (i=0; i<size; i++) |
| 93 | value[i] = __bswap_32(value[i]); |
| 94 | #endif |
| 95 | return value; |
| 96 | } |
| 97 | |
| 98 | hashrecord* read_hr(hashrecord *hr, unsigned char *pos, int index ) { |
| 99 | pos+=(8*index); |
| 100 | memcpy(hr, pos, sizeof(hashrecord)); |
| 101 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 102 | #elif BYTE_ORDER == BIG_ENDIAN |
| 103 | hr->nk_offset = __bswap_32(hr->nk_offset); |
| 104 | #endif |
| 105 | return hr; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | unsigned char* read_data(struct hive *h, int offset ) { |
| 110 | return ( (unsigned char*) (h->base + offset + 4) ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void _RegCloseHive( struct hive *h ) { |
| 115 | if( h->base != NULL ) |
| 116 | free( h->base ); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | void _InitHive( struct hive *h ) { |
| 121 | h->base = NULL; |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | int _RegOpenHive( unsigned char *filename, struct hive *h ) { |
| 126 | FILE *hiveh; |
| 127 | unsigned long hsize; |
| 128 | |
| 129 | /* Prova ad aprire l'hive */ |
| 130 | if( ( hiveh = fopen((char *) filename, "rb" ) ) != NULL ) { |
| 131 | if( fseek( hiveh, 0, SEEK_END ) == 0 ) { |
| 132 | hsize = ftell( hiveh ); |
| 133 | |
| 134 | /* Legge il file in memoria */ |
| 135 | /* MMF ??? -_- */ |
| 136 | h->base = (unsigned char *) malloc( hsize ); |
| 137 | |
| 138 | fseek( hiveh, 0, SEEK_SET ); |
| 139 | |
| 140 | if( fread( (void *) h->base, hsize, 1, hiveh ) == 1 ){ |
| 141 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 142 | if( *((int*)h->base) == 0x66676572 ) { |
| 143 | fclose( hiveh ); |
| 144 | return 0; |
| 145 | } |
| 146 | #elif BYTE_ORDER == BIG_ENDIAN |
| 147 | if( *((int*)h->base) == __bswap_32( 0x66676572)) { |
| 148 | fclose( hiveh ); |
| 149 | return 0; |
| 150 | } |
| 151 | #endif |
| 152 | } |
| 153 | } |
| 154 | fclose( hiveh ); |
| 155 | } |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | int _RegOpenHiveBuffer( unsigned char *buffer, unsigned long size, struct hive *h ) { |
| 160 | |
| 161 | h->base = (unsigned char *) malloc( size ); |
| 162 | memcpy(h->base, buffer, size); |
| 163 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 164 | if( *((int*)h->base) == 0x66676572 ) |
| 165 | return 0; |
| 166 | #elif BYTE_ORDER == BIG_ENDIAN |
| 167 | if( *((int*)h->base) == __bswap_32( 0x66676572)) |
| 168 | return 0; |
| 169 | #endif |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | long parself( struct hive *h, char *t, unsigned long off ) { |
| 174 | nk_hdr *n; |
| 175 | lf_hdr *l; |
| 176 | hashrecord *hr; |
| 177 | long res; |
| 178 | |
| 179 | int i; |
| 180 | |
| 181 | hr = (hashrecord*) malloc(sizeof(hashrecord)); |
| 182 | n = (nk_hdr*) malloc(sizeof(nk_hdr)); |
| 183 | l = (lf_hdr*) malloc(sizeof(lf_hdr)); |
| 184 | l = read_lf(l, h, off ); |
| 185 | #ifdef DEBUG |
| 186 | printf("off = %x, key_num = %d ", off, l->key_num); |
| 187 | #endif |
| 188 | |
| 189 | for( i = 0; i < l->key_num; i++ ){ |
| 190 | hr = read_hr(hr, l->hr, i); |
| 191 | n = read_nk(n, h, hr->nk_offset + 0x1000 ); |
| 192 | #ifdef DEBUG |
| 193 | printf("/ i=%d/%d, keyname = %x, nk_offset = %x\n", i, l->key_num, hr->keyname, hr->nk_offset); |
| 194 | printf("//t = %s, keyname = %s, len = %d\n", t, strndup(n->key_name, n->name_len), n->name_len); |
| 195 | #endif |
| 196 | if( !memcmp( t, n->key_name, n->name_len ) && (strlen(t) == n->name_len)) { |
| 197 | res = hr->nk_offset; |
| 198 | free(n); |
| 199 | free(l); |
| 200 | return res; |
| 201 | } |
| 202 | } |
| 203 | free(n); |
| 204 | free(l); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | int _RegGetRootKey(struct hive *h, char **root_key) { |
| 209 | nk_hdr *n; |
| 210 | |
| 211 | n = (nk_hdr*) malloc(sizeof(nk_hdr)); |
| 212 | n = read_nk(n, h, 0x1020); |
| 213 | |
| 214 | #ifdef DEBUG |
| 215 | printf("id = %x type = %x name_len = %d\n", n->id, n->type, n->name_len); |
| 216 | #endif |
| 217 | |
| 218 | if ( n->id == NK_ID && n->type == NK_ROOT ) { |
| 219 | *root_key = (char *)malloc(n->name_len+1); |
| 220 | strncpy(*root_key, (char*)n->key_name, n->name_len); |
| 221 | (*root_key)[n->name_len] = 0; |
| 222 | free(n); |
| 223 | return 0; |
| 224 | } |
| 225 | free(n); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | int _RegOpenKey( struct hive *h, char *path, nk_hdr **nr ) { |
| 230 | nk_hdr *n; |
| 231 | char *t, *tpath; |
| 232 | unsigned long noff; |
| 233 | |
| 234 | n = (nk_hdr*) malloc(sizeof(nk_hdr)); |
| 235 | n = read_nk(n, h, 0x1020 ); |
| 236 | |
| 237 | #ifdef DEBUG |
| 238 | printf("id = %x type = %x\n", n->id, n->type); |
| 239 | #endif |
| 240 | if( n->id == NK_ID && n->type == NK_ROOT ) { |
| 241 | tpath = strdup( path ); |
| 242 | t = strtok( tpath, "\\" ); |
| 243 | #ifdef DEBUG |
| 244 | printf("1 %s\n", t); |
| 245 | #endif |
| 246 | if( !memcmp( t, n->key_name, n->name_len ) ) { |
| 247 | t = strtok( NULL, "\\" ); |
| 248 | #ifdef DEBUG |
| 249 | printf("2 %s %x %x\n", t, n->unk2, n->lf_off); |
| 250 | #endif |
| 251 | while( t != NULL ) { |
| 252 | if( ( noff = parself( h, t, n->lf_off + 0x1000 ) ) == -1 ){ |
| 253 | free(n); |
| 254 | return -1; |
| 255 | } |
| 256 | n = read_nk(n, h, noff + 0x1000 ); |
| 257 | t = strtok( NULL, "\\" ); |
| 258 | #ifdef DEBUG |
| 259 | printf("t = %s, noff = %x\n", t, noff); |
| 260 | #endif |
| 261 | } |
| 262 | memcpy(*nr, n, sizeof(nk_hdr)); |
| 263 | free(n); |
| 264 | return 0; |
| 265 | } |
| 266 | free( tpath ); |
| 267 | } |
| 268 | free(n); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | int _RegQueryValue( struct hive *h, char *name, nk_hdr *nr, unsigned char **buff, int *len ) { |
| 273 | vk_hdr *v; |
| 274 | unsigned int i; |
| 275 | int *l; |
| 276 | #ifdef DEBUG |
| 277 | int j; |
| 278 | #endif |
| 279 | |
| 280 | v = (vk_hdr*) malloc(sizeof(vk_hdr)); |
| 281 | l = (int*) malloc(sizeof(int)*nr->value_cnt); |
| 282 | l = read_valuelist(l, h, nr->value_off + 0x1000, nr->value_cnt); |
| 283 | |
| 284 | *len = 0; |
| 285 | |
| 286 | for( i = 0; i < nr->value_cnt; i++ ) { |
| 287 | v = read_vk(v, h, l[i] + 0x1000 ); |
| 288 | if( !memcmp( name, v->value_name, strlen( name ) ) || (name == NULL && ( v->flag & 1 ) == 0 ) ) { |
| 289 | #ifdef DEBUG |
| 290 | printf("%s len=%x &off=%x off=%x type=%x\n", v->value_name, v->data_len&0x0000FFFF, &(v->data_off), v->data_off, v->data_type); |
| 291 | #endif |
| 292 | *len = v->data_len & 0x0000FFFF; |
| 293 | if (*buff!=NULL) |
| 294 | free(*buff); |
| 295 | *buff = (unsigned char*)malloc((*len)); |
| 296 | if ((*len)< 5 ) { |
| 297 | memcpy (*buff, &(v->data_off), (*len)); |
| 298 | } |
| 299 | else |
| 300 | memcpy (*buff, read_data( h, v->data_off + 0x1000),(*len)); |
| 301 | #ifdef DEBUG |
| 302 | printf("buff = ", (*len)); |
| 303 | for (j=0; j<(*len); j++) |
| 304 | printf("%.2x", (*buff)[j]); |
| 305 | printf("\n"); |
| 306 | #endif |
| 307 | |
| 308 | free(v); |
| 309 | return 0; |
| 310 | } |
| 311 | } |
| 312 | free(v); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | int _RegEnumKey( struct hive *h, nk_hdr *nr, int index, char *name, int *namelen ) { |
| 317 | lf_hdr *lf; |
| 318 | nk_hdr *nk; |
| 319 | hashrecord *hr; |
| 320 | |
| 321 | lf = (lf_hdr*) malloc(sizeof(lf_hdr)); |
| 322 | nk = (nk_hdr*) malloc(sizeof(nk_hdr)); |
| 323 | hr = (hashrecord*) malloc(sizeof(hashrecord)); |
| 324 | #ifdef DEBUG |
| 325 | printf("subkey_num = %d\n", nr->subkey_num); |
| 326 | #endif |
| 327 | |
| 328 | if( index < nr->subkey_num ) { |
| 329 | lf = read_lf(lf, h, nr->lf_off + 0x1000 ); |
| 330 | hr = read_hr(hr, lf->hr, index); |
| 331 | nk = read_nk(nk, h, hr->nk_offset + 0x1000 ); |
| 332 | memcpy( name, nk->key_name, min( *namelen, nk->name_len ) ); |
| 333 | name[ min( *namelen, nk->name_len ) ] = 0; |
| 334 | *namelen = nk->name_len; |
| 335 | free(lf); |
| 336 | return ( (index + 1) < nr->subkey_num ) ? (index + 1) : -1; |
| 337 | } |
| 338 | free(lf); |
| 339 | return -1; |
| 340 | } |