source: trunk/src/reglookup-recover.c @ 151

Last change on this file since 151 was 151, checked in by tim, 15 years ago

parsed more items from regf header structure

improved feedback from regfi_parse_data

  • Property svn:keywords set to Id
File size: 24.4 KB
Line 
1/*
2 * This program attempts to recover deleted data structures in a registry hive.
3 *
4 * Copyright (C) 2008-2009 Timothy D. Morgan
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 3 of the License.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * $Id: reglookup-recover.c 151 2009-03-04 21:14:09Z tim $
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24
25#include "talloc.h"
26#include "regfi.h"
27#include "range_list.h"
28#include "lru_cache.h"
29
30
31/* Globals, influenced by command line parameters */
32bool print_verbose = false;
33bool print_security = false;
34bool print_header = true;
35bool print_leftover = false;
36bool print_parsedraw = false;
37char* registry_file = NULL;
38
39#include "common.c"
40
41
42char* getQuotedData(int fd, uint32 offset, uint32 length)
43{
44  uint8* buf;
45  char* quoted_buf;
46  uint32 len;
47
48  if((lseek(fd, offset, SEEK_SET)) == -1)
49    return NULL;
50
51  buf = (uint8*)malloc(length);
52  if(buf == NULL)
53    return NULL;
54
55  len = length;
56  if((regfi_read(fd, buf, &length) != 0) || length != len)
57  {
58    free(buf);
59    return NULL;
60  }
61
62  quoted_buf = quote_buffer(buf, length, common_special_chars);
63  free(buf);
64
65  return quoted_buf;
66}
67
68
69void printKey(REGFI_FILE* f, REGFI_NK_REC* nk, const char* prefix)
70{
71  char mtime[20];
72  time_t tmp_time[1];
73  struct tm* tmp_time_s = NULL;
74  char* quoted_name = NULL;
75  char* quoted_raw = "";
76
77  *tmp_time = nt_time_to_unix(&nk->mtime);
78  tmp_time_s = gmtime(tmp_time);
79  strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
80
81  quoted_name = quote_string(nk->keyname, key_special_chars);
82  if (quoted_name == NULL)
83  {
84    quoted_name = malloc(1*sizeof(char));
85    if(quoted_name == NULL)
86      bailOut(REGLOOKUP_EXIT_OSERR, 
87              "ERROR: Could not allocate sufficient memory.\n");
88    quoted_name[0] = '\0';
89
90    fprintf(stderr, "WARN: NULL key name in NK record at offset %.8X.\n",
91            nk->offset);
92  }
93
94  if(print_parsedraw)
95    quoted_raw = getQuotedData(f->fd, nk->offset, nk->cell_size);
96
97  printf("%.8X,%.8X,KEY,%s,%s,%s,%d,,,,,,,,%s\n", nk->offset, nk->cell_size,
98         prefix, quoted_name, mtime, nk->num_values, quoted_raw);
99 
100  if(print_parsedraw)
101    free(quoted_raw);
102  free(quoted_name);
103}
104
105
106void printValue(REGFI_FILE* f, const REGFI_VK_REC* vk, const char* prefix)
107{
108  char* quoted_value = NULL;
109  char* quoted_name = NULL;
110  char* quoted_raw = "";
111  char* conv_error = NULL;
112  const char* str_type = NULL;
113  uint32 size = vk->data_size;
114
115  /* Microsoft's documentation indicates that "available memory" is
116   * the limit on value sizes.  Annoying.  We limit it to 1M which
117   * should rarely be exceeded, unless the file is corrupt or
118   * malicious. For more info, see:
119   *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
120   */
121  /* XXX: Should probably do something different here for this tool.
122   *      Also, It would be really nice if this message somehow included the
123   *      name of the current value we're having trouble with, since
124   *      stderr/stdout don't always sync nicely.
125   */
126  if(size > REGFI_VK_MAX_DATA_LENGTH)
127  {
128    fprintf(stderr, "WARN: value data size %d larger than "
129            "%d, truncating...\n", size, REGFI_VK_MAX_DATA_LENGTH);
130    size = REGFI_VK_MAX_DATA_LENGTH;
131  }
132 
133  quoted_name = quote_string(vk->valuename, key_special_chars);
134  if (quoted_name == NULL)
135  { /* Value names are NULL when we're looking at the "(default)" value.
136     * Currently we just return a 0-length string to try an eliminate
137     * ambiguity with a literal "(default)" value.  The data type of a line
138     * in the output allows one to differentiate between the parent key and
139     * this value.
140     */
141    quoted_name = malloc(1*sizeof(char));
142    if(quoted_name == NULL)
143      bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not allocate sufficient memory.\n");
144    quoted_name[0] = '\0';
145  }
146
147  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
148  if(quoted_value == NULL)
149  {
150    quoted_value = malloc(1*sizeof(char));
151    if(quoted_value == NULL)
152      bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not allocate sufficient memory.\n");
153    quoted_value[0] = '\0';
154
155    if(conv_error == NULL)
156      fprintf(stderr, "WARN: Could not quote value for '%s/%s'.  "
157              "Memory allocation failure likely.\n", prefix, quoted_name);
158    else if(print_verbose)
159      fprintf(stderr, "WARN: Could not quote value for '%s/%s'.  "
160              "Returned error: %s\n", prefix, quoted_name, conv_error);
161  }
162  /* XXX: should these always be printed? */
163  else if(conv_error != NULL && print_verbose)
164    fprintf(stderr, "INFO: While quoting value for '%s/%s', "
165            "warning returned: %s\n", prefix, quoted_name, conv_error);
166
167
168  if(print_parsedraw)
169    quoted_raw = getQuotedData(f->fd, vk->offset, vk->cell_size);
170
171  str_type = regfi_type_val2str(vk->type);
172  if(str_type == NULL)
173    printf("%.8X,%.8X,VALUE,%s,%s,,,0x%.8X,%s,%d,,,,,%s\n", 
174           vk->offset, vk->cell_size, prefix, quoted_name, 
175           vk->type, quoted_value, vk->data_size, quoted_raw);
176  else
177    printf("%.8X,%.8X,VALUE,%s,%s,,,%s,%s,%d,,,,,%s\n", 
178           vk->offset, vk->cell_size, prefix, quoted_name, 
179           str_type, quoted_value, vk->data_size, quoted_raw);
180
181  if(print_parsedraw)
182    free(quoted_raw);
183  if(quoted_value != NULL)
184    free(quoted_value);
185  if(quoted_name != NULL)
186    free(quoted_name);
187  if(conv_error != NULL)
188    free(conv_error);
189}
190
191
192void printSK(REGFI_FILE* f, REGFI_SK_REC* sk)
193{
194  char* quoted_raw = NULL;
195  char* empty_str = "";
196  char* owner = regfi_get_owner(sk->sec_desc);
197  char* group = regfi_get_group(sk->sec_desc);
198  char* sacl = regfi_get_sacl(sk->sec_desc);
199  char* dacl = regfi_get_dacl(sk->sec_desc);
200
201  if(print_parsedraw)
202    quoted_raw = getQuotedData(f->fd, sk->offset, sk->cell_size);
203
204  if(owner == NULL)
205    owner = empty_str;
206  if(group == NULL)
207    group = empty_str;
208  if(sacl == NULL)
209    sacl = empty_str;
210  if(dacl == NULL)
211    dacl = empty_str;
212
213  printf("%.8X,%.8X,SK,,,,,,,,%s,%s,%s,%s,%s\n", sk->offset, sk->cell_size,
214         owner, group, sacl, dacl, quoted_raw);
215 
216  if(owner != empty_str)
217    free(owner);
218  if(group != empty_str)
219    free(group);
220  if(sacl != empty_str)
221    free(sacl);
222  if(dacl != empty_str)
223    free(dacl);
224
225  if(print_parsedraw)
226    free(quoted_raw);
227}
228
229
230int printCell(REGFI_FILE* f, uint32 offset)
231{
232  char* quoted_buf;
233  uint32 cell_length;
234  bool unalloc;
235
236  if(!regfi_parse_cell(f->fd, offset, NULL, 0, &cell_length, &unalloc))
237    return 1;
238
239  quoted_buf = getQuotedData(f->fd, offset, cell_length);
240  if(quoted_buf == NULL)
241    return 2;
242
243  printf("%.8X,%.8X,RAW,,,,,,,,,,,,%s\n", offset, cell_length, quoted_buf);
244
245  free(quoted_buf);
246  return 0;
247}
248
249
250/* This function returns a properly quoted parent path or partial parent
251 * path for a given key.  Returns NULL on error, "" if no path was available.
252 * Paths returned must be free()d.
253 */
254/* XXX: This is not terribly efficient, as it may reparse many keys
255 *      repeatedly.  Should try to add caching.
256 */
257char* getParentPath(REGFI_FILE* f, REGFI_NK_REC* nk)
258{
259  void_stack* path_stack = void_stack_new(REGFI_MAX_DEPTH);
260  const REGFI_HBIN* hbin;
261  REGFI_NK_REC* cur_ancestor;
262  char* ret_val;
263  uint32 virt_offset, i, stack_size, ret_val_size, ret_val_used;
264  uint32 max_length;
265  REGFI_BUFFER* path_element;
266 
267  /* The path_stack size limit should guarantee that we don't recurse forever. */
268  virt_offset = nk->parent_off;
269  ret_val_size = 1; /* NUL */
270  while(virt_offset != REGFI_OFFSET_NONE)
271  { 
272    hbin = regfi_lookup_hbin(f, virt_offset);
273    if(hbin == NULL)
274      virt_offset = REGFI_OFFSET_NONE;
275    else
276    {
277      max_length = hbin->block_size + hbin->file_off
278        - (virt_offset+REGFI_REGF_SIZE);
279      cur_ancestor = regfi_parse_nk(f, virt_offset+REGFI_REGF_SIZE, 
280                                    max_length, true);
281      printMsgs(f);
282
283      if(cur_ancestor == NULL)
284        virt_offset = REGFI_OFFSET_NONE;
285      else
286      {
287        if((cur_ancestor->key_type == REGFI_NK_TYPE_ROOTKEY1) 
288           || (cur_ancestor->key_type == REGFI_NK_TYPE_ROOTKEY2))
289          virt_offset = REGFI_OFFSET_NONE;
290        else
291          virt_offset = cur_ancestor->parent_off;
292       
293        path_element = talloc(path_stack, REGFI_BUFFER);
294        if(path_element != NULL)
295          path_element->buf = (uint8*)quote_string(cur_ancestor->keyname, 
296                                                   key_special_chars);
297         
298        if(path_element == NULL || path_element->buf == NULL 
299           || !void_stack_push(path_stack, path_element))
300        {
301          /* XXX: Need to add a warning here */
302          regfi_free_key(cur_ancestor);
303          void_stack_free(path_stack);
304          return NULL;
305        }
306
307        /* Path element and preceeding delimiter
308         * Note that this integer can't overflow since key name lengths are
309         * 16 bits and the max depth is 512.
310         */
311        path_element->len = strlen((char*)path_element->buf);
312        ret_val_size += path_element->len + 1;
313
314        regfi_free_key(cur_ancestor);
315      }
316    }
317  }
318 
319  stack_size = void_stack_size(path_stack);
320  ret_val_used = 0;
321  ret_val = malloc(ret_val_size);
322  if(ret_val == NULL)
323  {
324    void_stack_free(path_stack);
325    return NULL;
326  }
327  ret_val[0] = '\0';
328
329  for(i=0; i<stack_size; i++)
330  {
331    path_element = void_stack_pop(path_stack);
332    snprintf(ret_val+ret_val_used, ret_val_size-ret_val_used, 
333             "/%s", path_element->buf);
334    ret_val_used += path_element->len + 1;
335    free(path_element->buf);
336    talloc_free(path_element);
337  }
338  void_stack_free(path_stack);
339
340  return ret_val;
341}
342
343
344static void usage(void)
345{
346  fprintf(stderr, "Usage: reglookup-recover [options] <REGISTRY_FILE>\n");
347  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
348  fprintf(stderr, "Options:\n");
349  fprintf(stderr, "\t-v\t sets verbose mode.\n");
350  fprintf(stderr, "\t-h\t enables header row. (default)\n");
351  fprintf(stderr, "\t-H\t disables header row.\n");
352  fprintf(stderr, "\t-l\t enables leftover(raw) cell output.\n");
353  fprintf(stderr, "\t-L\t disables leftover(raw) cell output. (default)\n");
354  fprintf(stderr, "\t-r\t enables raw cell output for parsed cells.\n");
355  fprintf(stderr, "\t-R\t disables raw cell output for parsed cells. (default)\n");
356  fprintf(stderr, "\n");
357}
358
359
360bool removeRange(range_list* rl, uint32 offset, uint32 length)
361{
362  int32 rm_idx;
363  const range_list_element* cur_elem;
364
365  rm_idx = range_list_find(rl, offset);
366  if(rm_idx < 0)
367  {
368    fprintf(stderr, "DEBUG: removeRange: rm_idx < 0; (%d)\n", rm_idx);
369    return false;
370  }
371
372  cur_elem = range_list_get(rl, rm_idx);
373  if(cur_elem == NULL)
374  {
375    fprintf(stderr, "DEBUG: removeRange: cur_elem == NULL.  rm_idx=%d\n", rm_idx);
376    return false;
377  }
378
379  if(offset > cur_elem->offset)
380  {
381    if(!range_list_split_element(rl, rm_idx, offset))
382    {
383      fprintf(stderr, "DEBUG: removeRange: first split failed\n");
384      return false;
385    }
386    rm_idx++;
387    cur_elem = range_list_get(rl, rm_idx);
388    if(cur_elem == NULL)
389    {
390      fprintf(stderr, 
391              "DEBUG: removeRange: cur_elem == NULL after first split.  rm_idx=%d\n",
392              rm_idx);
393      return false;
394    }
395  }
396 
397  if(offset+length < cur_elem->offset+cur_elem->length)
398  {
399    if(!range_list_split_element(rl, rm_idx, offset+length))
400    {
401      fprintf(stderr, "DEBUG: removeRange: second split failed\n");
402      return false;
403    }
404  }
405 
406  if(!range_list_remove(rl, rm_idx))
407  {
408    fprintf(stderr, "DEBUG: removeRange: remove failed\n");
409    return false;
410  }
411
412  return true;
413}
414
415
416int extractVKs(REGFI_FILE* f,
417               range_list* unalloc_cells,
418               range_list* unalloc_values)
419{
420  const range_list_element* cur_elem;
421  REGFI_VK_REC* vk;
422  uint32 i, j;
423
424  for(i=0; i < range_list_size(unalloc_cells); i++)
425  {
426    printMsgs(f);
427    cur_elem = range_list_get(unalloc_cells, i);
428    for(j=0; j <= cur_elem->length; j+=8)
429    {
430      vk = regfi_parse_vk(f, cur_elem->offset+j, 
431                           cur_elem->length-j, false);
432      printMsgs(f);
433
434      if(vk != NULL)
435      {
436        if(!range_list_add(unalloc_values, vk->offset,
437                           vk->cell_size, vk))
438        {
439          fprintf(stderr, "ERROR: Couldn't add value to unalloc_values.\n");
440          return 20;
441        }
442        j+=vk->cell_size-8;
443      }
444    }
445  }
446
447  /* Remove value ranges from the unalloc_cells before we continue. */
448  for(i=0; i<range_list_size(unalloc_values); i++)
449  {
450    cur_elem = range_list_get(unalloc_values, i);
451    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
452      return 30;
453  }
454
455  return 0;
456}
457
458
459int extractDataCells(REGFI_FILE* f,
460                     range_list* unalloc_cells,
461                     range_list* unalloc_values)
462{
463  const range_list_element* cur_elem;
464  REGFI_VK_REC* vk;
465  const REGFI_HBIN* hbin;
466  REGFI_BUFFER data;
467  uint32 i, off, data_offset, data_maxsize;
468
469  for(i=0; i<range_list_size(unalloc_values); i++)
470  {
471    cur_elem = range_list_get(unalloc_values, i);
472    vk = (REGFI_VK_REC*)cur_elem->data;
473    if(vk == NULL)
474      return 40;
475
476    if(vk->data_size == 0)
477      vk->data = NULL;
478    else
479    {
480      off = vk->data_off+REGFI_REGF_SIZE;
481
482      if(vk->data_in_offset)
483      {
484        data = regfi_parse_data(f, vk->type, vk->data_off,
485                                vk->data_size, 4,
486                                vk->data_in_offset, false);
487        vk->data = data.buf;
488        vk->data_size = data.len;
489      }
490      else if(range_list_has_range(unalloc_cells, off, vk->data_size))
491      {
492        hbin = regfi_lookup_hbin(f, vk->data_off);
493        if(hbin)
494        {
495          data_offset = vk->data_off+REGFI_REGF_SIZE;
496          data_maxsize = hbin->block_size + hbin->file_off - data_offset;
497          data = regfi_parse_data(f, vk->type, data_offset, 
498                                  vk->data_size, data_maxsize, 
499                                  vk->data_in_offset, false);
500          vk->data = data.buf;
501          vk->data_size = data.len;
502
503          if(vk->data != NULL)
504          {
505            /* XXX: This strict checking prevents partial recovery of data
506             *      cells.  Also, see code for regfi_parse_data and note that
507             *      lengths indicated in VK records are sometimes just plain
508             *      wrong.  Need a feedback mechanism to be more fuzzy with
509             *      data cell lengths and the ranges removed.
510             *
511             *      The introduction of REGFI_BUFFER in regfi_parse_data has
512             *      fixed some of this.  Should review again with respect to
513             *      the other issues mentioned above though.
514             */
515            /* A data record was recovered. Remove from unalloc_cells. */
516            if(!removeRange(unalloc_cells, off, vk->data_size))
517              return 50;
518          }
519        }
520        else
521          vk->data = NULL;
522      }
523    }
524  }
525
526  return 0;
527}
528
529
530/* NOTE: unalloc_keys should be an empty range_list. */
531int extractKeys(REGFI_FILE* f, 
532                range_list* unalloc_cells, 
533                range_list* unalloc_keys)
534{
535  const range_list_element* cur_elem;
536  REGFI_NK_REC* key;
537  uint32 i, j;
538  int error_code = 0;
539
540  for(i=0; i < range_list_size(unalloc_cells); i++)
541  {
542    printMsgs(f);
543    cur_elem = range_list_get(unalloc_cells, i);
544    for(j=0; cur_elem->length > REGFI_NK_MIN_LENGTH
545          && j <= cur_elem->length-REGFI_NK_MIN_LENGTH; j+=8)
546    {
547      key = regfi_parse_nk(f, cur_elem->offset+j,
548                           cur_elem->length-j, false);
549      printMsgs(f);
550
551      if(key != NULL)
552      {
553        if(!range_list_add(unalloc_keys, key->offset, 
554                           key->cell_size, key))
555        {
556          fprintf(stderr, "ERROR: Couldn't add key to unalloc_keys.\n");
557          error_code = 20;
558          goto fail;
559        }
560        talloc_steal(unalloc_keys, key);
561        j+=key->cell_size-8;
562      }
563    }
564  }
565
566  for(i=0; i<range_list_size(unalloc_keys); i++)
567  {
568    cur_elem = range_list_get(unalloc_keys, i);
569    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
570    {
571      error_code = 30;
572      goto fail;
573    }
574  }
575
576  return 0;
577
578 fail:
579  regfi_free_key(key);
580  return error_code;
581}
582
583int extractValueLists(REGFI_FILE* f,
584                      range_list* unalloc_cells,
585                      range_list* unalloc_keys,
586                      range_list* unalloc_linked_values)
587{
588  REGFI_NK_REC* nk;
589  REGFI_VK_REC* vk;
590  const REGFI_HBIN* hbin;
591  const range_list_element* cur_elem;
592  uint32 i, j, num_keys, off, values_length, max_length;
593
594  num_keys=range_list_size(unalloc_keys);
595  for(i=0; i<num_keys; i++)
596  {
597    cur_elem = range_list_get(unalloc_keys, i);
598    if(cur_elem == NULL)
599      return 10;
600    nk = cur_elem->data;
601
602    if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE))
603    {
604      hbin = regfi_lookup_hbin(f, nk->values_off);
605     
606      if(hbin != NULL)
607      {
608        off = nk->values_off + REGFI_REGF_SIZE;
609        max_length = hbin->block_size + hbin->file_off - off;
610        nk->values = regfi_load_valuelist(f, off, nk->num_values, 
611                                          max_length, false);
612        if(nk->values != NULL && nk->values->elements != NULL)
613        {
614          /* Number of elements in the value list may be shorter than advertised
615           * by NK record due to cell truncation.  We'll consider this valid and
616           * only throw out the whole value list if it bleeds into an already
617           * parsed structure.
618           */
619          values_length = (nk->values->num_values+1)*sizeof(uint32);
620          if(values_length != (values_length & 0xFFFFFFF8))
621            values_length = (values_length & 0xFFFFFFF8) + 8;
622
623          if(!range_list_has_range(unalloc_cells, off, values_length))
624          { /* We've parsed a values-list which isn't in the unallocated list,
625             * so prune it.
626             */
627            talloc_free(nk->values);
628            nk->values = NULL;
629          }
630          else
631          { /* Values-list was recovered.  Remove from unalloc_cells and
632             * inspect values.
633             */
634            if(!removeRange(unalloc_cells, off, values_length))
635              return 20;
636
637            for(j=0; j < nk->values->num_values; j++)
638            {
639              /* Don't bother to restrict cell length here, since we'll
640               * check our unalloc_cells range_list later.
641               */
642              vk = regfi_parse_vk(f, nk->values->elements[j]+REGFI_REGF_SIZE,
643                                  0x7FFFFFFF, false);
644              printMsgs(f);
645             
646              if(vk != NULL)
647              {
648                if(range_list_has_range(unalloc_cells, 
649                                        vk->offset, vk->cell_size))
650                {
651                  if(!range_list_add(unalloc_linked_values, vk->offset,
652                                     vk->cell_size, vk))
653                  {
654                    talloc_free(vk);
655                    return 30;
656                  }
657
658                  if(!removeRange(unalloc_cells, vk->offset, vk->cell_size))
659                    return 40;
660                }
661                else
662                  talloc_free(vk);
663              }
664            }
665          }
666        }
667      }
668    }
669  }
670
671  return 0;
672}
673
674
675
676/* NOTE: unalloc_sks should be an empty range_list. */
677int extractSKs(REGFI_FILE* f, 
678               range_list* unalloc_cells,
679               range_list* unalloc_sks)
680{
681  const range_list_element* cur_elem;
682  REGFI_SK_REC* sk;
683  uint32 i, j;
684
685  for(i=0; i < range_list_size(unalloc_cells); i++)
686  {
687    printMsgs(f);
688    cur_elem = range_list_get(unalloc_cells, i);
689    for(j=0; j <= cur_elem->length; j+=8)
690    {
691      sk = regfi_parse_sk(f, cur_elem->offset+j, 
692                          cur_elem->length-j, false);
693      printMsgs(f);
694
695      if(sk != NULL)
696      {
697        if(!range_list_add(unalloc_sks, sk->offset,
698                           sk->cell_size, sk))
699        {
700          fprintf(stderr, "ERROR: Couldn't add sk to unalloc_sks.\n");
701          return 20;
702        }
703        talloc_steal(unalloc_sks, sk);
704        j+=sk->cell_size-8;
705      }
706    }
707  }
708
709  for(i=0; i<range_list_size(unalloc_sks); i++)
710  {
711    cur_elem = range_list_get(unalloc_sks, i);
712    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
713      return 30;
714  }
715
716  return 0;
717}
718
719
720int main(int argc, char** argv)
721{ 
722  REGFI_FILE* f;
723  const range_list_element* cur_elem;
724  range_list* unalloc_cells;
725  range_list* unalloc_keys;
726  range_list* unalloc_linked_values;
727  range_list* unalloc_values;
728  range_list* unalloc_sks;
729  char** parent_paths;
730  char* tmp_name;
731  char* tmp_path;
732  REGFI_NK_REC* tmp_key;
733  REGFI_VK_REC* tmp_value;
734  uint32 argi, arge, i, j, ret, num_unalloc_keys;
735 
736  /* Process command line arguments */
737  if(argc < 2)
738  {
739    usage();
740    bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n");
741  }
742 
743  arge = argc-1;
744  for(argi = 1; argi < arge; argi++)
745  {
746    if (strcmp("-v", argv[argi]) == 0)
747      print_verbose = true;
748    else if (strcmp("-h", argv[argi]) == 0)
749      print_header = true;
750    else if (strcmp("-H", argv[argi]) == 0)
751      print_header = false;
752    else if (strcmp("-l", argv[argi]) == 0)
753      print_leftover = true;
754    else if (strcmp("-L", argv[argi]) == 0)
755      print_leftover = false;
756    else if (strcmp("-r", argv[argi]) == 0)
757      print_parsedraw = true;
758    else if (strcmp("-R", argv[argi]) == 0)
759      print_parsedraw = false;
760    else
761    {
762      usage();
763      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
764      bailOut(REGLOOKUP_EXIT_USAGE, "");
765    }
766  }
767  /*test_offset = strtol(argv[argi++], NULL, 16);*/
768
769  if((registry_file = strdup(argv[argi])) == NULL)
770    bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n");
771
772  f = regfi_open(registry_file);
773  if(f == NULL)
774  {
775    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
776    bailOut(REGLOOKUP_EXIT_NOINPUT, "");
777  }
778  if(print_verbose)
779    regfi_set_message_mask(f, REGFI_MSG_ERROR|REGFI_MSG_WARN|REGFI_MSG_INFO);
780  else
781    regfi_set_message_mask(f, REGFI_MSG_ERROR);
782
783  if(print_header)
784    printf("OFFSET,REC_LENGTH,REC_TYPE,PATH,NAME,"
785           "NK_MTIME,NK_NVAL,VK_TYPE,VK_VALUE,VK_DATA_LEN,"
786           "SK_OWNER,SK_GROUP,SK_SACL,SK_DACL,RAW_CELL\n");
787
788  unalloc_cells = regfi_parse_unalloc_cells(f);
789  if(unalloc_cells == NULL)
790  {
791    fprintf(stderr, "ERROR: Could not obtain list of unallocated cells.\n");
792    return 1;
793  }
794
795  unalloc_keys = range_list_new();
796  if(unalloc_keys == NULL)
797    return 10;
798
799  unalloc_linked_values = range_list_new();
800  if(unalloc_linked_values == NULL)
801    return 10;
802
803  unalloc_values = range_list_new();
804  if(unalloc_values == NULL)
805    return 10;
806
807  unalloc_sks = range_list_new();
808  if(unalloc_sks == NULL)
809    return 10;
810
811  ret = extractKeys(f, unalloc_cells, unalloc_keys);
812  if(ret != 0)
813  {
814    fprintf(stderr, "ERROR: extractKeys() failed with %d.\n", ret);
815    return ret;
816  }
817
818  ret = extractValueLists(f, unalloc_cells, unalloc_keys,unalloc_linked_values);
819  if(ret != 0)
820  {
821    fprintf(stderr, "ERROR: extractValueLists() failed with %d.\n", ret);
822    return ret;
823  }
824
825  /* Carve any orphan values */
826  ret = extractVKs(f, unalloc_cells, unalloc_values);
827  if(ret != 0)
828  {
829    fprintf(stderr, "ERROR: extractVKs() failed with %d.\n", ret);
830    return ret;
831  }
832
833  /* Carve any data associated with VK records */
834  ret = extractDataCells(f, unalloc_cells, unalloc_linked_values);
835  if(ret != 0)
836  {
837    fprintf(stderr, "ERROR: extractDataCells() failed with %d.\n", ret);
838    return ret;
839  }
840  ret = extractDataCells(f, unalloc_cells, unalloc_values);
841  if(ret != 0)
842  {
843    fprintf(stderr, "ERROR: extractDataCells() failed with %d.\n", ret);
844    return ret;
845  }
846 
847  /* Carve any SK records */
848  ret = extractSKs(f, unalloc_cells, unalloc_sks);
849  if(ret != 0)
850  {
851    fprintf(stderr, "ERROR: extractSKs() failed with %d.\n", ret);
852    return ret;
853  }
854
855  /* Now that we're done carving, associate recovered keys with parents,
856   * if at all possible.
857   */
858  num_unalloc_keys = range_list_size(unalloc_keys);
859  parent_paths = (char**)malloc(sizeof(char*)*num_unalloc_keys);
860  if(parent_paths == NULL)
861    return 10;
862
863  for(i=0; i < num_unalloc_keys; i++)
864  {
865    cur_elem = range_list_get(unalloc_keys, i);
866    tmp_key = (REGFI_NK_REC*)cur_elem->data;
867
868    if(tmp_key == NULL)
869      return 20;
870   
871    parent_paths[i] = getParentPath(f, tmp_key);
872    if(parent_paths[i] == NULL)
873      return 20;
874  }
875 
876  /* Now start the output */
877  for(i=0; i < num_unalloc_keys; i++)
878  {
879    cur_elem = range_list_get(unalloc_keys, i);
880    tmp_key = (REGFI_NK_REC*)cur_elem->data;
881
882    printKey(f, tmp_key, parent_paths[i]);
883    if(tmp_key->num_values > 0 && tmp_key->values != NULL)
884    {
885      tmp_name = quote_string(tmp_key->keyname, key_special_chars);
886      tmp_path = (char*)malloc(strlen(parent_paths[i])+strlen(tmp_name)+2);
887      if(tmp_path == NULL)
888      {
889        free(tmp_name);
890        return 10;
891      }
892
893      sprintf(tmp_path, "%s/%s", parent_paths[i], tmp_name);
894      for(j=0; j < tmp_key->values->num_values; j++)
895      {
896        tmp_value = 
897          (REGFI_VK_REC*)range_list_find_data(unalloc_linked_values, 
898                                              tmp_key->values->elements[j]
899                                              + REGFI_REGF_SIZE);
900        if(tmp_value != NULL)
901          printValue(f, tmp_value, tmp_path);
902      }
903      free(tmp_path);
904      free(tmp_name);
905      free(parent_paths[i]);
906    }
907  }
908  free(parent_paths);
909
910  /* Print out orphaned values */
911  for(i=0; i < range_list_size(unalloc_values); i++)
912  {
913    cur_elem = range_list_get(unalloc_values, i);
914    tmp_value = (REGFI_VK_REC*)cur_elem->data; 
915
916    printValue(f, tmp_value, "");
917  }
918 
919  if(print_leftover)
920  {
921    for(i=0; i < range_list_size(unalloc_cells); i++)
922    {
923      cur_elem = range_list_get(unalloc_cells, i);
924      printCell(f, cur_elem->offset);
925    }
926  }
927
928  range_list_free(unalloc_cells);
929  range_list_free(unalloc_keys);
930  range_list_free(unalloc_linked_values);
931  range_list_free(unalloc_values);
932  range_list_free(unalloc_sks);
933
934  return 0;
935}
Note: See TracBrowser for help on using the repository browser.