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