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"
63 static hive_h *h = NULL;
64 static char *prompt_string = NULL; /* Normal prompt string. */
65 static char *loaded = NULL; /* Basename of loaded file, if any. */
66 static hive_node_h cwd; /* Current node. */
67 static int open_flags = 0; /* Flags used when loading a hive file. */
69 static void usage (void) __attribute__((noreturn));
70 static void print_node_path (hive_node_h, FILE *);
71 static void set_prompt_string (void);
72 static void initialize_readline (void);
73 static void cleanup_readline (void);
74 static void add_history_line (const char *);
75 static char *rl_gets (const char *prompt_string);
76 static void sort_strings (char **strings, int len);
77 static int get_xdigit (char c);
78 static int dispatch (char *cmd, char *args);
79 static int cmd_cd (char *path);
80 static int cmd_close (char *path);
81 static int cmd_commit (char *path);
82 static int cmd_del (char *args);
83 static int cmd_help (char *args);
84 static int cmd_load (char *hivefile);
85 static int cmd_ls (char *args);
86 static int cmd_lsval (char *args);
87 static int cmd_setval (char *args);
92 fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
97 main (int argc, char *argv[])
99 setlocale (LC_ALL, "");
100 bindtextdomain (PACKAGE, LOCALEBASEDIR);
101 textdomain (PACKAGE);
104 const char *filename = NULL;
106 set_prompt_string ();
108 while ((c = getopt (argc, argv, "dfw")) != EOF) {
111 open_flags |= HIVEX_OPEN_DEBUG;
117 open_flags |= HIVEX_OPEN_WRITE;
125 if (optind + 1 != argc)
127 if (cmd_load (argv[optind]) == -1)
131 /* -f filename parameter */
134 if (open (filename, O_RDONLY) == -1) {
142 initialize_readline ();
147 "Welcome to hivexsh, the hivex interactive shell for examining\n"
148 "Windows Registry binary hive files.\n"
150 "Type: 'help' for help summary\n"
151 " 'quit' to quit the shell\n"
155 char *buf = rl_gets (prompt_string);
163 while (*buf && c_isspace (*buf))
166 /* Ignore blank line. */
169 /* If the next character is '#' then this is a comment. */
170 if (*buf == '#') continue;
172 /* Parsing is very simple - much simpler than guestfish. This is
173 * because Registry keys often contain spaces, and we don't want
174 * to bother with quoting. Therefore here we just split at the
175 * first whitespace into "cmd<whitespace>arg(s)". We let the
176 * command decide how to deal with arg(s), if at all.
178 size_t len = strcspn (buf, " \t");
180 if (len == 0) continue;
186 if (buf[len] == '\0') {
187 /* This is mostly safe. Although the cmd_* functions do sometimes
188 * modify args, then shouldn't do so when args is "".
195 args = buf + len + 1 + strspn (&buf[len+1], " \t");
198 while (len > 0 && c_isspace (args[len-1])) {
204 /*printf ("command: '%s' args: '%s'\n", cmd, args)*/;
205 int r = dispatch (cmd, args);
206 if (!is_tty && r == -1)
211 free (prompt_string);
213 if (h) hivex_close (h);
217 /* Set the prompt string. This is called whenever it could change, eg.
218 * after loading a file or changing directory.
221 set_prompt_string (void)
223 free (prompt_string);
224 prompt_string = NULL;
229 fp = open_memstream (&ptr, &size);
231 perror ("open_memstream");
236 assert (loaded != NULL);
240 print_node_path (cwd, fp);
248 /* Print the \full\path of a node. */
250 print_node_path (hive_node_h node, FILE *fp)
252 hive_node_h root = hivex_root (h);
259 hive_node_h parent = hivex_node_parent (h, node);
261 fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
264 print_node_path (parent, fp);
269 char *name = hivex_node_name (h, node);
271 fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
279 static char *line_read = NULL;
282 rl_gets (const char *prompt_string)
284 #ifdef HAVE_LIBREADLINE
292 line_read = readline (prompt_string);
294 if (line_read && *line_read)
295 add_history_line (line_read);
300 #endif /* HAVE_LIBREADLINE */
302 static char buf[8192];
306 printf ("%s", prompt_string);
307 line_read = fgets (buf, sizeof buf, stdin);
310 len = strlen (line_read);
311 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
317 #ifdef HAVE_LIBREADLINE
318 static char histfile[1024];
319 static int nr_history_lines = 0;
323 initialize_readline (void)
325 #ifdef HAVE_LIBREADLINE
328 home = getenv ("HOME");
330 snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
332 (void) read_history (histfile);
335 rl_readline_name = "hivexsh";
340 cleanup_readline (void)
342 #ifdef HAVE_LIBREADLINE
345 if (histfile[0] != '\0') {
346 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
353 (void) append_history (nr_history_lines, histfile);
359 add_history_line (const char *line)
361 #ifdef HAVE_LIBREADLINE
368 compare (const void *vp1, const void *vp2)
370 char * const *p1 = (char * const *) vp1;
371 char * const *p2 = (char * const *) vp2;
372 return strcasecmp (*p1, *p2);
376 sort_strings (char **strings, int len)
378 qsort (strings, len, sizeof (char *), compare);
385 case '0'...'9': return c - '0';
386 case 'a'...'f': return c - 'a' + 10;
387 case 'A'...'F': return c - 'A' + 10;
393 dispatch (char *cmd, char *args)
395 if (STRCASEEQ (cmd, "help"))
396 return cmd_help (args);
397 else if (STRCASEEQ (cmd, "load"))
398 return cmd_load (args);
399 else if (STRCASEEQ (cmd, "exit") ||
400 STRCASEEQ (cmd, "q") ||
401 STRCASEEQ (cmd, "quit")) {
406 /* If no hive file is loaded (!h) then only the small selection of
407 * commands above will work.
410 fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
414 if (STRCASEEQ (cmd, "cd"))
415 return cmd_cd (args);
416 else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
417 return cmd_close (args);
418 else if (STRCASEEQ (cmd, "commit"))
419 return cmd_commit (args);
420 else if (STRCASEEQ (cmd, "del"))
421 return cmd_del (args);
422 else if (STRCASEEQ (cmd, "ls"))
423 return cmd_ls (args);
424 else if (STRCASEEQ (cmd, "lsval"))
425 return cmd_lsval (args);
426 else if (STRCASEEQ (cmd, "setval"))
427 return cmd_setval (args);
429 fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
436 cmd_load (char *hivefile)
438 if (STREQ (hivefile, "")) {
439 fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
443 if (h) hivex_close (h);
451 h = hivex_open (hivefile, open_flags);
455 "hivexsh: failed to open hive file: %s: %m\n"
457 "If you think this file is a valid Windows binary hive file (_not_\n"
458 "a regedit *.reg file) then please run this command again using the\n"
459 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
460 "which fails into a bug report at https://bugzilla.redhat.com/\n"
466 /* Get the basename of the file for the prompt. */
467 char *p = strrchr (hivefile, '/');
469 loaded = strdup (p+1);
471 loaded = strdup (hivefile);
477 cwd = hivex_root (h);
479 set_prompt_string ();
485 cmd_close (char *args)
487 if (STRNEQ (args, "")) {
488 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
493 if (h) hivex_close (h);
501 set_prompt_string ();
507 cmd_commit (char *path)
509 if (STREQ (path, ""))
512 if (hivex_commit (h, path, 0) == -1) {
513 perror ("hivexsh: commit");
523 if (STREQ (path, "")) {
524 print_node_path (cwd, stdout);
525 fputc ('\n', stdout);
529 if (path[0] == '\\' && path[1] == '\\') {
530 fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
534 hive_node_h new_cwd = cwd;
535 hive_node_h root = hivex_root (h);
537 if (path[0] == '\\') {
543 size_t len = strcspn (path, "\\");
550 path = path[len] == '\0' ? &path[len] : &path[len+1];
553 if (len == 1 && STREQ (elem, "."))
556 if (len == 2 && STREQ (elem, "..")) {
558 new_cwd = hivex_node_parent (h, new_cwd);
562 new_cwd = hivex_node_get_child (h, new_cwd, elem);
564 fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
570 if (new_cwd != cwd) {
572 set_prompt_string ();
579 cmd_help (char *args)
582 "Navigate through the hive's keys using the 'cd' command, as if it\n"
583 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
584 "current key. Full documentation is in the hivexsh(1) manual page.\n"));
592 if (STRNEQ (args, "")) {
593 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
598 /* Get the subkeys. */
599 hive_node_h *children = hivex_node_children (h, cwd);
600 if (children == NULL) {
605 /* Get names for each subkey. */
607 for (len = 0; children[len] != 0; ++len)
610 char **names = calloc (len, sizeof (char *));
618 for (i = 0; i < len; ++i) {
619 names[i] = hivex_node_name (h, children[i]);
620 if (names[i] == NULL) {
621 perror ("hivex_node_name");
626 /* Sort the names. */
627 sort_strings (names, len);
629 for (i = 0; i < len; ++i)
630 printf ("%s\n", names[i]);
635 for (i = 0; i < len; ++i)
642 cmd_lsval (char *key)
644 if (STRNEQ (key, "")) {
648 if (STREQ (key, "@")) /* default key written as "@" */
649 value = hivex_node_get_value (h, cwd, "");
651 value = hivex_node_get_value (h, cwd, key);
656 /* else key not found */
657 fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
661 /* Print the value. */
664 if (hivex_value_type (h, value, &t, &len) == -1)
669 case hive_t_expand_string:
671 char *str = hivex_value_string (h, value);
675 puts (str); /* note: this adds a single \n character */
681 case hive_t_dword_be: {
682 int32_t j = hivex_value_dword (h, value);
683 printf ("%" PRIi32 "\n", j);
688 int64_t j = hivex_value_qword (h, value);
689 printf ("%" PRIi64 "\n", j);
693 case hive_t_multiple_strings: {
694 char **strs = hivex_value_multiple_strings (h, value);
698 for (j = 0; strs[j] != NULL; ++j) {
708 case hive_t_resource_list:
709 case hive_t_full_resource_description:
710 case hive_t_resource_requirements_list:
712 char *data = hivex_value_value (h, value, &t, &len);
716 if (fwrite (data, 1, len, stdout) != len)
724 /* No key specified, so print all keys in this node. We do this
725 * in a format which looks like the output of regedit, although
726 * this isn't a particularly useful format.
728 hive_value_h *values;
730 values = hivex_node_values (h, cwd);
735 for (i = 0; values[i] != 0; ++i) {
736 char *key = hivex_value_key (h, values[i]);
737 if (!key) goto error;
742 for (j = 0; key[j] != 0; ++j) {
743 if (key[j] == '"' || key[j] == '\\')
749 printf ("\"@\""); /* default key in regedit files */
755 if (hivex_value_type (h, values[i], &t, &len) == -1)
760 case hive_t_expand_string:
762 char *str = hivex_value_string (h, values[i]);
766 if (t != hive_t_string)
767 printf ("str(%d):", t);
770 for (j = 0; str[j] != 0; ++j) {
771 if (str[j] == '"' || str[j] == '\\')
781 case hive_t_dword_be: {
782 int32_t j = hivex_value_dword (h, values[i]);
783 printf ("dword:%08" PRIx32 "\"", j);
787 case hive_t_qword: /* sic */
790 case hive_t_multiple_strings:
791 case hive_t_resource_list:
792 case hive_t_full_resource_description:
793 case hive_t_resource_requirements_list:
795 char *data = hivex_value_value (h, values[i], &t, &len);
799 printf ("hex(%d):", t);
801 for (j = 0; j < len; ++j) {
804 printf ("%02x", data[j]);
819 perror ("hivexsh: lsval");
824 cmd_setval (char *nrvals_str)
828 /* Parse number of values. */
830 xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
831 if (xerr != LONGINT_OK) {
832 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
833 "setval", "nrvals", "xstrtol", xerr);
836 if (nrvals < 0 || nrvals > 1000) {
837 fprintf (stderr, _("%s: %s: integer out of range\n"),
842 struct hive_set_value *values =
843 calloc (nrvals, sizeof (struct hive_set_value));
844 if (values == NULL) {
851 /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
852 * explained in the man page.
854 int prompt = isatty (0) ? 2 : 0;
856 for (i = 0; i < nrvals; ++i) {
858 char *buf = rl_gets (" key> ");
860 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
865 /* Note that buf will be overwritten by the next call to rl_gets. */
866 if (STREQ (buf, "@"))
867 values[i].key = strdup ("");
869 values[i].key = strdup (buf);
870 if (values[i].key == NULL) {
876 buf = rl_gets ("value> ");
878 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
883 if (STREQ (buf, "none")) {
884 values[i].t = hive_t_none;
887 else if (STRPREFIX (buf, "string:")) {
889 values[i].t = hive_t_string;
890 int nr_chars = strlen (buf);
891 values[i].len = 2 * (nr_chars + 1);
892 values[i].value = malloc (values[i].len);
893 if (!values[i].value) {
897 for (j = 0; j <= /* sic */ nr_chars; ++j) {
899 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
902 values[i].value[2*j] = buf[j];
903 values[i].value[2*j+1] = '\0';
906 else if (STRPREFIX (buf, "expandstring:")) {
908 values[i].t = hive_t_string;
909 int nr_chars = strlen (buf);
910 values[i].len = 2 * (nr_chars + 1);
911 values[i].value = malloc (values[i].len);
912 if (!values[i].value) {
916 for (j = 0; j <= /* sic */ nr_chars; ++j) {
918 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
921 values[i].value[2*j] = buf[j];
922 values[i].value[2*j+1] = '\0';
925 else if (STRPREFIX (buf, "dword:")) {
927 values[i].t = hive_t_dword;
929 values[i].value = malloc (4);
930 if (!values[i].value) {
935 xerr = xstrtol (buf, NULL, 0, &n, "");
936 if (xerr != LONGINT_OK) {
937 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
938 "setval", "dword", "xstrtol", xerr);
941 if (n < 0 || n > UINT32_MAX) {
942 fprintf (stderr, _("%s: %s: integer out of range\n"),
946 uint32_t u32 = htole32 (n);
947 memcpy (values[i].value, &u32, 4);
949 else if (STRPREFIX (buf, "qword:")) {
951 values[i].t = hive_t_qword;
953 values[i].value = malloc (8);
954 if (!values[i].value) {
959 xerr = xstrtoll (buf, NULL, 0, &n, "");
960 if (xerr != LONGINT_OK) {
961 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
962 "setval", "dword", "xstrtoll", xerr);
966 if (n < 0 || n > UINT64_MAX) {
967 fprintf (stderr, _("%s: %s: integer out of range\n"),
972 uint64_t u64 = htole64 (n);
973 memcpy (values[i].value, &u64, 4);
975 else if (STRPREFIX (buf, "hex:")) {
978 size_t len = strcspn (buf, ":");
980 if (buf[len] == '\0') /* "hex:t" */
982 else { /* "hex:t:..." */
984 nextbuf = &buf[len+1];
988 xerr = xstrtol (buf, NULL, 0, &t, "");
989 if (xerr != LONGINT_OK) {
990 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
991 "setval", "hex", "xstrtol", xerr);
994 if (t < 0 || t > UINT32_MAX) {
995 fprintf (stderr, _("%s: %s: integer out of range\n"),
1001 /* Read the hex data. */
1004 /* The allocation length is an overestimate, but it doesn't matter. */
1005 values[i].value = malloc (1 + strlen (buf) / 2);
1006 if (!values[i].value) {
1008 exit (EXIT_FAILURE);
1015 for (j = 0; *buf && j < 2; buf++) {
1016 if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1018 c |= get_xdigit (*buf);
1023 if (j == 2) values[i].value[values[i].len++] = c;
1025 fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1032 _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1038 ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1041 /* Free values array. */
1042 for (i = 0; i < nrvals; ++i) {
1043 free (values[i].key);
1044 free (values[i].value);
1052 cmd_del (char *args)
1054 if (STRNEQ (args, "")) {
1055 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1060 if (cwd == hivex_root (h)) {
1061 fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1065 hive_node_h new_cwd = hivex_node_parent (h, cwd);
1067 if (hivex_node_delete_child (h, cwd) == -1) {
1073 set_prompt_string ();