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_add (char *name);
80 static int cmd_cd (char *path);
81 static int cmd_close (char *path);
82 static int cmd_commit (char *path);
83 static int cmd_del (char *args);
84 static int cmd_help (char *args);
85 static int cmd_load (char *hivefile);
86 static int cmd_ls (char *args);
87 static int cmd_lsval (char *args);
88 static int cmd_setval (char *args);
93 fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
98 main (int argc, char *argv[])
100 setlocale (LC_ALL, "");
101 bindtextdomain (PACKAGE, LOCALEBASEDIR);
102 textdomain (PACKAGE);
105 const char *filename = NULL;
107 set_prompt_string ();
109 while ((c = getopt (argc, argv, "dfw")) != EOF) {
112 open_flags |= HIVEX_OPEN_DEBUG;
118 open_flags |= HIVEX_OPEN_WRITE;
126 if (optind + 1 != argc)
128 if (cmd_load (argv[optind]) == -1)
132 /* -f filename parameter */
135 if (open (filename, O_RDONLY) == -1) {
143 initialize_readline ();
148 "Welcome to hivexsh, the hivex interactive shell for examining\n"
149 "Windows Registry binary hive files.\n"
151 "Type: 'help' for help summary\n"
152 " 'quit' to quit the shell\n"
156 char *buf = rl_gets (prompt_string);
164 while (*buf && c_isspace (*buf))
167 /* Ignore blank line. */
170 /* If the next character is '#' then this is a comment. */
171 if (*buf == '#') continue;
173 /* Parsing is very simple - much simpler than guestfish. This is
174 * because Registry keys often contain spaces, and we don't want
175 * to bother with quoting. Therefore here we just split at the
176 * first whitespace into "cmd<whitespace>arg(s)". We let the
177 * command decide how to deal with arg(s), if at all.
179 size_t len = strcspn (buf, " \t");
181 if (len == 0) continue;
187 if (buf[len] == '\0') {
188 /* This is mostly safe. Although the cmd_* functions do sometimes
189 * modify args, then shouldn't do so when args is "".
196 args = buf + len + 1 + strspn (&buf[len+1], " \t");
199 while (len > 0 && c_isspace (args[len-1])) {
205 /*printf ("command: '%s' args: '%s'\n", cmd, args)*/;
206 int r = dispatch (cmd, args);
207 if (!is_tty && r == -1)
212 free (prompt_string);
214 if (h) hivex_close (h);
218 /* Set the prompt string. This is called whenever it could change, eg.
219 * after loading a file or changing directory.
222 set_prompt_string (void)
224 free (prompt_string);
225 prompt_string = NULL;
230 fp = open_memstream (&ptr, &size);
232 perror ("open_memstream");
237 assert (loaded != NULL);
241 print_node_path (cwd, fp);
249 /* Print the \full\path of a node. */
251 print_node_path (hive_node_h node, FILE *fp)
253 hive_node_h root = hivex_root (h);
260 hive_node_h parent = hivex_node_parent (h, node);
262 fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
265 print_node_path (parent, fp);
270 char *name = hivex_node_name (h, node);
272 fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
280 static char *line_read = NULL;
283 rl_gets (const char *prompt_string)
285 #ifdef HAVE_LIBREADLINE
293 line_read = readline (prompt_string);
295 if (line_read && *line_read)
296 add_history_line (line_read);
301 #endif /* HAVE_LIBREADLINE */
303 static char buf[8192];
307 printf ("%s", prompt_string);
308 line_read = fgets (buf, sizeof buf, stdin);
311 len = strlen (line_read);
312 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
318 #ifdef HAVE_LIBREADLINE
319 static char histfile[1024];
320 static int nr_history_lines = 0;
324 initialize_readline (void)
326 #ifdef HAVE_LIBREADLINE
329 home = getenv ("HOME");
331 snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
333 (void) read_history (histfile);
336 rl_readline_name = "hivexsh";
341 cleanup_readline (void)
343 #ifdef HAVE_LIBREADLINE
346 if (histfile[0] != '\0') {
347 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
354 (void) append_history (nr_history_lines, histfile);
360 add_history_line (const char *line)
362 #ifdef HAVE_LIBREADLINE
369 compare (const void *vp1, const void *vp2)
371 char * const *p1 = (char * const *) vp1;
372 char * const *p2 = (char * const *) vp2;
373 return strcasecmp (*p1, *p2);
377 sort_strings (char **strings, int len)
379 qsort (strings, len, sizeof (char *), compare);
386 case '0'...'9': return c - '0';
387 case 'a'...'f': return c - 'a' + 10;
388 case 'A'...'F': return c - 'A' + 10;
394 dispatch (char *cmd, char *args)
396 if (STRCASEEQ (cmd, "help"))
397 return cmd_help (args);
398 else if (STRCASEEQ (cmd, "load"))
399 return cmd_load (args);
400 else if (STRCASEEQ (cmd, "exit") ||
401 STRCASEEQ (cmd, "q") ||
402 STRCASEEQ (cmd, "quit")) {
407 /* If no hive file is loaded (!h) then only the small selection of
408 * commands above will work.
411 fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
415 if (STRCASEEQ (cmd, "add"))
416 return cmd_add (args);
417 else if (STRCASEEQ (cmd, "cd"))
418 return cmd_cd (args);
419 else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
420 return cmd_close (args);
421 else if (STRCASEEQ (cmd, "commit"))
422 return cmd_commit (args);
423 else if (STRCASEEQ (cmd, "del"))
424 return cmd_del (args);
425 else if (STRCASEEQ (cmd, "ls"))
426 return cmd_ls (args);
427 else if (STRCASEEQ (cmd, "lsval"))
428 return cmd_lsval (args);
429 else if (STRCASEEQ (cmd, "setval"))
430 return cmd_setval (args);
432 fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
439 cmd_load (char *hivefile)
441 if (STREQ (hivefile, "")) {
442 fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
446 if (h) hivex_close (h);
454 h = hivex_open (hivefile, open_flags);
458 "hivexsh: failed to open hive file: %s: %m\n"
460 "If you think this file is a valid Windows binary hive file (_not_\n"
461 "a regedit *.reg file) then please run this command again using the\n"
462 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
463 "which fails into a bug report at https://bugzilla.redhat.com/\n"
469 /* Get the basename of the file for the prompt. */
470 char *p = strrchr (hivefile, '/');
472 loaded = strdup (p+1);
474 loaded = strdup (hivefile);
480 cwd = hivex_root (h);
482 set_prompt_string ();
488 cmd_close (char *args)
490 if (STRNEQ (args, "")) {
491 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
496 if (h) hivex_close (h);
504 set_prompt_string ();
510 cmd_commit (char *path)
512 if (STREQ (path, ""))
515 if (hivex_commit (h, path, 0) == -1) {
516 perror ("hivexsh: commit");
526 if (STREQ (path, "")) {
527 print_node_path (cwd, stdout);
528 fputc ('\n', stdout);
532 if (path[0] == '\\' && path[1] == '\\') {
533 fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
537 hive_node_h new_cwd = cwd;
538 hive_node_h root = hivex_root (h);
540 if (path[0] == '\\') {
546 size_t len = strcspn (path, "\\");
553 path = path[len] == '\0' ? &path[len] : &path[len+1];
556 if (len == 1 && STREQ (elem, "."))
559 if (len == 2 && STREQ (elem, "..")) {
561 new_cwd = hivex_node_parent (h, new_cwd);
566 new_cwd = hivex_node_get_child (h, new_cwd, elem);
569 perror ("hivexsh: cd");
571 fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
577 if (new_cwd != cwd) {
579 set_prompt_string ();
586 cmd_help (char *args)
589 "Navigate through the hive's keys using the 'cd' command, as if it\n"
590 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
591 "current key. Full documentation is in the hivexsh(1) manual page.\n"));
599 if (STRNEQ (args, "")) {
600 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
605 /* Get the subkeys. */
606 hive_node_h *children = hivex_node_children (h, cwd);
607 if (children == NULL) {
612 /* Get names for each subkey. */
614 for (len = 0; children[len] != 0; ++len)
617 char **names = calloc (len, sizeof (char *));
625 for (i = 0; i < len; ++i) {
626 names[i] = hivex_node_name (h, children[i]);
627 if (names[i] == NULL) {
628 perror ("hivex_node_name");
633 /* Sort the names. */
634 sort_strings (names, len);
636 for (i = 0; i < len; ++i)
637 printf ("%s\n", names[i]);
642 for (i = 0; i < len; ++i)
649 cmd_lsval (char *key)
651 if (STRNEQ (key, "")) {
655 if (STREQ (key, "@")) /* default key written as "@" */
656 value = hivex_node_get_value (h, cwd, "");
658 value = hivex_node_get_value (h, cwd, key);
663 /* else key not found */
664 fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
668 /* Print the value. */
671 if (hivex_value_type (h, value, &t, &len) == -1)
676 case hive_t_expand_string:
678 char *str = hivex_value_string (h, value);
682 puts (str); /* note: this adds a single \n character */
688 case hive_t_dword_be: {
689 int32_t j = hivex_value_dword (h, value);
690 printf ("%" PRIi32 "\n", j);
695 int64_t j = hivex_value_qword (h, value);
696 printf ("%" PRIi64 "\n", j);
700 case hive_t_multiple_strings: {
701 char **strs = hivex_value_multiple_strings (h, value);
705 for (j = 0; strs[j] != NULL; ++j) {
715 case hive_t_resource_list:
716 case hive_t_full_resource_description:
717 case hive_t_resource_requirements_list:
719 char *data = hivex_value_value (h, value, &t, &len);
723 if (fwrite (data, 1, len, stdout) != len)
731 /* No key specified, so print all keys in this node. We do this
732 * in a format which looks like the output of regedit, although
733 * this isn't a particularly useful format.
735 hive_value_h *values;
737 values = hivex_node_values (h, cwd);
742 for (i = 0; values[i] != 0; ++i) {
743 char *key = hivex_value_key (h, values[i]);
744 if (!key) goto error;
749 for (j = 0; key[j] != 0; ++j) {
750 if (key[j] == '"' || key[j] == '\\')
756 printf ("\"@\""); /* default key in regedit files */
762 if (hivex_value_type (h, values[i], &t, &len) == -1)
767 case hive_t_expand_string:
769 char *str = hivex_value_string (h, values[i]);
773 if (t != hive_t_string)
774 printf ("str(%d):", t);
777 for (j = 0; str[j] != 0; ++j) {
778 if (str[j] == '"' || str[j] == '\\')
788 case hive_t_dword_be: {
789 int32_t j = hivex_value_dword (h, values[i]);
790 printf ("dword:%08" PRIx32, j);
794 case hive_t_qword: /* sic */
797 case hive_t_multiple_strings:
798 case hive_t_resource_list:
799 case hive_t_full_resource_description:
800 case hive_t_resource_requirements_list:
802 char *data = hivex_value_value (h, values[i], &t, &len);
806 printf ("hex(%d):", t);
808 for (j = 0; j < len; ++j) {
811 printf ("%02x", data[j]);
826 perror ("hivexsh: lsval");
831 cmd_setval (char *nrvals_str)
835 /* Parse number of values. */
837 xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
838 if (xerr != LONGINT_OK) {
839 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
840 "setval", "nrvals", "xstrtol", xerr);
843 if (nrvals < 0 || nrvals > 1000) {
844 fprintf (stderr, _("%s: %s: integer out of range\n"),
849 struct hive_set_value *values =
850 calloc (nrvals, sizeof (struct hive_set_value));
851 if (values == NULL) {
858 /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
859 * explained in the man page.
861 int prompt = isatty (0) ? 2 : 0;
863 for (i = 0; i < nrvals; ++i) {
865 char *buf = rl_gets (" key> ");
867 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
872 /* Note that buf will be overwritten by the next call to rl_gets. */
873 if (STREQ (buf, "@"))
874 values[i].key = strdup ("");
876 values[i].key = strdup (buf);
877 if (values[i].key == NULL) {
883 buf = rl_gets ("value> ");
885 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
890 if (STREQ (buf, "none")) {
891 values[i].t = hive_t_none;
894 else if (STRPREFIX (buf, "string:")) {
896 values[i].t = hive_t_string;
897 int nr_chars = strlen (buf);
898 values[i].len = 2 * (nr_chars + 1);
899 values[i].value = malloc (values[i].len);
900 if (!values[i].value) {
904 for (j = 0; j <= /* sic */ nr_chars; ++j) {
906 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
909 values[i].value[2*j] = buf[j];
910 values[i].value[2*j+1] = '\0';
913 else if (STRPREFIX (buf, "expandstring:")) {
915 values[i].t = hive_t_string;
916 int nr_chars = strlen (buf);
917 values[i].len = 2 * (nr_chars + 1);
918 values[i].value = malloc (values[i].len);
919 if (!values[i].value) {
923 for (j = 0; j <= /* sic */ nr_chars; ++j) {
925 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
928 values[i].value[2*j] = buf[j];
929 values[i].value[2*j+1] = '\0';
932 else if (STRPREFIX (buf, "dword:")) {
934 values[i].t = hive_t_dword;
936 values[i].value = malloc (4);
937 if (!values[i].value) {
942 xerr = xstrtol (buf, NULL, 0, &n, "");
943 if (xerr != LONGINT_OK) {
944 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
945 "setval", "dword", "xstrtol", xerr);
948 if (n < 0 || n > UINT32_MAX) {
949 fprintf (stderr, _("%s: %s: integer out of range\n"),
953 uint32_t u32 = htole32 (n);
954 memcpy (values[i].value, &u32, 4);
956 else if (STRPREFIX (buf, "qword:")) {
958 values[i].t = hive_t_qword;
960 values[i].value = malloc (8);
961 if (!values[i].value) {
966 xerr = xstrtoll (buf, NULL, 0, &n, "");
967 if (xerr != LONGINT_OK) {
968 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
969 "setval", "dword", "xstrtoll", xerr);
973 if (n < 0 || n > UINT64_MAX) {
974 fprintf (stderr, _("%s: %s: integer out of range\n"),
979 uint64_t u64 = htole64 (n);
980 memcpy (values[i].value, &u64, 4);
982 else if (STRPREFIX (buf, "hex:")) {
985 size_t len = strcspn (buf, ":");
987 if (buf[len] == '\0') /* "hex:t" */
989 else { /* "hex:t:..." */
991 nextbuf = &buf[len+1];
995 xerr = xstrtol (buf, NULL, 0, &t, "");
996 if (xerr != LONGINT_OK) {
997 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
998 "setval", "hex", "xstrtol", xerr);
1001 if (t < 0 || t > UINT32_MAX) {
1002 fprintf (stderr, _("%s: %s: integer out of range\n"),
1008 /* Read the hex data. */
1011 /* The allocation length is an overestimate, but it doesn't matter. */
1012 values[i].value = malloc (1 + strlen (buf) / 2);
1013 if (!values[i].value) {
1015 exit (EXIT_FAILURE);
1022 for (j = 0; *buf && j < 2; buf++) {
1023 if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1025 c |= get_xdigit (*buf);
1030 if (j == 2) values[i].value[values[i].len++] = c;
1032 fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1039 _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1045 ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1048 /* Free values array. */
1049 for (i = 0; i < nrvals; ++i) {
1050 free (values[i].key);
1051 free (values[i].value);
1059 cmd_del (char *args)
1061 if (STRNEQ (args, "")) {
1062 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
1067 if (cwd == hivex_root (h)) {
1068 fprintf (stderr, _("hivexsh: del: the root node cannot be deleted\n"));
1072 hive_node_h new_cwd = hivex_node_parent (h, cwd);
1074 if (hivex_node_delete_child (h, cwd) == -1) {
1075 perror ("hivexsh: del");
1080 set_prompt_string ();
1085 cmd_add (char *name)
1087 hive_node_h node = hivex_node_add_child (h, cwd, name);
1089 perror ("hivexsh: add");