/
chkregf.c
286 lines (242 loc) · 7.08 KB
/
chkregf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* chkregf.c -- Check regf registry files
*
* This program is not meant for end-users, but for developers and skillful
* system administrators. It is meant to point out regf file inconsistencies
* in a manner that it's easy to fix them, so that Windows will parse them
* correctly.
*
* Licensed under the GNU GPL v2 or any later version
*
* Copyright (C) 2005-2010 Wilco Baan Hofman <wilco@baanhofman.nl>
*
* This file contains the main registry checking code.
*
* TODO:
* - Add pass 4, checking for orphans
* - Maybe add a pass 5, for specific registry value data, like incorrect
* policy values.
* - Big endian support and platforms with different alignment than x86
* - Check sk reference counts
* - Check sk pointer consistency
* - Check security descriptors
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <talloc.h>
#include <ctype.h>
#include "regf.h"
#include "chkregf.h"
#include "config.h"
/* [SYN] regf data should be available globally */
struct regf_block regf;
struct regf_block *get_regf_struct(void)
{
return ®f;
}
uint32_t get_hbin_header(FILE *fd, signed long int offset)
{
struct hbin_block hbin;
fseek(fd, offset + 0x1000, SEEK_SET);
if (!fread(&hbin, sizeof(hbin), 1, fd)) {
printf("Error: short read while reading hbin block at 0x%lx\n",
offset + 0x1000);
return 0;
}
/* [SYN] this should be a hbin block */
if (hbin.id != 0x6E696268) {
puts("Error: this is no hbin block!");
return 0;
}
/* [SYN] The offset from first data block should be offset - 0x1000 */
if (hbin.offset_from_first != offset
|| hbin.offset_from_first % 0x1000 != 0) {
printf("Error: hbin offset to first incorrect at 0x%lx\n",
offset+0x1000);
return 0;
}
/* [SYN] The offset to the next record should be a multiple of 0x1000 */
if (hbin.offset_to_next % 0x1000 != 0) {
printf("Error: hbin offset to next isn't a multiple of 0x1000 at 0x%lx\n",
offset+0x1000);
return 0;
}
/* [SYN] The size of the hbin should be identical to the relative
* offset of the next hbin. Windows XP doesn't use it. */
return (hbin.offset_to_next);
}
int read_regf_header(FILE *fd)
{
short int i;
uint32_t hash = 0;
fseek(fd, 0, SEEK_SET);
if (!fread(®f, sizeof(regf), 1, fd)) {
puts("Error: short read while reading regf block");
return 0;
}
/* [SYN] this should be a regf file */
if (regf.id != 0x66676572) { /* [SYN] 'regf' */
puts("No 'regf' found at 0x0 (is this an NT registry file?)");
return 0;
}
/* [SYN] uk1[0] should be the same as uk1[1] */
if (regf.uk1[0] != regf.uk1[1]) {
puts("Values at 0x0004 and 0x0008 should be identical.");
return 0;
}
/* [SYN] 0x1, 0x3(or 0x5), 0x0, 0x1 for D-words from 0x0014 (version)*/
if (regf.version[0] != 0x1 ||
(regf.version[1] != 0x3 && regf.version[1] != 0x5) ||
regf.version[2] != 0x0 || regf.version[3] != 0x1) {
puts("D-words from 0x0014 to 0x0020 should be 0x1, 0x3 or 0x5, 0x0, 0x1");
return 0;
}
/* [SYN] Check first record key offset, usually 0x20 */
if (regf.key_offset < 0x20) {
puts("Error: 1st record key offset smaller than hbin header.");
return 0;
}
if (regf.key_offset > 0x100) {
puts("Warning: 1st record offset seems large.");
}
/* [SYN] hbin data source should be a multiple of 0x1000 */
if ((regf.data_size % 0x1000) != 0) {
puts("Error: data size should be a multiple of 0x1000");
return 0;
}
/* [SYN] Check if unicode regf description is really unicode */
for (i = 0; i < sizeof(regf.description); i++) {
if ((i % 2) == 1) {
if (regf.description[i] > 0x2 &&
regf.description[i] != 0xFF) {
puts("Warning: regf description does not appear to be unicode");
break;
}
}
}
/* [SYN] Check the checksum */
for (i = 0; i < (0x1FC/4); i+=1) {
uint32_t *dword;
dword = (uint32_t *) ®f + i;
hash = hash ^ *dword;
}
if (hash != regf.checksum) {
printf("Error: checksum incorrect; got 0x%lx, must be 0x%lx\n",
(long)regf.checksum, (long)hash);
printf("Note: This could be caused by other malicious data in the header!\n");
return 0;
}
return 1;
}
struct hbin_data_block *get_hbin_data_block(TALLOC_CTX *mem_ctx, FILE *fd, long int offset, long int parent_off)
{
struct hbin_data_block *block;
long int cur_offset;
block = talloc_zero(mem_ctx, struct hbin_data_block);
/* [SYN] Set index to data block */
cur_offset = offset+0x1000;
/* [SYN] Seek to data block */
fseek(fd, cur_offset, SEEK_SET);
#if DODEBUG > 2
printf("Debug: Parsing block at cur_offset 0x%lx, parent 0x%lx\n", (long)cur_offset, (long) parent_off+0x1000);
#endif
if (!fread(block, 4, 1, fd)) {
printf("Error: short read while reading hbin data record size at 0x%lx\n",
(long)cur_offset);
return NULL;
}
if (block->size > 0) {
if (parent_off > 0) {
/* [SYN] Positive block->size means unused. Time to barf. */
printf("Error: Referencing unused block (0x%lx) with size 0x%lx from 0x%lx\n",
(long)cur_offset, (long)block->size, (long)parent_off);
return NULL;
} else {
block->size = -block->size;
return block;
}
}
if (block->size == 0) {
printf("Error: hbin data record size is NULL at 0x%lx\n",
(long)cur_offset);
return NULL;
}
block->size = -block->size;
/* [SYN] Check block->size, do not allocate it if bigger */
if (block->size > 32768) {
printf("Warning: hbin data record size (0x%lx) is quite large at 0x%lx\n",
(long)block->size, (long)cur_offset);
printf("Warning: NOT ALLOCATING THIS BLOCK.");
return NULL;
}
/* [SYN] Allocate memory and read the record into memory */
if ((block->data = talloc_array(block, uint8_t, block->size)) == NULL) {
printf("Failed to allocate %ld bytes at record 0x%lx\n",
(long)block->size, (long)cur_offset);
return NULL;
}
if (!fread(block->data, block->size, 1, fd)) {
printf("Error: Failed to read hbin data record at 0x%lx\n",
(long)cur_offset);
return NULL;
}
return block;
}
int main (int argc, char **argv)
{
FILE *fd;
uint16_t i;
int rv;
int error = 0;
TALLOC_CTX *mem_ctx;
if (argc == 1) {
puts("Usage: chkregf REGFILE");
return 1;
}
if (!(fd = fopen(argv[1], "r"))) {
puts("Error: file not found");
return 2;
}
mem_ctx = talloc_init("chkregf registry checker");
if (!mem_ctx) {
printf("Memory allocation error\n");
return 3;
}
printf("\nPass 1: Checking registry regf header\n\n");
if (!read_regf_header(fd)) {
printf("Regf header contains errors\n");
return 1;
}
printf("\nPass 2: Checking keys for incorrect values\n\n");
for(i = 0; i < regf.data_size / 0x1000; i++) {
uint32_t size;
if (!(size = get_hbin_header(fd, 0x1000 * i))) {
printf("Errors in hbin header at 0x%lx.",
(long int) (0x1000 * i) + 0x1000);
return 1;
}
rv = read_blocks(mem_ctx, fd, 0x1000 * i);
if (!rv) {
error = 1;
}
if (size / 0x1000 > 1) {
i += (size/0x1000) - 1;
}
}
printf("\nPass 3: Checking offsets and tree\n");
rv = parse_tree(mem_ctx, fd, regf.key_offset, 0, "nk", 0);
if (!rv) {
error = 1;
}
if (error) {
printf("Errors encountered\n");
return 1;
}
printf("\nDone checking, no errors...\n\n");
fclose(fd);
return 0;
}