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.
32 #ifdef HAVE_LIBREADLINE
33 #include <readline/readline.h>
34 #include <readline/history.h>
39 #define _(str) dgettext(PACKAGE, (str))
40 //#define N_(str) dgettext(PACKAGE, (str))
46 #define STREQ(a,b) (strcmp((a),(b)) == 0)
47 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
48 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
49 //#define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
50 //#define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
51 //#define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
52 //#define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
53 //#define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
54 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
60 #include "byte_conversions.h"
62 #define HIVEX_MAX_VALUES 1000
66 static hive_h *h = NULL;
67 static char *prompt_string = NULL; /* Normal prompt string. */
68 static char *loaded = NULL; /* Basename of loaded file, if any. */
69 static hive_node_h cwd; /* Current node. */
70 static int open_flags = 0; /* Flags used when loading a hive file. */
72 static void usage (void) __attribute__((noreturn));
73 static void print_node_path (hive_node_h, FILE *);
74 static void set_prompt_string (void);
75 static void initialize_readline (void);
76 static void cleanup_readline (void);
77 static void add_history_line (const char *);
78 static char *rl_gets (const char *prompt_string);
79 static void sort_strings (char **strings, int len);
80 static int get_xdigit (char c);
81 static int dispatch (char *cmd, char *args);
82 static int cmd_add (char *name);
83 static int cmd_cd (char *path);
84 static int cmd_close (char *path);
85 static int cmd_commit (char *path);
86 static int cmd_del (char *args);
87 static int cmd_help (char *args);
88 static int cmd_load (char *hivefile);
89 static int cmd_ls (char *args);
90 static int cmd_lsval (char *args);
91 static int cmd_setval (char *args);
96 fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
101 main (int argc, char *argv[])
103 setlocale (LC_ALL, "");
104 #ifdef HAVE_BINDTEXTDOMAIN
105 bindtextdomain (PACKAGE, LOCALEBASEDIR);
106 textdomain (PACKAGE);
110 const char *filename = NULL;
112 set_prompt_string ();
114 while ((c = getopt (argc, argv, "df:w")) != EOF) {
117 open_flags |= HIVEX_OPEN_DEBUG;
123 open_flags |= HIVEX_OPEN_WRITE;
131 if (optind + 1 != argc)
133 if (cmd_load (argv[optind]) == -1)
137 /* -f filename parameter */
140 if (open (filename, O_RDONLY) == -1) {
148 initialize_readline ();
153 "Welcome to hivexsh, the hivex interactive shell for examining\n"
154 "Windows Registry binary hive files.\n"
156 "Type: 'help' for help summary\n"
157 " 'quit' to quit the shell\n"
161 char *buf = rl_gets (prompt_string);
169 while (*buf && c_isspace (*buf))
172 /* Ignore blank line. */
175 /* If the next character is '#' then this is a comment. */
176 if (*buf == '#') continue;
178 /* Parsing is very simple - much simpler than guestfish. This is
179 * because Registry keys often contain spaces, and we don't want
180 * to bother with quoting. Therefore here we just split at the
181 * first whitespace into "cmd<whitespace>arg(s)". We let the
182 * command decide how to deal with arg(s), if at all.
184 size_t len = strcspn (buf, " \t");
186 if (len == 0) continue;
191 if (buf[len] == '\0') {
192 /* This is mostly safe. Although the cmd_* functions do sometimes
193 * modify args, then shouldn't do so when args is "".
200 args = buf + len + 1 + strspn (&buf[len+1], " \t");
203 while (len > 0 && c_isspace (args[len-1])) {
209 /*printf ("command: '%s' args: '%s'\n", cmd, args)*/;
210 int r = dispatch (cmd, args);
211 if (!is_tty && r == -1)
216 free (prompt_string);
218 if (h) hivex_close (h);
222 /* Set the prompt string. This is called whenever it could change, eg.
223 * after loading a file or changing directory.
226 set_prompt_string (void)
228 free (prompt_string);
229 prompt_string = NULL;
234 fp = open_memstream (&ptr, &size);
236 perror ("open_memstream");
241 assert (loaded != NULL);
245 print_node_path (cwd, fp);
253 /* Print the \full\path of a node. */
255 print_node_path (hive_node_h node, FILE *fp)
257 hive_node_h root = hivex_root (h);
264 hive_node_h parent = hivex_node_parent (h, node);
266 fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
269 print_node_path (parent, fp);
274 char *name = hivex_node_name (h, node);
276 fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
284 static char *line_read = NULL;
287 rl_gets (const char *prompt_string)
289 #ifdef HAVE_LIBREADLINE
297 line_read = readline (prompt_string);
299 if (line_read && *line_read)
300 add_history_line (line_read);
305 #endif /* HAVE_LIBREADLINE */
307 static char buf[8192];
311 printf ("%s", prompt_string);
312 line_read = fgets (buf, sizeof buf, stdin);
315 len = strlen (line_read);
316 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
322 #ifdef HAVE_LIBREADLINE
323 static char histfile[1024];
324 static int nr_history_lines = 0;
328 initialize_readline (void)
330 #ifdef HAVE_LIBREADLINE
333 home = getenv ("HOME");
335 snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
337 (void) read_history (histfile);
340 rl_readline_name = "hivexsh";
345 cleanup_readline (void)
347 #ifdef HAVE_LIBREADLINE
350 if (histfile[0] != '\0') {
351 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
358 (void) append_history (nr_history_lines, histfile);
364 add_history_line (const char *line)
366 #ifdef HAVE_LIBREADLINE
373 compare (const void *vp1, const void *vp2)
375 char * const *p1 = (char * const *) vp1;
376 char * const *p2 = (char * const *) vp2;
377 return strcasecmp (*p1, *p2);
381 sort_strings (char **strings, int len)
383 qsort (strings, len, sizeof (char *), compare);
390 case '0'...'9': return c - '0';
391 case 'a'...'f': return c - 'a' + 10;
392 case 'A'...'F': return c - 'A' + 10;
398 dispatch (char *cmd, char *args)
400 if (STRCASEEQ (cmd, "help"))
401 return cmd_help (args);
402 else if (STRCASEEQ (cmd, "load"))
403 return cmd_load (args);
404 else if (STRCASEEQ (cmd, "exit") ||
405 STRCASEEQ (cmd, "q") ||
406 STRCASEEQ (cmd, "quit")) {
411 /* If no hive file is loaded (!h) then only the small selection of
412 * commands above will work.
415 fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
419 if (STRCASEEQ (cmd, "add"))
420 return cmd_add (args);
421 else if (STRCASEEQ (cmd, "cd"))
422 return cmd_cd (args);
423 else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
424 return cmd_close (args);
425 else if (STRCASEEQ (cmd, "commit"))
426 return cmd_commit (args);
427 else if (STRCASEEQ (cmd, "del"))
428 return cmd_del (args);
429 else if (STRCASEEQ (cmd, "ls"))
430 return cmd_ls (args);
431 else if (STRCASEEQ (cmd, "lsval"))
432 return cmd_lsval (args);
433 else if (STRCASEEQ (cmd, "setval"))
434 return cmd_setval (args);
436 fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
443 cmd_load (char *hivefile)
445 if (STREQ (hivefile, "")) {
446 fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
450 if (h) hivex_close (h);
458 h = hivex_open (hivefile, open_flags);
462 "hivexsh: failed to open hive file: %s: %m\n"
464 "If you think this file is a valid Windows binary hive file (_not_\n"
465 "a regedit *.reg file) then please run this command again using the\n"
466 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
467 "which fails into a bug report at https://bugzilla.redhat.com/\n"
473 /* Get the basename of the file for the prompt. */
474 char *p = strrchr (hivefile, '/');
476 loaded = strdup (p+1);
478 loaded = strdup (hivefile);
484 cwd = hivex_root (h);
486 set_prompt_string ();
492 cmd_close (char *args)
494 if (STRNEQ (args, "")) {
495 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
500 if (h) hivex_close (h);
508 set_prompt_string ();
514 cmd_commit (char *path)
516 if (STREQ (path, ""))
519 if (hivex_commit (h, path, 0) == -1) {
520 perror ("hivexsh: commit");
530 if (STREQ (path, "")) {
531 print_node_path (cwd, stdout);
532 fputc ('\n', stdout);
536 if (path[0] == '\\' && path[1] == '\\') {
537 fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
541 hive_node_h new_cwd = cwd;
542 hive_node_h root = hivex_root (h);
544 if (path[0] == '\\') {
550 size_t len = strcspn (path, "\\");
557 path = path[len] == '\0' ? &path[len] : &path[len+1];
560 if (len == 1 && STREQ (elem, "."))
563 if (len == 2 && STREQ (elem, "..")) {
565 new_cwd = hivex_node_parent (h, new_cwd);
570 new_cwd = hivex_node_get_child (h, new_cwd, elem);
573 perror ("hivexsh: cd");
575 fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
581 if (new_cwd != cwd) {
583 set_prompt_string ();
590 cmd_help (char *args)
593 "Navigate through the hive's keys using the 'cd' command, as if it\n"
594 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
595 "current key. Full documentation is in the hivexsh(1) manual page.\n"));
603 if (STRNEQ (args, "")) {
604 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
609 /* Get the subkeys. */
610 hive_node_h *children = hivex_node_children (h, cwd);
611 if (children == NULL) {
616 /* Get names for each subkey. */
618 for (len = 0; children[len] != 0; ++len)
621 char **names = calloc (len, sizeof (char *));
629 for (i = 0; i < len; ++i) {
630 names[i] = hivex_node_name (h, children[i]);
631 if (names[i] == NULL) {
632 perror ("hivex_node_name");
637 /* Sort the names. */
638 sort_strings (names, len);
640 for (i = 0; i < len; ++i)
641 printf ("%s\n", names[i]);
646 for (i = 0; i < len; ++i)
653 cmd_lsval (char *key)
655 if (STRNEQ (key, "")) {
659 if (STREQ (key, "@")) /* default key written as "@" */
660 value = hivex_node_get_value (h, cwd, "");
662 value = hivex_node_get_value (h, cwd, key);
667 /* else key not found */
668 fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
672 /* Print the value. */
675 if (hivex_value_type (h, value, &t, &len) == -1)
680 case hive_t_expand_string:
682 char *str = hivex_value_string (h, value);
686 puts (str); /* note: this adds a single \n character */
692 case hive_t_dword_be: {
693 int32_t j = hivex_value_dword (h, value);
694 printf ("%" PRIi32 "\n", j);
699 int64_t j = hivex_value_qword (h, value);
700 printf ("%" PRIi64 "\n", j);
704 case hive_t_multiple_strings: {
705 char **strs = hivex_value_multiple_strings (h, value);
709 for (j = 0; strs[j] != NULL; ++j) {
719 case hive_t_resource_list:
720 case hive_t_full_resource_description:
721 case hive_t_resource_requirements_list:
723 char *data = hivex_value_value (h, value, &t, &len);
727 if (fwrite (data, 1, len, stdout) != len)
735 /* No key specified, so print all keys in this node. We do this
736 * in a format which looks like the output of regedit, although
737 * this isn't a particularly useful format.
739 hive_value_h *values;
741 values = hivex_node_values (h, cwd);
746 for (i = 0; values[i] != 0; ++i) {
747 char *key = hivex_value_key (h, values[i]);
748 if (!key) goto error;
753 for (j = 0; key[j] != 0; ++j) {
754 if (key[j] == '"' || key[j] == '\\')
760 printf ("\"@\""); /* default key in regedit files */
766 if (hivex_value_type (h, values[i], &t, &len) == -1)
771 case hive_t_expand_string:
773 char *str = hivex_value_string (h, values[i]);
777 if (t != hive_t_string)
778 printf ("str(%d):", t);
781 for (j = 0; str[j] != 0; ++j) {
782 if (str[j] == '"' || str[j] == '\\')
792 case hive_t_dword_be: {
793 int32_t j = hivex_value_dword (h, values[i]);
794 printf ("dword:%08" PRIx32, j);
798 case hive_t_qword: /* sic */
801 case hive_t_multiple_strings:
802 case hive_t_resource_list:
803 case hive_t_full_resource_description:
804 case hive_t_resource_requirements_list:
806 unsigned char *data =
807 (unsigned char *) hivex_value_value (h, values[i], &t, &len);
811 printf ("hex(%d):", t);
813 for (j = 0; j < len; ++j) {
816 printf ("%02x", data[j]);
831 perror ("hivexsh: lsval");
836 cmd_setval (char *nrvals_str)
840 /* Parse number of values. */
842 xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
843 if (xerr != LONGINT_OK) {
844 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
845 "setval", "nrvals", "xstrtol", xerr);
848 if (nrvals < 0 || nrvals > HIVEX_MAX_VALUES) {
849 fprintf (stderr, _("%s: %s: integer out of range\n"),
854 struct hive_set_value *values =
855 calloc (nrvals, sizeof (struct hive_set_value));
856 if (values == NULL) {
863 /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
864 * explained in the man page.
867 for (i = 0; i < nrvals; ++i) {
869 char *buf = rl_gets (" key> ");
871 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
876 /* Note that buf will be overwritten by the next call to rl_gets. */
877 if (STREQ (buf, "@"))
878 values[i].key = strdup ("");
880 values[i].key = strdup (buf);
881 if (values[i].key == NULL) {
887 buf = rl_gets ("value> ");
889 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
894 if (STREQ (buf, "none")) {
895 values[i].t = hive_t_none;
898 else if (STRPREFIX (buf, "string:")) {
900 values[i].t = hive_t_string;
901 int nr_chars = strlen (buf);
902 values[i].len = 2 * (nr_chars + 1);
903 values[i].value = malloc (values[i].len);
904 if (!values[i].value) {
908 for (j = 0; j <= /* sic */ nr_chars; ++j) {
910 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
913 values[i].value[2*j] = buf[j];
914 values[i].value[2*j+1] = '\0';
917 else if (STRPREFIX (buf, "expandstring:")) {
919 values[i].t = hive_t_expand_string;
920 int nr_chars = strlen (buf);
921 values[i].len = 2 * (nr_chars + 1);
922 values[i].value = malloc (values[i].len);
923 if (!values[i].value) {
927 for (j = 0; j <= /* sic */ nr_chars; ++j) {
929 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
932 values[i].value[2*j] = buf[j];
933 values[i].value[2*j+1] = '\0';
936 else if (STRPREFIX (buf, "dword:")) {
938 values[i].t = hive_t_dword;
940 values[i].value = malloc (4);
941 if (!values[i].value) {
946 xerr = xstrtol (buf, NULL, 0, &n, "");
947 if (xerr != LONGINT_OK) {
948 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
949 "setval", "dword", "xstrtol", xerr);
953 if (n < 0 || n > UINT32_MAX) {
954 fprintf (stderr, _("%s: %s: integer out of range\n"),
959 uint32_t u32 = htole32 (n);
960 memcpy (values[i].value, &u32, 4);
962 else if (STRPREFIX (buf, "qword:")) {
964 values[i].t = hive_t_qword;
966 values[i].value = malloc (8);
967 if (!values[i].value) {
972 xerr = xstrtoll (buf, NULL, 0, &n, "");
973 if (xerr != LONGINT_OK) {
974 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
975 "setval", "dword", "xstrtoll", xerr);
979 if (n < 0 || n > UINT64_MAX) {
980 fprintf (stderr, _("%s: %s: integer out of range\n"),
985 uint64_t u64 = htole64 (n);
986 memcpy (values[i].value, &u64, 4);
988 else if (STRPREFIX (buf, "hex:")) {
991 size_t len = strcspn (buf, ":");
993 if (buf[len] == '\0') /* "hex:t" */
995 else { /* "hex:t:..." */
997 nextbuf = &buf[len+1];
1001 xerr = xstrtol (buf, NULL, 0, &t, "");
1002 if (xerr != LONGINT_OK) {
1003 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
1004 "setval", "hex", "xstrtol", xerr);
1008 if (t < 0 || t > UINT32_MAX) {
1009 fprintf (stderr, _("%s: %s: integer out of range\n"),
1016 /* Read the hex data. */
1019 /* The allocation length is an overestimate, but it doesn't matter. */
1020 values[i].value = malloc (1 + strlen (buf) / 2);
1021 if (!values[i].value) {
1023 exit (EXIT_FAILURE);
1030 for (j = 0; *buf && j < 2; buf++) {
1031 if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1033 c |= get_xdigit (*buf);
1038 if (j == 2) values[i].value[values[i].len++] = c;
1040 fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1047 _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1053 ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1056 /* Free values array. */
1057 for (i = 0; i < nrvals; ++i) {
1058 free (values[i].key);
1059 free (values[i].value);
1067 cmd_del (char *args)
1069 if (STRNEQ (args, "")) {
1070 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1075 if (cwd == hivex_root (h)) {
1076 fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1080 hive_node_h new_cwd = hivex_node_parent (h, cwd);
1082 if (hivex_node_delete_child (h, cwd) == -1) {
1083 perror ("hivexsh: del");
1088 set_prompt_string ();
1093 cmd_add (char *name)
1095 hive_node_h node = hivex_node_add_child (h, cwd, name);
1097 perror ("hivexsh: add");