int hivex_commit (hive_h *h, const char *filename, int flags) { int fd; if (flags != 0) { SET_ERRNO (EINVAL, "flags != 0"); return -1; } CHECK_WRITABLE (-1); filename = filename ? : h->filename; #ifdef O_CLOEXEC fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC|O_BINARY, 0666); #else fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_BINARY, 0666); #endif if (fd == -1) return -1; #ifndef O_CLOEXEC fcntl (fd, F_SETFD, FD_CLOEXEC); #endif /* Update the header fields. */ uint32_t sequence = le32toh (h->hdr->sequence1); sequence++; h->hdr->sequence1 = htole32 (sequence); h->hdr->sequence2 = htole32 (sequence); /* XXX Ought to update h->hdr->last_modified. */ h->hdr->blocks = htole32 (h->endpages - 0x1000); /* Recompute header checksum. */ uint32_t sum = header_checksum (h); h->hdr->csum = htole32 (sum); DEBUG (2, "hivex_commit: new header checksum: 0x%x", sum); if (full_write (fd, h->addr, h->size) != h->size) { int err = errno; close (fd); errno = err; return -1; } if (close (fd) == -1) return -1; return 0; }
static void route_ip_packet(struct sr_instance *sr, uint8_t *packet, size_t len, char *interface) { // Copy the IP header struct ip *ip_header = calloc(1, sizeof(struct ip)); memcpy(ip_header, packet + sizeof(struct sr_ethernet_hdr), sizeof(struct ip)); // The IP header is a dirty liar int actual_header_length = ip_header->ip_hl * 4; int ip_packet_len = len - sizeof(struct sr_ethernet_hdr); // Decrement the TTL and check if it's 0 ip_header->ip_ttl -= 1; if(ip_header->ip_ttl <= 0) { return; } // Zero out the old checksum ip_header->ip_sum = 0; // Create a buffer to calculate the checksum uint16_t *header_buffer; // Create a chunk of memory aligned to 16 bits posix_memalign((void **) &header_buffer, CHECKSUM_ALIGNMENT, actual_header_length); bzero(header_buffer, actual_header_length); // Copy required IP header fields memcpy(header_buffer, ip_header, sizeof(struct ip)); // Copy the original IP header flags memcpy(header_buffer, packet + sizeof(struct sr_ethernet_hdr) + sizeof(struct ip), actual_header_length - sizeof(struct ip)); // Calculate the new checksum ip_header->ip_sum = header_checksum(header_buffer, actual_header_length / 2); free(header_buffer); // Get the IP packet's destination struct in_addr destination_addr = ip_header->ip_dst; uint32_t destination_ip = destination_addr.s_addr; // Check if we're the destination struct sr_if *iface = sr_get_interface(sr, interface); if(destination_ip == iface->ip) { printf("Dropping packet bound for router on %s\n", interface); return; } // Check the routing table for the correct gateway to forward the packet through struct sr_rt *table_entry = search_routing_table(sr, destination_ip); printf("\tThe nexthop is %s\n", inet_ntoa(table_entry->gw)); if(table_entry) { // Get the interface for the gateway struct sr_if *gw_iface = sr_get_interface(sr, table_entry->interface); // Determine the IP to forward to struct in_addr nexthop; // Determine if the IP to forward to is in our network if(table_entry->gw.s_addr == 0) { nexthop = destination_addr; } else { nexthop = table_entry->gw; } // Create an updated IP packet with the correct headers/data uint8_t *updated_packet = calloc(ip_packet_len, sizeof(uint8_t)); memcpy(updated_packet, ip_header, actual_header_length); memcpy(updated_packet + actual_header_length, packet + sizeof(struct sr_ethernet_hdr ) + actual_header_length, len - sizeof(struct sr_ethernet_hdr ) - actual_header_length); // Search the ARP cache for the nexthop uint8_t *gw_addr = search_arp_cache(cache, nexthop.s_addr); if(gw_addr) { printf("\tForwarding packet bound for %s through next hop @ ", inet_ntoa(destination_addr)); printf("%s (", inet_ntoa(table_entry->gw)); print_ethernet_addr(gw_addr, stdout); printf(")\n"); uint8_t *buffer = pack_ethernet_packet(gw_addr, gw_iface->addr, ETHERTYPE_IP, updated_packet, ip_packet_len); sr_send_packet(sr, buffer, len, gw_iface->name); } else { // Otherwise we cache the IP packet and make an ARP request printf("\tSending ARP request to %s (%s), to forward packet bound for ", inet_ntoa(nexthop), gw_iface->name); printf("%s\n", inet_ntoa(destination_addr)); add_ip_cache_entry(ip_cache, updated_packet, nexthop, ip_packet_len); send_arp_request(sr, gw_iface, nexthop); } } else { // TODO: What do we do here? } }
hive_h * hivex_open (const char *filename, int flags) { hive_h *h = NULL; assert (sizeof (struct ntreg_header) == 0x1000); assert (offsetof (struct ntreg_header, csum) == 0x1fc); h = calloc (1, sizeof *h); if (h == NULL) goto error; h->msglvl = flags & HIVEX_OPEN_MSGLVL_MASK; const char *debug = getenv ("HIVEX_DEBUG"); if (debug && STREQ (debug, "1")) h->msglvl = 2; DEBUG (2, "created handle %p", h); h->writable = !!(flags & HIVEX_OPEN_WRITE); h->unsafe = !!(flags & HIVEX_OPEN_UNSAFE); h->filename = strdup (filename); if (h->filename == NULL) goto error; #ifdef O_CLOEXEC h->fd = open (filename, O_RDONLY | O_CLOEXEC | O_BINARY); #else h->fd = open (filename, O_RDONLY | O_BINARY); #endif if (h->fd == -1) goto error; #ifndef O_CLOEXEC fcntl (h->fd, F_SETFD, FD_CLOEXEC); #endif struct stat statbuf; if (fstat (h->fd, &statbuf) == -1) goto error; h->size = statbuf.st_size; if (h->size < 0x2000) { SET_ERRNO (EINVAL, "%s: file is too small to be a Windows NT Registry hive file", filename); goto error; } if (!h->writable) { h->addr = mmap (NULL, h->size, PROT_READ, MAP_SHARED, h->fd, 0); if (h->addr == MAP_FAILED) goto error; DEBUG (2, "mapped file at %p", h->addr); } else { h->addr = malloc (h->size); if (h->addr == NULL) goto error; if (full_read (h->fd, h->addr, h->size) < h->size) goto error; /* We don't need the file descriptor along this path, since we * have read all the data. */ if (close (h->fd) == -1) goto error; h->fd = -1; } /* Check header. */ if (h->hdr->magic[0] != 'r' || h->hdr->magic[1] != 'e' || h->hdr->magic[2] != 'g' || h->hdr->magic[3] != 'f') { SET_ERRNO (ENOTSUP, "%s: not a Windows NT Registry hive file", filename); goto error; } /* Check major version. */ uint32_t major_ver = le32toh (h->hdr->major_ver); if (major_ver != 1) { SET_ERRNO (ENOTSUP, "%s: hive file major version %" PRIu32 " (expected 1)", filename, major_ver); goto error; } h->bitmap = calloc (1 + h->size / 32, 1); if (h->bitmap == NULL) goto error; /* Header checksum. */ uint32_t sum = header_checksum (h); if (sum != le32toh (h->hdr->csum)) { SET_ERRNO (EINVAL, "%s: bad checksum in hive header", filename); goto error; } for (int t=0; t<nr_recode_types; t++) { gl_lock_init (h->iconv_cache[t].mutex); h->iconv_cache[t].handle = NULL; } /* Last modified time. */ h->last_modified = le64toh ((int64_t) h->hdr->last_modified); if (h->msglvl >= 2) { char *name = _hivex_recode (h, utf16le_to_utf8, h->hdr->name, 64, NULL); fprintf (stderr, "hivex_open: header fields:\n" " file version %" PRIu32 ".%" PRIu32 "\n" " sequence nos %" PRIu32 " %" PRIu32 "\n" " (sequences nos should match if hive was synched at shutdown)\n" " last modified %" PRIi64 "\n" " (Windows filetime, x 100 ns since 1601-01-01)\n" " original file name %s\n" " (only 32 chars are stored, name is probably truncated)\n" " root offset 0x%x + 0x1000\n" " end of last page 0x%x + 0x1000 (total file size 0x%zx)\n" " checksum 0x%x (calculated 0x%x)\n", major_ver, le32toh (h->hdr->minor_ver), le32toh (h->hdr->sequence1), le32toh (h->hdr->sequence2), h->last_modified, name ? name : "(conversion failed)", le32toh (h->hdr->offset), le32toh (h->hdr->blocks), h->size, le32toh (h->hdr->csum), sum); free (name); } h->rootoffs = le32toh (h->hdr->offset) + 0x1000; h->endpages = le32toh (h->hdr->blocks) + 0x1000; DEBUG (2, "root offset = 0x%zx", h->rootoffs); /* We'll set this flag when we see a block with the root offset (ie. * the root block). */ int seen_root_block = 0, bad_root_block = 0; /* Collect some stats. */ size_t pages = 0; /* Number of hbin pages read. */ size_t smallest_page = SIZE_MAX, largest_page = 0; size_t blocks = 0; /* Total number of blocks found. */ size_t smallest_block = SIZE_MAX, largest_block = 0, blocks_bytes = 0; size_t used_blocks = 0; /* Total number of used blocks found. */ size_t used_size = 0; /* Total size (bytes) of used blocks. */ /* Read the pages and blocks. The aim here is to be robust against * corrupt or malicious registries. So we make sure the loops * always make forward progress. We add the address of each block * we read to a hash table so pointers will only reference the start * of valid blocks. */ size_t off; struct ntreg_hbin_page *page; for (off = 0x1000; off < h->size; off += le32toh (page->page_size)) { if (off >= h->endpages) break; page = (struct ntreg_hbin_page *) ((char *) h->addr + off); if (page->magic[0] != 'h' || page->magic[1] != 'b' || page->magic[2] != 'i' || page->magic[3] != 'n') { if (!h->unsafe) { SET_ERRNO (ENOTSUP, "%s: trailing garbage at end of file " "(at 0x%zx, after %zu pages)", filename, off, pages); goto error; } DEBUG (2, "page not found at expected offset 0x%zx, " "seeking until one is found or EOF is reached", off); int found = 0; while (off < h->size) { off += 0x1000; if (off >= h->endpages) break; page = (struct ntreg_hbin_page *) ((char *) h->addr + off); if (page->magic[0] == 'h' && page->magic[1] == 'b' && page->magic[2] == 'i' && page->magic[3] == 'n') { DEBUG (2, "found next page by seeking at 0x%zx", off); found = 1; break; } } if (!found) { DEBUG (2, "page not found and end of pages section reached"); break; } } size_t page_size = le32toh (page->page_size); DEBUG (2, "page at 0x%zx, size %zu", off, page_size); pages++; if (page_size < smallest_page) smallest_page = page_size; if (page_size > largest_page) largest_page = page_size; if (page_size <= sizeof (struct ntreg_hbin_page) || (page_size & 0x0fff) != 0) { SET_ERRNO (ENOTSUP, "%s: page size %zu at 0x%zx, bad registry", filename, page_size, off); goto error; } if (off + page_size > h->size) { SET_ERRNO (ENOTSUP, "%s: page size %zu at 0x%zx extends beyond end of file, bad registry", filename, page_size, off); goto error; } size_t page_offset = le32toh(page->offset_first) + 0x1000; if (page_offset != off) { SET_ERRNO (ENOTSUP, "%s: declared page offset (0x%zx) does not match computed " "offset (0x%zx), bad registry", filename, page_offset, off); goto error; } /* Read the blocks in this page. */ size_t blkoff; struct ntreg_hbin_block *block; size_t seg_len; for (blkoff = off + 0x20; blkoff < off + page_size; blkoff += seg_len) { blocks++; int is_root = blkoff == h->rootoffs; if (is_root) seen_root_block = 1; block = (struct ntreg_hbin_block *) ((char *) h->addr + blkoff); int used; seg_len = block_len (h, blkoff, &used); /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78665 */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wstrict-overflow" if (seg_len <= 4 || (seg_len & 3) != 0) { #pragma GCC diagnostic pop if (is_root || !h->unsafe) { SET_ERRNO (ENOTSUP, "%s, the block at 0x%zx has invalid size %" PRIu32 ", bad registry", filename, blkoff, le32toh (block->seg_len)); goto error; } else { DEBUG (2, "%s: block at 0x%zx has invalid size %" PRIu32 ", skipping", filename, blkoff, le32toh (block->seg_len)); break; } } if (h->msglvl >= 2) { unsigned char *id = (unsigned char *) block->id; int id0 = id[0], id1 = id[1]; fprintf (stderr, "%s: %s: " "%s block id %d,%d (%c%c) at 0x%zx size %zu%s\n", "hivex", __func__, used ? "used" : "free", id0, id1, c_isprint (id0) ? id0 : '.', c_isprint (id1) ? id1 : '.', blkoff, seg_len, is_root ? " (root)" : ""); } blocks_bytes += seg_len; if (seg_len < smallest_block) smallest_block = seg_len; if (seg_len > largest_block) largest_block = seg_len; if (is_root && !used) bad_root_block = 1; if (used) { used_blocks++; used_size += seg_len; /* Root block must be an nk-block. */ if (is_root && (block->id[0] != 'n' || block->id[1] != 'k')) bad_root_block = 1; /* Note this blkoff is a valid address. */ BITMAP_SET (h->bitmap, blkoff); } } } if (!seen_root_block) { SET_ERRNO (ENOTSUP, "%s: no root block found", filename); goto error; } if (bad_root_block) { SET_ERRNO (ENOTSUP, "%s: bad root block (free or not nk)", filename); goto error; } DEBUG (1, "successfully read Windows Registry hive file:\n" " pages: %zu [sml: %zu, lge: %zu]\n" " blocks: %zu [sml: %zu, avg: %zu, lge: %zu]\n" " blocks used: %zu\n" " bytes used: %zu", pages, smallest_page, largest_page, blocks, smallest_block, blocks_bytes / blocks, largest_block, used_blocks, used_size); return h; error:; int err = errno; if (h) { free (h->bitmap); if (h->addr && h->size && h->addr != MAP_FAILED) { if (!h->writable) munmap (h->addr, h->size); else free (h->addr); } if (h->fd >= 0) close (h->fd); free (h->filename); free (h); } errno = err; return NULL; }