1 /* hivexsh - Hive shell.
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #ifdef HAVE_LIBREADLINE
32 #include <readline/readline.h>
33 #include <readline/history.h>
38 #define _(str) dgettext(PACKAGE, (str))
39 //#define N_(str) dgettext(PACKAGE, (str))
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)
59 #include "byte_conversions.h"
61 #define HIVEX_MAX_VALUES 1000
65 static hive_h *h = NULL;
66 static char *prompt_string = NULL; /* Normal prompt string. */
67 static char *loaded = NULL; /* Basename of loaded file, if any. */
68 static hive_node_h cwd; /* Current node. */
69 static int open_flags = 0; /* Flags used when loading a hive file. */
71 static void usage (void) __attribute__((noreturn));
72 static void print_node_path (hive_node_h, FILE *);
73 static void set_prompt_string (void);
74 static void initialize_readline (void);
75 static void cleanup_readline (void);
76 static void add_history_line (const char *);
77 static char *rl_gets (const char *prompt_string);
78 static void sort_strings (char **strings, int len);
79 static int get_xdigit (char c);
80 static int dispatch (char *cmd, char *args);
81 static int cmd_add (char *name);
82 static int cmd_cd (char *path);
83 static int cmd_close (char *path);
84 static int cmd_commit (char *path);
85 static int cmd_del (char *args);
86 static int cmd_help (char *args);
87 static int cmd_load (char *hivefile);
88 static int cmd_ls (char *args);
89 static int cmd_lsval (char *args);
90 static int cmd_setval (char *args);
95 fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
100 main (int argc, char *argv[])
102 setlocale (LC_ALL, "");
103 bindtextdomain (PACKAGE, LOCALEBASEDIR);
104 textdomain (PACKAGE);
107 const char *filename = NULL;
109 set_prompt_string ();
111 while ((c = getopt (argc, argv, "dfw")) != EOF) {
114 open_flags |= HIVEX_OPEN_DEBUG;
120 open_flags |= HIVEX_OPEN_WRITE;
128 if (optind + 1 != argc)
130 if (cmd_load (argv[optind]) == -1)
134 /* -f filename parameter */
137 if (open (filename, O_RDONLY) == -1) {
145 initialize_readline ();
150 "Welcome to hivexsh, the hivex interactive shell for examining\n"
151 "Windows Registry binary hive files.\n"
153 "Type: 'help' for help summary\n"
154 " 'quit' to quit the shell\n"
158 char *buf = rl_gets (prompt_string);
166 while (*buf && c_isspace (*buf))
169 /* Ignore blank line. */
172 /* If the next character is '#' then this is a comment. */
173 if (*buf == '#') continue;
175 /* Parsing is very simple - much simpler than guestfish. This is
176 * because Registry keys often contain spaces, and we don't want
177 * to bother with quoting. Therefore here we just split at the
178 * first whitespace into "cmd<whitespace>arg(s)". We let the
179 * command decide how to deal with arg(s), if at all.
181 size_t len = strcspn (buf, " \t");
183 if (len == 0) continue;
189 if (buf[len] == '\0') {
190 /* This is mostly safe. Although the cmd_* functions do sometimes
191 * modify args, then shouldn't do so when args is "".
198 args = buf + len + 1 + strspn (&buf[len+1], " \t");
201 while (len > 0 && c_isspace (args[len-1])) {
207 /*printf ("command: '%s' args: '%s'\n", cmd, args)*/;
208 int r = dispatch (cmd, args);
209 if (!is_tty && r == -1)
214 free (prompt_string);
216 if (h) hivex_close (h);
220 /* Set the prompt string. This is called whenever it could change, eg.
221 * after loading a file or changing directory.
224 set_prompt_string (void)
226 free (prompt_string);
227 prompt_string = NULL;
232 fp = open_memstream (&ptr, &size);
234 perror ("open_memstream");
239 assert (loaded != NULL);
243 print_node_path (cwd, fp);
251 /* Print the \full\path of a node. */
253 print_node_path (hive_node_h node, FILE *fp)
255 hive_node_h root = hivex_root (h);
262 hive_node_h parent = hivex_node_parent (h, node);
264 fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
267 print_node_path (parent, fp);
272 char *name = hivex_node_name (h, node);
274 fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
282 static char *line_read = NULL;
285 rl_gets (const char *prompt_string)
287 #ifdef HAVE_LIBREADLINE
295 line_read = readline (prompt_string);
297 if (line_read && *line_read)
298 add_history_line (line_read);
303 #endif /* HAVE_LIBREADLINE */
305 static char buf[8192];
309 printf ("%s", prompt_string);
310 line_read = fgets (buf, sizeof buf, stdin);
313 len = strlen (line_read);
314 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
320 #ifdef HAVE_LIBREADLINE
321 static char histfile[1024];
322 static int nr_history_lines = 0;
326 initialize_readline (void)
328 #ifdef HAVE_LIBREADLINE
331 home = getenv ("HOME");
333 snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
335 (void) read_history (histfile);
338 rl_readline_name = "hivexsh";
343 cleanup_readline (void)
345 #ifdef HAVE_LIBREADLINE
348 if (histfile[0] != '\0') {
349 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
356 (void) append_history (nr_history_lines, histfile);
362 add_history_line (const char *line)
364 #ifdef HAVE_LIBREADLINE
371 compare (const void *vp1, const void *vp2)
373 char * const *p1 = (char * const *) vp1;
374 char * const *p2 = (char * const *) vp2;
375 return strcasecmp (*p1, *p2);
379 sort_strings (char **strings, int len)
381 qsort (strings, len, sizeof (char *), compare);
388 case '0'...'9': return c - '0';
389 case 'a'...'f': return c - 'a' + 10;
390 case 'A'...'F': return c - 'A' + 10;
396 dispatch (char *cmd, char *args)
398 if (STRCASEEQ (cmd, "help"))
399 return cmd_help (args);
400 else if (STRCASEEQ (cmd, "load"))
401 return cmd_load (args);
402 else if (STRCASEEQ (cmd, "exit") ||
403 STRCASEEQ (cmd, "q") ||
404 STRCASEEQ (cmd, "quit")) {
409 /* If no hive file is loaded (!h) then only the small selection of
410 * commands above will work.
413 fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
417 if (STRCASEEQ (cmd, "add"))
418 return cmd_add (args);
419 else if (STRCASEEQ (cmd, "cd"))
420 return cmd_cd (args);
421 else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
422 return cmd_close (args);
423 else if (STRCASEEQ (cmd, "commit"))
424 return cmd_commit (args);
425 else if (STRCASEEQ (cmd, "del"))
426 return cmd_del (args);
427 else if (STRCASEEQ (cmd, "ls"))
428 return cmd_ls (args);
429 else if (STRCASEEQ (cmd, "lsval"))
430 return cmd_lsval (args);
431 else if (STRCASEEQ (cmd, "setval"))
432 return cmd_setval (args);
434 fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
441 cmd_load (char *hivefile)
443 if (STREQ (hivefile, "")) {
444 fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
448 if (h) hivex_close (h);
456 h = hivex_open (hivefile, open_flags);
460 "hivexsh: failed to open hive file: %s: %m\n"
462 "If you think this file is a valid Windows binary hive file (_not_\n"
463 "a regedit *.reg file) then please run this command again using the\n"
464 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
465 "which fails into a bug report at https://bugzilla.redhat.com/\n"
471 /* Get the basename of the file for the prompt. */
472 char *p = strrchr (hivefile, '/');
474 loaded = strdup (p+1);
476 loaded = strdup (hivefile);
482 cwd = hivex_root (h);
484 set_prompt_string ();
490 cmd_close (char *args)
492 if (STRNEQ (args, "")) {
493 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
498 if (h) hivex_close (h);
506 set_prompt_string ();
512 cmd_commit (char *path)
514 if (STREQ (path, ""))
517 if (hivex_commit (h, path, 0) == -1) {
518 perror ("hivexsh: commit");
528 if (STREQ (path, "")) {
529 print_node_path (cwd, stdout);
530 fputc ('\n', stdout);
534 if (path[0] == '\\' && path[1] == '\\') {
535 fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
539 hive_node_h new_cwd = cwd;
540 hive_node_h root = hivex_root (h);
542 if (path[0] == '\\') {
548 size_t len = strcspn (path, "\\");
555 path = path[len] == '\0' ? &path[len] : &path[len+1];
558 if (len == 1 && STREQ (elem, "."))
561 if (len == 2 && STREQ (elem, "..")) {
563 new_cwd = hivex_node_parent (h, new_cwd);
568 new_cwd = hivex_node_get_child (h, new_cwd, elem);
571 perror ("hivexsh: cd");
573 fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
579 if (new_cwd != cwd) {
581 set_prompt_string ();
588 cmd_help (char *args)
591 "Navigate through the hive's keys using the 'cd' command, as if it\n"
592 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
593 "current key. Full documentation is in the hivexsh(1) manual page.\n"));
601 if (STRNEQ (args, "")) {
602 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
607 /* Get the subkeys. */
608 hive_node_h *children = hivex_node_children (h, cwd);
609 if (children == NULL) {
614 /* Get names for each subkey. */
616 for (len = 0; children[len] != 0; ++len)
619 char **names = calloc (len, sizeof (char *));
627 for (i = 0; i < len; ++i) {
628 names[i] = hivex_node_name (h, children[i]);
629 if (names[i] == NULL) {
630 perror ("hivex_node_name");
635 /* Sort the names. */
636 sort_strings (names, len);
638 for (i = 0; i < len; ++i)
639 printf ("%s\n", names[i]);
644 for (i = 0; i < len; ++i)
651 cmd_lsval (char *key)
653 if (STRNEQ (key, "")) {
657 if (STREQ (key, "@")) /* default key written as "@" */
658 value = hivex_node_get_value (h, cwd, "");
660 value = hivex_node_get_value (h, cwd, key);
665 /* else key not found */
666 fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
670 /* Print the value. */
673 if (hivex_value_type (h, value, &t, &len) == -1)
678 case hive_t_expand_string:
680 char *str = hivex_value_string (h, value);
684 puts (str); /* note: this adds a single \n character */
690 case hive_t_dword_be: {
691 int32_t j = hivex_value_dword (h, value);
692 printf ("%" PRIi32 "\n", j);
697 int64_t j = hivex_value_qword (h, value);
698 printf ("%" PRIi64 "\n", j);
702 case hive_t_multiple_strings: {
703 char **strs = hivex_value_multiple_strings (h, value);
707 for (j = 0; strs[j] != NULL; ++j) {
717 case hive_t_resource_list:
718 case hive_t_full_resource_description:
719 case hive_t_resource_requirements_list:
721 char *data = hivex_value_value (h, value, &t, &len);
725 if (fwrite (data, 1, len, stdout) != len)
733 /* No key specified, so print all keys in this node. We do this
734 * in a format which looks like the output of regedit, although
735 * this isn't a particularly useful format.
737 hive_value_h *values;
739 values = hivex_node_values (h, cwd);
744 for (i = 0; values[i] != 0; ++i) {
745 char *key = hivex_value_key (h, values[i]);
746 if (!key) goto error;
751 for (j = 0; key[j] != 0; ++j) {
752 if (key[j] == '"' || key[j] == '\\')
758 printf ("\"@\""); /* default key in regedit files */
764 if (hivex_value_type (h, values[i], &t, &len) == -1)
769 case hive_t_expand_string:
771 char *str = hivex_value_string (h, values[i]);
775 if (t != hive_t_string)
776 printf ("str(%d):", t);
779 for (j = 0; str[j] != 0; ++j) {
780 if (str[j] == '"' || str[j] == '\\')
790 case hive_t_dword_be: {
791 int32_t j = hivex_value_dword (h, values[i]);
792 printf ("dword:%08" PRIx32, j);
796 case hive_t_qword: /* sic */
799 case hive_t_multiple_strings:
800 case hive_t_resource_list:
801 case hive_t_full_resource_description:
802 case hive_t_resource_requirements_list:
804 char *data = hivex_value_value (h, values[i], &t, &len);
808 printf ("hex(%d):", t);
810 for (j = 0; j < len; ++j) {
813 printf ("%02x", data[j]);
828 perror ("hivexsh: lsval");
833 cmd_setval (char *nrvals_str)
837 /* Parse number of values. */
839 xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
840 if (xerr != LONGINT_OK) {
841 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
842 "setval", "nrvals", "xstrtol", xerr);
845 if (nrvals < 0 || nrvals > HIVEX_MAX_VALUES) {
846 fprintf (stderr, _("%s: %s: integer out of range\n"),
851 struct hive_set_value *values =
852 calloc (nrvals, sizeof (struct hive_set_value));
853 if (values == NULL) {
860 /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
861 * explained in the man page.
864 for (i = 0; i < nrvals; ++i) {
866 char *buf = rl_gets (" key> ");
868 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
873 /* Note that buf will be overwritten by the next call to rl_gets. */
874 if (STREQ (buf, "@"))
875 values[i].key = strdup ("");
877 values[i].key = strdup (buf);
878 if (values[i].key == NULL) {
884 buf = rl_gets ("value> ");
886 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
891 if (STREQ (buf, "none")) {
892 values[i].t = hive_t_none;
895 else if (STRPREFIX (buf, "string:")) {
897 values[i].t = hive_t_string;
898 int nr_chars = strlen (buf);
899 values[i].len = 2 * (nr_chars + 1);
900 values[i].value = malloc (values[i].len);
901 if (!values[i].value) {
905 for (j = 0; j <= /* sic */ nr_chars; ++j) {
907 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
910 values[i].value[2*j] = buf[j];
911 values[i].value[2*j+1] = '\0';
914 else if (STRPREFIX (buf, "expandstring:")) {
916 values[i].t = hive_t_expand_string;
917 int nr_chars = strlen (buf);
918 values[i].len = 2 * (nr_chars + 1);
919 values[i].value = malloc (values[i].len);
920 if (!values[i].value) {
924 for (j = 0; j <= /* sic */ nr_chars; ++j) {
926 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
929 values[i].value[2*j] = buf[j];
930 values[i].value[2*j+1] = '\0';
933 else if (STRPREFIX (buf, "dword:")) {
935 values[i].t = hive_t_dword;
937 values[i].value = malloc (4);
938 if (!values[i].value) {
943 xerr = xstrtol (buf, NULL, 0, &n, "");
944 if (xerr != LONGINT_OK) {
945 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
946 "setval", "dword", "xstrtol", xerr);
949 if (n < 0 || n > UINT32_MAX) {
950 fprintf (stderr, _("%s: %s: integer out of range\n"),
954 uint32_t u32 = htole32 (n);
955 memcpy (values[i].value, &u32, 4);
957 else if (STRPREFIX (buf, "qword:")) {
959 values[i].t = hive_t_qword;
961 values[i].value = malloc (8);
962 if (!values[i].value) {
967 xerr = xstrtoll (buf, NULL, 0, &n, "");
968 if (xerr != LONGINT_OK) {
969 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
970 "setval", "dword", "xstrtoll", xerr);
974 if (n < 0 || n > UINT64_MAX) {
975 fprintf (stderr, _("%s: %s: integer out of range\n"),
980 uint64_t u64 = htole64 (n);
981 memcpy (values[i].value, &u64, 4);
983 else if (STRPREFIX (buf, "hex:")) {
986 size_t len = strcspn (buf, ":");
988 if (buf[len] == '\0') /* "hex:t" */
990 else { /* "hex:t:..." */
992 nextbuf = &buf[len+1];
996 xerr = xstrtol (buf, NULL, 0, &t, "");
997 if (xerr != LONGINT_OK) {
998 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
999 "setval", "hex", "xstrtol", xerr);
1002 if (t < 0 || t > UINT32_MAX) {
1003 fprintf (stderr, _("%s: %s: integer out of range\n"),
1009 /* Read the hex data. */
1012 /* The allocation length is an overestimate, but it doesn't matter. */
1013 values[i].value = malloc (1 + strlen (buf) / 2);
1014 if (!values[i].value) {
1016 exit (EXIT_FAILURE);
1023 for (j = 0; *buf && j < 2; buf++) {
1024 if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1026 c |= get_xdigit (*buf);
1031 if (j == 2) values[i].value[values[i].len++] = c;
1033 fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1040 _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1046 ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1049 /* Free values array. */
1050 for (i = 0; i < nrvals; ++i) {
1051 free (values[i].key);
1052 free (values[i].value);
1060 cmd_del (char *args)
1062 if (STRNEQ (args, "")) {
1063 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1068 if (cwd == hivex_root (h)) {
1069 fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1073 hive_node_h new_cwd = hivex_node_parent (h, cwd);
1075 if (hivex_node_delete_child (h, cwd) == -1) {
1076 perror ("hivexsh: del");
1081 set_prompt_string ();
1086 cmd_add (char *name)
1088 hive_node_h node = hivex_node_add_child (h, cwd, name);
1090 perror ("hivexsh: add");