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_help (char *args);
83 static int cmd_load (char *hivefile);
84 static int cmd_ls (char *args);
85 static int cmd_lsval (char *args);
86 static int cmd_setval (char *args);
91 fprintf (stderr, "hivexsh [-dfw] [hivefile]\n");
96 main (int argc, char *argv[])
98 setlocale (LC_ALL, "");
99 bindtextdomain (PACKAGE, LOCALEBASEDIR);
100 textdomain (PACKAGE);
103 const char *filename = NULL;
105 set_prompt_string ();
107 while ((c = getopt (argc, argv, "dfw")) != EOF) {
110 open_flags |= HIVEX_OPEN_DEBUG;
116 open_flags |= HIVEX_OPEN_WRITE;
124 if (optind + 1 != argc)
126 if (cmd_load (argv[optind]) == -1)
130 /* -f filename parameter */
133 if (open (filename, O_RDONLY) == -1) {
141 initialize_readline ();
146 "Welcome to hivexsh, the hivex interactive shell for examining\n"
147 "Windows Registry binary hive files.\n"
149 "Type: 'help' for help summary\n"
150 " 'quit' to quit the shell\n"
154 char *buf = rl_gets (prompt_string);
162 while (*buf && c_isspace (*buf))
165 /* Ignore blank line. */
168 /* If the next character is '#' then this is a comment. */
169 if (*buf == '#') continue;
171 /* Parsing is very simple - much simpler than guestfish. This is
172 * because Registry keys often contain spaces, and we don't want
173 * to bother with quoting. Therefore here we just split at the
174 * first whitespace into "cmd<whitespace>arg(s)". We let the
175 * command decide how to deal with arg(s), if at all.
177 size_t len = strcspn (buf, " \t");
179 if (len == 0) continue;
185 if (buf[len] == '\0') {
186 /* This is mostly safe. Although the cmd_* functions do sometimes
187 * modify args, then shouldn't do so when args is "".
194 args = buf + len + 1 + strspn (&buf[len+1], " \t");
197 while (len > 0 && c_isspace (args[len-1])) {
203 /*printf ("command: '%s' args: '%s'\n", cmd, args)*/;
204 int r = dispatch (cmd, args);
205 if (!is_tty && r == -1)
210 free (prompt_string);
212 if (h) hivex_close (h);
216 /* Set the prompt string. This is called whenever it could change, eg.
217 * after loading a file or changing directory.
220 set_prompt_string (void)
222 free (prompt_string);
223 prompt_string = NULL;
228 fp = open_memstream (&ptr, &size);
230 perror ("open_memstream");
235 assert (loaded != NULL);
239 print_node_path (cwd, fp);
247 /* Print the \full\path of a node. */
249 print_node_path (hive_node_h node, FILE *fp)
251 hive_node_h root = hivex_root (h);
258 hive_node_h parent = hivex_node_parent (h, node);
260 fprintf (stderr, _("hivexsh: error getting parent of node %zu\n"), node);
263 print_node_path (parent, fp);
268 char *name = hivex_node_name (h, node);
270 fprintf (stderr, _("hivexsh: error getting node name of node %zx\n"), node);
278 static char *line_read = NULL;
281 rl_gets (const char *prompt_string)
283 #ifdef HAVE_LIBREADLINE
291 line_read = readline (prompt_string);
293 if (line_read && *line_read)
294 add_history_line (line_read);
299 #endif /* HAVE_LIBREADLINE */
301 static char buf[8192];
305 printf ("%s", prompt_string);
306 line_read = fgets (buf, sizeof buf, stdin);
309 len = strlen (line_read);
310 if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
316 #ifdef HAVE_LIBREADLINE
317 static char histfile[1024];
318 static int nr_history_lines = 0;
322 initialize_readline (void)
324 #ifdef HAVE_LIBREADLINE
327 home = getenv ("HOME");
329 snprintf (histfile, sizeof histfile, "%s/.hivexsh", home);
331 (void) read_history (histfile);
334 rl_readline_name = "hivexsh";
339 cleanup_readline (void)
341 #ifdef HAVE_LIBREADLINE
344 if (histfile[0] != '\0') {
345 fd = open (histfile, O_WRONLY|O_CREAT, 0644);
352 (void) append_history (nr_history_lines, histfile);
358 add_history_line (const char *line)
360 #ifdef HAVE_LIBREADLINE
367 compare (const void *vp1, const void *vp2)
369 char * const *p1 = (char * const *) vp1;
370 char * const *p2 = (char * const *) vp2;
371 return strcasecmp (*p1, *p2);
375 sort_strings (char **strings, int len)
377 qsort (strings, len, sizeof (char *), compare);
384 case '0'...'9': return c - '0';
385 case 'a'...'f': return c - 'a' + 10;
386 case 'A'...'F': return c - 'A' + 10;
392 dispatch (char *cmd, char *args)
394 if (STRCASEEQ (cmd, "help"))
395 return cmd_help (args);
396 else if (STRCASEEQ (cmd, "load"))
397 return cmd_load (args);
398 else if (STRCASEEQ (cmd, "exit") ||
399 STRCASEEQ (cmd, "q") ||
400 STRCASEEQ (cmd, "quit")) {
405 /* If no hive file is loaded (!h) then only the small selection of
406 * commands above will work.
409 fprintf (stderr, _("hivexsh: you must load a hive file first using 'load hivefile'\n"));
413 if (STRCASEEQ (cmd, "cd"))
414 return cmd_cd (args);
415 else if (STRCASEEQ (cmd, "close") || STRCASEEQ (cmd, "unload"))
416 return cmd_close (args);
417 else if (STRCASEEQ (cmd, "commit"))
418 return cmd_commit (args);
419 else if (STRCASEEQ (cmd, "ls"))
420 return cmd_ls (args);
421 else if (STRCASEEQ (cmd, "lsval"))
422 return cmd_lsval (args);
423 else if (STRCASEEQ (cmd, "setval"))
424 return cmd_setval (args);
426 fprintf (stderr, _("hivexsh: unknown command '%s', use 'help' for help summary\n"),
433 cmd_load (char *hivefile)
435 if (STREQ (hivefile, "")) {
436 fprintf (stderr, _("hivexsh: load: no hive file name given to load\n"));
440 if (h) hivex_close (h);
448 h = hivex_open (hivefile, open_flags);
452 "hivexsh: failed to open hive file: %s: %m\n"
454 "If you think this file is a valid Windows binary hive file (_not_\n"
455 "a regedit *.reg file) then please run this command again using the\n"
456 "hivexsh option '-d' and attach the complete output _and_ the hive file\n"
457 "which fails into a bug report at https://bugzilla.redhat.com/\n"
463 /* Get the basename of the file for the prompt. */
464 char *p = strrchr (hivefile, '/');
466 loaded = strdup (p+1);
468 loaded = strdup (hivefile);
474 cwd = hivex_root (h);
476 set_prompt_string ();
482 cmd_close (char *args)
484 if (STRNEQ (args, "")) {
485 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
490 if (h) hivex_close (h);
498 set_prompt_string ();
504 cmd_commit (char *path)
506 if (STREQ (path, ""))
509 if (hivex_commit (h, path, 0) == -1) {
510 perror ("hivexsh: commit");
520 if (STREQ (path, "")) {
521 print_node_path (cwd, stdout);
522 fputc ('\n', stdout);
526 if (path[0] == '\\' && path[1] == '\\') {
527 fprintf (stderr, _("%s: %s: \\ characters in path are doubled - are you escaping the path parameter correctly?\n"), "hivexsh", path);
531 hive_node_h new_cwd = cwd;
532 hive_node_h root = hivex_root (h);
534 if (path[0] == '\\') {
540 size_t len = strcspn (path, "\\");
547 path = path[len] == '\0' ? &path[len] : &path[len+1];
550 if (len == 1 && STREQ (elem, "."))
553 if (len == 2 && STREQ (elem, "..")) {
555 new_cwd = hivex_node_parent (h, new_cwd);
559 new_cwd = hivex_node_get_child (h, new_cwd, elem);
561 fprintf (stderr, _("hivexsh: cd: subkey '%s' not found\n"),
567 if (new_cwd != cwd) {
569 set_prompt_string ();
576 cmd_help (char *args)
579 "Navigate through the hive's keys using the 'cd' command, as if it\n"
580 "contained a filesystem, and use 'ls' to list the subkeys of the\n"
581 "current key. Full documentation is in the hivexsh(1) manual page.\n"));
589 if (STRNEQ (args, "")) {
590 fprintf (stderr, _("hivexsh: '%s' command should not be given arguments\n"),
595 /* Get the subkeys. */
596 hive_node_h *children = hivex_node_children (h, cwd);
597 if (children == NULL) {
602 /* Get names for each subkey. */
604 for (len = 0; children[len] != 0; ++len)
607 char **names = calloc (len, sizeof (char *));
615 for (i = 0; i < len; ++i) {
616 names[i] = hivex_node_name (h, children[i]);
617 if (names[i] == NULL) {
618 perror ("hivex_node_name");
623 /* Sort the names. */
624 sort_strings (names, len);
626 for (i = 0; i < len; ++i)
627 printf ("%s\n", names[i]);
632 for (i = 0; i < len; ++i)
639 cmd_lsval (char *key)
641 if (STRNEQ (key, "")) {
645 if (STREQ (key, "@")) /* default key written as "@" */
646 value = hivex_node_get_value (h, cwd, "");
648 value = hivex_node_get_value (h, cwd, key);
653 /* else key not found */
654 fprintf (stderr, _("%s: %s: key not found\n"), "hivexsh", key);
658 /* Print the value. */
661 if (hivex_value_type (h, value, &t, &len) == -1)
666 case hive_t_expand_string:
668 char *str = hivex_value_string (h, value);
672 puts (str); /* note: this adds a single \n character */
678 case hive_t_dword_be: {
679 int32_t j = hivex_value_dword (h, value);
680 printf ("%" PRIi32 "\n", j);
685 int64_t j = hivex_value_qword (h, value);
686 printf ("%" PRIi64 "\n", j);
690 case hive_t_multiple_strings: {
691 char **strs = hivex_value_multiple_strings (h, value);
695 for (j = 0; strs[j] != NULL; ++j) {
705 case hive_t_resource_list:
706 case hive_t_full_resource_description:
707 case hive_t_resource_requirements_list:
709 char *data = hivex_value_value (h, value, &t, &len);
713 if (fwrite (data, 1, len, stdout) != len)
721 /* No key specified, so print all keys in this node. We do this
722 * in a format which looks like the output of regedit, although
723 * this isn't a particularly useful format.
725 hive_value_h *values;
727 values = hivex_node_values (h, cwd);
732 for (i = 0; values[i] != 0; ++i) {
733 char *key = hivex_value_key (h, values[i]);
734 if (!key) goto error;
739 for (j = 0; key[j] != 0; ++j) {
740 if (key[j] == '"' || key[j] == '\\')
746 printf ("\"@\""); /* default key in regedit files */
752 if (hivex_value_type (h, values[i], &t, &len) == -1)
757 case hive_t_expand_string:
759 char *str = hivex_value_string (h, values[i]);
763 if (t != hive_t_string)
764 printf ("str(%d):", t);
767 for (j = 0; str[j] != 0; ++j) {
768 if (str[j] == '"' || str[j] == '\\')
778 case hive_t_dword_be: {
779 int32_t j = hivex_value_dword (h, values[i]);
780 printf ("dword:%08" PRIx32 "\"", j);
784 case hive_t_qword: /* sic */
787 case hive_t_multiple_strings:
788 case hive_t_resource_list:
789 case hive_t_full_resource_description:
790 case hive_t_resource_requirements_list:
792 char *data = hivex_value_value (h, values[i], &t, &len);
796 printf ("hex(%d):", t);
798 for (j = 0; j < len; ++j) {
801 printf ("%02x", data[j]);
816 perror ("hivexsh: lsval");
821 cmd_setval (char *nrvals_str)
825 /* Parse number of values. */
827 xerr = xstrtol (nrvals_str, NULL, 0, &nrvals, "");
828 if (xerr != LONGINT_OK) {
829 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
830 "setval", "nrvals", "xstrtol", xerr);
833 if (nrvals < 0 || nrvals > 1000) {
834 fprintf (stderr, _("%s: %s: integer out of range\n"),
839 struct hive_set_value *values =
840 calloc (nrvals, sizeof (struct hive_set_value));
841 if (values == NULL) {
848 /* Read nrvals * 2 lines of input, nrvals * (key, value) pairs, as
849 * explained in the man page.
851 int prompt = isatty (0) ? 2 : 0;
853 for (i = 0; i < nrvals; ++i) {
855 char *buf = rl_gets (" key> ");
857 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
862 /* Note that buf will be overwritten by the next call to rl_gets. */
863 if (STREQ (buf, "@"))
864 values[i].key = strdup ("");
866 values[i].key = strdup (buf);
867 if (values[i].key == NULL) {
873 buf = rl_gets ("value> ");
875 fprintf (stderr, _("hivexsh: setval: unexpected end of input\n"));
880 if (STREQ (buf, "none")) {
881 values[i].t = hive_t_none;
884 else if (STRPREFIX (buf, "string:")) {
886 values[i].t = hive_t_string;
887 int nr_chars = strlen (buf);
888 values[i].len = 2 * (nr_chars + 1);
889 values[i].value = malloc (values[i].len);
890 if (!values[i].value) {
894 for (j = 0; j <= /* sic */ nr_chars; ++j) {
896 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
899 values[i].value[2*j] = buf[j];
900 values[i].value[2*j+1] = '\0';
903 else if (STRPREFIX (buf, "expandstring:")) {
905 values[i].t = hive_t_string;
906 int nr_chars = strlen (buf);
907 values[i].len = 2 * (nr_chars + 1);
908 values[i].value = malloc (values[i].len);
909 if (!values[i].value) {
913 for (j = 0; j <= /* sic */ nr_chars; ++j) {
915 fprintf (stderr, _("hivexsh: string(utf16le): only 7 bit ASCII strings are supported for input\n"));
918 values[i].value[2*j] = buf[j];
919 values[i].value[2*j+1] = '\0';
922 else if (STRPREFIX (buf, "dword:")) {
924 values[i].t = hive_t_dword;
926 values[i].value = malloc (4);
927 if (!values[i].value) {
932 xerr = xstrtol (buf, NULL, 0, &n, "");
933 if (xerr != LONGINT_OK) {
934 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
935 "setval", "dword", "xstrtol", xerr);
938 if (n < 0 || n > UINT32_MAX) {
939 fprintf (stderr, _("%s: %s: integer out of range\n"),
943 uint32_t u32 = htole32 (n);
944 memcpy (values[i].value, &u32, 4);
946 else if (STRPREFIX (buf, "qword:")) {
948 values[i].t = hive_t_qword;
950 values[i].value = malloc (8);
951 if (!values[i].value) {
956 xerr = xstrtoll (buf, NULL, 0, &n, "");
957 if (xerr != LONGINT_OK) {
958 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
959 "setval", "dword", "xstrtoll", xerr);
963 if (n < 0 || n > UINT64_MAX) {
964 fprintf (stderr, _("%s: %s: integer out of range\n"),
969 uint64_t u64 = htole64 (n);
970 memcpy (values[i].value, &u64, 4);
972 else if (STRPREFIX (buf, "hex:")) {
975 size_t len = strcspn (buf, ":");
977 if (buf[len] == '\0') /* "hex:t" */
979 else { /* "hex:t:..." */
981 nextbuf = &buf[len+1];
985 xerr = xstrtol (buf, NULL, 0, &t, "");
986 if (xerr != LONGINT_OK) {
987 fprintf (stderr, _("%s: %s: invalid integer parameter (%s returned %d)\n"),
988 "setval", "hex", "xstrtol", xerr);
991 if (t < 0 || t > UINT32_MAX) {
992 fprintf (stderr, _("%s: %s: integer out of range\n"),
998 /* Read the hex data. */
1001 /* The allocation length is an overestimate, but it doesn't matter. */
1002 values[i].value = malloc (1 + strlen (buf) / 2);
1003 if (!values[i].value) {
1005 exit (EXIT_FAILURE);
1012 for (j = 0; *buf && j < 2; buf++) {
1013 if (c_isxdigit (*buf)) { /* NB: ignore non-hex digits. */
1015 c |= get_xdigit (*buf);
1020 if (j == 2) values[i].value[values[i].len++] = c;
1022 fprintf (stderr, _("hivexsh: setval: trailing garbage after hex string\n"));
1029 _("hivexsh: setval: cannot parse value string, please refer to the man page hivexsh(1) for help: %s\n"),
1035 ret = hivex_node_set_values (h, cwd, nrvals, values, 0);
1038 /* Free values array. */
1039 for (i = 0; i < nrvals; ++i) {
1040 free (values[i].key);
1041 free (values[i].value);