| 1 |
#!/usr/bin/env perl |
| 2 |
# Copyright (C) 2010-2011 Red Hat Inc. |
| 3 |
# |
| 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. |
| 8 |
# |
| 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. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 |
|
| 18 |
use warnings; |
| 19 |
use strict; |
| 20 |
|
| 21 |
use Win::Hivex; |
| 22 |
use Win::Hivex::Regedit qw(reg_import reg_export); |
| 23 |
|
| 24 |
use Pod::Usage; |
| 25 |
use Getopt::Long; |
| 26 |
|
| 27 |
=encoding utf8 |
| 28 |
|
| 29 |
=head1 NAME |
| 30 |
|
| 31 |
hivexregedit - Merge and export Registry changes from regedit-format files. |
| 32 |
|
| 33 |
=head1 SYNOPSIS |
| 34 |
|
| 35 |
hivexregedit --merge [--prefix prefix] [--encoding enc] \ |
| 36 |
hivefile [regfile] |
| 37 |
|
| 38 |
hivexregedit --export [--prefix prefix] hivefile key > regfile |
| 39 |
|
| 40 |
=head1 DESCRIPTION |
| 41 |
|
| 42 |
Please note hivexregedit is a low-level tool for manipulating hive |
| 43 |
files directly. To merge or export registry changes to Windows |
| 44 |
virtual machines it's better to use L<virt-win-reg(1)>. |
| 45 |
|
| 46 |
Given a local binary ("hive") file, there are two modes. C<--merge> |
| 47 |
imports (merges) changes from a regedit-format file into the hive. It |
| 48 |
is similar to using the C</s> switch in Windows regedit.exe. |
| 49 |
|
| 50 |
C<--export> exports a Registry key (recursively) into the regedit format. |
| 51 |
|
| 52 |
=head2 ENCODING |
| 53 |
|
| 54 |
C<hivexregedit> expects that regedit files have already been re-encoded |
| 55 |
in the local encoding. Usually on Linux hosts, this means UTF-8 with |
| 56 |
Unix-style line endings. Since Windows regedit files are often in |
| 57 |
UTF-16LE with Windows-style line endings, you may need to re-encode the |
| 58 |
whole file before or after processing. |
| 59 |
|
| 60 |
To re-encode a file from Windows format to Linux (before processing it |
| 61 |
with the C<--merge> option), you would do something like this: |
| 62 |
|
| 63 |
iconv -f utf-16le -t utf-8 < win.reg | dos2unix > linux.reg |
| 64 |
|
| 65 |
To go in the opposite direction, after using C<--export> and before |
| 66 |
sending the file to a Windows user, do something like this: |
| 67 |
|
| 68 |
unix2dos < linux.reg | iconv -f utf-8 -t utf-16le > win.reg |
| 69 |
|
| 70 |
For more information about encoding, see L<Win::Hivex::Regedit(3)>. |
| 71 |
|
| 72 |
If you are unsure about the current encoding, use the L<file(1)> |
| 73 |
command. Recent versions of Windows regedit.exe produce a UTF-16LE |
| 74 |
file with Windows-style (CRLF) line endings, like this: |
| 75 |
|
| 76 |
$ file software.reg |
| 77 |
software.reg: Little-endian UTF-16 Unicode text, with very long lines, |
| 78 |
with CRLF line terminators |
| 79 |
|
| 80 |
This file would need conversion before you could C<--merge> it. |
| 81 |
|
| 82 |
=head2 SHELL QUOTING |
| 83 |
|
| 84 |
Be careful when passing parameters containing C<\> (backslash) in the |
| 85 |
shell. Usually you will have to use 'single quotes' or double |
| 86 |
backslashes (but not both) to protect them from the shell. |
| 87 |
|
| 88 |
=head2 CurrentControlSet etc. |
| 89 |
|
| 90 |
Registry keys like C<CurrentControlSet> don't really exist in the |
| 91 |
Windows Registry at the level of the hive file, and therefore you |
| 92 |
cannot modify these. |
| 93 |
|
| 94 |
C<CurrentControlSet> is usually an alias for C<ControlSet001>. In |
| 95 |
some circumstances it might refer to another control set. The way |
| 96 |
to find out is to look at the C<HKLM\SYSTEM\Select> key: |
| 97 |
|
| 98 |
$ hivexregedit --export SYSTEM '\Select' |
| 99 |
[\Select] |
| 100 |
"Current"=dword:00000001 |
| 101 |
"Default"=dword:00000001 |
| 102 |
"Failed"=dword:00000000 |
| 103 |
"LastKnownGood"=dword:00000002 |
| 104 |
|
| 105 |
"Current" is the one which Windows will choose when it boots. |
| 106 |
|
| 107 |
Similarly, other C<Current...> keys in the path may need to |
| 108 |
be replaced. |
| 109 |
|
| 110 |
=head1 EXAMPLE |
| 111 |
|
| 112 |
$ virt-cat WindowsGuest /Windows/System32/config/software > software.hive |
| 113 |
$ hivexregedit --export \ |
| 114 |
--prefix 'HKEY_LOCAL_MACHINE\SOFTWARE' \ |
| 115 |
software.hive '\Microsoft' > ms-keys.reg |
| 116 |
|
| 117 |
$ hivexregedit --merge system.hive \ |
| 118 |
--prefix 'HKEY_LOCAL_MACHINE\SYSTEM' additions.reg |
| 119 |
|
| 120 |
=head1 OPTIONS |
| 121 |
|
| 122 |
=over 4 |
| 123 |
|
| 124 |
=cut |
| 125 |
|
| 126 |
my $help; |
| 127 |
|
| 128 |
=item B<--help> |
| 129 |
|
| 130 |
Display help. |
| 131 |
|
| 132 |
=cut |
| 133 |
|
| 134 |
my $debug; |
| 135 |
|
| 136 |
=item B<--debug> |
| 137 |
|
| 138 |
Enable debugging in the hivex library. This is useful for diagnosing |
| 139 |
bugs and also malformed hive files. |
| 140 |
|
| 141 |
=cut |
| 142 |
|
| 143 |
my $merge; |
| 144 |
|
| 145 |
=item B<--merge> |
| 146 |
|
| 147 |
hivexregedit --merge [--prefix prefix] [--encoding enc] \ |
| 148 |
hivefile [regfile] |
| 149 |
|
| 150 |
Merge C<regfile> (a regedit-format text file) into the hive |
| 151 |
C<hivefile>. If C<regfile> is omitted, then the program reads from |
| 152 |
standard input. (Also you can give multiple input files). |
| 153 |
|
| 154 |
C<--prefix> specifies the Windows Registry prefix. It is almost |
| 155 |
always necessary to use this when dealing with real hive files. |
| 156 |
|
| 157 |
C<--encoding> specifies the encoding for unmarked strings in the |
| 158 |
input. It defaults to C<UTF-16LE> which should work for recent |
| 159 |
versions of Windows. Another possibility is to use C<ASCII>. |
| 160 |
|
| 161 |
=cut |
| 162 |
|
| 163 |
my $export; |
| 164 |
|
| 165 |
=item B<--export> |
| 166 |
|
| 167 |
hivexregedit --export [--prefix prefix] hivefile key > regfile |
| 168 |
|
| 169 |
C<key> is a path within the hive C<hivefile>. (The key should not |
| 170 |
contain any prefix and should be quoted to defend backslashes from the |
| 171 |
shell). The key is exported, recursively, to standard output in the |
| 172 |
textual regedit format. |
| 173 |
|
| 174 |
C<--prefix> specifies the Windows Registry prefix. It is almost |
| 175 |
always necessary to use this when dealing with real hive files. |
| 176 |
|
| 177 |
=cut |
| 178 |
|
| 179 |
my $prefix; |
| 180 |
|
| 181 |
=item B<--prefix> prefix |
| 182 |
|
| 183 |
Hive files and Windows Registry key names are indirectly related. For |
| 184 |
example, inside the software hive, all keys are stored relative to |
| 185 |
C<HKEY_LOCAL_MACHINE\SOFTWARE>. Thus |
| 186 |
C<HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft> appears in the hive file as |
| 187 |
C<\Microsoft>. |
| 188 |
|
| 189 |
The hive format itself does not store this prefix, so you have to |
| 190 |
supply it based on outside knowledge. (L<virt-win-reg(1)>, amongst |
| 191 |
other things, already knows about this). |
| 192 |
|
| 193 |
Usually it is sufficient to pass the parameter |
| 194 |
C<--prefix 'HKEY_LOCAL_MACHINE\SOFTWARE'> or similar when doing |
| 195 |
merges and exports. |
| 196 |
|
| 197 |
=cut |
| 198 |
|
| 199 |
my $encoding; |
| 200 |
|
| 201 |
=item B<--encoding> UTF-16LE|ASCII |
| 202 |
|
| 203 |
When merging (only), you may need to specify the encoding for strings |
| 204 |
to be used in the hive file. This is explained in detail in |
| 205 |
L<Win::Hivex::Regedit(3)/ENCODING STRINGS>. |
| 206 |
|
| 207 |
The default is to use UTF-16LE, which should work with recent versions |
| 208 |
of Windows. |
| 209 |
|
| 210 |
=cut |
| 211 |
|
| 212 |
my $unsafe_printable_strings; |
| 213 |
|
| 214 |
=item B<--unsafe-printable-strings> |
| 215 |
|
| 216 |
When exporting (only), assume strings are UTF-16LE and print them as |
| 217 |
strings instead of hex sequences. Remove the final zero codepoint |
| 218 |
from strings if present. |
| 219 |
|
| 220 |
This is unsafe and does not preserve the fidelity of strings in the |
| 221 |
original hive for various reasons: |
| 222 |
|
| 223 |
=over 4 |
| 224 |
|
| 225 |
=item * |
| 226 |
|
| 227 |
Assumes the original encoding is UTF-16LE. ASCII strings and strings |
| 228 |
in other encodings will be corrupted by this transformation. |
| 229 |
|
| 230 |
=item * |
| 231 |
|
| 232 |
Assumes that everything which has type 1 or 2 is really a string |
| 233 |
and that everything else is not a string, but the type field in |
| 234 |
real hives is not reliable. |
| 235 |
|
| 236 |
=item * |
| 237 |
|
| 238 |
Loses information about whether a zero codepoint followed the string |
| 239 |
in the hive or not. |
| 240 |
|
| 241 |
=back |
| 242 |
|
| 243 |
This all happens because the hive itself contains no information about |
| 244 |
how strings are encoded (see |
| 245 |
L<Win::Hivex::Regedit(3)/ENCODING STRINGS>). |
| 246 |
|
| 247 |
You should only use this option for quick hacking and debugging of the |
| 248 |
hive contents, and I<never> use it if the output is going to be passed |
| 249 |
into another program or stored in another hive. |
| 250 |
|
| 251 |
=cut |
| 252 |
|
| 253 |
my $unsafe; |
| 254 |
|
| 255 |
=item B<--unsafe> |
| 256 |
|
| 257 |
Use heuristics to tolerate certain levels of corruption within hives. |
| 258 |
|
| 259 |
This is unsafe but may allow to export/merge valid keys/values in an |
| 260 |
othewise corrupted hive. |
| 261 |
|
| 262 |
=cut |
| 263 |
|
| 264 |
my $max_depth; |
| 265 |
|
| 266 |
=item B<--max-depth> depth |
| 267 |
|
| 268 |
Limits the recursion depth of the export. For example, an export |
| 269 |
with a max depth of 1 will only include values directly under the |
| 270 |
specified key/prefix. A max depth of 0 will return no values. |
| 271 |
|
| 272 |
Exports include all child keys by default (fully recursive), |
| 273 |
which may take a while if the registry hive is large / bloated. |
| 274 |
This behavior can also be achieved by providing a negative max depth. |
| 275 |
|
| 276 |
=back |
| 277 |
|
| 278 |
=cut |
| 279 |
|
| 280 |
GetOptions ("help|?" => \$help, |
| 281 |
"debug" => \$debug, |
| 282 |
"merge|import" => \$merge, |
| 283 |
"export" => \$export, |
| 284 |
"prefix=s" => \$prefix, |
| 285 |
"encoding=s" => \$encoding, |
| 286 |
"unsafe-printable-strings" => \$unsafe_printable_strings, |
| 287 |
"unsafe" => \$unsafe, |
| 288 |
"max-depth=i" => \$max_depth, |
| 289 |
) or pod2usage (2); |
| 290 |
pod2usage (1) if $help; |
| 291 |
|
| 292 |
if ($merge && $export) { |
| 293 |
die "hivexregedit: cannot use --merge and --export at the same time\n" |
| 294 |
} |
| 295 |
|
| 296 |
unless ($merge || $export) { |
| 297 |
die "hivexregedit: use --merge or --export, see the manpage for help\n" |
| 298 |
} |
| 299 |
|
| 300 |
if ($export && defined $encoding) { |
| 301 |
die "hivexregedit: --encoding has no effect when used with --export\n" |
| 302 |
} |
| 303 |
|
| 304 |
if ($merge) { # --merge (reg_import) |
| 305 |
if (@ARGV < 1) { |
| 306 |
die "hivexregedit --merge hivefile [input.reg ...]\n" |
| 307 |
} |
| 308 |
|
| 309 |
my $hivefile = shift @ARGV; |
| 310 |
|
| 311 |
my $h = Win::Hivex->open ($hivefile, write => 1, debug => $debug, |
| 312 |
unsafe => $unsafe); |
| 313 |
|
| 314 |
# Read from stdin unless other files have been specified. |
| 315 |
unshift (@ARGV, '-') unless @ARGV; |
| 316 |
|
| 317 |
foreach (@ARGV) { |
| 318 |
open FILE, $_ or die "$_: $!"; |
| 319 |
reg_import (\*FILE, sub { |
| 320 |
local $_ = shift; |
| 321 |
# Remove prefix from the start of the path, matching |
| 322 |
# case insensitively. |
| 323 |
if (defined $prefix) { |
| 324 |
my $len = length $prefix; |
| 325 |
if (length $_ >= $len && |
| 326 |
lc (substr ($_, 0, $len)) eq lc ($prefix)) { |
| 327 |
$_ = substr ($_, $len); |
| 328 |
} |
| 329 |
} |
| 330 |
($h, $_) |
| 331 |
}); |
| 332 |
} |
| 333 |
|
| 334 |
$h->commit (undef); |
| 335 |
} else { # --export (reg_export) |
| 336 |
if (@ARGV != 2) { |
| 337 |
die "hivexregedit --export hivefile key\n" |
| 338 |
} |
| 339 |
|
| 340 |
my $hivefile = shift @ARGV; |
| 341 |
my $key = shift @ARGV; |
| 342 |
|
| 343 |
my $h = Win::Hivex->open ($hivefile, debug => $debug, unsafe => $unsafe); |
| 344 |
|
| 345 |
print "Windows Registry Editor Version 5.00\n\n"; |
| 346 |
|
| 347 |
reg_export ($h, $key, \*STDOUT, |
| 348 |
prefix => $prefix, |
| 349 |
unsafe_printable_strings => $unsafe_printable_strings, |
| 350 |
unsafe => $unsafe, |
| 351 |
max_depth => $max_depth); |
| 352 |
} |
| 353 |
|
| 354 |
=head1 SEE ALSO |
| 355 |
|
| 356 |
L<virt-win-reg(1)>, |
| 357 |
L<Win::Hivex::Regedit(3)>, |
| 358 |
L<Win::Hivex(3)>, |
| 359 |
L<hivexsh(1)>, |
| 360 |
L<dos2unix(1)>, |
| 361 |
L<unix2dos(1)>, |
| 362 |
L<iconv(1)>, |
| 363 |
L<http://libguestfs.org/>. |
| 364 |
|
| 365 |
=head1 AUTHOR |
| 366 |
|
| 367 |
Richard W.M. Jones L<http://people.redhat.com/~rjones/> |
| 368 |
|
| 369 |
=head1 COPYRIGHT |
| 370 |
|
| 371 |
Copyright (C) 2010 Red Hat Inc. |
| 372 |
|
| 373 |
This program is free software; you can redistribute it and/or modify |
| 374 |
it under the terms of the GNU General Public License as published by |
| 375 |
the Free Software Foundation; either version 2 of the License, or |
| 376 |
(at your option) any later version. |
| 377 |
|
| 378 |
This program is distributed in the hope that it will be useful, |
| 379 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 380 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 381 |
GNU General Public License for more details. |
| 382 |
|
| 383 |
You should have received a copy of the GNU General Public License |
| 384 |
along with this program; if not, write to the Free Software |
| 385 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |