[111] | 1 | /* |
---|
| 2 | * Copyright (C) 2008 Timothy D. Morgan |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; version 3 of the License. |
---|
| 7 | * |
---|
| 8 | * This program is distributed in the hope that it will be useful, |
---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | * GNU General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program; if not, write to the Free Software |
---|
| 15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 16 | * |
---|
| 17 | * $Id: $ |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include <stdio.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <sysexits.h> |
---|
| 23 | |
---|
| 24 | #include "../include/regfi.h" |
---|
| 25 | #include "../include/range_list.h" |
---|
| 26 | #include "../include/lru_cache.h" |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /* Globals, influenced by command line parameters */ |
---|
| 30 | bool print_verbose = false; |
---|
| 31 | bool print_security = false; |
---|
| 32 | bool print_header = true; |
---|
| 33 | bool print_leftover = false; |
---|
| 34 | bool print_parsedraw = false; |
---|
| 35 | char* registry_file = NULL; |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | #include "common.c" |
---|
| 39 | |
---|
| 40 | /* Output format: |
---|
| 41 | * real_offset,min_length,record_type,parent_path,name,data_type,mtime,num_values,value,data_length,raw_data |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | void regfi_print_nk(REGF_NK_REC* nk) |
---|
| 45 | { |
---|
| 46 | printf("Found key at offset 0x%.8X:\n", nk->offset); |
---|
| 47 | printf(" keyname: \"%s\"\n", nk->keyname); |
---|
| 48 | printf(" parent_off (virtual): 0x%.8X\n", nk->parent_off); |
---|
| 49 | printf(" cell_size: %d\n", nk->cell_size); |
---|
| 50 | printf(" key_type: 0x%.4X\n", nk->key_type); |
---|
| 51 | printf(" magic: %c%c\n", nk->magic[0], nk->magic[1]); |
---|
| 52 | printf(" mtime: 0x%.8X 0x%.8X\n", nk->mtime.low, nk->mtime.high); |
---|
| 53 | printf(" name_length: %d\n", nk->name_length); |
---|
| 54 | printf(" classname_length: %d\n", nk->classname_length); |
---|
| 55 | printf(" classname_off (virtual): 0x%.8X\n", nk->classname_off); |
---|
| 56 | printf(" max_bytes_subkeyname: %d\n", nk->max_bytes_subkeyname); |
---|
| 57 | printf(" max_bytes_subkeyclassname: %d\n", nk->max_bytes_subkeyclassname); |
---|
| 58 | printf(" max_bytes_valuename: %d\n", nk->max_bytes_valuename); |
---|
| 59 | printf(" max_bytes_value: %d\n", nk->max_bytes_value); |
---|
| 60 | printf(" unknown1: 0x%.8X\n", nk->unknown1); |
---|
| 61 | printf(" unknown2: 0x%.8X\n", nk->unknown2); |
---|
| 62 | printf(" unknown3: 0x%.8X\n", nk->unknown3); |
---|
| 63 | printf(" unk_index: 0x%.8X\n", nk->unk_index); |
---|
| 64 | printf(" num_subkeys: %d\n", nk->num_subkeys); |
---|
| 65 | printf(" subkeys_off (virtual): 0x%.8X\n", nk->subkeys_off); |
---|
| 66 | printf(" num_values: %d\n", nk->num_values); |
---|
| 67 | printf(" values_off (virtual): 0x%.8X\n", nk->values_off); |
---|
| 68 | printf(" sk_off (virtual): 0x%.8X\n", nk->sk_off); |
---|
| 69 | printf("\n"); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | char* getQuotedData(int fd, uint32 offset, uint32 length) |
---|
| 74 | { |
---|
| 75 | uint8* buf; |
---|
| 76 | char* quoted_buf; |
---|
| 77 | uint32 len; |
---|
| 78 | |
---|
| 79 | if((lseek(fd, offset, SEEK_SET)) == -1) |
---|
| 80 | return NULL; |
---|
| 81 | |
---|
| 82 | buf = (uint8*)malloc(length); |
---|
| 83 | if(buf == NULL) |
---|
| 84 | return NULL; |
---|
| 85 | |
---|
| 86 | len = length; |
---|
| 87 | if((regfi_read(fd, buf, &length) != 0) || length != len) |
---|
| 88 | { |
---|
| 89 | free(buf); |
---|
| 90 | return NULL; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | quoted_buf = quote_buffer(buf, length, common_special_chars); |
---|
| 94 | free(buf); |
---|
| 95 | |
---|
| 96 | return quoted_buf; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | void printKey(REGF_FILE* f, REGF_NK_REC* nk, const char* prefix) |
---|
| 101 | { |
---|
| 102 | char mtime[20]; |
---|
| 103 | time_t tmp_time[1]; |
---|
| 104 | struct tm* tmp_time_s = NULL; |
---|
| 105 | char* quoted_name = NULL; |
---|
| 106 | char* quoted_raw = ""; |
---|
| 107 | |
---|
| 108 | *tmp_time = nt_time_to_unix(&nk->mtime); |
---|
| 109 | tmp_time_s = gmtime(tmp_time); |
---|
| 110 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s); |
---|
| 111 | |
---|
| 112 | quoted_name = quote_string(nk->keyname, key_special_chars); |
---|
| 113 | if (quoted_name == NULL) |
---|
| 114 | { |
---|
| 115 | quoted_name = malloc(1*sizeof(char)); |
---|
| 116 | if(quoted_name == NULL) |
---|
| 117 | bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n"); |
---|
| 118 | quoted_name[0] = '\0'; |
---|
| 119 | |
---|
| 120 | fprintf(stderr, "WARNING: NULL key name in NK record at offset %.8X.\n", |
---|
| 121 | nk->offset); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if(print_parsedraw) |
---|
| 125 | quoted_raw = getQuotedData(f->fd, nk->offset, nk->cell_size); |
---|
| 126 | |
---|
| 127 | printf("%.8X,%.8X,KEY,%s,%s,%s,%d,,,,,,,,%s\n", nk->offset, nk->cell_size, |
---|
| 128 | prefix, quoted_name, mtime, nk->num_values, quoted_raw); |
---|
| 129 | |
---|
| 130 | if(print_parsedraw) |
---|
| 131 | free(quoted_raw); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | void printValue(REGF_FILE* f, const REGF_VK_REC* vk, const char* prefix) |
---|
| 136 | { |
---|
| 137 | char* quoted_value = NULL; |
---|
| 138 | char* quoted_name = NULL; |
---|
| 139 | char* quoted_raw = ""; |
---|
| 140 | char* conv_error = NULL; |
---|
| 141 | const char* str_type = NULL; |
---|
| 142 | uint32 size = vk->data_size; |
---|
| 143 | |
---|
| 144 | /* Microsoft's documentation indicates that "available memory" is |
---|
| 145 | * the limit on value sizes. Annoying. We limit it to 1M which |
---|
| 146 | * should rarely be exceeded, unless the file is corrupt or |
---|
| 147 | * malicious. For more info, see: |
---|
| 148 | * http://msdn2.microsoft.com/en-us/library/ms724872.aspx |
---|
| 149 | */ |
---|
| 150 | /* TODO: should probably do something different here for this tool.*/ |
---|
| 151 | if(size > VK_MAX_DATA_LENGTH) |
---|
| 152 | { |
---|
| 153 | fprintf(stderr, "WARNING: value data size %d larger than " |
---|
| 154 | "%d, truncating...\n", size, VK_MAX_DATA_LENGTH); |
---|
| 155 | size = VK_MAX_DATA_LENGTH; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | quoted_name = quote_string(vk->valuename, key_special_chars); |
---|
| 159 | if (quoted_name == NULL) |
---|
| 160 | { /* Value names are NULL when we're looking at the "(default)" value. |
---|
| 161 | * Currently we just return a 0-length string to try an eliminate |
---|
| 162 | * ambiguity with a literal "(default)" value. The data type of a line |
---|
| 163 | * in the output allows one to differentiate between the parent key and |
---|
| 164 | * this value. |
---|
| 165 | */ |
---|
| 166 | quoted_name = malloc(1*sizeof(char)); |
---|
| 167 | if(quoted_name == NULL) |
---|
| 168 | bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n"); |
---|
| 169 | quoted_name[0] = '\0'; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error); |
---|
| 173 | if(quoted_value == NULL) |
---|
| 174 | { |
---|
| 175 | quoted_value = malloc(1*sizeof(char)); |
---|
| 176 | if(quoted_value == NULL) |
---|
| 177 | bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n"); |
---|
| 178 | quoted_value[0] = '\0'; |
---|
| 179 | |
---|
| 180 | if(conv_error == NULL) |
---|
| 181 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
| 182 | "Memory allocation failure likely.\n", prefix, quoted_name); |
---|
| 183 | else if(print_verbose) |
---|
| 184 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
| 185 | "Returned error: %s\n", prefix, quoted_name, conv_error); |
---|
| 186 | } |
---|
| 187 | /* XXX: should these always be printed? */ |
---|
| 188 | else if(conv_error != NULL && print_verbose) |
---|
| 189 | fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', " |
---|
| 190 | "warning returned: %s\n", prefix, quoted_name, conv_error); |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | if(print_parsedraw) |
---|
| 194 | quoted_raw = getQuotedData(f->fd, vk->offset, vk->cell_size); |
---|
| 195 | |
---|
| 196 | str_type = regfi_type_val2str(vk->type); |
---|
| 197 | if(str_type == NULL) |
---|
| 198 | printf("%.8X,%.8X,VALUE,%s,%s,,,0x%.8X,%s,%d,,,,,%s\n", |
---|
| 199 | vk->offset, vk->cell_size, prefix, quoted_name, |
---|
| 200 | vk->type, quoted_value, vk->data_size, quoted_raw); |
---|
| 201 | else |
---|
| 202 | printf("%.8X,%.8X,VALUE,%s,%s,,,%s,%s,%d,,,,,%s\n", |
---|
| 203 | vk->offset, vk->cell_size, prefix, quoted_name, |
---|
| 204 | str_type, quoted_value, vk->data_size, quoted_raw); |
---|
| 205 | |
---|
| 206 | if(print_parsedraw) |
---|
| 207 | free(quoted_raw); |
---|
| 208 | if(quoted_value != NULL) |
---|
| 209 | free(quoted_value); |
---|
| 210 | if(quoted_name != NULL) |
---|
| 211 | free(quoted_name); |
---|
| 212 | if(conv_error != NULL) |
---|
| 213 | free(conv_error); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | void printSK(REGF_FILE* f, REGF_SK_REC* sk) |
---|
| 218 | { |
---|
| 219 | char* quoted_raw = NULL; |
---|
| 220 | char* empty_str = ""; |
---|
| 221 | char* owner = regfi_get_owner(sk->sec_desc); |
---|
| 222 | char* group = regfi_get_group(sk->sec_desc); |
---|
| 223 | char* sacl = regfi_get_sacl(sk->sec_desc); |
---|
| 224 | char* dacl = regfi_get_dacl(sk->sec_desc); |
---|
| 225 | |
---|
| 226 | if(print_parsedraw) |
---|
| 227 | quoted_raw = getQuotedData(f->fd, sk->offset, sk->cell_size); |
---|
| 228 | |
---|
| 229 | if(owner == NULL) |
---|
| 230 | owner = empty_str; |
---|
| 231 | if(group == NULL) |
---|
| 232 | group = empty_str; |
---|
| 233 | if(sacl == NULL) |
---|
| 234 | sacl = empty_str; |
---|
| 235 | if(dacl == NULL) |
---|
| 236 | dacl = empty_str; |
---|
| 237 | |
---|
| 238 | printf("%.8X,%.8X,SK,,,,,,,,%s,%s,%s,%s,%s\n", sk->offset, sk->cell_size, |
---|
| 239 | owner, group, sacl, dacl, quoted_raw); |
---|
| 240 | |
---|
| 241 | if(owner != empty_str) |
---|
| 242 | free(owner); |
---|
| 243 | if(group != empty_str) |
---|
| 244 | free(group); |
---|
| 245 | if(sacl != empty_str) |
---|
| 246 | free(sacl); |
---|
| 247 | if(dacl != empty_str) |
---|
| 248 | free(dacl); |
---|
| 249 | |
---|
| 250 | if(print_parsedraw) |
---|
| 251 | free(quoted_raw); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | int printCell(REGF_FILE* f, uint32 offset) |
---|
| 256 | { |
---|
| 257 | char* quoted_buf; |
---|
| 258 | uint32 cell_length; |
---|
| 259 | bool unalloc; |
---|
| 260 | |
---|
| 261 | if(!regfi_parse_cell(f->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
| 262 | return 1; |
---|
| 263 | |
---|
| 264 | quoted_buf = getQuotedData(f->fd, offset, cell_length); |
---|
| 265 | if(quoted_buf == NULL) |
---|
| 266 | return 2; |
---|
| 267 | |
---|
| 268 | printf("%.8X,%.8X,RAW,,,,,,,,,,,,%s\n", offset, cell_length, quoted_buf); |
---|
| 269 | |
---|
| 270 | free(quoted_buf); |
---|
| 271 | return 0; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | /* |
---|
| 276 | void dump_cell(int fd, uint32 offset) |
---|
| 277 | { |
---|
| 278 | uint8* buf; |
---|
| 279 | uint32 length, j; |
---|
| 280 | int32 cell_length; |
---|
| 281 | |
---|
| 282 | if((lseek(fd, offset, SEEK_SET)) == -1) |
---|
| 283 | exit(8); |
---|
| 284 | |
---|
| 285 | length = 4; |
---|
| 286 | regfi_read(fd, (void*)&cell_length, &length); |
---|
| 287 | if(cell_length < 0) |
---|
| 288 | cell_length *= -1; |
---|
| 289 | |
---|
| 290 | buf = (uint8*)malloc(cell_length); |
---|
| 291 | if(buf == NULL) |
---|
| 292 | exit(9); |
---|
| 293 | if((lseek(fd, offset, SEEK_SET)) == -1) |
---|
| 294 | exit(8); |
---|
| 295 | |
---|
| 296 | length = cell_length; |
---|
| 297 | regfi_read(fd, buf, &length); |
---|
| 298 | for(j=0; j < length; j++) |
---|
| 299 | { |
---|
| 300 | printf("%.2X", buf[j]); |
---|
| 301 | if(j%4 == 3) |
---|
| 302 | printf(" "); |
---|
| 303 | } |
---|
| 304 | printf("\n"); |
---|
| 305 | free(buf); |
---|
| 306 | } |
---|
| 307 | */ |
---|
| 308 | |
---|
| 309 | static void usage(void) |
---|
| 310 | { |
---|
| 311 | fprintf(stderr, "Usage: reglookup-recover [options] <REGISTRY_FILE>\n"); |
---|
| 312 | fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION); |
---|
| 313 | fprintf(stderr, "Options:\n"); |
---|
| 314 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
| 315 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 316 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
| 317 | fprintf(stderr, "\t-l\t enables leftover(raw) cell output.\n"); |
---|
| 318 | fprintf(stderr, "\t-L\t disables leftover(raw) cell output. (default)\n"); |
---|
| 319 | fprintf(stderr, "\t-r\t enables raw cell output for parsed cells.\n"); |
---|
| 320 | fprintf(stderr, "\t-R\t disables raw cell output for parsed cells. (default)\n"); |
---|
| 321 | fprintf(stderr, "\n"); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | bool removeRange(range_list* rl, uint32 rm_idx, uint32 offset, uint32 length) |
---|
| 326 | { |
---|
| 327 | const range_list_element* cur_elem = range_list_get(rl, rm_idx); |
---|
| 328 | |
---|
| 329 | if(cur_elem == NULL) |
---|
| 330 | { |
---|
| 331 | printf("removeRange: cur_elem == NULL. rm_idx=%d\n", rm_idx); |
---|
| 332 | return false; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | if(offset > cur_elem->offset) |
---|
| 336 | { |
---|
| 337 | if(!range_list_split_element(rl, rm_idx, offset)) |
---|
| 338 | { |
---|
| 339 | printf("removeRange: first split failed\n"); |
---|
| 340 | return false; |
---|
| 341 | } |
---|
| 342 | rm_idx++; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if(offset+length < cur_elem->offset+cur_elem->length) |
---|
| 346 | { |
---|
| 347 | if(!range_list_split_element(rl, rm_idx, offset+length)) |
---|
| 348 | { |
---|
| 349 | printf("removeRange: second split failed\n"); |
---|
| 350 | return false; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | if(!range_list_remove(rl, rm_idx)) |
---|
| 355 | { |
---|
| 356 | printf("removeRange: remove failed\n"); |
---|
| 357 | return false; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | return true; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | int extractKeys(REGF_FILE* f, |
---|
| 365 | range_list* unalloc_cells, |
---|
| 366 | range_list* unalloc_keys) |
---|
| 367 | { |
---|
| 368 | const range_list_element* cur_elem; |
---|
| 369 | REGF_NK_REC* key; |
---|
| 370 | uint32 i, j; |
---|
| 371 | |
---|
| 372 | for(i=0; i < range_list_size(unalloc_cells); i++) |
---|
| 373 | { |
---|
| 374 | cur_elem = range_list_get(unalloc_cells, i); |
---|
| 375 | for(j=0; j <= cur_elem->length; j+=8) |
---|
| 376 | { |
---|
| 377 | key = regfi_parse_nk(f, cur_elem->offset+j, |
---|
| 378 | cur_elem->length-j, false); |
---|
| 379 | if(key != NULL) |
---|
| 380 | { |
---|
| 381 | if(!range_list_add(unalloc_keys, key->offset, |
---|
| 382 | key->cell_size, key)) |
---|
| 383 | { |
---|
| 384 | fprintf(stderr, "ERROR: Couldn't add key to unalloc_keys.\n"); |
---|
| 385 | return 20; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | if(!removeRange(unalloc_cells, i, key->offset, key->cell_size)) |
---|
| 389 | return 30; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | return 0; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | |
---|
| 398 | int extractValueLists(REGF_FILE* f, |
---|
| 399 | range_list* unalloc_cells, |
---|
| 400 | range_list* unalloc_keys) |
---|
| 401 | { |
---|
| 402 | REGF_NK_REC* nk; |
---|
| 403 | REGF_HBIN* hbin; |
---|
| 404 | const range_list_element* cur_elem; |
---|
| 405 | uint32 i, j, num_keys, off, values_length, max_length; |
---|
| 406 | int32 idx; |
---|
| 407 | |
---|
| 408 | num_keys=range_list_size(unalloc_keys); |
---|
| 409 | for(i=0; i<num_keys; i++) |
---|
| 410 | { |
---|
| 411 | cur_elem = range_list_get(unalloc_keys, i); |
---|
| 412 | if(cur_elem == NULL) |
---|
| 413 | return 10; |
---|
| 414 | nk = cur_elem->data; |
---|
| 415 | |
---|
| 416 | if(nk->num_values && (nk->values_off!=REGF_OFFSET_NONE)) |
---|
| 417 | { |
---|
| 418 | hbin = regfi_lookup_hbin(f, nk->values_off); |
---|
| 419 | |
---|
| 420 | if(hbin != NULL) |
---|
| 421 | { |
---|
| 422 | off = nk->values_off + REGF_BLOCKSIZE; |
---|
| 423 | max_length = hbin->block_size + hbin->file_off - off; |
---|
| 424 | /* TODO: This is kind of a hack. We parse all value-lists, VK records, |
---|
| 425 | * and data records without regard for current allocation status. |
---|
| 426 | * On the off chance that such a record correctly parsed but is |
---|
| 427 | * actually a reallocated structure used by something else, we |
---|
| 428 | * simply prune it after the fact. Would be faster to check this |
---|
| 429 | * up front somehow. |
---|
| 430 | */ |
---|
| 431 | nk->values = regfi_load_valuelist(f, off, nk->num_values, max_length, |
---|
| 432 | false); |
---|
| 433 | |
---|
| 434 | if(nk->values != NULL) |
---|
| 435 | { |
---|
| 436 | idx = range_list_find(unalloc_cells, off); |
---|
| 437 | if(idx < 0) |
---|
| 438 | { /* We've parsed a values-list which isn't in the unallocated list, |
---|
| 439 | * so prune it. |
---|
| 440 | */ |
---|
| 441 | for(j=0; j<nk->num_values; j++) |
---|
| 442 | { |
---|
| 443 | if(nk->values[j] != NULL) |
---|
| 444 | { |
---|
| 445 | if(nk->values[j]->data != NULL) |
---|
| 446 | free(nk->values[j]->data); |
---|
| 447 | free(nk->values[j]); |
---|
| 448 | free(nk->values); |
---|
| 449 | nk->values = NULL; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | else |
---|
| 454 | { /* Values-list was recovered. Remove from unalloc_cells and |
---|
| 455 | * inspect values. |
---|
| 456 | */ |
---|
| 457 | values_length = (nk->num_values+1)*sizeof(uint32); |
---|
| 458 | if(values_length != (values_length & 0xFFFFFFF8)) |
---|
| 459 | values_length = (values_length & 0xFFFFFFF8) + 8; |
---|
| 460 | if(!removeRange(unalloc_cells, idx, off, values_length)) |
---|
| 461 | return 20; |
---|
| 462 | |
---|
| 463 | for(j=0; j < nk->num_values; j++) |
---|
| 464 | { |
---|
| 465 | if(nk->values[j] != NULL) |
---|
| 466 | { |
---|
| 467 | idx = range_list_find(unalloc_cells, nk->values[j]->offset); |
---|
| 468 | if(idx < 0) |
---|
| 469 | { /* We've parsed a value which isn't in the unallocated list, |
---|
| 470 | * so prune it. |
---|
| 471 | */ |
---|
| 472 | if(nk->values[j]->data != NULL) |
---|
| 473 | free(nk->values[j]->data); |
---|
| 474 | free(nk->values[j]); |
---|
| 475 | nk->values[j] = NULL; |
---|
| 476 | } |
---|
| 477 | else |
---|
| 478 | { |
---|
| 479 | /* A VK record was recovered. Remove from unalloc_cells |
---|
| 480 | * and inspect data. |
---|
| 481 | */ |
---|
| 482 | if(!removeRange(unalloc_cells, idx, nk->values[j]->offset, |
---|
| 483 | nk->values[j]->cell_size)) |
---|
| 484 | return 21; |
---|
| 485 | |
---|
| 486 | /* Don't bother pruning or removing from unalloc_cells if |
---|
| 487 | * there is no data, if it is stored the offset. |
---|
| 488 | */ |
---|
| 489 | if(nk->values[j]->data != NULL && !nk->values[j]->data_in_offset) |
---|
| 490 | { |
---|
| 491 | off = nk->values[j]->data_off+REGF_BLOCKSIZE; |
---|
| 492 | idx = range_list_find(unalloc_cells, off); |
---|
| 493 | if(idx < 0) |
---|
| 494 | { /* We've parsed a data cell which isn't in the unallocated |
---|
| 495 | * list, so prune it. |
---|
| 496 | */ |
---|
| 497 | free(nk->values[j]->data); |
---|
| 498 | nk->values[j]->data = NULL; |
---|
| 499 | } |
---|
| 500 | else |
---|
| 501 | { /*A data record was recovered. Remove from unalloc_cells.*/ |
---|
| 502 | if(!removeRange(unalloc_cells, idx, off, |
---|
| 503 | nk->values[j]->data_size)) |
---|
| 504 | return 22; |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | } |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | return 0; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | |
---|
| 520 | |
---|
| 521 | int extractValues(REGF_FILE* f, |
---|
| 522 | range_list* unalloc_cells, |
---|
| 523 | range_list* unalloc_values) |
---|
| 524 | { |
---|
| 525 | const range_list_element* cur_elem; |
---|
| 526 | REGF_VK_REC* vk; |
---|
| 527 | uint32 i, j; |
---|
| 528 | |
---|
| 529 | for(i=0; i < range_list_size(unalloc_cells); i++) |
---|
| 530 | { |
---|
| 531 | cur_elem = range_list_get(unalloc_cells, i); |
---|
| 532 | for(j=0; j <= cur_elem->length; j+=8) |
---|
| 533 | { |
---|
| 534 | vk = regfi_parse_vk(f, cur_elem->offset+j, |
---|
| 535 | cur_elem->length-j, false); |
---|
| 536 | if(vk != NULL) |
---|
| 537 | { |
---|
| 538 | if(!range_list_add(unalloc_values, vk->offset, |
---|
| 539 | vk->cell_size, vk)) |
---|
| 540 | { |
---|
| 541 | fprintf(stderr, "ERROR: Couldn't add value to unalloc_values.\n"); |
---|
| 542 | return 20; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | if(!removeRange(unalloc_cells, i, vk->offset, vk->cell_size)) |
---|
| 546 | return 30; |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | return 0; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | |
---|
| 555 | int extractSKs(REGF_FILE* f, |
---|
| 556 | range_list* unalloc_cells, |
---|
| 557 | range_list* unalloc_sks) |
---|
| 558 | { |
---|
| 559 | const range_list_element* cur_elem; |
---|
| 560 | REGF_SK_REC* sk; |
---|
| 561 | uint32 i, j; |
---|
| 562 | |
---|
| 563 | for(i=0; i < range_list_size(unalloc_cells); i++) |
---|
| 564 | { |
---|
| 565 | cur_elem = range_list_get(unalloc_cells, i); |
---|
| 566 | for(j=0; j <= cur_elem->length; j+=8) |
---|
| 567 | { |
---|
| 568 | sk = regfi_parse_sk(f, cur_elem->offset+j, |
---|
| 569 | cur_elem->length-j, false); |
---|
| 570 | if(sk != NULL) |
---|
| 571 | { |
---|
| 572 | if(!range_list_add(unalloc_sks, sk->offset, |
---|
| 573 | sk->cell_size, sk)) |
---|
| 574 | { |
---|
| 575 | fprintf(stderr, "ERROR: Couldn't add sk to unalloc_sks.\n"); |
---|
| 576 | return 20; |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | if(!removeRange(unalloc_cells, i, sk->offset, sk->cell_size)) |
---|
| 580 | return 30; |
---|
| 581 | } |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | return 0; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | int main(int argc, char** argv) |
---|
| 590 | { |
---|
| 591 | REGF_FILE* f; |
---|
| 592 | const range_list_element* cur_elem; |
---|
| 593 | range_list* unalloc_cells; |
---|
| 594 | range_list* unalloc_keys; |
---|
| 595 | range_list* unalloc_values; |
---|
| 596 | range_list* unalloc_sks; |
---|
| 597 | REGF_NK_REC* tmp_key; |
---|
| 598 | REGF_VK_REC* tmp_value; |
---|
| 599 | uint32 argi, arge, i, j, ret; |
---|
| 600 | /* uint32 test_offset;*/ |
---|
| 601 | |
---|
| 602 | /* Process command line arguments */ |
---|
| 603 | if(argc < 2) |
---|
| 604 | { |
---|
| 605 | usage(); |
---|
| 606 | bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n"); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | arge = argc-1; |
---|
| 610 | for(argi = 1; argi < arge; argi++) |
---|
| 611 | { |
---|
| 612 | if (strcmp("-v", argv[argi]) == 0) |
---|
| 613 | print_verbose = true; |
---|
| 614 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 615 | print_header = true; |
---|
| 616 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 617 | print_header = false; |
---|
| 618 | else if (strcmp("-l", argv[argi]) == 0) |
---|
| 619 | print_leftover = true; |
---|
| 620 | else if (strcmp("-L", argv[argi]) == 0) |
---|
| 621 | print_leftover = false; |
---|
| 622 | else if (strcmp("-r", argv[argi]) == 0) |
---|
| 623 | print_parsedraw = true; |
---|
| 624 | else if (strcmp("-R", argv[argi]) == 0) |
---|
| 625 | print_parsedraw = false; |
---|
| 626 | else |
---|
| 627 | { |
---|
| 628 | usage(); |
---|
| 629 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
| 630 | bailOut(EX_USAGE, ""); |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | /*test_offset = strtol(argv[argi++], NULL, 16);*/ |
---|
| 634 | |
---|
| 635 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
| 636 | bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
| 637 | |
---|
| 638 | f = regfi_open(registry_file); |
---|
| 639 | if(f == NULL) |
---|
| 640 | { |
---|
| 641 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
| 642 | bailOut(EX_NOINPUT, ""); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | if(print_header) |
---|
| 646 | printf("OFFSET,REC_LENGTH,REC_TYPE,PATH,NAME," |
---|
| 647 | "NK_MTIME,NK_NVAL,VK_TYPE,VK_VALUE,VK_DATA_LEN," |
---|
| 648 | "SK_OWNER,SK_GROUP,SK_SACL,SK_DACL,RAW_CELL\n"); |
---|
| 649 | |
---|
| 650 | /* |
---|
| 651 | * 1. Build a set of all empty cells. May need to keep these in a |
---|
| 652 | * sorted list based on offset. May also need to have them in a hash |
---|
| 653 | * table. |
---|
| 654 | * |
---|
| 655 | * 2. Scour all cells for NK records (regardless of signature offset), |
---|
| 656 | * caching those records separately when they successfully parsed. |
---|
| 657 | * |
---|
| 658 | * 3. Follow each NK record's parent pointers up until a non-deleted key |
---|
| 659 | * is found. Associate this full path with the NK records somehow. |
---|
| 660 | * |
---|
| 661 | * 4. Follow each NK record's pointers to Value-Lists and attempt to |
---|
| 662 | * recover VK/data records from remaining cells. Associate good VK |
---|
| 663 | * records with their full paths. |
---|
| 664 | * |
---|
| 665 | * 5. For each remaining cell or partial cell, attempt to identify the |
---|
| 666 | * record type and parse it. |
---|
| 667 | * |
---|
| 668 | * At each step, claimed cells are removed from the global cell |
---|
| 669 | * list/hash. If only part of a cell is claimed, it is removed from |
---|
| 670 | * the list and the remaining fragments are re-added. |
---|
| 671 | */ |
---|
| 672 | |
---|
| 673 | unalloc_cells = regfi_parse_unalloc_cells(f); |
---|
| 674 | if(unalloc_cells == NULL) |
---|
| 675 | { |
---|
| 676 | fprintf(stderr, "ERROR: Could not obtain list of unallocated cells.\n"); |
---|
| 677 | return 1; |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | unalloc_keys = range_list_new(); |
---|
| 681 | if(unalloc_keys == NULL) |
---|
| 682 | return 10; |
---|
| 683 | |
---|
| 684 | unalloc_values = range_list_new(); |
---|
| 685 | if(unalloc_values == NULL) |
---|
| 686 | return 10; |
---|
| 687 | |
---|
| 688 | unalloc_sks = range_list_new(); |
---|
| 689 | if(unalloc_sks == NULL) |
---|
| 690 | return 10; |
---|
| 691 | |
---|
| 692 | ret = extractKeys(f, unalloc_cells, unalloc_keys); |
---|
| 693 | if(ret != 0) |
---|
| 694 | { |
---|
| 695 | fprintf(stderr, "ERROR: extractKeys() failed with %d.\n", ret); |
---|
| 696 | return ret; |
---|
| 697 | } |
---|
| 698 | |
---|
| 699 | ret = extractValueLists(f, unalloc_cells, unalloc_keys); |
---|
| 700 | if(ret != 0) |
---|
| 701 | { |
---|
| 702 | fprintf(stderr, "ERROR: extractValueLists() failed with %d.\n", ret); |
---|
| 703 | return ret; |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | /* Carve any orphan values and associated data */ |
---|
| 707 | ret = extractValues(f, unalloc_cells, unalloc_values); |
---|
| 708 | if(ret != 0) |
---|
| 709 | { |
---|
| 710 | fprintf(stderr, "ERROR: extractValues() failed with %d.\n", ret); |
---|
| 711 | return ret; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | /* TODO: Carve SKs */ |
---|
| 715 | /* Carve any SK records */ |
---|
| 716 | ret = extractSKs(f, unalloc_cells, unalloc_sks); |
---|
| 717 | if(ret != 0) |
---|
| 718 | { |
---|
| 719 | fprintf(stderr, "ERROR: extractSKs() failed with %d.\n", ret); |
---|
| 720 | return ret; |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | |
---|
| 724 | for(i=0; i < range_list_size(unalloc_keys); i++) |
---|
| 725 | { |
---|
| 726 | cur_elem = range_list_get(unalloc_keys, i); |
---|
| 727 | tmp_key = (REGF_NK_REC*)cur_elem->data; |
---|
| 728 | |
---|
| 729 | printKey(f, tmp_key, ""); |
---|
| 730 | |
---|
| 731 | for(j=0; j < tmp_key->num_values; j++) |
---|
| 732 | { |
---|
| 733 | tmp_value = tmp_key->values[j]; |
---|
| 734 | if(tmp_value != NULL) |
---|
| 735 | printValue(f, tmp_value, tmp_key->keyname); |
---|
| 736 | } |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | /* Print out orphaned values */ |
---|
| 740 | for(i=0; i < range_list_size(unalloc_values); i++) |
---|
| 741 | { |
---|
| 742 | cur_elem = range_list_get(unalloc_values, i); |
---|
| 743 | tmp_value = (REGF_VK_REC*)cur_elem->data; |
---|
| 744 | |
---|
| 745 | printValue(f, tmp_value, ""); |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | if(print_leftover) |
---|
| 749 | { |
---|
| 750 | for(i=0; i < range_list_size(unalloc_cells); i++) |
---|
| 751 | { |
---|
| 752 | cur_elem = range_list_get(unalloc_cells, i); |
---|
| 753 | printf("0x%X,%d,", cur_elem->offset, cur_elem->length); |
---|
| 754 | |
---|
| 755 | printCell(f, cur_elem->offset); |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | /* |
---|
| 760 | printf("Analyzing test_offset...\n"); |
---|
| 761 | if((tmp_key = regfi_parse_nk(f, test_offset, 4096, false)) != NULL) |
---|
| 762 | regfi_print_nk(tmp_key); |
---|
| 763 | else |
---|
| 764 | dump_cell(f->fd, test_offset); |
---|
| 765 | */ |
---|
| 766 | |
---|
| 767 | return 0; |
---|
| 768 | } |
---|