source: trunk/include/regfi.h @ 232

Last change on this file since 232 was 232, checked in by tim, 13 years ago

added a convenience openHive function in pyregfi
standardized static function names
improvements to scons release script and library versioning

  • Property svn:keywords set to Id
File size: 47.8 KB
Line 
1/*
2 * Copyright (C) 2005-2010 Timothy D. Morgan
3 * Copyright (C) 2010 Michael Cohen
4 * Copyright (C) 2005 Gerald (Jerry) Carter
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: regfi.h 232 2011-04-28 16:45:10Z tim $
20 */
21
22/**
23 * @file
24 * Windows NT (and later) read-only registry library
25 *
26 * This library is intended for use in digital forensics investigations, but
27 * is likely useful in other applications.
28 *
29 * Branched from Samba project Subversion repository, version #6903:
30 *   http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/regfio.h?rev=6903&view=auto
31 *
32 * Since then, it has been heavily rewritten, simplified, and improved.
33 */
34
35/**
36 * @mainpage Home
37 *
38 * The regfi library is a read-only NT registry library which serves as the main
39 * engine behind the reglookup tool.  It is designed with digital forensic
40 * analysis in mind, but it should also be useful in other tools which need to
41 * efficiently traverse and query registry data structures.
42 *
43 * The library is broken down into four main parts, the
44 * @ref regfiBase "Base Layer", which any code dependent on the library will
45 * likely need to rely on, as well as three main functional layers:
46 * @li @ref regfiIteratorLayer
47 * @li @ref regfiGlueLayer
48 * @li @ref regfiParseLayer
49 *
50 * Most users will find that a combination of the Base Layer and the Iterator Layer
51 * will be sufficient for accessing registry hive files.  Those who are willing
52 * to dive deep into registry data structures, for instance to recover deleted
53 * data structures or to research Windows registry behavior in detail, will
54 * find the Parse Layer to be quite useful.
55 */
56
57
58#ifndef _REGFI_H
59#define _REGFI_H
60
61#include <stdlib.h>
62#include <stdio.h>
63#include <stdbool.h>
64#include <string.h>
65#include <errno.h>
66#include <time.h>
67#include <fcntl.h>
68#include <sys/stat.h>
69#include <sys/types.h>
70#include <unistd.h>
71#include <iconv.h>
72#include <pthread.h>
73#include <talloc.h>
74
75/* regfi headers */
76#include <byteorder.h>
77#include <winsec.h>
78#include <void_stack.h>
79#include <range_list.h>
80#include <lru_cache.h>
81
82/* Library version can be overridden at build time */
83#ifndef REGFI_VERSION
84#define REGFI_VERSION "trunk"
85#endif
86
87/* GCC-specific macro for library exports */
88#ifdef _EXPORT
89#undef _EXPORT
90#endif
91#ifdef REGFI_WIN32
92#define _EXPORT __declspec(dllexport)
93#else
94#define _EXPORT __attribute__((visibility("default")))
95#endif
96
97#ifndef EOVERFLOW
98# define EOVERFLOW E2BIG
99#endif
100
101/******************************************************************************/
102/* Constants for use while interacting with the library                       */
103/******************************************************************************/
104
105/* regfi library error message types */
106#define REGFI_LOG_INFO  0x0001
107#define REGFI_LOG_WARN  0x0004
108#define REGFI_LOG_ERROR 0x0010
109#define REGFI_DEFAULT_LOG_MASK REGFI_LOG_ERROR|REGFI_LOG_WARN
110
111/* regfi library supported character encodings */
112/* UTF16LE is not supported for output */
113typedef enum {
114  REGFI_ENCODING_DEFAULT  = 0,
115  REGFI_ENCODING_ASCII =   0,
116  REGFI_ENCODING_UTF8  =  1,
117  REGFI_ENCODING_UTF16LE = 2,
118  REGFI_NUM_ENCODINGS  =  3
119} REGFI_ENCODING;
120
121/* Registry data types */
122typedef enum {
123  REG_NONE                   =    0,
124  REG_SZ                     =    1,
125  REG_EXPAND_SZ              =    2,
126  REG_BINARY                 =    3,
127  REG_DWORD                  =    4,
128  REG_DWORD_LE               =    4 , /* DWORD, little endian */
129  REG_DWORD_BE               =    5 , /* DWORD, big endian */
130  REG_LINK                   =    6,
131  REG_MULTI_SZ               =    7,
132  REG_RESOURCE_LIST          =    8,
133  REG_FULL_RESOURCE_DESCRIPTOR=   9,
134  REG_RESOURCE_REQUIREMENTS_LIST= 10,
135  REG_QWORD                     = 11, /* 64-bit little endian */
136/* XXX: Has MS defined a REG_QWORD_BE? */
137/* Not a real type in the registry */
138  REG_KEY                 =   0x7FFFFFFF
139} REGFI_DATA_TYPE;
140#define REGFI_OFFSET_NONE          0xffffffff
141
142
143
144/******************************************************************************/
145/* Various resource limits and related constants                              */
146/******************************************************************************/
147
148/* Flags determining whether or not to cache various record types internally */
149#define REGFI_CACHE_SK             0
150
151/* This maximum depth is described here:
152 * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
153 */
154#define REGFI_MAX_DEPTH            512
155
156/* This limit defines the maximum number of levels deep that ri subkey list
157 * trees can go.
158 */
159/* XXX: This is totally arbitrary right now.
160 *      The actual limit may need to be discovered by experimentation.
161 */
162#define REGFI_MAX_SUBKEY_DEPTH     255
163
164
165/******************************************************************************/
166/* Symbols for internal use                                                   */
167/******************************************************************************/
168
169/* Global thread-local storage key */
170pthread_key_t regfi_log_key;
171
172/* Header sizes and magic number lengths for various records */
173#define REGFI_HBIN_ALLOC           0x1000 /* Minimum allocation unit for HBINs */
174#define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
175#define REGFI_REGF_MAGIC_SIZE      4
176#define REGFI_REGF_NAME_SIZE       64
177#define REGFI_REGF_RESERVED1_SIZE  340
178#define REGFI_REGF_RESERVED2_SIZE  3528
179#define REGFI_HBIN_MAGIC_SIZE      4
180#define REGFI_CELL_MAGIC_SIZE      2
181#define REGFI_HBIN_HEADER_SIZE     0x20
182#define REGFI_NK_MIN_LENGTH        0x4C
183#define REGFI_VK_MIN_LENGTH        0x14
184#define REGFI_SK_MIN_LENGTH        0x14
185#define REGFI_SUBKEY_LIST_MIN_LEN  0x4
186#define REGFI_BIG_DATA_MIN_LENGTH  0xC
187
188
189/* Constants used for validation */
190/* XXX: Can we add clock resolution validation as well as range?  It has
191 *      been reported that Windows timestamps are never more than a
192 *      certain granularity (250ms?), which could be used to help
193 *      eliminate false positives.  Would need to verify this and
194 *      perhaps conservatively implement a check.
195 */
196 /* Minimum time is Jan 1, 1990 00:00:00 */
197#define REGFI_MTIME_MIN_HIGH       0x01B41E6D
198
199 /* Maximum time is Jan 1, 2290 00:00:00
200  * (We hope no one is using Windows by then...)
201  */
202#define REGFI_MTIME_MAX_HIGH       0x03047543
203
204
205/* Flags for the vk records */
206#define REGFI_VK_FLAG_ASCIINAME    0x0001
207#define REGFI_VK_DATA_IN_OFFSET    0x80000000
208#define REGFI_VK_MAX_DATA_LENGTH   1024*1024  /* XXX: This is arbitrary */
209
210
211/* Known key flags */
212/*******************/
213/* These next two show up on normal-seeming keys in Vista and W2K3 registries */
214#define REGFI_NK_FLAG_UNKNOWN1     0x4000
215#define REGFI_NK_FLAG_UNKNOWN2     0x1000
216
217/* This next one shows up in some Vista "software" registries */
218/* XXX: This shows up in the following two SOFTWARE keys in Vista:
219 *   /Wow6432Node/Microsoft
220 *   /Wow6432Node/Microsoft/Cryptography
221 * 
222 * It comes along with UNKNOWN2 and ASCIINAME for a total flags value of 0x10A0
223 */
224#define REGFI_NK_FLAG_UNKNOWN3     0x0080
225
226/* Predefined handle.  Rumor has it that the valuelist count for this key is
227 * where the handle is stored.
228 * http://msdn.microsoft.com/en-us/library/ms724836(VS.85).aspx
229 */
230#define REGFI_NK_FLAG_PREDEF_KEY   0x0040
231
232/* The name will be in ASCII if this next bit is set, otherwise UTF-16LE */
233#define REGFI_NK_FLAG_ASCIINAME    0x0020
234
235/* Symlink key. 
236 * See: http://www.codeproject.com/KB/system/regsymlink.aspx
237 */
238#define REGFI_NK_FLAG_LINK         0x0010
239
240/* This key cannot be deleted */
241#define REGFI_NK_FLAG_NO_RM        0x0008
242
243/* Root of a hive */
244#define REGFI_NK_FLAG_ROOT         0x0004
245
246/* Mount point of another hive.  NULL/(default) value indicates which hive
247 * and where in the hive it points to.
248 */
249#define REGFI_NK_FLAG_HIVE_LINK    0x0002
250
251/* These keys shouldn't be stored on disk, according to:
252 * http://geekswithblogs.net/sdorman/archive/2007/12/24/volatile-registry-keys.aspx
253 */
254#define REGFI_NK_FLAG_VOLATILE     0x0001
255
256/* Useful for identifying unknown flag types */
257#define REGFI_NK_KNOWN_FLAGS       (REGFI_NK_FLAG_PREDEF_KEY\
258                                    | REGFI_NK_FLAG_ASCIINAME\
259                                    | REGFI_NK_FLAG_LINK\
260                                    | REGFI_NK_FLAG_NO_RM\
261                                    | REGFI_NK_FLAG_ROOT\
262                                    | REGFI_NK_FLAG_HIVE_LINK\
263                                    | REGFI_NK_FLAG_VOLATILE\
264                                    | REGFI_NK_FLAG_UNKNOWN1\
265                                    | REGFI_NK_FLAG_UNKNOWN2\
266                                    | REGFI_NK_FLAG_UNKNOWN3)
267
268
269#ifndef CHAR_BIT
270#define CHAR_BIT 8
271#endif
272
273#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
274                    : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
275#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
276#define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
277
278
279
280/******************************************************************************/
281/* Structures                                                                 */
282/******************************************************************************/
283
284typedef struct _regfi_nttime
285{
286  uint32_t low;
287  uint32_t high;
288} REGFI_NTTIME;
289
290
291typedef struct _regfi_log
292{
293  /* Error/warning/info messages returned by lower layer functions */
294  char* messages;
295
296  /* Mask for error message types that will be stored. */
297  uint16_t msg_mask;
298
299} REGFI_LOG;
300
301
302/** HBIN block information
303 * @ingroup regfiMiddleLayer
304 */
305typedef struct _regfi_hbin
306{
307  /** Offset of this HBIN in the registry file */
308  uint32_t file_off;
309
310  /** Number of active records pointing to this block (not used currently) */
311  uint32_t ref_count;
312
313  /** Offset from first hbin block */
314  uint32_t first_hbin_off;
315
316  /** Block size of this block Should be a multiple of 4096 (0x1000) */
317  uint32_t block_size;
318
319  /** Relative offset to next block. 
320   *
321   * @note This value may be unreliable!
322   */
323  uint32_t next_block;
324
325  /** Magic number for the HBIN (should be "hbin"). */
326  uint8_t magic[REGFI_HBIN_MAGIC_SIZE];
327} REGFI_HBIN;
328
329
330/* Subkey List -- list of key offsets and hashed names for consistency */
331typedef struct 
332{
333  /* Virtual offset of NK record or additional subkey list,
334   * depending on this list's type.
335   */
336  uint32_t offset;
337
338  uint32_t hash;
339} REGFI_SUBKEY_LIST_ELEM;
340
341
342/** Subkey-list structure
343 * @ingroup regfiMiddleLayer
344 */
345typedef struct _regfi_subkey_list
346{
347  /* Real offset of this record's cell in the file */
348  uint32_t offset;
349
350  uint32_t cell_size;
351 
352  /* Number of immediate children */
353  uint32_t num_children;
354
355  /* Total number of keys referenced by this list and its children */
356  uint32_t num_keys;
357
358  REGFI_SUBKEY_LIST_ELEM* elements;
359  uint8_t magic[REGFI_CELL_MAGIC_SIZE];
360
361  /* Set if the magic indicates this subkey list points to child subkey lists */
362  bool recursive_type;
363} REGFI_SUBKEY_LIST;
364
365
366typedef uint32_t REGFI_VALUE_LIST_ELEM;
367/** Value-list structure
368 * @ingroup regfiMiddleLayer
369 */
370typedef struct _regfi_value_list
371{
372  /* Real offset of this record's cell in the file */
373  uint32_t offset;
374
375  uint32_t cell_size;
376
377  /* Actual number of values referenced by this list. 
378   * May differ from parent key's num_values if there were parsing errors.
379   */
380  uint32_t num_values;
381
382  REGFI_VALUE_LIST_ELEM* elements;
383} REGFI_VALUE_LIST;
384
385
386/** Class name structure (used in storing SysKeys)
387 * @ingroup regfiBase
388 */
389typedef struct _regfi_classname
390{
391  /** Real offset of this record's cell in the file */
392  uint32_t offset;
393
394  /** As converted to requested REGFI_ENCODING */
395  char* interpreted;
396
397  /** Represents raw buffer read from classname cell.
398   *
399   * Length of this item is specified in the size field.
400   */
401  uint8_t* raw;
402
403  /** Length of the raw data.
404   *
405   * May be shorter than that indicated by parent key.
406   */
407  uint16_t size;
408} REGFI_CLASSNAME;
409
410
411/** Data record structure
412 * @ingroup regfiBase
413 */
414typedef struct _regfi_data
415{
416  /* XXX: this isn't populated yet. Should set it to start of data cell
417   *      or big data cell.
418   */
419  uint32_t offset;
420
421  /** Data type of this data, as indicated by the referencing VK record. */
422  REGFI_DATA_TYPE type;
423
424  /** Length of the raw data. */
425  uint32_t size;
426
427  /** This is always present, representing the raw data cell contents. */
428  uint8_t* raw;
429
430  /** Represents the length of the interpreted value. Meaning is type-specific.
431   *  Will be 0 if interpretation failed for any reason.
432   */
433  uint32_t interpreted_size;
434
435  /** These items represent interpreted versions of the REGFI_DATA::raw field.
436   *
437   * Only use the appropriate member according to the REGFI_DATA::type field.
438   * In the event of an unknown type, use only the REGFI_DATA::raw field.
439   */
440  union _regfi_data_interpreted
441  {
442    /** REG_NONE
443     *
444     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
445     * length.
446     */
447    uint8_t* none; 
448
449    /** REG_SZ
450     *
451     * Stored as a NUL terminated string.  Converted to the specified
452     * REGFI_ENCODING.
453     */
454    uint8_t* string;
455
456    /** REG_EXPAND_SZ
457     *
458     * Stored as a NUL terminated string.  Converted to the specified
459     * REGFI_ENCODING.
460     */
461    uint8_t* expand_string;
462
463    /** REG_BINARY
464     *
465     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
466     * length.
467     */
468    uint8_t* binary;
469
470    /** REG_DWORD */
471    uint32_t dword;
472
473    /** REG_DWORD_BE */
474    uint32_t dword_be;
475
476    /** REG_LINK
477     *
478     * Stored as a NUL terminated string.  Converted to the specified
479     * REGFI_ENCODING.
480     */
481    uint8_t* link;
482
483    /** REG_MULTI_SZ
484     *
485     * Stored as a list of uint8_t* pointers, terminated with a NULL pointer.
486     * Each string element in the list is NUL terminated, and the character set
487     * is determined by the specified REGFI_ENCODING.
488     */
489    uint8_t** multiple_string;
490
491    /** REG_QWORD */
492    uint64_t qword;
493
494    /* The following are treated as binary currently, but this may change in
495     * the future as the formats become better understood.
496     */
497
498    /** REG_RESOURCE_LIST
499     *
500     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
501     * length.
502     */
503    uint8_t* resource_list;
504
505    /** REG_FULL_RESOURCE_DESCRIPTOR
506     *
507     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
508     * length.
509     */
510    uint8_t* full_resource_descriptor;
511
512    /** REG_RESOURCE_REQUIREMENTS_LIST
513     *
514     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
515     * length.
516     */
517    uint8_t* resource_requirements_list;
518  } interpreted;
519} REGFI_DATA;
520
521
522/** Value structure
523 * @ingroup regfiBase
524 */
525typedef struct _regfi_vk
526{
527  /** Real offset of this record's cell in the file */
528  uint32_t offset;     
529
530  /** ((start_offset - end_offset) & 0xfffffff8) */
531  uint32_t cell_size;
532
533  /** The name of this value converted to desired REGFI_ENCODING. 
534   *
535   * This conversion typically occurs automatically through REGFI_ITERATOR
536   * settings.  String is NUL terminated.
537   */
538  char* name;
539
540  /** The raw value name
541   *
542   * Length of the buffer is stored in name_length.
543   */
544  uint8_t* name_raw;
545
546  /** Length of name_raw */
547  uint16_t name_length;
548
549  /** Offset from beginning of this hbin block */
550  uint32_t hbin_off;
551 
552  /** Size of the value's data as reported in the VK record.
553   *
554   * May be different than that obtained while parsing the data cell itself.
555   */
556  uint32_t data_size;
557
558  /** Virtual offset of data cell */
559  uint32_t data_off;
560
561  /** Value's data type */
562  REGFI_DATA_TYPE type;
563
564  /** VK record's magic number (should be "vk") */
565  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
566
567  /** VK record flags */
568  uint16_t flags;
569
570  /* XXX: A 2-byte field of unknown purpose stored in the VK record */
571  uint16_t unknown1;
572
573  /** Whether or not the data record is stored in the VK record's data_off field.
574   *
575   * This information is derived from the high bit of the raw data size field.
576   */
577  bool     data_in_offset;
578
579  /* XXX: deprecated */
580  REGFI_DATA* data;
581
582} REGFI_VK;
583
584
585/* Key Security */
586struct _regfi_sk;
587
588/** Security structure
589 * @ingroup regfiBase
590 */
591typedef struct _regfi_sk
592{
593  /** Real file offset of this record */
594  uint32_t offset;
595
596  /** ((start_offset - end_offset) & 0xfffffff8) */
597  uint32_t cell_size;
598
599  /** The stored Windows security descriptor for this SK record */
600  WINSEC_DESC* sec_desc;
601
602  /** Offset of this record from beginning of this hbin block */
603  uint32_t hbin_off;
604 
605  /** Offset of the previous SK record in the linked list of SK records */
606  uint32_t prev_sk_off;
607
608  /** Offset of the next SK record in the linked list of SK records */
609  uint32_t next_sk_off;
610
611  /** Number of keys referencing this SK record */
612  uint32_t ref_count;
613
614  /** Size of security descriptor (sec_desc) */
615  uint32_t desc_size;
616
617  /* XXX: A 2-byte field of unknown purpose */
618  uint16_t unknown_tag;
619
620  /** The magic number for this record (should be "sk") */
621  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
622} REGFI_SK;
623
624
625/** Key structure
626 * @ingroup regfiBase
627 */
628typedef struct _regfi_nk
629{
630  /** Real offset of this record's cell in the file */
631  uint32_t offset;
632
633  /** Actual or estimated length of the cell. 
634   * Always in multiples of 8.
635   */
636  uint32_t cell_size;
637
638  /** Preloaded value-list for this key.
639   * This element is loaded automatically when using the iterator interface and
640   * possibly some lower layer interfaces.
641   */
642  REGFI_VALUE_LIST* values;
643
644
645  /** Preloaded subkey-list for this key.
646   * This element is loaded automatically when using the iterator interface and
647   * possibly some lower layer interfaces.
648   */
649  REGFI_SUBKEY_LIST* subkeys;
650 
651  /** Key flags */
652  uint16_t flags;
653
654  /** Magic number of key (should be "nk") */
655  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
656
657  /** Key's last modification time */
658  REGFI_NTTIME mtime;
659
660  /** Length of keyname_raw */
661  uint16_t name_length;
662
663  /** Length of referenced classname */
664  uint16_t classname_length;
665
666  /** The name of this key converted to desired REGFI_ENCODING. 
667   *
668   * This conversion typically occurs automatically through REGFI_ITERATOR
669   * settings.  String is NUL terminated.
670   */
671  char* name;
672
673  /** The raw key name
674   *
675   * Length of the buffer is stored in name_length.
676   */
677  uint8_t* name_raw;
678
679  /** Virtual offset of parent key */
680  uint32_t parent_off;
681
682  /** Virtual offset of classname key */
683  uint32_t classname_off;
684 
685  /* XXX: max subkey name * 2 */
686  uint32_t max_bytes_subkeyname;
687
688  /* XXX: max subkey classname length (as if) */
689  uint32_t max_bytes_subkeyclassname;
690
691  /* XXX: max value name * 2 */
692  uint32_t max_bytes_valuename;
693
694  /* XXX: max value data size */
695  uint32_t max_bytes_value;
696 
697  /* XXX: Fields of unknown purpose */
698  uint32_t unknown1;
699  uint32_t unknown2;
700  uint32_t unknown3;
701  uint32_t unk_index;               /* nigel says run time index ? */
702 
703  /** Number of subkeys */
704  uint32_t num_subkeys;
705
706  /** Virtual offset of subkey-list */
707  uint32_t subkeys_off;
708
709  /** Number of values for this key */
710  uint32_t num_values;
711
712  /** Virtual offset of value-list */
713  uint32_t values_off;
714
715  /** Virtual offset of SK record */
716  uint32_t sk_off;
717} REGFI_NK;
718
719
720typedef struct _regfi_raw_file
721{
722  int64_t  (* seek)(); /* (REGFI_RAW_FILE* self, uint64_t offset, int whence) */
723  ssize_t  (* read)(); /* (REGFI_RAW_FILE* self, void* buf, size_t count) */
724
725  uint64_t cur_off;
726  uint64_t size;
727  void*    state;
728} REGFI_RAW_FILE;
729
730
731/** Registry hive file data structure
732 *
733 * This essential structure stores run-time information about a single open
734 * registry hive as well as file header (REGF block) data.  This structure
735 * also stores a list of warnings and error messages generated while parsing
736 * the registry hive.  These can be tuned using @ref regfi_set_message_mask. 
737 * Messages may be retrieved using @ref regfi_log_get_str.
738 *
739 * @note If the message mask is set to record any messages, dependent code
740 *       must use @ref regfi_log_get_str periodically to clear the message
741 *       queue. Otherwise, this structure will grow in size over time as
742 *       messages queue up.
743 *
744 * @ingroup regfiBase
745 */ 
746typedef struct _regfi_file
747{
748  /* Data parsed from file header */
749  /********************************/
750  uint8_t  magic[REGFI_REGF_MAGIC_SIZE];/* "regf" */
751
752 /* These sequence numbers should match if
753  * the hive was properly synced to disk.
754  */
755  uint32_t sequence1;           
756  uint32_t sequence2;
757
758  REGFI_NTTIME mtime;
759  uint32_t major_version;  /* Set to 1 in all known hives */
760  uint32_t minor_version;  /* Set to 3 or 5 in all known hives */
761  uint32_t type;           /* XXX: Unverified.  Set to 0 in all known hives */
762  uint32_t format;         /* XXX: Unverified.  Set to 1 in all known hives */
763
764  uint32_t root_cell;  /* Offset to root cell in the first (or any?) hbin block */
765  uint32_t last_block; /* Offset to last hbin block in file */
766
767  uint32_t cluster;    /* XXX: Unverified. Set to 1 in all known hives */
768
769  /* Matches hive's base file name. Stored in UTF-16LE */
770  uint8_t file_name[REGFI_REGF_NAME_SIZE];
771
772  WINSEC_UUID* rm_id;       /* XXX: Unverified. */
773  WINSEC_UUID* log_id;      /* XXX: Unverified. */
774  WINSEC_UUID* tm_id;       /* XXX: Unverified. */
775  uint32_t flags;             /* XXX: Unverified. */
776  uint32_t guid_signature;    /* XXX: Unverified. */
777
778  uint32_t checksum;          /* Stored checksum from file */
779  uint32_t computed_checksum; /* Our own calculation of the checksum.
780                             * (XOR of bytes 0x0000 - 0x01FB) */
781
782  WINSEC_UUID* thaw_tm_id;  /* XXX: Unverified. */
783  WINSEC_UUID* thaw_rm_id;  /* XXX: Unverified. */
784  WINSEC_UUID* thaw_log_id; /* XXX: Unverified. */
785  uint32_t boot_type;         /* XXX: Unverified. */
786  uint32_t boot_recover;      /* XXX: Unverified. */
787
788  /* This seems to include random junk.  Possibly unsanitized memory left over
789   * from when header block was written.  For instance, chunks of nk records
790   * can be found, though often it's all 0s. */
791  uint8_t reserved1[REGFI_REGF_RESERVED1_SIZE];
792
793  /* This is likely reserved and unusued currently.  (Should be all 0s.)
794   * Included here for easier access in looking for hidden data
795   * or doing research. */
796  uint8_t reserved2[REGFI_REGF_RESERVED2_SIZE];
797
798
799  /* Run-time information */
800  /************************/
801  /* For sanity checking (not part of the registry header) */
802  uint32_t file_length;
803
804  /** The encoding that all strings are converted to during interpretation.
805   */
806  REGFI_ENCODING string_encoding;
807
808  /* Functions for accessing the file */
809  REGFI_RAW_FILE* cb;
810
811  /* Mutex for all cb access.  This is done to prevent one thread from moving
812   * the file offset while another thread is in the middle of a multi-read
813   * parsing transaction */
814  pthread_mutex_t cb_lock;
815
816  /* Metadata about hbins */
817  range_list* hbins;
818
819  /* Multiple read access allowed, write access is exclusive */
820  pthread_rwlock_t hbins_lock;
821
822  /* SK record cached since they're repeatedly reused */
823  lru_cache* sk_cache;
824
825  /* Need exclusive access for LRUs, since lookups make changes */
826  pthread_mutex_t sk_lock;
827
828  /* Needed to protect various talloc calls */
829  pthread_mutex_t mem_lock;
830
831} REGFI_FILE;
832
833
834/** Registry hive iterator
835 * @ingroup regfiIteratorLayer
836 */
837typedef struct _regfi_iterator
838{
839  /** The registry hive this iterator is associated with */
840  REGFI_FILE* f;
841
842  /** All current parent keys and associated iterator positions */
843  void_stack* key_positions;
844
845  /** The current key */
846  REGFI_NK* cur_key;
847
848  /** Index of the current subkey */
849  uint32_t cur_subkey;
850
851  /** Index of the current value */
852  uint32_t cur_value;
853} REGFI_ITERATOR;
854
855
856typedef struct _regfi_iter_position
857{
858  REGFI_NK* nk;
859  uint32_t cur_subkey;
860  /* We could store a cur_value here as well, but didn't see
861   * the use in it right now.
862   */
863} REGFI_ITER_POSITION;
864
865
866/** General purpose buffer with stored length
867 * @ingroup regfiBottomLayer
868 */
869typedef struct _regfi_buffer
870{
871  uint8_t* buf;
872  uint32_t len;
873} REGFI_BUFFER;
874
875
876
877/******************************************************************************/
878/**
879 * @defgroup regfiBase Base Layer: Essential Functions and Data Structures
880 *
881 * These functions are either necessary for normal use of the regfi API or just
882 * don't fit particularly well in any of the other layers.
883 */
884/******************************************************************************/
885
886/** Parses file headers of an already open registry hive file and
887 *  allocates related structures for further parsing.
888 *
889 * @param fd A file descriptor of an already open file.  Must be seekable.
890 *
891 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
892 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
893 *
894 * @ingroup regfiBase
895 */
896_EXPORT
897REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding);
898
899
900/** Parses file headers returned by supplied callback functions.
901 *
902 * This interface is useful if you have a registry hive in memory
903 * or have some other reason to emulate a real file.
904 *
905 * @param file_cb A structure defining the callback functions needed to access the file.
906 *
907 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
908 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
909 *
910 * @ingroup regfiBase
911 */
912_EXPORT
913REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb,
914                           REGFI_ENCODING output_encoding);
915
916
917/** Frees a hive's data structures without closing the underlying file.
918 *
919 * @param file The registry structure to free.
920 *
921 * @ingroup regfiBase
922 */
923_EXPORT
924void regfi_free(REGFI_FILE* file);
925
926
927/** Get errors, warnings, and/or verbose information relating to processing of
928 *  the given registry file.
929 *
930 * @return A newly allocated char* which must be free()d by the caller.
931 *
932 * @ingroup regfiBase
933 */
934_EXPORT
935char* regfi_log_get_str();
936
937
938/** Set the verbosity level of messages generated by the library for the
939 *  current thread.
940 *
941 * @param mask   An integer representing the types of messages desired.
942 *               Acceptable values are created through bitwise ORs of
943 *               REGFI_LOG_* values.  For instance, if only errors and
944 *               informational messages were desired (but not warnings),
945 *               then one would specify: REGFI_LOG_ERROR|REGFI_LOG_INFO
946 *               By default the message mask is: REGFI_LOG_ERROR|REGFI_LOG_WARN.
947 *
948 * @return       true on success and false on failure.  Failure occurs if
949 *               underlying pthread functions fail.  errno is set in this case.
950 *
951 * Message masks are set in a thread-specific way.  If one were to set a message
952 * mask in one thread and then spawn a new thread, then the new thread will have
953 * it's message mask reset to the default.  This function may be called at any
954 * time and will take effect immediately for the current thread.
955 *
956 * @note When a non-zero message mask is set, messages will
957 *       accumulate in memory without limit if they are not fetched using
958 *       @ref regfi_get_log_str and subsequently freed by the caller.  It is
959 *       recommended that messsages be fetched after each regfi API call in
960 *       order to provide the most context.
961 *
962 * @ingroup regfiBase
963 */
964_EXPORT
965bool regfi_log_set_mask(uint16_t mask);
966
967
968/** Fetches a hive's root key.
969 *
970 * @return Returns the root key or NULL on failure.  Key must be freed using
971 *         @ref regfi_free_record.
972 *
973 * @ingroup regfiBase
974 */
975_EXPORT
976const REGFI_NK*       regfi_get_rootkey(REGFI_FILE* file);
977
978
979/** Frees a record previously returned by one of the API functions.
980 *
981 * @param file The file from which the record originated. 
982 *             (This is needed for memory management reasons.)
983 *
984 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
985 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
986 *
987 * @note The "const" in the record data type is a bit misleading and is there just for
988 * convenience.  Since records returned previously must not be modified by users
989 * of the API due to internal caching, these are returned as const, so this
990 * function is const to make passing those records back easy.
991 *
992 * @ingroup regfiBase
993 */
994_EXPORT
995void regfi_free_record(REGFI_FILE* file, const void* record);
996
997
998/** Increments reference count on record
999 *
1000 * Adds an extra internal reference to specified record, making it necessary to
1001 * call regfi_free_record on it an additional time before it is freed.  This is
1002 * useful in cases where multiple threads/structures need access to a record,
1003 * without requiring them to be in sync with when it is freed.
1004 *
1005 * @param file The file from which the record originated. 
1006 *             (This is needed for memory management reasons.)
1007 *
1008 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
1009 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
1010 *
1011 * @return true on success, false otherwise
1012 *
1013 * @ingroup regfiBase
1014 */
1015_EXPORT
1016bool regfi_reference_record(REGFI_FILE* file, const void* record);
1017
1018
1019/** Retrieves number of subkeys referenced by this key.
1020 *
1021 * Number of subkeyss in key structure and subkey list structure could differ,
1022 * so this provides a standard/sane way of determining the number.
1023 *
1024 * @param key  the key whose number of subkeys is desired
1025 *
1026 * @return Returns the number of subkeys referenced by this key.
1027 *
1028 * @ingroup regfiBase
1029 */
1030_EXPORT
1031uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key);
1032
1033
1034/** Retrieves number of values referenced by this key.
1035 *
1036 * Number of values in key structure and value list structure could differ,
1037 * so this provides a standard/sane way of determining the number.
1038 *
1039 * @param key  the key whose number of values is desired
1040 *
1041 * @return Returns the number of values referenced by this key.
1042 *
1043 * @ingroup regfiBase
1044 */
1045_EXPORT
1046uint32_t regfi_fetch_num_values(const REGFI_NK* key);
1047
1048
1049/** Retrieves classname for a given key.
1050 *
1051 * @param file the file from which key is derived
1052 * @param key the key whose classname is desired
1053 *
1054 * @return Returns a newly allocated classname structure, or NULL on failure.
1055 *         Classname structures must be freed with @ref regfi_free_record.
1056 *
1057 * @ingroup regfiBase
1058 */
1059_EXPORT
1060const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file, 
1061                                             const REGFI_NK* key);
1062
1063
1064/** Returns the SK (security) record referenced by the supplied key.
1065 *
1066 * @param file the file from which key is derived
1067 * @param key  the key whose SK record is desired
1068 *
1069 * @return A read-only SK structure, or NULL on failure.
1070 *
1071 * @ingroup regfiBase
1072 */
1073_EXPORT
1074const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key);
1075
1076
1077/** Retrieves data for a given value.
1078 *
1079 * @param file the file from which value is derived
1080 * @param value the value whose data is desired
1081 *
1082 * @return Returns a newly allocated data structure, or NULL on failure.
1083 *         Data structures must be freed with @ref regfi_free_record.
1084 *
1085 * @ingroup regfiBase
1086 */
1087_EXPORT
1088const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file,
1089                                   const REGFI_VK* value);
1090
1091
1092/** Locates a specific subkey of a given key.
1093 *
1094 * @param file  the file from which key is derived
1095 * @param key   the key whose subkey is desired
1096 * @param name  name of the desired subkey
1097 * @param index a return value: the index of the desired subkey.
1098 *              undefined on error
1099 *
1100 * @return true if the subkey is found, false if an error occurred or if the
1101 *         specified name could not be found. If an error occurs, messages
1102 *         will be written explaining the issue. (See regfi_log_get_str.)
1103 *
1104 * @ingroup regfiBase
1105 */
1106_EXPORT
1107bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1108                       const char* name, uint32_t* index);
1109
1110
1111/** Locates a specific value of a given key.
1112 *
1113 * @param file  the file from which key is derived
1114 * @param key   the key whose value is desired
1115 * @param name  name of the desired value
1116 * @param index a return value: the index of the desired value. 
1117 *              undefined on error
1118 *
1119 * @return true if the value is found, false if an error occurred or if the
1120 *         specified name could not be found. If an error occurs, messages
1121 *         will be written explaining the issue. (See regfi_log_get_str.)
1122 *
1123 * @ingroup regfiBase
1124 */
1125_EXPORT
1126bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key,
1127                      const char* name, uint32_t* index);
1128
1129
1130/** Retrieves a specific subkey of a given key.
1131 *
1132 * @param file  the file from which key is derived
1133 * @param key   the key whose subkey is desired
1134 * @param index the index of the desired subkey
1135 *
1136 * @return the requested subkey or NULL on error.
1137 *
1138 * @ingroup regfiBase
1139 */
1140_EXPORT
1141const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1142                                 uint32_t index);
1143
1144
1145/** Retrieves a specific value of a given key.
1146 *
1147 * @param file  the file from which key is derived
1148 * @param key   the key whose value is desired
1149 * @param index the index of the desired value
1150 *
1151 * @return the requested value or NULL on error.
1152 *
1153 * @ingroup regfiBase
1154 */
1155_EXPORT
1156const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
1157                                uint32_t index);
1158
1159
1160
1161/** Uses a key's parent_off reference to retrieve it's parent.
1162 *
1163 * @param file  the file from which key is derived
1164 * @param key   the key whose parent is desired
1165 *
1166 * @return the requested subkey or NULL on error.
1167 *
1168 * @ingroup regfiBase
1169 */
1170_EXPORT
1171const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key);
1172
1173
1174/******************************************************************************/
1175/**
1176 * @defgroup regfiIteratorLayer Iterator Layer: Primary regfi Library Interface
1177 *
1178 * This top layer of API functions provides an iterator interface which makes
1179 * traversing registry data structures easy in both single-threaded and
1180 * multi-threaded scenarios.
1181 */
1182/******************************************************************************/
1183
1184/** Creates a new iterator for the provided registry file.
1185 *
1186 * @param file The opened registry file the iterator should be created for.
1187 *
1188 * @param output_encoding Character encoding that strings should be returned in.
1189 *                        Only supply the REGFI_ENCODING_* constants, as others
1190 *                        will be rejected.
1191 *                        The following values are currently accepted:
1192 *                        REGFI_ENCODING_DEFAULT (currently REGFI_ENCODING_ASCII)
1193 *                        REGFI_ENCODING_ASCII
1194 *                        REGFI_ENCODING_UTF8
1195 *
1196 * @return A newly allocated REGFI_ITERATOR.
1197 *         Must be free()d with regfi_iterator_free.
1198 *
1199 * @ingroup regfiIteratorLayer
1200 */
1201_EXPORT
1202REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file);
1203
1204
1205/** Frees a registry file iterator previously created by regfi_iterator_new.
1206 *
1207 * This does not affect the underlying registry file's allocation status.
1208 *
1209 * @param i the iterator to be freed
1210 *
1211 * @ingroup regfiIteratorLayer
1212 */
1213_EXPORT
1214void regfi_iterator_free(REGFI_ITERATOR* i);
1215
1216
1217/** Traverse deeper into the registry tree at the current subkey.
1218 *
1219 * @param i the iterator
1220 *
1221 * @return  true on success, false on failure. 
1222 *          Note that subkey and value indexes are preserved.  That is, if a
1223 *          regfi_iterator_up call occurs later (reversing the effect of this
1224 *          call) then the subkey and value referenced prior to the
1225 *          regfi_iterator_down call will still be referenced.  This  makes
1226 *          depth-first iteration particularly easy.
1227 *
1228 * @ingroup regfiIteratorLayer
1229 */
1230_EXPORT
1231bool regfi_iterator_down(REGFI_ITERATOR* i);
1232
1233
1234/** Traverse up to the current key's parent key.
1235 *
1236 * @param i the iterator
1237 *
1238 * @return  true on success, false on failure.  Any subkey or value state
1239 *          associated with the current key is lost.
1240 *
1241 * @ingroup regfiIteratorLayer
1242 */
1243_EXPORT
1244bool regfi_iterator_up(REGFI_ITERATOR* i);
1245
1246
1247/** Traverse up to the root key of the hive.
1248 *
1249 * @param i the iterator
1250 *
1251 * @return true on success, false on failure.
1252 *
1253 * @ingroup regfiIteratorLayer
1254 */
1255_EXPORT
1256bool regfi_iterator_to_root(REGFI_ITERATOR* i);
1257
1258
1259/** Traverse down multiple levels in the registry hive.
1260 *
1261 * XXX: This currently only accepts ASCII key names.  Need to look into
1262 *      accepting other encodings.
1263 *
1264 * @param i    the iterator
1265 * @param path a list of key names representing the path.  This list must
1266 *             contain NUL terminated strings.  The list itself is
1267 *             terminated with a NULL pointer.  All path elements must be
1268 *             keys; value names are not accepted (even as the last
1269 *             element).
1270 *
1271 * @return true on success, false on failure.  If any element of path is not
1272 *                 found, false will be returned and the iterator will remain
1273 *                 in its original position.
1274 *
1275 * @ingroup regfiIteratorLayer
1276 */
1277_EXPORT
1278bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path);
1279
1280
1281/** Returns the currently referenced key.
1282 *
1283 * @param i the iterator
1284 *
1285 * @return A read-only key structure for the current key, or NULL on failure.
1286 *         Data structures must be freed with @ref regfi_free_record.
1287 *
1288 * @ingroup regfiIteratorLayer
1289 */
1290_EXPORT
1291const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i);
1292
1293
1294/** Sets the internal subkey index to the first subkey referenced by the current
1295 *  key.
1296 *
1297 * @param i the iterator
1298 *
1299 * @return True if the current key has any subkeys, false otherwise.
1300 *
1301 * @ingroup regfiIteratorLayer
1302 */
1303_EXPORT
1304bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
1305
1306
1307/** Returns the currently indexed subkey.
1308 *
1309 * @param i the iterator
1310 *
1311 * @return A newly allocated key structure for the currently referenced subkey,
1312 *         or NULL on failure.  Newly allocated keys must be freed with
1313 *         @ref regfi_free_record.
1314 *
1315 * @ingroup regfiIteratorLayer
1316 */
1317_EXPORT
1318const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
1319
1320
1321/** Increments the internal subkey index to the next key in the subkey-list.
1322 *
1323 * @param i the iterator
1324 *
1325 * @return True if another subkey should exist, false otherwise.
1326 *
1327 * @ingroup regfiIteratorLayer
1328 */
1329_EXPORT
1330bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
1331
1332
1333/** Searches for a subkey with a given name under the current key.
1334 *
1335 * @param i     the iterator
1336 * @param name  subkey name to search for
1337 *
1338 * @return True if such a subkey was found, false otherwise.  If a subkey is
1339 *         found, the current subkey index is set to that subkey.  Otherwise,
1340 *         the subkey index remains at the same location as before the call.
1341 *
1342 * @ingroup regfiIteratorLayer
1343 */
1344_EXPORT
1345bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name);
1346
1347
1348/** Sets the internal value index to the first value referenced by the current
1349 *  key.
1350 *
1351 * @param i the iterator
1352 *
1353 * @return True if the current key has any values, false otherwise.
1354 *
1355 * @ingroup regfiIteratorLayer
1356 */
1357_EXPORT
1358bool regfi_iterator_first_value(REGFI_ITERATOR* i);
1359
1360
1361/** Returns the currently indexed value.
1362 *
1363 * @param i the iterator
1364 *
1365 * @return A newly allocated value structure for the currently referenced value,
1366 *         or NULL on failure.  Newly allocated values must be freed with
1367 *         @ref regfi_free_record.
1368 *
1369 * @ingroup regfiIteratorLayer
1370 */
1371_EXPORT
1372const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i);
1373
1374
1375/** Increments the internal value index to the next value in the value-list.
1376 *
1377 * @param i the iterator
1378 *
1379 * @return True if another value should exist, false otherwise.
1380 *
1381 * @ingroup regfiIteratorLayer
1382 */
1383_EXPORT
1384bool regfi_iterator_next_value(REGFI_ITERATOR* i);
1385
1386
1387/** Searches for a value with a given name under the current key.
1388 *
1389 * @param i     the iterator
1390 * @param name  value name to search for
1391 *
1392 * @return True if such a value was found, false otherwise.  If a value is
1393 *         found, the current value index is set to that value.  Otherwise,
1394 *         the value index remains at the same location as before the call.
1395 *
1396 * @ingroup regfiIteratorLayer
1397 */
1398_EXPORT
1399bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name);
1400
1401
1402/******************************************************************************/
1403/**
1404 * @defgroup regfiGlueLayer Glue Layer: Logical Data Structure Loading
1405 */
1406/******************************************************************************/
1407
1408/** Loads a key and associated data structures given a file offset.
1409 *
1410 * XXX: finish documenting
1411 *
1412 * @ingroup regfiGlueLayer
1413 */
1414_EXPORT
1415REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
1416                         REGFI_ENCODING output_encoding, 
1417                         bool strict);
1418
1419
1420/** Loads a value at a given file offset alng with associated data structures.
1421 *
1422 * XXX: finish documenting
1423 *
1424 * @ingroup regfiGlueLayer
1425 */
1426_EXPORT
1427REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
1428                               REGFI_ENCODING output_encoding, 
1429                               bool strict);
1430
1431
1432/** Loads a logical subkey list in its entirety which may span multiple records.
1433 *
1434 * XXX: finish documenting
1435 *
1436 * @ingroup regfiGlueLayer
1437 */
1438_EXPORT
1439REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset,
1440                                         uint32_t num_keys, uint32_t max_size,
1441                                         bool strict);
1442
1443
1444/** Loads a valuelist.
1445 *
1446 * XXX: finish documenting
1447 *
1448 * @ingroup regfiGlueLayer
1449 */
1450_EXPORT
1451REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1452                                       uint32_t num_values, uint32_t max_size,
1453                                       bool strict);
1454
1455
1456/** Loads a data record which may be contained in the virtual offset, in a
1457 *  single cell, or in multiple cells through big data records.
1458 *
1459 * XXX: finish documenting
1460 *
1461 * @ingroup regfiGlueLayer
1462 */
1463_EXPORT
1464REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
1465                             uint32_t length, bool data_in_offset,
1466                             bool strict);
1467
1468
1469/** Loads the data associated with a big data record at the specified offset.
1470 *
1471 * XXX: finish documenting
1472 *
1473 * @ingroup regfiGlueLayer
1474 */
1475_EXPORT
1476REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, uint32_t offset, 
1477                                 uint32_t data_length,uint32_t cell_length,
1478                                 range_list* used_ranges,
1479                                 bool strict);
1480
1481
1482/** Given raw data, attempts to interpret the data based on a specified registry
1483 *  data type.
1484 *
1485 * XXX: finish documenting
1486 *
1487 * @ingroup regfiGlueLayer
1488 */
1489_EXPORT
1490bool regfi_interpret_data(REGFI_FILE* file, 
1491                          REGFI_ENCODING string_encoding,
1492                          uint32_t type, REGFI_DATA* data);
1493
1494
1495
1496/* These are cached so return values don't need to be freed. */
1497
1498/** Loads an "sk" security record at the specified offset.
1499 *
1500 * XXX: finish documenting
1501 *
1502 * @ingroup regfiGlueLayer
1503 */
1504_EXPORT
1505const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset,
1506                                  bool strict);
1507
1508
1509
1510
1511/** Retrieves the HBIN data structure stored at the specified offset.
1512 *
1513 * XXX: finish documenting
1514 *
1515 * @ingroup regfiGlueLayer
1516 */
1517_EXPORT
1518const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset);
1519
1520
1521
1522/******************************************************************************/
1523/**
1524 * @defgroup regfiParseLayer Parsing Layer: Direct Data Structure Access
1525 */
1526/******************************************************************************/
1527
1528_EXPORT
1529REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict);
1530
1531_EXPORT
1532REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, 
1533                             bool strict);
1534
1535
1536/** Parses an NK record at the specified offset
1537 *
1538 * @param file     the registry file structure
1539 * @param offset   the offset of the cell (not the record) to be parsed.
1540 * @param max_size the maximum size the NK cell could be. (for validation)
1541 * @param strict   if true, rejects any malformed records.  Otherwise,
1542 *                 tries to minimally validate integrity.
1543 *
1544 * @return A newly allocated NK record structure, or NULL on failure.
1545 *
1546 * @ingroup regfiParseLayer
1547 */
1548_EXPORT
1549REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset,
1550                             uint32_t max_size, bool strict);
1551
1552
1553/** Parses a single cell containing a subkey-list record.
1554 *
1555 * XXX: finish documenting
1556 *
1557 * @ingroup regfiParseLayer
1558 */
1559_EXPORT
1560REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset,
1561                                          uint32_t max_size, bool strict);
1562
1563
1564/** Parses a VK (value) record at the specified offset
1565 *
1566 * XXX: finish documenting
1567 *
1568 * @ingroup regfiParseLayer
1569 */
1570_EXPORT
1571REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
1572                             uint32_t max_size, bool strict);
1573
1574
1575/** Parses an SK (security) record at the specified offset
1576 *
1577 * XXX: finish documenting
1578 *
1579 * @ingroup regfiParseLayer
1580 */
1581_EXPORT
1582REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, 
1583                             uint32_t max_size, bool strict);
1584
1585
1586/** Retrieves information on all cells in the registry hive which are
1587 *  currently in the unallocated status. 
1588 *
1589 * The unallocated status is determined based soley on the cell length sign.
1590 *
1591 * XXX: finish documenting
1592 *
1593 * @ingroup regfiParseLayer
1594 */
1595_EXPORT
1596range_list* regfi_parse_unalloc_cells(REGFI_FILE* file);
1597
1598
1599/** Helper function to parse a cell
1600 *
1601 * XXX: finish documenting
1602 *
1603 * @ingroup regfiParseLayer
1604 */
1605_EXPORT
1606bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset,
1607                      uint8_t* hdr, uint32_t hdr_len,
1608                      uint32_t* cell_length, bool* unalloc);
1609
1610
1611/** Parses a classname cell
1612 *
1613 * XXX: finish documenting
1614 *
1615 * @ingroup regfiParseLayer
1616 */
1617_EXPORT
1618uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset,
1619                               uint16_t* name_length, 
1620                               uint32_t max_size, bool strict);
1621
1622
1623/** Parses a single-cell data record
1624 *
1625 * XXX: finish documenting
1626 *
1627 * @ingroup regfiParseLayer
1628 */
1629_EXPORT
1630REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
1631                              uint32_t length, bool strict);
1632
1633
1634/** Parses a "little data" record which is stored entirely within the
1635 *  provided virtual offset.
1636 *
1637 * XXX: finish documenting
1638 *
1639 * @ingroup regfiParseLayer
1640 */
1641_EXPORT
1642REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, 
1643                                     uint32_t length, bool strict);
1644
1645
1646/******************************************************************************/
1647/*    Private (and undocumented) Functions                                    */
1648/******************************************************************************/
1649int64_t               regfi_raw_seek(REGFI_RAW_FILE* self, 
1650                                     uint64_t offset, int whence);
1651ssize_t               regfi_raw_read(REGFI_RAW_FILE* self, 
1652                                     void* buf, size_t count);
1653_EXPORT
1654uint64_t              regfi_seek(REGFI_RAW_FILE* file_cb, 
1655                                 uint64_t offset, int whence);
1656_EXPORT
1657uint32_t              regfi_read(REGFI_RAW_FILE* file_cb, 
1658                                 uint8_t* buf, uint32_t* length);
1659
1660_EXPORT
1661const char*           regfi_type_val2str(unsigned int val);
1662_EXPORT
1663int                   regfi_type_str2val(const char* str);
1664
1665_EXPORT
1666char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
1667_EXPORT
1668char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
1669_EXPORT
1670char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
1671_EXPORT
1672char*                 regfi_get_group(WINSEC_DESC* sec_desc);
1673
1674REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16_t num_lists, 
1675                                              REGFI_SUBKEY_LIST** lists,
1676                                              bool strict);
1677REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset,
1678                                                uint32_t max_size, bool strict,
1679                                                uint8_t depth_left);
1680void                  regfi_add_message(REGFI_FILE* file, uint16_t msg_type, 
1681                                        const char* fmt, ...);
1682REGFI_NK*             regfi_copy_nk(const REGFI_NK* nk);
1683REGFI_VK*             regfi_copy_vk(const REGFI_VK* vk);
1684_EXPORT
1685int32_t               regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset);
1686int32_t               regfi_conv_charset(const char* input_charset, 
1687                                         const char* output_charset,
1688                                         uint8_t* input, char* output, 
1689                                         uint32_t input_len, uint32_t output_max);
1690_EXPORT
1691REGFI_DATA*           regfi_buffer_to_data(REGFI_BUFFER raw_data);
1692
1693/* XXX: move to base API and document */
1694_EXPORT
1695void                  regfi_unix2nt_time(REGFI_NTTIME* nt, time_t t);
1696_EXPORT
1697double                regfi_nt2unix_time(const REGFI_NTTIME* nt);
1698
1699
1700_EXPORT
1701void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
1702                             REGFI_ENCODING output_encoding, bool strict);
1703_EXPORT
1704void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, 
1705                               REGFI_ENCODING output_encoding, bool strict);
1706
1707_EXPORT
1708void regfi_init();
1709
1710
1711#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.