| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | binmode(STDOUT, ':utf8'); | ||
| 6 | |||
| 7 | use File::Basename; | ||
| 8 | use Parse::Win32Registry 0.50 qw(hexdump); | ||
| 9 | use Term::ReadLine; | ||
| 10 | |||
| 11 | Parse::Win32Registry->disable_warnings; | ||
| 12 | |||
| 13 | my $filename = shift or die usage(); | ||
| 14 | my $initial_key_path = shift; | ||
| 15 | |||
| 16 | my $registry = Parse::Win32Registry->new($filename) | ||
| 17 | or die "'$filename' is not a registry file\n"; | ||
| 18 | my $root_key = $registry->get_root_key | ||
| 19 | or die "Could not get root key of '$filename'\n"; | ||
| 20 | |||
| 21 | my $key = $root_key; # location as we navigate the registry tree | ||
| 22 | |||
| 23 | my $term = Term::ReadLine->new("regshell"); | ||
| 24 | my $attribs = $term->Attribs; | ||
| 25 | $attribs->{completion_function} = sub { | ||
| 26 | my ($text, $line, $start) = @_; | ||
| 27 | my $preceding_text = substr($line, 0, $start); | ||
| 28 | if ($preceding_text =~ /^\s*$/) { # first word = command completion | ||
| 29 | return grep /^\Q$text/, | ||
| 30 | qw(help cd pwd ls dir cat type xxd hexdump find next exit quit); | ||
| 31 | } | ||
| 32 | else { # second word = parameter completion | ||
| 33 | if ($preceding_text =~ /\b(cd)\b/) { | ||
| 34 | # subkey path completion | ||
| 35 | if ($text =~ /^(.*)\\[^\\]*$/) { | ||
| 36 | my $path = $1; | ||
| 37 | if (my $subkey = $key->get_subkey($path)) { | ||
| 38 | my @subkeys = $subkey->get_list_of_subkeys; | ||
| 39 | my @names = map { "$path\\" . $_->get_name } @subkeys; | ||
| 40 | return grep /^\Q$text/, @names; | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | my @names = map { $_->get_name } $key->get_list_of_subkeys; | ||
| 47 | return grep /^\Q$text/, @names; | ||
| 48 | } | ||
| 49 | elsif ($preceding_text =~ /\b(cat|type|xxd|hexdump)\b/) { | ||
| 50 | # value name completion | ||
| 51 | my @names = map { $_->get_name } $key->get_list_of_values; | ||
| 52 | return grep /^\Q$text/, @names; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | $attribs->{completer_word_break_characters} = ' '; | ||
| 60 | $attribs->{completer_quote_characters} = '"'; | ||
| 61 | |||
| 62 | my $find_iter; | ||
| 63 | my $find_param; | ||
| 64 | my $prompt = $key->get_path; | ||
| 65 | |||
| 66 | while (defined(my $line = $term->readline("$prompt> "))) { | ||
| 67 | # trim white space from line | ||
| 68 | $line =~ s/^\s+//; | ||
| 69 | $line =~ s/\s+$//; | ||
| 70 | |||
| 71 | my ($cmd, $param) = split /\s+/, $line, 2; | ||
| 72 | |||
| 73 | # strip quotes around $param if present | ||
| 74 | if (defined $param && $param =~ /^"(.*)"$/) { | ||
| 75 | $param = $1; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($cmd) { | ||
| 79 | if ($cmd eq 'help') { | ||
| 80 | print <<HELP; | ||
| 81 | cd <subkey> Change to the specified subkey. | ||
| 82 | Specify '..' to change to the parent key. | ||
| 83 | Omit the subkey name to change to the root key. | ||
| 84 | ls | dir List the subkeys and values of the current key. | ||
| 85 | cat | type <value> Display the specified value. | ||
| 86 | Omit the value name to display the default value. | ||
| 87 | xxd | hexdump <value> Display the specified value in hex. | ||
| 88 | Omit the value name to display the default value. | ||
| 89 | find <string> Start a search for a key or value matching | ||
| 90 | the supplied string. The search is not case sensitive. | ||
| 91 | next | n Search for the next matching key or value. | ||
| 92 | exit | quit Exit the program. | ||
| 93 | HELP | ||
| 94 | } | ||
| 95 | elsif ($cmd eq 'cd') { | ||
| 96 | if (!defined $param) { | ||
| 97 | $key = $root_key; # go to root key if no param supplied | ||
| 98 | } | ||
| 99 | elsif ($param =~ /\.\.(\\\.\.)*/) { | ||
| 100 | my $count = ($param =~ tr/\\//); | ||
| 101 | my $new_key = $key; | ||
| 102 | for (my $i = 0; $i <= $count; $i++) { | ||
| 103 | $new_key = $new_key->get_parent; | ||
| 104 | if (!defined $new_key) { | ||
| 105 | last; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | if (defined $new_key) { | ||
| 109 | print $new_key->as_string, "\n"; | ||
| 110 | $key = $new_key; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | print "Invalid parent key\n"; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | if (my $new_key = $key->get_subkey($param)) { | ||
| 118 | $key = $new_key; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | print "No subkey named '$param'\n"; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | elsif ($cmd eq 'pwd') { | ||
| 126 | print $key->get_path, "\n"; | ||
| 127 | } | ||
| 128 | elsif ($cmd eq 'ls' || $cmd eq 'dir') { | ||
| 129 | foreach my $subkey ($key->get_list_of_subkeys) { | ||
| 130 | if ($cmd eq 'ls') { | ||
| 131 | print $subkey->get_name, "\n"; | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | print "[", $subkey->get_name, "]\n"; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | foreach my $value ($key->get_list_of_values) { | ||
| 138 | if ($cmd eq 'ls') { | ||
| 139 | print $value->as_string, "\n"; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | print $value->as_regedit_export; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | elsif ($cmd eq 'cat' || $cmd eq 'type') { | ||
| 147 | if (!defined $param) { | ||
| 148 | $param = ''; # assume default value if no param supplied | ||
| 149 | } | ||
| 150 | if (my $value = $key->get_value($param)) { | ||
| 151 | if ($cmd eq 'cat') { | ||
| 152 | print $value->as_string, "\n"; | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | print $value->as_regedit_export; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | print "No value named '$param'\n"; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | elsif ($cmd eq 'xxd' | $cmd eq 'hexdump') { | ||
| 163 | if (!defined $param) { | ||
| 164 | $param = ''; # assume default value if no param supplied | ||
| 165 | } | ||
| 166 | if (my $value = $key->get_value($param)) { | ||
| 167 | print hexdump($value->get_raw_data); | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | print "No value named '$param'\n"; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | elsif ($cmd eq 'exit' || $cmd eq 'quit') { | ||
| 174 | last; | ||
| 175 | } | ||
| 176 | elsif ($cmd eq 'find') { | ||
| 177 | if (!defined $param) { | ||
| 178 | if (defined $find_param) { | ||
| 179 | print "Currently searching for '$find_param'\n"; | ||
| 180 | } | ||
| 181 | print "Specify a search term to start a new search\n"; | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $find_param = $param; | ||
| 185 | $find_iter = $root_key->get_subtree_iterator; | ||
| 186 | find_next(); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | elsif ($cmd eq 'next' || $cmd eq 'n') { | ||
| 190 | find_next(); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | print "Unrecognised command '$cmd'\n"; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | $prompt = $key->get_path; | ||
| 198 | } | ||
| 199 | print "\nGoodbye...\n"; | ||
| 200 | |||
| 201 | sub usage { | ||
| 202 | my $script_name = basename $0; | ||
| 203 | return <<USAGE; | ||
| 204 | $script_name for Parse::Win32Registry $Parse::Win32Registry::VERSION | ||
| 205 | |||
| 206 | An interactive shell for examining registry files where you navigate keys | ||
| 207 | using 'cd' and display keys and values using 'ls' or 'dir'. Tab completion | ||
| 208 | for key and value names is available if the underlying platform supports | ||
| 209 | it. Type 'help' at the prompt for a list of available commands. | ||
| 210 | |||
| 211 | $script_name <filename> | ||
| 212 | USAGE | ||
| 213 | } | ||
| 214 | |||
| 215 | sub find_next { | ||
| 216 | if (!defined $find_param || !defined $find_iter) { | ||
| 217 | print "No search started...\n"; | ||
| 218 | return; | ||
| 219 | } | ||
| 220 | |||
| 221 | while (my ($next_key, $next_value) = $find_iter->get_next) { | ||
| 222 | my $key_name = $next_key->get_name; | ||
| 223 | |||
| 224 | if (defined $next_value) { | ||
| 225 | my $value_name = $next_value->get_name; | ||
| 226 | if (index(lc $value_name, lc $find_param) > -1) { | ||
| 227 | print "Found value '$value_name' in key '$key_name'\n"; | ||
| 228 | $key = $next_key; | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | next; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | if (index(lc $key_name, lc $find_param) > -1) { | ||
| 237 | print "Found key '$key_name'\n"; | ||
| 238 | $key = $next_key; | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | else { | ||
| 242 | next; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | print "No (more) matches found\n"; | ||
| 246 | } |