summaryrefslogtreecommitdiff
path: root/main.c
blob: f9d7b42ea52738044af2f0a7c607a70040be0b24 (plain)
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
#define READ_INCREMENT	4096

#define REG_NONE			0UL /* No value type */
#define REG_SZ				1UL /* Unicode nul terminated string */
#define REG_EXPAND_SZ			2UL /* Unicode nul terminated string */
#define REG_BINARY			3UL /* Free form binary */
#define REG_DWORD			4UL /* 32-bit number */
#define REG_DWORD_LITTLE_ENDIAN		4UL /* 32-bit number (same as REG_DWORD) */
#define REG_DWORD_BIG_ENDIAN		5UL /* 32-bit number */
#define REG_LINK			6UL /* Symbolic Link (unicode) */
#define REG_MULTI_SZ			7UL /* Multiple Unicode strings */
#define REG_RESOURCE_LIST		8UL /* Resource list in the resource map */
#define REG_FULL_RESOURCE_DESCRIPTOR	9UL /* Resource list in the hardware description */
#define REG_RESOURCE_REQUIREMENTS_LIST	10UL
#define REG_QWORD			11UL /* 64-bit number */
#define REG_QWORD_LITTLE_ENDIAN		11UL /* 64-bit number (same as REG_QWORD) */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

static size_t buf_len = 0;
static size_t pol_len = 0;
static size_t pol_index = 0;
static char *buf = NULL;

static void cleanup(void) {
	if (buf) {
		free(buf);
	}
}

static const char *typestr(uint32_t type) {
	static char unknown_name[7];
	switch (type) {
		case REG_NONE: return "REG_NONE";
		case REG_SZ: return "REG_SZ";
		case REG_EXPAND_SZ: return "REG_EXPAND_SZ";
		case REG_BINARY: return "REG_BINARY";
		case REG_DWORD: return "REG_DWORD";
		case REG_DWORD_BIG_ENDIAN: return "REG_DWORD_BIG_ENDIAN";
		case REG_LINK: return "REG_LINK";
		case REG_MULTI_SZ: return "REG_MULTI_SZ";
		case REG_RESOURCE_LIST: return "REG_RESOURCE_LIST";
		case REG_FULL_RESOURCE_DESCRIPTOR: return "REG_FULL_RESOURCE_DESCRIPTOR";
		case REG_RESOURCE_REQUIREMENTS_LIST: return "REG_RESOURCE_REQUIREMENTS_LIST";
		case REG_QWORD: return "REG_QWORD";
		default: {
				 snprintf(unknown_name, sizeof(unknown_name), "0x%x", type);
				 return unknown_name;
			 }
	}
}

static char *bufget(size_t len, bool force, size_t *len_out) {
	char *buf_pre = buf + pol_index;
	if (len_out != NULL) {
		*len_out = len;
	}
	/* pol_index + len = the index to read in the NEXT call 
	 * This call reads buf[pol_index_pre, pol_index_pre + len - 1] */
	if ((pol_index += len) >= (pol_len + 1)) {
		printf("NPI %u\n", pol_index);
		if (force) {
			fprintf(stderr, "Invalid registry policy file: Unexpected EOF at %u, require %u more bytes.\n", pol_index - len, len);
			exit(1);
		} else {
			pol_index = pol_len - 1;
			if (len_out != NULL) {
				*len_out = pol_index - (buf_pre - buf) /* pol_index pre */ + 1;
			}
		}
	}
	return buf_pre;
}

static void bufret(size_t len) {
	pol_index -= len;
}

static char *bufgetf(size_t len) {
	return bufget(len, true, NULL);
}

static char *bufgett(size_t len, size_t *len_out) {
	return bufget(len, false, len_out);
}

static char bufgetchr(void) {
	char *b = bufgetf(2);
	if (b[1] != 0) {
		fprintf(stderr, "Illegal char at %u\n", pol_index);
		exit(1);
	}
	return b[0];
}

static void bufprintnstr(uint32_t len) {
	for (len - 1; len >= 0; len --) {
		char c = bufgetchr();
		if (c == 0) {
			return;
		}
		printf("%c", c);
	}
}

static void bufprintstr(void) {
	bufprintnstr(UINT32_MAX);
}

static void bufasschr(char c) {
	const char a = bufgetchr();
	if (a != c) {
		fprintf(stderr, "Expected a '%c' at %u, but got a '%c'\n", c, pol_index, a);
		exit(4);
	}
}

static uint32_t bufgetdword(void) {
	return (uint32_t) *bufgetf(4);
}


int main(int argc, char **argv) {
	atexit(cleanup);
	char *path;
	if (argc == 2) {
		path = argv[1];
	} else if (argc == 1) {
		path = "Registry.pol";
	} else {
		fprintf(stderr, "Usage: %s /path/to/Registry.pol\n", argv[0]);
		return 64;
	}
	FILE *pol = fopen(path, "r");
	if (!pol) {
		fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
		return errno;
	}
	
	for (; !feof(pol) && !ferror(pol); ) {
		void *b = realloc(buf, buf_len += READ_INCREMENT);
		if (!b) {
			fprintf(stderr, "Cannot allocate memory: %s\n", strerror(errno));
			return errno;
		}
		buf = b;
		pol_len += fread((void *)(buf + pol_len), 1, READ_INCREMENT, pol);
	}
	if (ferror(pol)) {
		fprintf(strerror, "Cannot read the file: %s\n", strerror(ferror(pol)));
		return ferror(pol);
	}
	fclose(pol);
	pol = NULL;

	if (memcmp(bufgetf(4), "PReg", 4) != 0) {
		fprintf(stderr, "Not a valid registry policy file.\n");
		return 1;
	}
	const uint32_t ver = bufgetdword();
	if (ver != 1) {
		fprintf(stderr, "Unsupported format version: %u. Only supported version 1 at this time.\n", ver);
		return 1;
	}

	while (pol_index < pol_len) {
		bufasschr('[');
		/* key */
		bufprintstr();
		bufasschr(';');
		printf(", Value = ");

		/* value */
		bufprintstr();
		bufasschr(';');
		printf(" (");

		/* type */
		const uint32_t type = bufgetdword();
		bufasschr(';');
		printf("%s) ", typestr(type));

		/* size */
		const uint32_t size = bufgetdword();
		bufasschr(';');

		/* data */
		const uint32_t endi = pol_index + size;
		switch (type) {
			case REG_SZ:
			case REG_EXPAND_SZ: {
						    bufprintnstr(size);
						    break;
					    }
			case REG_DWORD: {
						if (size != 4) {
							fprintf(stderr, "Incorrect size of REG_DWORD at %d\n", pol_index);
							return 1;
						}
						printf("%d", bufgetdword());
						break;
					}
			case REG_DWORD_BIG_ENDIAN: 
			case REG_NONE: 
			case REG_BINARY: 
			case REG_LINK: 
			case REG_MULTI_SZ: 
			case REG_RESOURCE_LIST: 
			case REG_FULL_RESOURCE_DESCRIPTOR: 
			case REG_RESOURCE_REQUIREMENTS_LIST: 
			case REG_QWORD: 
			default: {
					 /* seek */
					 bufgetf(size);
					 break;
				 }
		}

		bufasschr(']');
		printf("\n");

	}
}