1 /* hivex - Windows Registry "hive" extraction library.
2 * Copyright (C) 2009-2010 Red Hat Inc.
3 * Derived from code by Petter Nordahl-Hagen under a compatible license:
4 * Copyright (c) 1997-2007 Petter Nordahl-Hagen.
5 * Derived from code by Markus Stephany under a compatible license:
6 * Copyright (c) 2000-2004, Markus Stephany.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * See file LICENSE for the full license.
38 #include "full-read.h"
39 #include "full-write.h"
45 #define STREQ(a,b) (strcmp((a),(b)) == 0)
46 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
47 //#define STRNEQ(a,b) (strcmp((a),(b)) != 0)
48 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
49 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
50 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
51 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
52 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
53 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
56 #include "byte_conversions.h"
58 /* These limits are in place to stop really stupid stuff and/or exploits. */
59 #define HIVEX_MAX_SUBKEYS 10000
60 #define HIVEX_MAX_VALUES 1000
61 #define HIVEX_MAX_VALUE_LEN 1000000
62 #define HIVEX_MAX_ALLOCATION 1000000
64 static char *windows_utf16_to_utf8 (/* const */ char *input, size_t len);
73 /* Registry file, memory mapped if read-only, or malloc'd if writing. */
76 struct ntreg_header *hdr;
79 /* Use a bitmap to store which file offsets are valid (point to a
80 * used block). We only need to store 1 bit per 32 bits of the file
81 * (because blocks are 4-byte aligned). We found that the average
82 * block size in a registry file is ~50 bytes. So roughly 1 in 12
83 * bits in the bitmap will be set, making it likely a more efficient
84 * structure than a hash table.
87 #define BITMAP_SET(bitmap,off) (bitmap[(off)>>5] |= 1 << (((off)>>2)&7))
88 #define BITMAP_CLR(bitmap,off) (bitmap[(off)>>5] &= ~ (1 << (((off)>>2)&7)))
89 #define BITMAP_TST(bitmap,off) (bitmap[(off)>>5] & (1 << (((off)>>2)&7)))
90 #define IS_VALID_BLOCK(h,off) \
91 (((off) & 3) == 0 && \
93 (off) < (h)->size && \
94 BITMAP_TST((h)->bitmap,(off)))
96 /* Fields from the header, extracted from little-endianness hell. */
97 size_t rootoffs; /* Root key offset (always an nk-block). */
98 size_t endpages; /* Offset of end of pages. */
101 size_t endblocks; /* Offset to next block allocation (0
102 if not allocated anything yet). */
105 /* NB. All fields are little endian. */
106 struct ntreg_header {
107 char magic[4]; /* "regf" */
110 char last_modified[8];
111 uint32_t major_ver; /* 1 */
112 uint32_t minor_ver; /* 3 */
113 uint32_t unknown5; /* 0 */
114 uint32_t unknown6; /* 1 */
115 uint32_t offset; /* offset of root key record - 4KB */
116 uint32_t blocks; /* pointer AFTER last hbin in file - 4KB */
117 uint32_t unknown7; /* 1 */
119 char name[64]; /* original file name of hive */
120 char unknown_guid1[16];
121 char unknown_guid2[16];
124 char unknown_guid3[16];
129 uint32_t csum; /* checksum: xor of dwords 0-0x1fb. */
131 char unknown11[3528];
133 char unknown_guid4[16];
134 char unknown_guid5[16];
135 char unknown_guid6[16];
139 } __attribute__((__packed__));
141 struct ntreg_hbin_page {
142 char magic[4]; /* "hbin" */
143 uint32_t offset_first; /* offset from 1st block */
144 uint32_t page_size; /* size of this page (multiple of 4KB) */
146 /* Linked list of blocks follows here. */
147 } __attribute__((__packed__));
149 struct ntreg_hbin_block {
150 int32_t seg_len; /* length of this block (-ve for used block) */
151 char id[2]; /* the block type (eg. "nk" for nk record) */
152 /* Block data follows here. */
153 } __attribute__((__packed__));
155 #define BLOCK_ID_EQ(h,offs,eqid) \
156 (STREQLEN (((struct ntreg_hbin_block *)((h)->addr + (offs)))->id, (eqid), 2))
159 block_len (hive_h *h, size_t blkoff, int *used)
161 struct ntreg_hbin_block *block;
162 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
164 int32_t len = le32toh (block->seg_len);
175 struct ntreg_nk_record {
176 int32_t seg_len; /* length (always -ve because used) */
177 char id[2]; /* "nk" */
181 uint32_t parent; /* offset of owner/parent */
182 uint32_t nr_subkeys; /* number of subkeys */
183 uint32_t nr_subkeys_volatile;
184 uint32_t subkey_lf; /* lf record containing list of subkeys */
185 uint32_t subkey_lf_volatile;
186 uint32_t nr_values; /* number of values */
187 uint32_t vallist; /* value-list record */
188 uint32_t sk; /* offset of sk-record */
189 uint32_t classname; /* offset of classname record */
190 uint16_t max_subkey_name_len; /* maximum length of a subkey name in bytes
191 if the subkey was reencoded as UTF-16LE */
194 uint32_t max_vk_name_len; /* maximum length of any vk name in bytes
195 if the name was reencoded as UTF-16LE */
196 uint32_t max_vk_data_len; /* maximum length of any vk data in bytes */
198 uint16_t name_len; /* length of name */
199 uint16_t classname_len; /* length of classname */
200 char name[1]; /* name follows here */
201 } __attribute__((__packed__));
203 struct ntreg_lf_record {
205 char id[2]; /* "lf"|"lh" */
206 uint16_t nr_keys; /* number of keys in this record */
208 uint32_t offset; /* offset of nk-record for this subkey */
209 char hash[4]; /* hash of subkey name */
211 } __attribute__((__packed__));
213 struct ntreg_ri_record {
215 char id[2]; /* "ri" */
216 uint16_t nr_offsets; /* number of pointers to lh records */
217 uint32_t offset[1]; /* list of pointers to lh records */
218 } __attribute__((__packed__));
220 /* This has no ID header. */
221 struct ntreg_value_list {
223 uint32_t offset[1]; /* list of pointers to vk records */
224 } __attribute__((__packed__));
226 struct ntreg_vk_record {
227 int32_t seg_len; /* length (always -ve because used) */
228 char id[2]; /* "vk" */
229 uint16_t name_len; /* length of name */
230 /* length of the data:
231 * If data_len is <= 4, then it's stored inline.
232 * Top bit is set to indicate inline.
235 uint32_t data_offset; /* pointer to the data (or data if inline) */
236 uint32_t data_type; /* type of the data */
237 uint16_t flags; /* bit 0 set => key name ASCII,
238 bit 0 clr => key name UTF-16.
239 Only seen ASCII here in the wild.
240 NB: this is CLEAR for default key. */
242 char name[1]; /* key name follows here */
243 } __attribute__((__packed__));
245 struct ntreg_sk_record {
246 int32_t seg_len; /* length (always -ve because used) */
247 char id[2]; /* "sk" */
249 uint32_t sk_next; /* linked into a circular list */
251 uint32_t refcount; /* reference count */
252 uint32_t sec_len; /* length of security info */
253 char sec_desc[1]; /* security info follows */
254 } __attribute__((__packed__));
257 header_checksum (const hive_h *h)
259 uint32_t *daddr = (uint32_t *) h->addr;
263 for (i = 0; i < 0x1fc / 4; ++i) {
264 sum ^= le32toh (*daddr);
272 hivex_open (const char *filename, int flags)
276 assert (sizeof (struct ntreg_header) == 0x1000);
277 assert (offsetof (struct ntreg_header, csum) == 0x1fc);
279 h = calloc (1, sizeof *h);
283 h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK;
285 const char *debug = getenv ("HIVEX_DEBUG");
286 if (debug && STREQ (debug, "1"))
290 fprintf (stderr, "hivex_open: created handle %p\n", h);
292 h->writable = !!(flags & HIVEX_OPEN_WRITE);
293 h->filename = strdup (filename);
294 if (h->filename == NULL)
297 h->fd = open (filename, O_RDONLY | O_CLOEXEC);
302 if (fstat (h->fd, &statbuf) == -1)
305 h->size = statbuf.st_size;
308 h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0);
309 if (h->addr == MAP_FAILED)
313 fprintf (stderr, "hivex_open: mapped file at %p\n", h->addr);
315 h->addr = malloc (h->size);
319 if (full_read (h->fd, h->addr, h->size) < h->size)
324 if (h->hdr->magic[0] != 'r' ||
325 h->hdr->magic[1] != 'e' ||
326 h->hdr->magic[2] != 'g' ||
327 h->hdr->magic[3] != 'f') {
328 fprintf (stderr, "hivex: %s: not a Windows NT Registry hive file\n",
334 /* Check major version. */
335 uint32_t major_ver = le32toh (h->hdr->major_ver);
336 if (major_ver != 1) {
338 "hivex: %s: hive file major version %" PRIu32 " (expected 1)\n",
339 filename, major_ver);
344 h->bitmap = calloc (1 + h->size / 32, 1);
345 if (h->bitmap == NULL)
348 /* Header checksum. */
349 uint32_t sum = header_checksum (h);
350 if (sum != le32toh (h->hdr->csum)) {
351 fprintf (stderr, "hivex: %s: bad checksum in hive header\n", filename);
356 if (h->msglvl >= 2) {
357 char *name = windows_utf16_to_utf8 (h->hdr->name, 64);
360 "hivex_open: header fields:\n"
361 " file version %" PRIu32 ".%" PRIu32 "\n"
362 " sequence nos %" PRIu32 " %" PRIu32 "\n"
363 " (sequences nos should match if hive was synched at shutdown)\n"
364 " original file name %s\n"
365 " (only 32 chars are stored, name is probably truncated)\n"
366 " root offset 0x%x + 0x1000\n"
367 " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n"
368 " checksum 0x%x (calculated 0x%x)\n",
369 major_ver, le32toh (h->hdr->minor_ver),
370 le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2),
371 name ? name : "(conversion failed)",
372 le32toh (h->hdr->offset),
373 le32toh (h->hdr->blocks), h->size,
374 le32toh (h->hdr->csum), sum);
378 h->rootoffs = le32toh (h->hdr->offset) + 0x1000;
379 h->endpages = le32toh (h->hdr->blocks) + 0x1000;
382 fprintf (stderr, "hivex_open: root offset = 0x%zx\n", h->rootoffs);
384 /* We'll set this flag when we see a block with the root offset (ie.
387 int seen_root_block = 0, bad_root_block = 0;
389 /* Collect some stats. */
390 size_t pages = 0; /* Number of hbin pages read. */
391 size_t smallest_page = SIZE_MAX, largest_page = 0;
392 size_t blocks = 0; /* Total number of blocks found. */
393 size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0;
394 size_t used_blocks = 0; /* Total number of used blocks found. */
395 size_t used_size = 0; /* Total size (bytes) of used blocks. */
397 /* Read the pages and blocks. The aim here is to be robust against
398 * corrupt or malicious registries. So we make sure the loops
399 * always make forward progress. We add the address of each block
400 * we read to a hash table so pointers will only reference the start
404 struct ntreg_hbin_page *page;
405 for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) {
406 if (off >= h->endpages)
409 page = (struct ntreg_hbin_page *) (h->addr + off);
410 if (page->magic[0] != 'h' ||
411 page->magic[1] != 'b' ||
412 page->magic[2] != 'i' ||
413 page->magic[3] != 'n') {
414 fprintf (stderr, "hivex: %s: trailing garbage at end of file (at 0x%zx, after %zu pages)\n",
415 filename, off, pages);
420 size_t page_size = le32toh (page->page_size);
422 fprintf (stderr, "hivex_open: page at 0x%zx, size %zu\n", off, page_size);
424 if (page_size < smallest_page) smallest_page = page_size;
425 if (page_size > largest_page) largest_page = page_size;
427 if (page_size <= sizeof (struct ntreg_hbin_page) ||
428 (page_size & 0x0fff) != 0) {
429 fprintf (stderr, "hivex: %s: page size %zu at 0x%zx, bad registry\n",
430 filename, page_size, off);
435 /* Read the blocks in this page. */
437 struct ntreg_hbin_block *block;
439 for (blkoff = off + 0x20;
440 blkoff < off + page_size;
444 int is_root = blkoff == h->rootoffs;
448 block = (struct ntreg_hbin_block *) (h->addr + blkoff);
450 seg_len = block_len (h, blkoff, &used);
451 if (seg_len <= 4 || (seg_len & 3) != 0) {
452 fprintf (stderr, "hivex: %s: block size %" PRIu32 " at 0x%zx, bad registry\n",
453 filename, le32toh (block->seg_len), blkoff);
459 fprintf (stderr, "hivex_open: %s block id %d,%d at 0x%zx size %zu%s\n",
460 used ? "used" : "free", block->id[0], block->id[1], blkoff,
461 seg_len, is_root ? " (root)" : "");
463 blocks_bytes += seg_len;
464 if (seg_len < smallest_block) smallest_block = seg_len;
465 if (seg_len > largest_block) largest_block = seg_len;
467 if (is_root && !used)
472 used_size += seg_len;
474 /* Root block must be an nk-block. */
475 if (is_root && (block->id[0] != 'n' || block->id[1] != 'k'))
478 /* Note this blkoff is a valid address. */
479 BITMAP_SET (h->bitmap, blkoff);
484 if (!seen_root_block) {
485 fprintf (stderr, "hivex: %s: no root block found\n", filename);
490 if (bad_root_block) {
491 fprintf (stderr, "hivex: %s: bad root block (free or not nk)\n", filename);
498 "hivex_open: successfully read Windows Registry hive file:\n"
499 " pages: %zu [sml: %zu, lge: %zu]\n"
500 " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n"
501 " blocks used: %zu\n"
502 " bytes used: %zu\n",
503 pages, smallest_page, largest_page,
504 blocks, smallest_block, blocks_bytes / blocks, largest_block,
505 used_blocks, used_size);
513 if (h->addr && h->size && h->addr != MAP_FAILED) {
515 munmap (h->addr, h->size);
529 hivex_close (hive_h *h)
535 munmap (h->addr, h->size);
545 /*----------------------------------------------------------------------
550 hivex_root (hive_h *h)
552 hive_node_h ret = h->rootoffs;
553 if (!IS_VALID_BLOCK (h, ret)) {
561 hivex_node_name (hive_h *h, hive_node_h node)
563 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
568 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
570 /* AFAIK the node name is always plain ASCII, so no conversion
571 * to UTF-8 is necessary. However we do need to nul-terminate
575 /* nk->name_len is unsigned, 16 bit, so this is safe ... However
576 * we have to make sure the length doesn't exceed the block length.
578 size_t len = le16toh (nk->name_len);
579 size_t seg_len = block_len (h, node, NULL);
580 if (sizeof (struct ntreg_nk_record) + len - 1 > seg_len) {
582 fprintf (stderr, "hivex_node_name: returning EFAULT because node name is too long (%zu, %zu)\n",
588 char *ret = malloc (len + 1);
591 memcpy (ret, nk->name, len);
597 /* I think the documentation for the sk and classname fields in the nk
598 * record is wrong, or else the offset field is in the wrong place.
599 * Otherwise this makes no sense. Disabled this for now -- it's not
600 * useful for reading the registry anyway.
604 hivex_node_security (hive_h *h, hive_node_h node)
606 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
611 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
613 hive_node_h ret = le32toh (nk->sk);
615 if (!IS_VALID_BLOCK (h, ret)) {
623 hivex_node_classname (hive_h *h, hive_node_h node)
625 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
630 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
632 hive_node_h ret = le32toh (nk->classname);
634 if (!IS_VALID_BLOCK (h, ret)) {
642 /* Structure for returning 0-terminated lists of offsets (nodes,
652 init_offset_list (struct offset_list *list)
656 list->offsets = NULL;
659 #define INIT_OFFSET_LIST(name) \
660 struct offset_list name; \
661 init_offset_list (&name)
663 /* Preallocates the offset_list, but doesn't make the contents longer. */
665 grow_offset_list (struct offset_list *list, size_t alloc)
667 assert (alloc >= list->len);
668 size_t *p = realloc (list->offsets, alloc * sizeof (size_t));
677 add_to_offset_list (struct offset_list *list, size_t offset)
679 if (list->len >= list->alloc) {
680 if (grow_offset_list (list, list->alloc ? list->alloc * 2 : 4) == -1)
683 list->offsets[list->len] = offset;
689 free_offset_list (struct offset_list *list)
691 free (list->offsets);
695 return_offset_list (struct offset_list *list)
697 if (add_to_offset_list (list, 0) == -1)
699 return list->offsets; /* caller frees */
702 /* Iterate over children, returning child nodes and intermediate blocks. */
703 #define GET_CHILDREN_NO_CHECK_NK 1
706 get_children (hive_h *h, hive_node_h node,
707 hive_node_h **children_ret, size_t **blocks_ret,
710 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
715 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
717 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
719 INIT_OFFSET_LIST (children);
720 INIT_OFFSET_LIST (blocks);
722 /* Deal with the common "no subkeys" case quickly. */
723 if (nr_subkeys_in_nk == 0)
726 /* Arbitrarily limit the number of subkeys we will ever deal with. */
727 if (nr_subkeys_in_nk > HIVEX_MAX_SUBKEYS) {
732 /* Preallocate space for the children. */
733 if (grow_offset_list (&children, nr_subkeys_in_nk) == -1)
736 /* The subkey_lf field can point either to an lf-record, which is
737 * the common case, or if there are lots of subkeys, to an
740 size_t subkey_lf = le32toh (nk->subkey_lf);
742 if (!IS_VALID_BLOCK (h, subkey_lf)) {
744 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey_lf is not a valid block (0x%zx)\n",
750 if (add_to_offset_list (&blocks, subkey_lf) == -1)
753 struct ntreg_hbin_block *block =
754 (struct ntreg_hbin_block *) (h->addr + subkey_lf);
756 /* Points to lf-record? (Note, also "lh" but that is basically the
757 * same as "lf" as far as we are concerned here).
759 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
760 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
762 /* Check number of subkeys in the nk-record matches number of subkeys
765 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
768 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, nr_subkeys_in_lf = %zu\n",
769 nr_subkeys_in_nk, nr_subkeys_in_lf);
771 if (nr_subkeys_in_nk != nr_subkeys_in_lf) {
776 size_t len = block_len (h, subkey_lf, NULL);
777 if (8 + nr_subkeys_in_lf * 8 > len) {
779 fprintf (stderr, "hivex_node_children: returning EFAULT because too many subkeys (%zu, %zu)\n",
780 nr_subkeys_in_lf, len);
786 for (i = 0; i < nr_subkeys_in_lf; ++i) {
787 hive_node_h subkey = le32toh (lf->keys[i].offset);
789 if (!(flags & GET_CHILDREN_NO_CHECK_NK)) {
790 if (!IS_VALID_BLOCK (h, subkey)) {
792 fprintf (stderr, "hivex_node_children: returning EFAULT because subkey is not a valid block (0x%zx)\n",
798 if (add_to_offset_list (&children, subkey) == -1)
803 /* Points to ri-record? */
804 else if (block->id[0] == 'r' && block->id[1] == 'i') {
805 struct ntreg_ri_record *ri = (struct ntreg_ri_record *) block;
807 size_t nr_offsets = le16toh (ri->nr_offsets);
809 /* Count total number of children. */
811 for (i = 0; i < nr_offsets; ++i) {
812 hive_node_h offset = le32toh (ri->offset[i]);
814 if (!IS_VALID_BLOCK (h, offset)) {
816 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
821 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
823 fprintf (stderr, "get_children: returning ENOTSUP because ri-record offset does not point to lf/lh (0x%zx)\n",
829 if (add_to_offset_list (&blocks, offset) == -1)
832 struct ntreg_lf_record *lf =
833 (struct ntreg_lf_record *) (h->addr + offset);
835 count += le16toh (lf->nr_keys);
839 fprintf (stderr, "hivex_node_children: nr_subkeys_in_nk = %zu, counted = %zu\n",
840 nr_subkeys_in_nk, count);
842 if (nr_subkeys_in_nk != count) {
847 /* Copy list of children. Note nr_subkeys_in_nk is limited to
848 * something reasonable above.
850 for (i = 0; i < nr_offsets; ++i) {
851 hive_node_h offset = le32toh (ri->offset[i]);
853 if (!IS_VALID_BLOCK (h, offset)) {
855 fprintf (stderr, "hivex_node_children: returning EFAULT because ri-offset is not a valid block (0x%zx)\n",
860 if (!BLOCK_ID_EQ (h, offset, "lf") && !BLOCK_ID_EQ (h, offset, "lh")) {
862 fprintf (stderr, "get_children: returning ENOTSUP because ri-record offset does not point to lf/lh (0x%zx)\n",
868 struct ntreg_lf_record *lf =
869 (struct ntreg_lf_record *) (h->addr + offset);
872 for (j = 0; j < le16toh (lf->nr_keys); ++j) {
873 hive_node_h subkey = le32toh (lf->keys[j].offset);
875 if (!(flags & GET_CHILDREN_NO_CHECK_NK)) {
876 if (!IS_VALID_BLOCK (h, subkey)) {
878 fprintf (stderr, "hivex_node_children: returning EFAULT because indirect subkey is not a valid block (0x%zx)\n",
884 if (add_to_offset_list (&children, subkey) == -1)
890 /* else not supported, set errno and fall through */
892 fprintf (stderr, "get_children: returning ENOTSUP because subkey block is not lf/lh/ri (0x%zx, %d, %d)\n",
893 subkey_lf, block->id[0], block->id[1]);
896 free_offset_list (&children);
897 free_offset_list (&blocks);
901 *children_ret = return_offset_list (&children);
902 *blocks_ret = return_offset_list (&blocks);
903 if (!*children_ret || !*blocks_ret)
909 hivex_node_children (hive_h *h, hive_node_h node)
911 hive_node_h *children;
914 if (get_children (h, node, &children, &blocks, 0) == -1)
921 /* Very inefficient, but at least having a separate API call
922 * allows us to make it more efficient in future.
925 hivex_node_get_child (hive_h *h, hive_node_h node, const char *nname)
927 hive_node_h *children = NULL;
931 children = hivex_node_children (h, node);
932 if (!children) goto error;
935 for (i = 0; children[i] != 0; ++i) {
936 name = hivex_node_name (h, children[i]);
937 if (!name) goto error;
938 if (STRCASEEQ (name, nname)) {
942 free (name); name = NULL;
952 hivex_node_parent (hive_h *h, hive_node_h node)
954 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
959 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
961 hive_node_h ret = le32toh (nk->parent);
963 if (!IS_VALID_BLOCK (h, ret)) {
965 fprintf (stderr, "hivex_node_parent: returning EFAULT because parent is not a valid block (0x%zx)\n",
974 get_values (hive_h *h, hive_node_h node,
975 hive_value_h **values_ret, size_t **blocks_ret)
977 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
982 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
984 size_t nr_values = le32toh (nk->nr_values);
987 fprintf (stderr, "hivex_node_values: nr_values = %zu\n", nr_values);
989 INIT_OFFSET_LIST (values);
990 INIT_OFFSET_LIST (blocks);
992 /* Deal with the common "no values" case quickly. */
996 /* Arbitrarily limit the number of values we will ever deal with. */
997 if (nr_values > HIVEX_MAX_VALUES) {
1002 /* Preallocate space for the values. */
1003 if (grow_offset_list (&values, nr_values) == -1)
1006 /* Get the value list and check it looks reasonable. */
1007 size_t vlist_offset = le32toh (nk->vallist);
1008 vlist_offset += 0x1000;
1009 if (!IS_VALID_BLOCK (h, vlist_offset)) {
1011 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is not a valid block (0x%zx)\n",
1017 if (add_to_offset_list (&blocks, vlist_offset) == -1)
1020 struct ntreg_value_list *vlist =
1021 (struct ntreg_value_list *) (h->addr + vlist_offset);
1023 size_t len = block_len (h, vlist_offset, NULL);
1024 if (4 + nr_values * 4 > len) {
1026 fprintf (stderr, "hivex_node_values: returning EFAULT because value list is too long (%zu, %zu)\n",
1033 for (i = 0; i < nr_values; ++i) {
1034 hive_node_h value = vlist->offset[i];
1036 if (!IS_VALID_BLOCK (h, value)) {
1038 fprintf (stderr, "hivex_node_values: returning EFAULT because value is not a valid block (0x%zx)\n",
1043 if (add_to_offset_list (&values, value) == -1)
1048 *values_ret = return_offset_list (&values);
1049 *blocks_ret = return_offset_list (&blocks);
1050 if (!*values_ret || !*blocks_ret)
1055 free_offset_list (&values);
1056 free_offset_list (&blocks);
1061 hivex_node_values (hive_h *h, hive_node_h node)
1063 hive_value_h *values;
1066 if (get_values (h, node, &values, &blocks) == -1)
1073 /* Very inefficient, but at least having a separate API call
1074 * allows us to make it more efficient in future.
1077 hivex_node_get_value (hive_h *h, hive_node_h node, const char *key)
1079 hive_value_h *values = NULL;
1081 hive_value_h ret = 0;
1083 values = hivex_node_values (h, node);
1084 if (!values) goto error;
1087 for (i = 0; values[i] != 0; ++i) {
1088 name = hivex_value_key (h, values[i]);
1089 if (!name) goto error;
1090 if (STRCASEEQ (name, key)) {
1094 free (name); name = NULL;
1104 hivex_value_key (hive_h *h, hive_value_h value)
1106 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1111 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1113 /* AFAIK the key is always plain ASCII, so no conversion to UTF-8 is
1114 * necessary. However we do need to nul-terminate the string.
1117 /* vk->name_len is unsigned, 16 bit, so this is safe ... However
1118 * we have to make sure the length doesn't exceed the block length.
1120 size_t len = le16toh (vk->name_len);
1121 size_t seg_len = block_len (h, value, NULL);
1122 if (sizeof (struct ntreg_vk_record) + len - 1 > seg_len) {
1124 fprintf (stderr, "hivex_value_key: returning EFAULT because key length is too long (%zu, %zu)\n",
1130 char *ret = malloc (len + 1);
1133 memcpy (ret, vk->name, len);
1139 hivex_value_type (hive_h *h, hive_value_h value, hive_type *t, size_t *len)
1141 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1146 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1149 *t = le32toh (vk->data_type);
1152 *len = le32toh (vk->data_len);
1153 *len &= 0x7fffffff; /* top bit indicates if data is stored inline */
1160 hivex_value_value (hive_h *h, hive_value_h value,
1161 hive_type *t_rtn, size_t *len_rtn)
1163 if (!IS_VALID_BLOCK (h, value) || !BLOCK_ID_EQ (h, value, "vk")) {
1168 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + value);
1174 t = le32toh (vk->data_type);
1176 len = le32toh (vk->data_len);
1177 is_inline = !!(len & 0x80000000);
1181 fprintf (stderr, "hivex_value_value: value=0x%zx, t=%d, len=%zu, inline=%d\n",
1182 value, t, len, is_inline);
1189 if (is_inline && len > 4) {
1194 /* Arbitrarily limit the length that we will read. */
1195 if (len > HIVEX_MAX_VALUE_LEN) {
1200 char *ret = malloc (len);
1205 memcpy (ret, (char *) &vk->data_offset, len);
1209 size_t data_offset = le32toh (vk->data_offset);
1210 data_offset += 0x1000;
1211 if (!IS_VALID_BLOCK (h, data_offset)) {
1213 fprintf (stderr, "hivex_value_value: returning EFAULT because data offset is not a valid block (0x%zx)\n",
1220 /* Check that the declared size isn't larger than the block its in.
1222 * XXX Some apparently valid registries are seen to have this,
1223 * so turn this into a warning and substitute the smaller length
1226 size_t blen = block_len (h, data_offset, NULL);
1227 if (len > blen - 4 /* subtract 4 for block header */) {
1229 fprintf (stderr, "hivex_value_value: warning: declared data length is longer than the block it is in (data 0x%zx, data len %zu, block len %zu)\n",
1230 data_offset, len, blen);
1234 char *data = h->addr + data_offset + 4;
1235 memcpy (ret, data, len);
1240 windows_utf16_to_utf8 (/* const */ char *input, size_t len)
1242 iconv_t ic = iconv_open ("UTF-8", "UTF-16");
1243 if (ic == (iconv_t) -1)
1246 /* iconv(3) has an insane interface ... */
1248 /* Mostly UTF-8 will be smaller, so this is a good initial guess. */
1249 size_t outalloc = len;
1253 size_t outlen = outalloc;
1254 char *out = malloc (outlen + 1);
1264 size_t r = iconv (ic, &inp, &inlen, &outp, &outlen);
1265 if (r == (size_t) -1) {
1266 if (errno == E2BIG) {
1267 size_t prev = outalloc;
1268 /* Try again with a larger output buffer. */
1271 if (outalloc < prev)
1276 /* Else some conversion failure, eg. EILSEQ, EINVAL. */
1292 hivex_value_string (hive_h *h, hive_value_h value)
1296 char *data = hivex_value_value (h, value, &t, &len);
1301 if (t != hive_t_string && t != hive_t_expand_string && t != hive_t_link) {
1307 char *ret = windows_utf16_to_utf8 (data, len);
1316 free_strings (char **argv)
1321 for (i = 0; argv[i] != NULL; ++i)
1327 /* Get the length of a UTF-16 format string. Handle the string as
1328 * pairs of bytes, looking for the first \0\0 pair.
1331 utf16_string_len_in_bytes (const char *str)
1335 while (str[0] || str[1]) {
1343 /* http://blogs.msdn.com/oldnewthing/archive/2009/10/08/9904646.aspx */
1345 hivex_value_multiple_strings (hive_h *h, hive_value_h value)
1349 char *data = hivex_value_value (h, value, &t, &len);
1354 if (t != hive_t_multiple_strings) {
1360 size_t nr_strings = 0;
1361 char **ret = malloc ((1 + nr_strings) * sizeof (char *));
1371 while (p < data + len && (plen = utf16_string_len_in_bytes (p)) > 0) {
1373 char **ret2 = realloc (ret, (1 + nr_strings) * sizeof (char *));
1381 ret[nr_strings-1] = windows_utf16_to_utf8 (p, plen);
1382 ret[nr_strings] = NULL;
1383 if (ret[nr_strings-1] == NULL) {
1389 p += plen + 2 /* skip over UTF-16 \0\0 at the end of this string */;
1397 hivex_value_dword (hive_h *h, hive_value_h value)
1401 char *data = hivex_value_value (h, value, &t, &len);
1406 if ((t != hive_t_dword && t != hive_t_dword_be) || len != 4) {
1412 int32_t ret = *(int32_t*)data;
1414 if (t == hive_t_dword) /* little endian */
1415 ret = le32toh (ret);
1417 ret = be32toh (ret);
1423 hivex_value_qword (hive_h *h, hive_value_h value)
1427 char *data = hivex_value_value (h, value, &t, &len);
1432 if (t != hive_t_qword || len != 8) {
1438 int64_t ret = *(int64_t*)data;
1440 ret = le64toh (ret); /* always little endian */
1445 /*----------------------------------------------------------------------
1450 hivex_visit (hive_h *h, const struct hivex_visitor *visitor, size_t len,
1451 void *opaque, int flags)
1453 return hivex_visit_node (h, hivex_root (h), visitor, len, opaque, flags);
1456 static int hivex__visit_node (hive_h *h, hive_node_h node, const struct hivex_visitor *vtor, char *unvisited, void *opaque, int flags);
1459 hivex_visit_node (hive_h *h, hive_node_h node,
1460 const struct hivex_visitor *visitor, size_t len, void *opaque,
1463 struct hivex_visitor vtor;
1464 memset (&vtor, 0, sizeof vtor);
1466 /* Note that len might be larger *or smaller* than the expected size. */
1467 size_t copysize = len <= sizeof vtor ? len : sizeof vtor;
1468 memcpy (&vtor, visitor, copysize);
1470 /* This bitmap records unvisited nodes, so we don't loop if the
1471 * registry contains cycles.
1473 char *unvisited = malloc (1 + h->size / 32);
1474 if (unvisited == NULL)
1476 memcpy (unvisited, h->bitmap, 1 + h->size / 32);
1478 int r = hivex__visit_node (h, node, &vtor, unvisited, opaque, flags);
1484 hivex__visit_node (hive_h *h, hive_node_h node,
1485 const struct hivex_visitor *vtor, char *unvisited,
1486 void *opaque, int flags)
1488 int skip_bad = flags & HIVEX_VISIT_SKIP_BAD;
1490 hive_value_h *values = NULL;
1491 hive_node_h *children = NULL;
1497 /* Return -1 on all callback errors. However on internal errors,
1498 * check if skip_bad is set and suppress those errors if so.
1502 if (!BITMAP_TST (unvisited, node)) {
1504 fprintf (stderr, "hivex__visit_node: contains cycle: visited node 0x%zx already\n",
1508 return skip_bad ? 0 : -1;
1510 BITMAP_CLR (unvisited, node);
1512 name = hivex_node_name (h, node);
1513 if (!name) return skip_bad ? 0 : -1;
1514 if (vtor->node_start && vtor->node_start (h, opaque, node, name) == -1)
1517 values = hivex_node_values (h, node);
1519 ret = skip_bad ? 0 : -1;
1523 for (i = 0; values[i] != 0; ++i) {
1527 if (hivex_value_type (h, values[i], &t, &len) == -1) {
1528 ret = skip_bad ? 0 : -1;
1532 key = hivex_value_key (h, values[i]);
1534 ret = skip_bad ? 0 : -1;
1538 if (vtor->value_any) {
1539 str = hivex_value_value (h, values[i], &t, &len);
1541 ret = skip_bad ? 0 : -1;
1544 if (vtor->value_any (h, opaque, node, values[i], t, len, key, str) == -1)
1546 free (str); str = NULL;
1551 str = hivex_value_value (h, values[i], &t, &len);
1553 ret = skip_bad ? 0 : -1;
1556 if (t != hive_t_none) {
1557 ret = skip_bad ? 0 : -1;
1560 if (vtor->value_none &&
1561 vtor->value_none (h, opaque, node, values[i], t, len, key, str) == -1)
1563 free (str); str = NULL;
1567 case hive_t_expand_string:
1569 str = hivex_value_string (h, values[i]);
1571 if (errno != EILSEQ && errno != EINVAL) {
1572 ret = skip_bad ? 0 : -1;
1575 if (vtor->value_string_invalid_utf16) {
1576 str = hivex_value_value (h, values[i], &t, &len);
1577 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1579 free (str); str = NULL;
1583 if (vtor->value_string &&
1584 vtor->value_string (h, opaque, node, values[i], t, len, key, str) == -1)
1586 free (str); str = NULL;
1590 case hive_t_dword_be: {
1591 int32_t i32 = hivex_value_dword (h, values[i]);
1592 if (vtor->value_dword &&
1593 vtor->value_dword (h, opaque, node, values[i], t, len, key, i32) == -1)
1598 case hive_t_qword: {
1599 int64_t i64 = hivex_value_qword (h, values[i]);
1600 if (vtor->value_qword &&
1601 vtor->value_qword (h, opaque, node, values[i], t, len, key, i64) == -1)
1607 str = hivex_value_value (h, values[i], &t, &len);
1609 ret = skip_bad ? 0 : -1;
1612 if (t != hive_t_binary) {
1613 ret = skip_bad ? 0 : -1;
1616 if (vtor->value_binary &&
1617 vtor->value_binary (h, opaque, node, values[i], t, len, key, str) == -1)
1619 free (str); str = NULL;
1622 case hive_t_multiple_strings:
1623 strs = hivex_value_multiple_strings (h, values[i]);
1625 if (errno != EILSEQ && errno != EINVAL) {
1626 ret = skip_bad ? 0 : -1;
1629 if (vtor->value_string_invalid_utf16) {
1630 str = hivex_value_value (h, values[i], &t, &len);
1631 if (vtor->value_string_invalid_utf16 (h, opaque, node, values[i], t, len, key, str) == -1)
1633 free (str); str = NULL;
1637 if (vtor->value_multiple_strings &&
1638 vtor->value_multiple_strings (h, opaque, node, values[i], t, len, key, strs) == -1)
1640 free_strings (strs); strs = NULL;
1643 case hive_t_resource_list:
1644 case hive_t_full_resource_description:
1645 case hive_t_resource_requirements_list:
1647 str = hivex_value_value (h, values[i], &t, &len);
1649 ret = skip_bad ? 0 : -1;
1652 if (vtor->value_other &&
1653 vtor->value_other (h, opaque, node, values[i], t, len, key, str) == -1)
1655 free (str); str = NULL;
1660 free (key); key = NULL;
1663 children = hivex_node_children (h, node);
1664 if (children == NULL) {
1665 ret = skip_bad ? 0 : -1;
1669 for (i = 0; children[i] != 0; ++i) {
1671 fprintf (stderr, "hivex__visit_node: %s: visiting subkey %d (0x%zx)\n",
1672 name, i, children[i]);
1674 if (hivex__visit_node (h, children[i], vtor, unvisited, opaque, flags) == -1)
1678 if (vtor->node_end && vtor->node_end (h, opaque, node, name) == -1)
1689 free_strings (strs);
1693 /*----------------------------------------------------------------------
1697 /* Allocate an hbin (page), extending the malloc'd space if necessary,
1698 * and updating the hive handle fields (but NOT the hive disk header
1699 * -- the hive disk header is updated when we commit). This function
1700 * also extends the bitmap if necessary.
1702 * 'allocation_hint' is the size of the block allocation we would like
1703 * to make. Normally registry blocks are very small (avg 50 bytes)
1704 * and are contained in standard-sized pages (4KB), but the registry
1705 * can support blocks which are larger than a standard page, in which
1706 * case it creates a page of 8KB, 12KB etc.
1709 * > 0 : offset of first usable byte of new page (after page header)
1710 * 0 : error (errno set)
1713 allocate_page (hive_h *h, size_t allocation_hint)
1715 /* In almost all cases this will be 1. */
1716 size_t nr_4k_pages =
1717 1 + (allocation_hint + sizeof (struct ntreg_hbin_page) - 1) / 4096;
1718 assert (nr_4k_pages >= 1);
1720 /* 'extend' is the number of bytes to extend the file by. Note that
1721 * hives found in the wild often contain slack between 'endpages'
1722 * and the actual end of the file, so we don't always need to make
1725 ssize_t extend = h->endpages + nr_4k_pages * 4096 - h->size;
1727 if (h->msglvl >= 2) {
1728 fprintf (stderr, "allocate_page: current endpages = 0x%zx, current size = 0x%zx\n",
1729 h->endpages, h->size);
1730 fprintf (stderr, "allocate_page: extending file by %zd bytes (<= 0 if no extension)\n",
1735 size_t oldsize = h->size;
1736 size_t newsize = h->size + extend;
1737 char *newaddr = realloc (h->addr, newsize);
1738 if (newaddr == NULL)
1741 size_t oldbitmapsize = 1 + oldsize / 32;
1742 size_t newbitmapsize = 1 + newsize / 32;
1743 char *newbitmap = realloc (h->bitmap, newbitmapsize);
1744 if (newbitmap == NULL) {
1751 h->bitmap = newbitmap;
1753 memset (h->addr + oldsize, 0, newsize - oldsize);
1754 memset (h->bitmap + oldbitmapsize, 0, newbitmapsize - oldbitmapsize);
1757 size_t offset = h->endpages;
1758 h->endpages += nr_4k_pages * 4096;
1761 fprintf (stderr, "allocate_page: new endpages = 0x%zx, new size = 0x%zx\n",
1762 h->endpages, h->size);
1764 /* Write the hbin header. */
1765 struct ntreg_hbin_page *page =
1766 (struct ntreg_hbin_page *) (h->addr + offset);
1767 page->magic[0] = 'h';
1768 page->magic[1] = 'b';
1769 page->magic[2] = 'i';
1770 page->magic[3] = 'n';
1771 page->offset_first = htole32 (offset - 0x1000);
1772 page->page_size = htole32 (nr_4k_pages * 4096);
1773 memset (page->unknown, 0, sizeof (page->unknown));
1776 fprintf (stderr, "allocate_page: new page at 0x%zx\n", offset);
1778 /* Offset of first usable byte after the header. */
1779 return offset + sizeof (struct ntreg_hbin_page);
1782 /* Allocate a single block, first allocating an hbin (page) at the end
1783 * of the current file if necessary. NB. To keep the implementation
1784 * simple and more likely to be correct, we do not reuse existing free
1787 * seg_len is the size of the block (this INCLUDES the block header).
1788 * The header of the block is initialized to -seg_len (negative to
1789 * indicate used). id[2] is the block ID (type), eg. "nk" for nk-
1790 * record. The block bitmap is updated to show this block as valid.
1791 * The rest of the contents of the block will be zero.
1794 * > 0 : offset of new block
1795 * 0 : error (errno set)
1798 allocate_block (hive_h *h, size_t seg_len, const char id[2])
1806 /* The caller probably forgot to include the header. Note that
1807 * value lists have no ID field, so seg_len == 4 would be possible
1808 * for them, albeit unusual.
1811 fprintf (stderr, "allocate_block: refusing too small allocation (%zu), returning ERANGE\n",
1817 /* Refuse really large allocations. */
1818 if (seg_len > HIVEX_MAX_ALLOCATION) {
1820 fprintf (stderr, "allocate_block: refusing large allocation (%zu), returning ERANGE\n",
1826 /* Round up allocation to multiple of 8 bytes. All blocks must be
1827 * on an 8 byte boundary.
1829 seg_len = (seg_len + 7) & ~7;
1831 /* Allocate a new page if necessary. */
1832 if (h->endblocks == 0 || h->endblocks + seg_len > h->endpages) {
1833 size_t newendblocks = allocate_page (h, seg_len);
1834 if (newendblocks == 0)
1836 h->endblocks = newendblocks;
1839 size_t offset = h->endblocks;
1842 fprintf (stderr, "allocate_block: new block at 0x%zx, size %zu\n",
1845 struct ntreg_hbin_block *blockhdr =
1846 (struct ntreg_hbin_block *) (h->addr + offset);
1848 blockhdr->seg_len = htole32 (- (int32_t) seg_len);
1849 if (id[0] && id[1] && seg_len >= sizeof (struct ntreg_hbin_block)) {
1850 blockhdr->id[0] = id[0];
1851 blockhdr->id[1] = id[1];
1854 BITMAP_SET (h->bitmap, offset);
1856 h->endblocks += seg_len;
1858 /* If there is space after the last block in the last page, then we
1859 * have to put a dummy free block header here to mark the rest of
1862 ssize_t rem = h->endpages - h->endblocks;
1865 fprintf (stderr, "allocate_block: marking remainder of page free starting at 0x%zx, size %zd\n",
1870 blockhdr = (struct ntreg_hbin_block *) (h->addr + h->endblocks);
1871 blockhdr->seg_len = htole32 ((int32_t) rem);
1877 /* 'offset' must point to a valid, used block. This function marks
1878 * the block unused (by updating the seg_len field) and invalidates
1879 * the bitmap. It does NOT do this recursively, so to avoid creating
1880 * unreachable used blocks, callers may have to recurse over the hive
1881 * structures. Also callers must ensure there are no references to
1882 * this block from other parts of the hive.
1885 mark_block_unused (hive_h *h, size_t offset)
1887 assert (h->writable);
1888 assert (IS_VALID_BLOCK (h, offset));
1891 fprintf (stderr, "mark_block_unused: marking 0x%zx unused\n", offset);
1893 struct ntreg_hbin_block *blockhdr =
1894 (struct ntreg_hbin_block *) (h->addr + offset);
1896 size_t seg_len = block_len (h, offset, NULL);
1897 blockhdr->seg_len = htole32 (seg_len);
1899 BITMAP_CLR (h->bitmap, offset);
1902 /* Delete all existing values at this node. */
1904 delete_values (hive_h *h, hive_node_h node)
1906 assert (h->writable);
1908 hive_value_h *values;
1910 if (get_values (h, node, &values, &blocks) == -1)
1914 for (i = 0; blocks[i] != 0; ++i)
1915 mark_block_unused (h, blocks[i]);
1919 for (i = 0; values[i] != 0; ++i) {
1920 struct ntreg_vk_record *vk =
1921 (struct ntreg_vk_record *) (h->addr + values[i]);
1925 len = le32toh (vk->data_len);
1926 is_inline = !!(len & 0x80000000); /* top bit indicates is inline */
1929 if (!is_inline) { /* non-inline, so remove data block */
1930 size_t data_offset = le32toh (vk->data_offset);
1931 data_offset += 0x1000;
1932 mark_block_unused (h, data_offset);
1935 /* remove vk record */
1936 mark_block_unused (h, values[i]);
1941 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
1942 nk->nr_values = htole32 (0);
1943 nk->vallist = htole32 (0xffffffff);
1949 hivex_commit (hive_h *h, const char *filename, int flags)
1961 filename = filename ? : h->filename;
1962 int fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
1966 /* Update the header fields. */
1967 uint32_t sequence = le32toh (h->hdr->sequence1);
1969 h->hdr->sequence1 = htole32 (sequence);
1970 h->hdr->sequence2 = htole32 (sequence);
1971 /* XXX Ought to update h->hdr->last_modified. */
1972 h->hdr->blocks = htole32 (h->endpages - 0x1000);
1974 /* Recompute header checksum. */
1975 uint32_t sum = header_checksum (h);
1976 h->hdr->csum = htole32 (sum);
1979 fprintf (stderr, "hivex_commit: new header checksum: 0x%x\n", sum);
1981 if (full_write (fd, h->addr, h->size) != h->size) {
1988 if (close (fd) == -1)
1994 /* Calculate the hash for a lf or lh record offset.
1997 calc_hash (const char *type, const char *name, char *ret)
1999 size_t len = strlen (name);
2001 if (STRPREFIX (type, "lf"))
2002 /* Old-style, not used in current registries. */
2003 memcpy (ret, name, len < 4 ? len : 4);
2005 /* New-style for lh-records. */
2008 for (i = 0; i < len; ++i) {
2009 c = c_toupper (name[i]);
2013 *((uint32_t *) ret) = htole32 (h);
2017 /* Create a completely new lh-record containing just the single node. */
2019 new_lh_record (hive_h *h, const char *name, hive_node_h node)
2021 static const char id[2] = { 'l', 'h' };
2022 size_t seg_len = sizeof (struct ntreg_lf_record);
2023 size_t offset = allocate_block (h, seg_len, id);
2027 struct ntreg_lf_record *lh = (struct ntreg_lf_record *) (h->addr + offset);
2028 lh->nr_keys = htole16 (1);
2029 lh->keys[0].offset = htole32 (node - 0x1000);
2030 calc_hash ("lh", name, lh->keys[0].hash);
2035 /* Insert node into existing lf/lh-record at position.
2036 * This allocates a new record and marks the old one as unused.
2039 insert_lf_record (hive_h *h, size_t old_offs, size_t posn,
2040 const char *name, hive_node_h node)
2042 assert (IS_VALID_BLOCK (h, old_offs));
2044 /* Work around C stupidity.
2045 * http://www.redhat.com/archives/libguestfs/2010-February/msg00056.html
2047 int test = BLOCK_ID_EQ (h, old_offs, "lf") || BLOCK_ID_EQ (h, old_offs, "lh");
2050 struct ntreg_lf_record *old_lf =
2051 (struct ntreg_lf_record *) (h->addr + old_offs);
2052 size_t nr_keys = le16toh (old_lf->nr_keys);
2054 nr_keys++; /* in new record ... */
2056 size_t seg_len = sizeof (struct ntreg_lf_record) + (nr_keys-1) * 8;
2057 size_t new_offs = allocate_block (h, seg_len, old_lf->id);
2061 struct ntreg_lf_record *new_lf =
2062 (struct ntreg_lf_record *) (h->addr + new_offs);
2063 new_lf->nr_keys = htole16 (nr_keys);
2065 /* Copy the keys until we reach posn, insert the new key there, then
2066 * copy the remaining keys.
2069 for (i = 0; i < posn; ++i)
2070 new_lf->keys[i] = old_lf->keys[i];
2072 new_lf->keys[i].offset = htole32 (node - 0x1000);
2073 calc_hash (new_lf->id, name, new_lf->keys[i].hash);
2075 for (i = posn+1; i < nr_keys; ++i)
2076 new_lf->keys[i] = old_lf->keys[i-1];
2078 /* Old block is unused, return new block. */
2079 mark_block_unused (h, old_offs);
2083 /* Compare name with name in nk-record. */
2085 compare_name_with_nk_name (hive_h *h, const char *name, hive_node_h nk_offs)
2087 assert (IS_VALID_BLOCK (h, nk_offs));
2088 assert (BLOCK_ID_EQ (h, nk_offs, "nk"));
2090 /* Name in nk is not necessarily nul-terminated. */
2091 char *nname = hivex_node_name (h, nk_offs);
2093 /* Unfortunately we don't have a way to return errors here. */
2095 perror ("compare_name_with_nk_name");
2099 int r = strcasecmp (name, nname);
2106 hivex_node_add_child (hive_h *h, hive_node_h parent, const char *name)
2113 if (!IS_VALID_BLOCK (h, parent) || !BLOCK_ID_EQ (h, parent, "nk")) {
2118 if (name == NULL || strlen (name) == 0) {
2123 if (hivex_node_get_child (h, parent, name) != 0) {
2128 /* Create the new nk-record. */
2129 static const char nk_id[2] = { 'n', 'k' };
2130 size_t seg_len = sizeof (struct ntreg_nk_record) + strlen (name);
2131 hive_node_h node = allocate_block (h, seg_len, nk_id);
2136 fprintf (stderr, "hivex_node_add_child: allocated new nk-record for child at 0x%zx\n", node);
2138 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2139 nk->flags = htole16 (0x0020); /* key is ASCII. */
2140 nk->parent = htole32 (parent - 0x1000);
2141 nk->subkey_lf = htole32 (0xffffffff);
2142 nk->subkey_lf_volatile = htole32 (0xffffffff);
2143 nk->vallist = htole32 (0xffffffff);
2144 nk->classname = htole32 (0xffffffff);
2145 nk->name_len = htole16 (strlen (name));
2146 strcpy (nk->name, name);
2148 /* Inherit parent sk. */
2149 struct ntreg_nk_record *parent_nk =
2150 (struct ntreg_nk_record *) (h->addr + parent);
2151 size_t parent_sk_offset = le32toh (parent_nk->sk);
2152 parent_sk_offset += 0x1000;
2153 if (!IS_VALID_BLOCK (h, parent_sk_offset) ||
2154 !BLOCK_ID_EQ (h, parent_sk_offset, "sk")) {
2156 fprintf (stderr, "hivex_node_add_child: returning EFAULT because parent sk is not a valid block (%zu)\n",
2161 struct ntreg_sk_record *sk =
2162 (struct ntreg_sk_record *) (h->addr + parent_sk_offset);
2163 sk->refcount = htole32 (le32toh (sk->refcount) + 1);
2164 nk->sk = htole32 (parent_sk_offset - 0x1000);
2166 /* Inherit parent timestamp. */
2167 memcpy (nk->timestamp, parent_nk->timestamp, sizeof (parent_nk->timestamp));
2169 /* What I found out the hard way (not documented anywhere): the
2170 * subkeys in lh-records must be kept sorted. If you just add a
2171 * subkey in a non-sorted position (eg. just add it at the end) then
2172 * Windows won't see the subkey _and_ Windows will corrupt the hive
2173 * itself when it modifies or saves it.
2175 * So use get_children() to get a list of intermediate
2176 * lf/lh-records. get_children() returns these in reading order
2177 * (which is sorted), so we look for the lf/lh-records in sequence
2178 * until we find the key name just after the one we are inserting,
2179 * and we insert the subkey just before it.
2181 * The only other case is the no-subkeys case, where we have to
2182 * create a brand new lh-record.
2184 hive_node_h *unused;
2187 if (get_children (h, parent, &unused, &blocks, 0) == -1)
2192 size_t nr_subkeys_in_parent_nk = le32toh (parent_nk->nr_subkeys);
2193 if (nr_subkeys_in_parent_nk == 0) { /* No subkeys case. */
2194 /* Free up any existing intermediate blocks. */
2195 for (i = 0; blocks[i] != 0; ++i)
2196 mark_block_unused (h, blocks[i]);
2197 size_t lh_offs = new_lh_record (h, name, node);
2204 fprintf (stderr, "hivex_node_add_child: no keys, allocated new lh-record at 0x%zx\n", lh_offs);
2206 parent_nk->subkey_lf = htole32 (lh_offs - 0x1000);
2208 else { /* Insert subkeys case. */
2209 size_t old_offs = 0, new_offs = 0;
2210 struct ntreg_lf_record *old_lf = NULL;
2212 /* Find lf/lh key name just after the one we are inserting. */
2213 for (i = 0; blocks[i] != 0; ++i) {
2214 if (BLOCK_ID_EQ (h, blocks[i], "lf") ||
2215 BLOCK_ID_EQ (h, blocks[i], "lh")) {
2216 old_offs = blocks[i];
2217 old_lf = (struct ntreg_lf_record *) (h->addr + old_offs);
2218 for (j = 0; j < le16toh (old_lf->nr_keys); ++j) {
2219 hive_node_h nk_offs = le32toh (old_lf->keys[j].offset);
2221 if (compare_name_with_nk_name (h, name, nk_offs) < 0)
2227 /* Insert it at the end.
2228 * old_offs points to the last lf record, set j.
2230 assert (old_offs != 0); /* should never happen if nr_subkeys > 0 */
2231 j = le16toh (old_lf->nr_keys);
2236 fprintf (stderr, "hivex_node_add_child: insert key in existing lh-record at 0x%zx, posn %zu\n", old_offs, j);
2238 new_offs = insert_lf_record (h, old_offs, j, name, node);
2239 if (new_offs == 0) {
2245 fprintf (stderr, "hivex_node_add_child: new lh-record at 0x%zx\n",
2248 /* If the lf/lh-record was directly referenced by the parent nk,
2249 * then update the parent nk.
2251 if (le32toh (parent_nk->subkey_lf) + 0x1000 == old_offs)
2252 parent_nk->subkey_lf = htole32 (new_offs - 0x1000);
2253 /* Else we have to look for the intermediate ri-record and update
2257 for (i = 0; blocks[i] != 0; ++i) {
2258 if (BLOCK_ID_EQ (h, blocks[i], "ri")) {
2259 struct ntreg_ri_record *ri =
2260 (struct ntreg_ri_record *) (h->addr + blocks[i]);
2261 for (j = 0; j < le16toh (ri->nr_offsets); ++j)
2262 if (le32toh (ri->offset[j] + 0x1000) == old_offs) {
2263 ri->offset[j] = htole32 (new_offs - 0x1000);
2269 /* Not found .. This is an internal error. */
2271 fprintf (stderr, "hivex_node_add_child: returning ENOTSUP because could not find ri->lf link\n");
2283 /* Update nr_subkeys in parent nk. */
2284 nr_subkeys_in_parent_nk++;
2285 parent_nk->nr_subkeys = htole32 (nr_subkeys_in_parent_nk);
2287 /* Update max_subkey_name_len in parent nk. */
2288 uint16_t max = le16toh (parent_nk->max_subkey_name_len);
2289 if (max < strlen (name) * 2) /* *2 because "recoded" in UTF16-LE. */
2290 parent_nk->max_subkey_name_len = htole16 (strlen (name) * 2);
2295 /* Decrement the refcount of an sk-record, and if it reaches zero,
2296 * unlink it from the chain and delete it.
2299 delete_sk (hive_h *h, size_t sk_offset)
2301 if (!IS_VALID_BLOCK (h, sk_offset) || !BLOCK_ID_EQ (h, sk_offset, "sk")) {
2303 fprintf (stderr, "delete_sk: not an sk record: 0x%zx\n", sk_offset);
2308 struct ntreg_sk_record *sk = (struct ntreg_sk_record *) (h->addr + sk_offset);
2310 if (sk->refcount == 0) {
2312 fprintf (stderr, "delete_sk: sk record already has refcount 0: 0x%zx\n",
2320 if (sk->refcount == 0) {
2321 size_t sk_prev_offset = sk->sk_prev;
2322 sk_prev_offset += 0x1000;
2324 size_t sk_next_offset = sk->sk_next;
2325 sk_next_offset += 0x1000;
2327 /* Update sk_prev/sk_next SKs, unless they both point back to this
2328 * cell in which case we are deleting the last SK.
2330 if (sk_prev_offset != sk_offset && sk_next_offset != sk_offset) {
2331 struct ntreg_sk_record *sk_prev =
2332 (struct ntreg_sk_record *) (h->addr + sk_prev_offset);
2333 struct ntreg_sk_record *sk_next =
2334 (struct ntreg_sk_record *) (h->addr + sk_next_offset);
2336 sk_prev->sk_next = htole32 (sk_next_offset - 0x1000);
2337 sk_next->sk_prev = htole32 (sk_prev_offset - 0x1000);
2340 /* Refcount is zero so really delete this block. */
2341 mark_block_unused (h, sk_offset);
2347 /* Callback from hivex_node_delete_child which is called to delete a
2348 * node AFTER its subnodes have been visited. The subnodes have been
2349 * deleted but we still have to delete any lf/lh/li/ri records and the
2350 * value list block and values, followed by deleting the node itself.
2353 delete_node (hive_h *h, void *opaque, hive_node_h node, const char *name)
2355 /* Get the intermediate blocks. The subkeys have already been
2356 * deleted by this point, so tell get_children() not to check for
2357 * validity of the nk-records.
2359 hive_node_h *unused;
2361 if (get_children (h, node, &unused, &blocks, GET_CHILDREN_NO_CHECK_NK) == -1)
2365 /* We don't care what's in these intermediate blocks, so we can just
2366 * delete them unconditionally.
2369 for (i = 0; blocks[i] != 0; ++i)
2370 mark_block_unused (h, blocks[i]);
2374 /* Delete the values in the node. */
2375 if (delete_values (h, node) == -1)
2378 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2380 /* If the NK references an SK, delete it. */
2381 size_t sk_offs = le32toh (nk->sk);
2382 if (sk_offs != 0xffffffff) {
2384 if (delete_sk (h, sk_offs) == -1)
2386 nk->sk = htole32 (0xffffffff);
2389 /* If the NK references a classname, delete it. */
2390 size_t cl_offs = le32toh (nk->classname);
2391 if (cl_offs != 0xffffffff) {
2393 mark_block_unused (h, cl_offs);
2394 nk->classname = htole32 (0xffffffff);
2397 /* Delete the node itself. */
2398 mark_block_unused (h, node);
2404 hivex_node_delete_child (hive_h *h, hive_node_h node)
2411 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
2416 if (node == hivex_root (h)) {
2418 fprintf (stderr, "hivex_node_delete_child: cannot delete root node\n");
2423 hive_node_h parent = hivex_node_parent (h, node);
2427 /* Delete node and all its children and values recursively. */
2428 static const struct hivex_visitor visitor = { .node_end = delete_node };
2429 if (hivex_visit_node (h, node, &visitor, sizeof visitor, NULL, 0) == -1)
2432 /* Delete the link from parent to child. We need to find the lf/lh
2433 * record which contains the offset and remove the offset from that
2434 * record, then decrement the element count in that record, and
2435 * decrement the overall number of subkeys stored in the parent
2438 hive_node_h *unused;
2440 if (get_children (h, parent, &unused, &blocks, GET_CHILDREN_NO_CHECK_NK)== -1)
2445 for (i = 0; blocks[i] != 0; ++i) {
2446 struct ntreg_hbin_block *block =
2447 (struct ntreg_hbin_block *) (h->addr + blocks[i]);
2449 if (block->id[0] == 'l' && (block->id[1] == 'f' || block->id[1] == 'h')) {
2450 struct ntreg_lf_record *lf = (struct ntreg_lf_record *) block;
2452 size_t nr_subkeys_in_lf = le16toh (lf->nr_keys);
2454 for (j = 0; j < nr_subkeys_in_lf; ++j)
2455 if (le32toh (lf->keys[j].offset) + 0x1000 == node) {
2456 for (; j < nr_subkeys_in_lf - 1; ++j)
2457 memcpy (&lf->keys[j], &lf->keys[j+1], sizeof (lf->keys[j]));
2458 lf->nr_keys = htole16 (nr_subkeys_in_lf - 1);
2464 fprintf (stderr, "hivex_node_delete_child: could not find parent to child link\n");
2469 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + parent);
2470 size_t nr_subkeys_in_nk = le32toh (nk->nr_subkeys);
2471 nk->nr_subkeys = htole32 (nr_subkeys_in_nk - 1);
2474 fprintf (stderr, "hivex_node_delete_child: updating nr_subkeys in parent 0x%zx to %zu\n",
2475 parent, nr_subkeys_in_nk);
2481 hivex_node_set_values (hive_h *h, hive_node_h node,
2482 size_t nr_values, const hive_set_value *values,
2490 if (!IS_VALID_BLOCK (h, node) || !BLOCK_ID_EQ (h, node, "nk")) {
2495 /* Delete all existing values. */
2496 if (delete_values (h, node) == -1)
2502 /* Allocate value list node. Value lists have no id field. */
2503 static const char nul_id[2] = { 0, 0 };
2505 sizeof (struct ntreg_value_list) + (nr_values - 1) * sizeof (uint32_t);
2506 size_t vallist_offs = allocate_block (h, seg_len, nul_id);
2507 if (vallist_offs == 0)
2510 struct ntreg_nk_record *nk = (struct ntreg_nk_record *) (h->addr + node);
2511 nk->nr_values = htole32 (nr_values);
2512 nk->vallist = htole32 (vallist_offs - 0x1000);
2514 struct ntreg_value_list *vallist =
2515 (struct ntreg_value_list *) (h->addr + vallist_offs);
2518 for (i = 0; i < nr_values; ++i) {
2519 /* Allocate vk record to store this (key, value) pair. */
2520 static const char vk_id[2] = { 'v', 'k' };
2521 seg_len = sizeof (struct ntreg_vk_record) + strlen (values[i].key);
2522 size_t vk_offs = allocate_block (h, seg_len, vk_id);
2526 vallist->offset[i] = htole32 (vk_offs - 0x1000);
2528 struct ntreg_vk_record *vk = (struct ntreg_vk_record *) (h->addr + vk_offs);
2529 size_t name_len = strlen (values[i].key);
2530 vk->name_len = htole16 (name_len);
2531 strcpy (vk->name, values[i].key);
2532 vk->data_type = htole32 (values[i].t);
2533 uint32_t len = values[i].len;
2534 if (len <= 4) /* store it inline => set MSB flag */
2536 vk->data_len = htole32 (len);
2537 vk->flags = name_len == 0 ? 0 : 1;
2539 if (values[i].len <= 4) /* store it inline */
2540 memcpy (&vk->data_offset, values[i].value, values[i].len);
2542 size_t offs = allocate_block (h, values[i].len + 4, nul_id);
2545 memcpy (h->addr + offs + 4, values[i].value, values[i].len);
2546 vk->data_offset = htole32 (offs - 0x1000);
2549 if (name_len * 2 > le32toh (nk->max_vk_name_len))
2550 /* * 2 for UTF16-LE "reencoding" */
2551 nk->max_vk_name_len = htole32 (name_len * 2);
2552 if (values[i].len > le32toh (nk->max_vk_data_len))
2553 nk->max_vk_data_len = htole32 (values[i].len);