[30] | 1 | /* |
---|
[42] | 2 | * A utility to read a Windows NT/2K/XP/2K3 registry file, using |
---|
| 3 | * Gerald Carter''s regfio interface. |
---|
[30] | 4 | * |
---|
[81] | 5 | * Copyright (C) 2005-2007 Timothy D. Morgan |
---|
[42] | 6 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com |
---|
[30] | 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2 of the License. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 20 | * |
---|
| 21 | * $Id: reglookup.c 81 2007-01-17 16:47:39Z tim $ |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <string.h> |
---|
[33] | 28 | #include <strings.h> |
---|
[42] | 29 | #include <time.h> |
---|
[61] | 30 | #include <iconv.h> |
---|
[79] | 31 | #include "../include/regfi.h" |
---|
[31] | 32 | #include "../include/void_stack.h" |
---|
[30] | 33 | |
---|
[40] | 34 | /* Globals, influenced by command line parameters */ |
---|
| 35 | bool print_verbose = false; |
---|
| 36 | bool print_security = false; |
---|
[42] | 37 | bool print_header = true; |
---|
[40] | 38 | bool path_filter_enabled = false; |
---|
| 39 | bool type_filter_enabled = false; |
---|
| 40 | char* path_filter = NULL; |
---|
| 41 | int type_filter; |
---|
| 42 | char* registry_file = NULL; |
---|
| 43 | |
---|
[42] | 44 | /* Other globals */ |
---|
[66] | 45 | const char* key_special_chars = ",\"\\/"; |
---|
| 46 | const char* subfield_special_chars = ",\"\\|"; |
---|
| 47 | const char* common_special_chars = ",\"\\"; |
---|
| 48 | |
---|
[61] | 49 | iconv_t conv_desc; |
---|
[40] | 50 | |
---|
[61] | 51 | |
---|
[38] | 52 | void bailOut(int code, char* message) |
---|
| 53 | { |
---|
| 54 | fprintf(stderr, message); |
---|
| 55 | exit(code); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
[41] | 59 | /* Returns a newly malloc()ed string which contains original buffer, |
---|
| 60 | * except for non-printable or special characters are quoted in hex |
---|
| 61 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
[61] | 62 | * character. A null terminator is added, since only ascii, not binary, |
---|
[41] | 63 | * is returned. |
---|
| 64 | */ |
---|
| 65 | static char* quote_buffer(const unsigned char* str, |
---|
[44] | 66 | unsigned int len, const char* special) |
---|
[41] | 67 | { |
---|
[61] | 68 | unsigned int i, added_len; |
---|
| 69 | unsigned int num_written = 0; |
---|
[41] | 70 | |
---|
[61] | 71 | unsigned int buf_len = sizeof(char)*(len+1); |
---|
| 72 | char* ret_val = malloc(buf_len); |
---|
| 73 | char* tmp_buf; |
---|
| 74 | |
---|
[41] | 75 | if(ret_val == NULL) |
---|
| 76 | return NULL; |
---|
| 77 | |
---|
| 78 | for(i=0; i<len; i++) |
---|
| 79 | { |
---|
[61] | 80 | if(buf_len <= (num_written+5)) |
---|
| 81 | { |
---|
| 82 | /* Expand the buffer by the memory consumption rate seen so far |
---|
| 83 | * times the amount of input left to process. The expansion is bounded |
---|
| 84 | * below by a minimum safety increase, and above by the maximum possible |
---|
[69] | 85 | * output string length. This should minimize both the number of |
---|
| 86 | * reallocs() and the amount of wasted memory. |
---|
[61] | 87 | */ |
---|
| 88 | added_len = (len-i)*num_written/(i+1); |
---|
| 89 | if((buf_len+added_len) > (len*4+1)) |
---|
| 90 | buf_len = len*4+1; |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | if (added_len < 5) |
---|
| 94 | buf_len += 5; |
---|
| 95 | else |
---|
| 96 | buf_len += added_len; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | tmp_buf = realloc(ret_val, buf_len); |
---|
| 100 | if(tmp_buf == NULL) |
---|
| 101 | { |
---|
| 102 | free(ret_val); |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | ret_val = tmp_buf; |
---|
| 106 | } |
---|
| 107 | |
---|
[41] | 108 | if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL) |
---|
| 109 | { |
---|
[61] | 110 | num_written += snprintf(ret_val + num_written, buf_len - num_written, |
---|
[41] | 111 | "\\x%.2X", str[i]); |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | ret_val[num_written++] = str[i]; |
---|
| 115 | } |
---|
| 116 | ret_val[num_written] = '\0'; |
---|
| 117 | |
---|
| 118 | return ret_val; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | /* Returns a newly malloc()ed string which contains original string, |
---|
| 123 | * except for non-printable or special characters are quoted in hex |
---|
| 124 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
| 125 | * character. |
---|
| 126 | */ |
---|
[44] | 127 | static char* quote_string(const char* str, const char* special) |
---|
[41] | 128 | { |
---|
[42] | 129 | unsigned int len; |
---|
[41] | 130 | |
---|
[42] | 131 | if(str == NULL) |
---|
| 132 | return NULL; |
---|
| 133 | |
---|
| 134 | len = strlen(str); |
---|
| 135 | return quote_buffer((const unsigned char*)str, len, special); |
---|
[41] | 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /* |
---|
[69] | 140 | * Convert from UTF-16LE to ASCII. Accepts a Unicode buffer, uni, and |
---|
| 141 | * it's length, uni_max. Writes ASCII to the buffer ascii, whose size |
---|
| 142 | * is ascii_max. Writes at most (ascii_max-1) bytes to ascii, and null |
---|
| 143 | * terminates the string. Returns the length of the string stored in |
---|
| 144 | * ascii. On error, returns a negative errno code. |
---|
[41] | 145 | */ |
---|
[61] | 146 | static int uni_to_ascii(unsigned char* uni, char* ascii, |
---|
| 147 | unsigned int uni_max, unsigned int ascii_max) |
---|
[41] | 148 | { |
---|
[61] | 149 | char* inbuf = (char*)uni; |
---|
| 150 | char* outbuf = ascii; |
---|
[70] | 151 | size_t in_len = (size_t)uni_max; |
---|
| 152 | size_t out_len = (size_t)(ascii_max-1); |
---|
[61] | 153 | int ret; |
---|
[41] | 154 | |
---|
[61] | 155 | /* Set up conversion descriptor. */ |
---|
| 156 | conv_desc = iconv_open("US-ASCII", "UTF-16LE"); |
---|
| 157 | |
---|
[70] | 158 | ret = iconv(conv_desc, &inbuf, &in_len, &outbuf, &out_len); |
---|
[61] | 159 | if(ret == -1) |
---|
[41] | 160 | { |
---|
[61] | 161 | iconv_close(conv_desc); |
---|
[66] | 162 | return -errno; |
---|
[41] | 163 | } |
---|
[66] | 164 | *outbuf = '\0'; |
---|
[41] | 165 | |
---|
[61] | 166 | iconv_close(conv_desc); |
---|
| 167 | return strlen(ascii); |
---|
[41] | 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | /* |
---|
[69] | 172 | * Convert a data value to a string for display. Returns NULL on error, |
---|
| 173 | * and the string to display if there is no error, or a non-fatal |
---|
| 174 | * error. On any error (fatal or non-fatal) occurs, (*error_msg) will |
---|
| 175 | * be set to a newly allocated string, containing an error message. If |
---|
| 176 | * a memory allocation failure occurs while generating the error |
---|
| 177 | * message, both the return value and (*error_msg) will be NULL. It |
---|
| 178 | * is the responsibility of the caller to free both a non-NULL return |
---|
| 179 | * value, and a non-NULL (*error_msg). |
---|
[41] | 180 | */ |
---|
[77] | 181 | static char* data_to_ascii(unsigned char *datap, uint32 len, uint32 type, |
---|
[69] | 182 | char** error_msg) |
---|
[41] | 183 | { |
---|
[61] | 184 | char* asciip; |
---|
| 185 | char* ascii; |
---|
[41] | 186 | unsigned char* cur_str; |
---|
[61] | 187 | char* cur_ascii; |
---|
[41] | 188 | char* cur_quoted; |
---|
[69] | 189 | char* tmp_err; |
---|
| 190 | const char* str_type; |
---|
[77] | 191 | uint32 i; |
---|
| 192 | uint32 cur_str_len; |
---|
| 193 | uint32 ascii_max, cur_str_max; |
---|
| 194 | uint32 str_rem, cur_str_rem, alen; |
---|
[66] | 195 | int ret_err; |
---|
[69] | 196 | unsigned short num_nulls; |
---|
[41] | 197 | |
---|
[69] | 198 | *error_msg = NULL; |
---|
| 199 | |
---|
[41] | 200 | switch (type) |
---|
| 201 | { |
---|
[66] | 202 | case REG_SZ: |
---|
| 203 | case REG_EXPAND_SZ: |
---|
[61] | 204 | /* REG_LINK is a symbolic link, stored as a unicode string. */ |
---|
| 205 | case REG_LINK: |
---|
[69] | 206 | ascii_max = sizeof(char)*(len+1); |
---|
| 207 | ascii = malloc(ascii_max); |
---|
[41] | 208 | if(ascii == NULL) |
---|
| 209 | return NULL; |
---|
| 210 | |
---|
[66] | 211 | /* Sometimes values have binary stored in them. If the unicode |
---|
| 212 | * conversion fails, just quote it raw. |
---|
| 213 | */ |
---|
| 214 | ret_err = uni_to_ascii(datap, ascii, len, ascii_max); |
---|
| 215 | if(ret_err < 0) |
---|
[61] | 216 | { |
---|
[69] | 217 | tmp_err = strerror(-ret_err); |
---|
[78] | 218 | str_type = regfi_type_val2str(type); |
---|
[69] | 219 | *error_msg = (char*)malloc(65+strlen(str_type)+strlen(tmp_err)+1); |
---|
| 220 | if(*error_msg == NULL) |
---|
[71] | 221 | { |
---|
| 222 | free(ascii); |
---|
[69] | 223 | return NULL; |
---|
[71] | 224 | } |
---|
[69] | 225 | sprintf(*error_msg, "Unicode conversion failed on %s field; " |
---|
| 226 | "printing as binary. Error: %s", str_type, tmp_err); |
---|
| 227 | |
---|
[66] | 228 | cur_quoted = quote_buffer(datap, len, common_special_chars); |
---|
[61] | 229 | } |
---|
[66] | 230 | else |
---|
| 231 | cur_quoted = quote_string(ascii, common_special_chars); |
---|
[42] | 232 | free(ascii); |
---|
[71] | 233 | if(cur_quoted == NULL) |
---|
| 234 | { |
---|
| 235 | *error_msg = (char*)malloc(27+1); |
---|
| 236 | if(*error_msg != NULL) |
---|
| 237 | strcpy(*error_msg, "Buffer could not be quoted."); |
---|
| 238 | } |
---|
[61] | 239 | return cur_quoted; |
---|
[41] | 240 | break; |
---|
| 241 | |
---|
| 242 | case REG_DWORD: |
---|
[72] | 243 | ascii_max = sizeof(char)*(8+2+1); |
---|
[58] | 244 | ascii = malloc(ascii_max); |
---|
[41] | 245 | if(ascii == NULL) |
---|
| 246 | return NULL; |
---|
| 247 | |
---|
[61] | 248 | snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", |
---|
[58] | 249 | datap[0], datap[1], datap[2], datap[3]); |
---|
[41] | 250 | return ascii; |
---|
| 251 | break; |
---|
| 252 | |
---|
[58] | 253 | case REG_DWORD_BE: |
---|
[72] | 254 | ascii_max = sizeof(char)*(8+2+1); |
---|
[58] | 255 | ascii = malloc(ascii_max); |
---|
| 256 | if(ascii == NULL) |
---|
| 257 | return NULL; |
---|
| 258 | |
---|
[61] | 259 | snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", |
---|
[58] | 260 | datap[3], datap[2], datap[1], datap[0]); |
---|
| 261 | return ascii; |
---|
| 262 | break; |
---|
| 263 | |
---|
[72] | 264 | case REG_QWORD: |
---|
| 265 | ascii_max = sizeof(char)*(16+2+1); |
---|
| 266 | ascii = malloc(ascii_max); |
---|
| 267 | if(ascii == NULL) |
---|
| 268 | return NULL; |
---|
| 269 | |
---|
| 270 | snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X", |
---|
| 271 | datap[7], datap[6], datap[5], datap[4], |
---|
| 272 | datap[3], datap[2], datap[1], datap[0]); |
---|
| 273 | return ascii; |
---|
| 274 | break; |
---|
| 275 | |
---|
| 276 | |
---|
[42] | 277 | /* XXX: this MULTI_SZ parser is pretty inefficient. Should be |
---|
[54] | 278 | * redone with fewer malloc calls and better string concatenation. |
---|
[42] | 279 | */ |
---|
[41] | 280 | case REG_MULTI_SZ: |
---|
[69] | 281 | ascii_max = sizeof(char)*(len*4+1); |
---|
| 282 | cur_str_max = sizeof(char)*(len+1); |
---|
[41] | 283 | cur_str = malloc(cur_str_max); |
---|
| 284 | cur_ascii = malloc(cur_str_max); |
---|
[69] | 285 | ascii = malloc(ascii_max); |
---|
[42] | 286 | if(ascii == NULL || cur_str == NULL || cur_ascii == NULL) |
---|
[41] | 287 | return NULL; |
---|
| 288 | |
---|
| 289 | /* Reads until it reaches 4 consecutive NULLs, |
---|
| 290 | * which is two nulls in unicode, or until it reaches len, or until we |
---|
| 291 | * run out of buffer. The latter should never happen, but we shouldn't |
---|
| 292 | * trust our file to have the right lengths/delimiters. |
---|
| 293 | */ |
---|
| 294 | asciip = ascii; |
---|
| 295 | num_nulls = 0; |
---|
| 296 | str_rem = ascii_max; |
---|
| 297 | cur_str_rem = cur_str_max; |
---|
| 298 | cur_str_len = 0; |
---|
| 299 | |
---|
| 300 | for(i=0; (i < len) && str_rem > 0; i++) |
---|
| 301 | { |
---|
| 302 | *(cur_str+cur_str_len) = *(datap+i); |
---|
| 303 | if(*(cur_str+cur_str_len) == 0) |
---|
| 304 | num_nulls++; |
---|
| 305 | else |
---|
| 306 | num_nulls = 0; |
---|
| 307 | cur_str_len++; |
---|
| 308 | |
---|
| 309 | if(num_nulls == 2) |
---|
| 310 | { |
---|
[66] | 311 | ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max); |
---|
| 312 | if(ret_err < 0) |
---|
[61] | 313 | { |
---|
[69] | 314 | /* XXX: should every sub-field error be enumerated? */ |
---|
| 315 | if(*error_msg == NULL) |
---|
| 316 | { |
---|
| 317 | tmp_err = strerror(-ret_err); |
---|
| 318 | *error_msg = (char*)malloc(90+strlen(tmp_err)+1); |
---|
| 319 | if(*error_msg == NULL) |
---|
[71] | 320 | { |
---|
| 321 | free(cur_str); |
---|
| 322 | free(cur_ascii); |
---|
| 323 | free(ascii); |
---|
[69] | 324 | return NULL; |
---|
[71] | 325 | } |
---|
[69] | 326 | sprintf(*error_msg, "Unicode conversion failed on at least one " |
---|
| 327 | "MULTI_SZ sub-field; printing as binary. Error: %s", |
---|
| 328 | tmp_err); |
---|
| 329 | } |
---|
[66] | 330 | cur_quoted = quote_buffer(cur_str, cur_str_len-1, |
---|
| 331 | subfield_special_chars); |
---|
[61] | 332 | } |
---|
[66] | 333 | else |
---|
| 334 | cur_quoted = quote_string(cur_ascii, subfield_special_chars); |
---|
[61] | 335 | |
---|
| 336 | alen = snprintf(asciip, str_rem, "%s", cur_quoted); |
---|
[41] | 337 | asciip += alen; |
---|
| 338 | str_rem -= alen; |
---|
| 339 | free(cur_quoted); |
---|
| 340 | |
---|
| 341 | if(*(datap+i+1) == 0 && *(datap+i+2) == 0) |
---|
| 342 | break; |
---|
| 343 | else |
---|
| 344 | { |
---|
[61] | 345 | if(str_rem > 0) |
---|
| 346 | { |
---|
| 347 | asciip[0] = '|'; |
---|
| 348 | asciip[1] = '\0'; |
---|
| 349 | asciip++; |
---|
| 350 | str_rem--; |
---|
| 351 | } |
---|
[41] | 352 | memset(cur_str, 0, cur_str_max); |
---|
| 353 | cur_str_len = 0; |
---|
| 354 | num_nulls = 0; |
---|
| 355 | /* To eliminate leading nulls in subsequent strings. */ |
---|
| 356 | i++; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | *asciip = 0; |
---|
[42] | 361 | free(cur_str); |
---|
| 362 | free(cur_ascii); |
---|
[41] | 363 | return ascii; |
---|
| 364 | break; |
---|
| 365 | |
---|
[42] | 366 | /* XXX: Dont know what to do with these yet, just print as binary... */ |
---|
[77] | 367 | default: |
---|
| 368 | fprintf(stderr, "WARNING: Unrecognized registry data type (0x%.8X); quoting as binary.\n", type); |
---|
| 369 | |
---|
[61] | 370 | case REG_NONE: |
---|
[42] | 371 | case REG_RESOURCE_LIST: |
---|
| 372 | case REG_FULL_RESOURCE_DESCRIPTOR: |
---|
| 373 | case REG_RESOURCE_REQUIREMENTS_LIST: |
---|
| 374 | |
---|
| 375 | case REG_BINARY: |
---|
[66] | 376 | return quote_buffer(datap, len, common_special_chars); |
---|
[42] | 377 | break; |
---|
[71] | 378 | } |
---|
[42] | 379 | |
---|
[41] | 380 | return NULL; |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | |
---|
[81] | 384 | char** splitPath(const char* s) |
---|
[30] | 385 | { |
---|
[81] | 386 | char** ret_val; |
---|
[38] | 387 | const char* cur = s; |
---|
[33] | 388 | char* next = NULL; |
---|
[38] | 389 | char* copy; |
---|
[81] | 390 | uint32 ret_cur = 0; |
---|
[38] | 391 | |
---|
[81] | 392 | ret_val = (char**)malloc((REGF_MAX_DEPTH+1+1)*sizeof(char**)); |
---|
| 393 | if (ret_val == NULL) |
---|
[38] | 394 | return NULL; |
---|
[81] | 395 | ret_val[0] = NULL; |
---|
| 396 | |
---|
| 397 | /* We return a well-formed, 0-length, path even when input is icky. */ |
---|
[37] | 398 | if (s == NULL) |
---|
[81] | 399 | return ret_val; |
---|
[38] | 400 | |
---|
| 401 | while((next = strchr(cur, '/')) != NULL) |
---|
[33] | 402 | { |
---|
[38] | 403 | if ((next-cur) > 0) |
---|
| 404 | { |
---|
| 405 | copy = (char*)malloc((next-cur+1)*sizeof(char)); |
---|
| 406 | if(copy == NULL) |
---|
| 407 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
| 408 | |
---|
| 409 | memcpy(copy, cur, next-cur); |
---|
| 410 | copy[next-cur] = '\0'; |
---|
[81] | 411 | ret_val[ret_cur++] = copy; |
---|
| 412 | if(ret_cur < (REGF_MAX_DEPTH+1+1)) |
---|
| 413 | ret_val[ret_cur] = NULL; |
---|
| 414 | else |
---|
[78] | 415 | bailOut(2, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 416 | } |
---|
| 417 | cur = next+1; |
---|
[33] | 418 | } |
---|
[81] | 419 | |
---|
| 420 | /* Grab last element, if path doesn't end in '/'. */ |
---|
[33] | 421 | if(strlen(cur) > 0) |
---|
[38] | 422 | { |
---|
| 423 | copy = strdup(cur); |
---|
[81] | 424 | ret_val[ret_cur++] = copy; |
---|
| 425 | if(ret_cur < (REGF_MAX_DEPTH+1+1)) |
---|
| 426 | ret_val[ret_cur] = NULL; |
---|
| 427 | else |
---|
[78] | 428 | bailOut(2, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 429 | } |
---|
[33] | 430 | |
---|
| 431 | return ret_val; |
---|
| 432 | } |
---|
| 433 | |
---|
[81] | 434 | |
---|
| 435 | /* Returns a quoted path from an iterator's stack */ |
---|
| 436 | /* XXX: Some way should be found to integrate this into regfi's API |
---|
| 437 | * The problem is that the escaping is sorta reglookup-specific. |
---|
| 438 | */ |
---|
| 439 | char* iter2Path(REGFI_ITERATOR* i) |
---|
[33] | 440 | { |
---|
[81] | 441 | const REGFI_ITER_POSITION* cur; |
---|
[37] | 442 | uint32 buf_left = 127; |
---|
| 443 | uint32 buf_len = buf_left+1; |
---|
| 444 | uint32 name_len = 0; |
---|
| 445 | uint32 grow_amt; |
---|
[81] | 446 | char* buf; |
---|
[31] | 447 | char* new_buf; |
---|
[66] | 448 | char* name; |
---|
[81] | 449 | const char* cur_name; |
---|
[31] | 450 | void_stack_iterator* iter; |
---|
| 451 | |
---|
| 452 | buf = (char*)malloc((buf_len)*sizeof(char)); |
---|
| 453 | if (buf == NULL) |
---|
| 454 | return NULL; |
---|
[54] | 455 | buf[0] = '\0'; |
---|
[30] | 456 | |
---|
[81] | 457 | iter = void_stack_iterator_new(i->key_positions); |
---|
[31] | 458 | if (iter == NULL) |
---|
[30] | 459 | { |
---|
[31] | 460 | free(buf); |
---|
| 461 | return NULL; |
---|
[30] | 462 | } |
---|
| 463 | |
---|
[33] | 464 | /* skip root element */ |
---|
[81] | 465 | if(void_stack_size(i->key_positions) < 1) |
---|
| 466 | { |
---|
| 467 | buf[0] = '/'; |
---|
| 468 | buf[1] = '\0'; |
---|
| 469 | return buf; |
---|
| 470 | } |
---|
[33] | 471 | cur = void_stack_iterator_next(iter); |
---|
| 472 | |
---|
[81] | 473 | do |
---|
[31] | 474 | { |
---|
[81] | 475 | cur = void_stack_iterator_next(iter); |
---|
| 476 | if (cur == NULL) |
---|
| 477 | cur_name = i->cur_key->keyname; |
---|
| 478 | else |
---|
| 479 | cur_name = cur->nk->keyname; |
---|
| 480 | |
---|
[33] | 481 | buf[buf_len-buf_left-1] = '/'; |
---|
| 482 | buf_left -= 1; |
---|
[81] | 483 | name = quote_string(cur_name, key_special_chars); |
---|
[66] | 484 | name_len = strlen(name); |
---|
[31] | 485 | if(name_len+1 > buf_left) |
---|
| 486 | { |
---|
[37] | 487 | grow_amt = (uint32)(buf_len/2); |
---|
[31] | 488 | buf_len += name_len+1+grow_amt-buf_left; |
---|
| 489 | if((new_buf = realloc(buf, buf_len)) == NULL) |
---|
| 490 | { |
---|
| 491 | free(buf); |
---|
| 492 | free(iter); |
---|
| 493 | return NULL; |
---|
| 494 | } |
---|
| 495 | buf = new_buf; |
---|
| 496 | buf_left = grow_amt + name_len + 1; |
---|
| 497 | } |
---|
[66] | 498 | strncpy(buf+(buf_len-buf_left-1), name, name_len); |
---|
[31] | 499 | buf_left -= name_len; |
---|
| 500 | buf[buf_len-buf_left-1] = '\0'; |
---|
[66] | 501 | free(name); |
---|
[81] | 502 | } while(cur != NULL); |
---|
[30] | 503 | |
---|
[31] | 504 | return buf; |
---|
| 505 | } |
---|
[30] | 506 | |
---|
[31] | 507 | |
---|
[33] | 508 | void printValue(REGF_VK_REC* vk, char* prefix) |
---|
[31] | 509 | { |
---|
[66] | 510 | char* quoted_value = NULL; |
---|
| 511 | char* quoted_name = NULL; |
---|
[69] | 512 | char* conv_error = NULL; |
---|
[77] | 513 | const char* str_type = NULL; |
---|
| 514 | uint32 size; |
---|
| 515 | uint8 tmp_buf[4]; |
---|
[41] | 516 | |
---|
[43] | 517 | /* Thanks Microsoft for making this process so straight-forward!!! */ |
---|
| 518 | size = (vk->data_size & ~VK_DATA_IN_OFFSET); |
---|
| 519 | if(vk->data_size & VK_DATA_IN_OFFSET) |
---|
[41] | 520 | { |
---|
[43] | 521 | tmp_buf[0] = (uint8)((vk->data_off >> 3) & 0xFF); |
---|
| 522 | tmp_buf[1] = (uint8)((vk->data_off >> 2) & 0xFF); |
---|
| 523 | tmp_buf[2] = (uint8)((vk->data_off >> 1) & 0xFF); |
---|
| 524 | tmp_buf[3] = (uint8)(vk->data_off & 0xFF); |
---|
| 525 | if(size > 4) |
---|
[77] | 526 | /* XXX: should we kick out a warning here? If it is in the |
---|
| 527 | * offset and longer than four, file could be corrupt |
---|
| 528 | * or malicious... */ |
---|
[43] | 529 | size = 4; |
---|
[69] | 530 | quoted_value = data_to_ascii(tmp_buf, 4, vk->type, &conv_error); |
---|
[43] | 531 | } |
---|
| 532 | else |
---|
| 533 | { |
---|
[77] | 534 | /* Microsoft's documentation indicates that "available memory" is |
---|
| 535 | * the limit on value sizes. Annoying. We limit it to 1M which |
---|
| 536 | * should rarely be exceeded, unless the file is corrupt or |
---|
| 537 | * malicious. For more info, see: |
---|
| 538 | * http://msdn2.microsoft.com/en-us/library/ms724872.aspx |
---|
[43] | 539 | */ |
---|
[77] | 540 | if(size > VK_MAX_DATA_LENGTH) |
---|
[41] | 541 | { |
---|
[77] | 542 | fprintf(stderr, "WARNING: value data size %d larger than " |
---|
| 543 | "%d, truncating...\n", size, VK_MAX_DATA_LENGTH); |
---|
| 544 | size = VK_MAX_DATA_LENGTH; |
---|
[41] | 545 | } |
---|
[61] | 546 | |
---|
[69] | 547 | quoted_value = data_to_ascii(vk->data, vk->data_size, |
---|
| 548 | vk->type, &conv_error); |
---|
[43] | 549 | } |
---|
| 550 | |
---|
| 551 | /* XXX: Sometimes value names can be NULL in registry. Need to |
---|
| 552 | * figure out why and when, and generate the appropriate output |
---|
| 553 | * for that condition. |
---|
| 554 | */ |
---|
[66] | 555 | quoted_name = quote_string(vk->valuename, common_special_chars); |
---|
[69] | 556 | |
---|
| 557 | if(quoted_value == NULL) |
---|
| 558 | { |
---|
| 559 | if(conv_error == NULL) |
---|
[71] | 560 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
[69] | 561 | "Memory allocation failure likely.\n", prefix, quoted_name); |
---|
| 562 | else |
---|
[71] | 563 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
[69] | 564 | "Returned error: %s\n", prefix, quoted_name, conv_error); |
---|
| 565 | } |
---|
| 566 | /* XXX: should these always be printed? */ |
---|
| 567 | else if(conv_error != NULL && print_verbose) |
---|
| 568 | fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', " |
---|
| 569 | "warning returned: %s\n", prefix, quoted_name, conv_error); |
---|
| 570 | |
---|
[78] | 571 | str_type = regfi_type_val2str(vk->type); |
---|
[43] | 572 | if(print_security) |
---|
[77] | 573 | { |
---|
| 574 | if(str_type == NULL) |
---|
| 575 | printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name, |
---|
| 576 | vk->type, quoted_value); |
---|
| 577 | else |
---|
| 578 | printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name, |
---|
| 579 | str_type, quoted_value); |
---|
| 580 | } |
---|
[43] | 581 | else |
---|
[77] | 582 | { |
---|
| 583 | if(str_type == NULL) |
---|
| 584 | printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name, |
---|
| 585 | vk->type, quoted_value); |
---|
| 586 | else |
---|
| 587 | printf("%s/%s,%s,%s,\n", prefix, quoted_name, |
---|
| 588 | str_type, quoted_value); |
---|
| 589 | } |
---|
| 590 | |
---|
[43] | 591 | if(quoted_value != NULL) |
---|
| 592 | free(quoted_value); |
---|
| 593 | if(quoted_name != NULL) |
---|
| 594 | free(quoted_name); |
---|
[69] | 595 | if(conv_error != NULL) |
---|
| 596 | free(conv_error); |
---|
[32] | 597 | } |
---|
| 598 | |
---|
| 599 | |
---|
[81] | 600 | void printValueList(REGFI_ITERATOR* i, char* prefix) |
---|
[32] | 601 | { |
---|
[80] | 602 | REGF_VK_REC* value; |
---|
| 603 | |
---|
| 604 | value = regfi_iterator_first_value(i); |
---|
| 605 | while(value != NULL) |
---|
[81] | 606 | { |
---|
| 607 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
[80] | 608 | printValue(value, prefix); |
---|
[81] | 609 | value = regfi_iterator_next_value(i); |
---|
| 610 | } |
---|
[33] | 611 | } |
---|
| 612 | |
---|
[37] | 613 | |
---|
[43] | 614 | void printKey(REGF_NK_REC* k, char* full_path) |
---|
[33] | 615 | { |
---|
[43] | 616 | static char empty_str[1] = ""; |
---|
[42] | 617 | char* owner = NULL; |
---|
| 618 | char* group = NULL; |
---|
| 619 | char* sacl = NULL; |
---|
| 620 | char* dacl = NULL; |
---|
| 621 | char mtime[20]; |
---|
| 622 | time_t tmp_time[1]; |
---|
| 623 | struct tm* tmp_time_s = NULL; |
---|
| 624 | |
---|
[43] | 625 | *tmp_time = nt_time_to_unix(&k->mtime); |
---|
| 626 | tmp_time_s = gmtime(tmp_time); |
---|
| 627 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s); |
---|
| 628 | |
---|
| 629 | if(print_security) |
---|
| 630 | { |
---|
[78] | 631 | owner = regfi_get_owner(k->sec_desc->sec_desc); |
---|
| 632 | group = regfi_get_group(k->sec_desc->sec_desc); |
---|
| 633 | sacl = regfi_get_sacl(k->sec_desc->sec_desc); |
---|
| 634 | dacl = regfi_get_dacl(k->sec_desc->sec_desc); |
---|
[43] | 635 | if(owner == NULL) |
---|
| 636 | owner = empty_str; |
---|
| 637 | if(group == NULL) |
---|
| 638 | group = empty_str; |
---|
| 639 | if(sacl == NULL) |
---|
| 640 | sacl = empty_str; |
---|
| 641 | if(dacl == NULL) |
---|
| 642 | dacl = empty_str; |
---|
| 643 | |
---|
[66] | 644 | printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime, |
---|
[43] | 645 | owner, group, sacl, dacl); |
---|
| 646 | |
---|
| 647 | if(owner != empty_str) |
---|
| 648 | free(owner); |
---|
| 649 | if(group != empty_str) |
---|
| 650 | free(group); |
---|
| 651 | if(sacl != empty_str) |
---|
| 652 | free(sacl); |
---|
| 653 | if(dacl != empty_str) |
---|
| 654 | free(dacl); |
---|
| 655 | } |
---|
| 656 | else |
---|
[66] | 657 | printf("%s,KEY,,%s\n", full_path, mtime); |
---|
[43] | 658 | } |
---|
| 659 | |
---|
| 660 | |
---|
[81] | 661 | void printKeyTree(REGFI_ITERATOR* iter) |
---|
[43] | 662 | { |
---|
[81] | 663 | REGF_NK_REC* root = NULL; |
---|
[52] | 664 | REGF_NK_REC* cur = NULL; |
---|
| 665 | REGF_NK_REC* sub = NULL; |
---|
[43] | 666 | char* path = NULL; |
---|
[78] | 667 | int key_type = regfi_type_str2val("KEY"); |
---|
[81] | 668 | bool print_this = true; |
---|
| 669 | |
---|
| 670 | root = cur = regfi_iterator_cur_key(iter); |
---|
| 671 | sub = regfi_iterator_first_subkey(iter); |
---|
[43] | 672 | |
---|
[81] | 673 | if(root == NULL) |
---|
| 674 | bailOut(3, "ERROR: root cannot be NULL.\n"); |
---|
| 675 | |
---|
| 676 | do |
---|
[31] | 677 | { |
---|
[81] | 678 | if(print_this) |
---|
[54] | 679 | { |
---|
[81] | 680 | path = iter2Path(iter); |
---|
| 681 | if(path == NULL) |
---|
| 682 | bailOut(2, "ERROR: Could not construct iterator's path.\n"); |
---|
| 683 | |
---|
| 684 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
| 685 | printKey(cur, path); |
---|
| 686 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
| 687 | printValueList(iter, path); |
---|
| 688 | |
---|
| 689 | free(path); |
---|
[54] | 690 | } |
---|
[66] | 691 | |
---|
[81] | 692 | if(sub == NULL) |
---|
[31] | 693 | { |
---|
[81] | 694 | if(cur != root) |
---|
[31] | 695 | { |
---|
[81] | 696 | /* We're done with this sub-tree, going up and hitting other branches. */ |
---|
| 697 | if(!regfi_iterator_up(iter)) |
---|
| 698 | bailOut(3, "ERROR: could not traverse iterator upward.\n"); |
---|
| 699 | |
---|
| 700 | cur = regfi_iterator_cur_key(iter); |
---|
| 701 | if(cur == NULL) |
---|
| 702 | bailOut(3, "ERROR: unexpected NULL for key.\n"); |
---|
| 703 | |
---|
| 704 | sub = regfi_iterator_next_subkey(iter); |
---|
[66] | 705 | } |
---|
[81] | 706 | print_this = false; |
---|
[31] | 707 | } |
---|
[81] | 708 | else |
---|
| 709 | { /* We have unexplored sub-keys. |
---|
| 710 | * Let's move down and print this first sub-tree out. |
---|
| 711 | */ |
---|
| 712 | if(!regfi_iterator_down(iter)) |
---|
| 713 | bailOut(3, "ERROR: could not traverse iterator downward.\n"); |
---|
| 714 | |
---|
| 715 | cur = sub; |
---|
| 716 | sub = regfi_iterator_first_subkey(iter); |
---|
| 717 | print_this = true; |
---|
| 718 | } |
---|
| 719 | } while(!((cur == root) && (sub == NULL))); |
---|
| 720 | |
---|
[54] | 721 | if(print_verbose) |
---|
| 722 | fprintf(stderr, "VERBOSE: Finished printing key tree.\n"); |
---|
[30] | 723 | } |
---|
| 724 | |
---|
[81] | 725 | |
---|
[33] | 726 | /* |
---|
[80] | 727 | * Returns 0 if path was not found. |
---|
| 728 | * Returns 1 if path was found as value. |
---|
| 729 | * Returns 2 if path was found as key. |
---|
[33] | 730 | * Returns less than 0 on other error. |
---|
| 731 | */ |
---|
[80] | 732 | int retrievePath(REGFI_ITERATOR* iter, char** path) |
---|
[33] | 733 | { |
---|
[81] | 734 | REGF_VK_REC* value; |
---|
| 735 | char* tmp_path_joined; |
---|
| 736 | const char** tmp_path; |
---|
[80] | 737 | uint32 i; |
---|
| 738 | |
---|
| 739 | if(path == NULL) |
---|
[33] | 740 | return -1; |
---|
| 741 | |
---|
[80] | 742 | /* One extra for any value at the end, and one more for NULL */ |
---|
[81] | 743 | tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1)); |
---|
[80] | 744 | if(tmp_path == NULL) |
---|
[33] | 745 | return -2; |
---|
| 746 | |
---|
[80] | 747 | /* Strip any potential value name at end of path */ |
---|
| 748 | for(i=0; |
---|
| 749 | (path[i] != NULL) && (path[i+1] != NULL) |
---|
| 750 | && (i < REGF_MAX_DEPTH+1+1); |
---|
| 751 | i++) |
---|
| 752 | tmp_path[i] = path[i]; |
---|
[33] | 753 | |
---|
[80] | 754 | tmp_path[i] = NULL; |
---|
| 755 | |
---|
[54] | 756 | if(print_verbose) |
---|
[80] | 757 | fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n", |
---|
[54] | 758 | path_filter); |
---|
| 759 | |
---|
[80] | 760 | if(!regfi_iterator_walk_path(iter, tmp_path)) |
---|
[33] | 761 | { |
---|
[80] | 762 | free(tmp_path); |
---|
| 763 | return 0; |
---|
[33] | 764 | } |
---|
| 765 | |
---|
[80] | 766 | if(regfi_iterator_find_value(iter, path[i])) |
---|
| 767 | { |
---|
| 768 | if(print_verbose) |
---|
| 769 | fprintf(stderr, "VERBOSE: Found final path element as value.\n"); |
---|
[33] | 770 | |
---|
[80] | 771 | value = regfi_iterator_cur_value(iter); |
---|
[81] | 772 | tmp_path_joined = iter2Path(iter); |
---|
[54] | 773 | |
---|
[80] | 774 | if((value == NULL) || (tmp_path_joined == NULL)) |
---|
| 775 | bailOut(2, "ERROR: Unexpected error before printValue.\n"); |
---|
[54] | 776 | |
---|
[80] | 777 | printValue(value, tmp_path_joined); |
---|
[54] | 778 | |
---|
[80] | 779 | free(tmp_path); |
---|
| 780 | free(tmp_path_joined); |
---|
| 781 | return 1; |
---|
[33] | 782 | } |
---|
[80] | 783 | else if(regfi_iterator_find_subkey(iter, path[i])) |
---|
[33] | 784 | { |
---|
[80] | 785 | if(print_verbose) |
---|
| 786 | fprintf(stderr, "VERBOSE: Found final path element as key.\n"); |
---|
| 787 | return 2; |
---|
[33] | 788 | } |
---|
| 789 | |
---|
[54] | 790 | if(print_verbose) |
---|
| 791 | fprintf(stderr, "VERBOSE: Could not find last element of path.\n"); |
---|
| 792 | |
---|
[80] | 793 | return 0; |
---|
[33] | 794 | } |
---|
| 795 | |
---|
| 796 | |
---|
[37] | 797 | static void usage(void) |
---|
| 798 | { |
---|
[61] | 799 | fprintf(stderr, "Usage: reglookup [-v] [-s]" |
---|
[40] | 800 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" |
---|
[39] | 801 | " <REGISTRY_FILE>\n"); |
---|
[61] | 802 | fprintf(stderr, "Version: 0.3.0\n"); |
---|
[39] | 803 | fprintf(stderr, "Options:\n"); |
---|
| 804 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
[47] | 805 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 806 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
[44] | 807 | fprintf(stderr, "\t-s\t enables security descriptor output.\n"); |
---|
| 808 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n"); |
---|
[40] | 809 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n"); |
---|
| 810 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n"); |
---|
[37] | 811 | fprintf(stderr, "\n"); |
---|
| 812 | } |
---|
| 813 | |
---|
| 814 | |
---|
[30] | 815 | int main(int argc, char** argv) |
---|
| 816 | { |
---|
[80] | 817 | char** path = NULL; |
---|
[31] | 818 | REGF_FILE* f; |
---|
[80] | 819 | REGFI_ITERATOR* iter; |
---|
[33] | 820 | int retr_path_ret; |
---|
[44] | 821 | uint32 argi, arge; |
---|
[31] | 822 | |
---|
[37] | 823 | /* Process command line arguments */ |
---|
[30] | 824 | if(argc < 2) |
---|
| 825 | { |
---|
[37] | 826 | usage(); |
---|
[44] | 827 | bailOut(1, "ERROR: Requires at least one argument.\n"); |
---|
[30] | 828 | } |
---|
[37] | 829 | |
---|
[44] | 830 | arge = argc-1; |
---|
| 831 | for(argi = 1; argi < arge; argi++) |
---|
[37] | 832 | { |
---|
[40] | 833 | if (strcmp("-p", argv[argi]) == 0) |
---|
[37] | 834 | { |
---|
[44] | 835 | if(++argi >= arge) |
---|
[37] | 836 | { |
---|
| 837 | usage(); |
---|
[40] | 838 | bailOut(1, "ERROR: '-p' option requires parameter.\n"); |
---|
[37] | 839 | } |
---|
[40] | 840 | if((path_filter = strdup(argv[argi])) == NULL) |
---|
[38] | 841 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
| 842 | |
---|
[40] | 843 | path_filter_enabled = true; |
---|
[37] | 844 | } |
---|
| 845 | else if (strcmp("-t", argv[argi]) == 0) |
---|
| 846 | { |
---|
[44] | 847 | if(++argi >= arge) |
---|
[37] | 848 | { |
---|
| 849 | usage(); |
---|
[38] | 850 | bailOut(1, "ERROR: '-t' option requires parameter.\n"); |
---|
[37] | 851 | } |
---|
[78] | 852 | if((type_filter = regfi_type_str2val(argv[argi])) < 0) |
---|
[40] | 853 | { |
---|
| 854 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); |
---|
| 855 | bailOut(1, ""); |
---|
| 856 | } |
---|
[37] | 857 | type_filter_enabled = true; |
---|
| 858 | } |
---|
[47] | 859 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 860 | print_header = true; |
---|
| 861 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 862 | print_header = false; |
---|
[37] | 863 | else if (strcmp("-s", argv[argi]) == 0) |
---|
| 864 | print_security = true; |
---|
[44] | 865 | else if (strcmp("-S", argv[argi]) == 0) |
---|
| 866 | print_security = false; |
---|
[37] | 867 | else if (strcmp("-v", argv[argi]) == 0) |
---|
| 868 | print_verbose = true; |
---|
[44] | 869 | else |
---|
[37] | 870 | { |
---|
[38] | 871 | usage(); |
---|
[37] | 872 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
[38] | 873 | bailOut(1, ""); |
---|
[37] | 874 | } |
---|
| 875 | } |
---|
[44] | 876 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
| 877 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
[30] | 878 | |
---|
[78] | 879 | f = regfi_open(registry_file); |
---|
[37] | 880 | if(f == NULL) |
---|
| 881 | { |
---|
| 882 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
[38] | 883 | bailOut(3, ""); |
---|
[37] | 884 | } |
---|
[38] | 885 | |
---|
[80] | 886 | iter = regfi_iterator_new(f); |
---|
| 887 | if(iter == NULL) |
---|
| 888 | bailOut(3, "ERROR: Couldn't create registry iterator.\n"); |
---|
[30] | 889 | |
---|
[81] | 890 | if(print_header) |
---|
| 891 | { |
---|
| 892 | if(print_security) |
---|
| 893 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n"); |
---|
| 894 | else |
---|
| 895 | printf("PATH,TYPE,VALUE,MTIME\n"); |
---|
| 896 | } |
---|
| 897 | |
---|
[80] | 898 | if(path_filter_enabled && path_filter != NULL) |
---|
| 899 | path = splitPath(path_filter); |
---|
[81] | 900 | |
---|
[80] | 901 | if(path != NULL) |
---|
[33] | 902 | { |
---|
[80] | 903 | retr_path_ret = retrievePath(iter, path); |
---|
| 904 | if(retr_path_ret == 0) |
---|
| 905 | fprintf(stderr, "WARNING: specified path not found.\n"); |
---|
| 906 | else if (retr_path_ret == 2) |
---|
[81] | 907 | printKeyTree(iter); |
---|
[80] | 908 | else if(retr_path_ret != 0) |
---|
| 909 | bailOut(4, "ERROR: Unknown error occurred in retrieving path.\n"); |
---|
[33] | 910 | } |
---|
[37] | 911 | else |
---|
[81] | 912 | printKeyTree(iter); |
---|
[31] | 913 | |
---|
[80] | 914 | regfi_iterator_free(iter); |
---|
[78] | 915 | regfi_close(f); |
---|
[30] | 916 | |
---|
| 917 | return 0; |
---|
| 918 | } |
---|