2 # Copyright (C) 2010 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
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 use Win::Hivex::Regedit qw(reg_import reg_export);
31 hivexregedit - Merge and export Registry changes from regedit-format files.
35 hivexregedit --merge [--prefix prefix] [--encoding enc] \
38 hivexregedit --export [--prefix prefix] hivefile key > regfile
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)>.
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.
50 C<--export> exports a Registry key (recursively) into the regedit format.
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.
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:
63 iconv -f utf-16le -t utf-8 < win.reg | dos2unix > linux.reg
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:
68 unix2dos linux.reg | iconv -f utf-8 -t utf-16le > win.reg
70 For more information about encoding, see L<Win::Hivex::Regedit(3)>.
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:
77 software.reg: Little-endian UTF-16 Unicode text, with very long lines,
78 with CRLF line terminators
80 This file would need conversion before you could C<--merge> it.
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.
88 =head2 CurrentControlSet etc.
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. Replace this with C<ControlSet001>, and
93 similarly for other C<Current...> keys.
97 $ virt-cat WindowsGuest /Windows/System32/config/software > software.hive
98 $ hivexregedit --export \
99 --prefix 'HKEY_LOCAL_MACHINE\SOFTWARE' \
100 software.hive '\Microsoft' > ms-keys.reg
102 $ hivexregedit --merge system.hive \
103 --prefix 'HKEY_LOCAL_MACHINE\SYSTEM' additions.reg
123 Enable debugging in the hivex library. This is useful for diagnosing
124 bugs and also malformed hive files.
132 hivexregedit --merge [--prefix prefix] [--encoding enc] \
135 Merge C<regfile> (a regedit-format text file) into the hive
136 C<hivefile>. If C<regfile> is omitted, then the program reads from
137 standard input. (Also you can give multiple input files).
139 C<--prefix> specifies the Windows Registry prefix. It is almost
140 always necessary to use this when dealing with real hive files.
142 C<--encoding> specifies the encoding for unmarked strings in the
143 input. It defaults to C<UTF-16LE> which should work for recent
144 versions of Windows. Another possibility is to use C<ASCII>.
152 hivexregedit --export [--prefix prefix] hivefile key > regfile
154 C<key> is a path within the hive C<hivefile>. (The key should not
155 contain any prefix and should be quoted to defend backslashes from the
156 shell). The key is exported, recursively, to standard output in the
157 textual regedit format.
159 C<--prefix> specifies the Windows Registry prefix. It is almost
160 always necessary to use this when dealing with real hive files.
166 =item B<--prefix> prefix
168 Hive files and Windows Registry key names are indirectly related. For
169 example, inside the software hive, all keys are stored relative to
170 C<HKEY_LOCAL_MACHINE\SOFTWARE>. Thus
171 C<HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft> appears in the hive file as
174 The hive format itself does not store this prefix, so you have to
175 supply it based on outside knowledge. (L<virt-win-reg(1)>, amongst
176 other things, already knows about this).
178 Usually it is sufficient to pass the parameter
179 C<--prefix 'HKEY_LOCAL_MACHINE\SOFTWARE'> or similar when doing
186 =item B<--encoding> UTF-16LE|ASCII
188 When merging (only), you may need to specify the encoding for strings
189 to be used in the hive file. This is explained in detail in
190 L<Win::Hivex::Regedit(3)/ENCODING STRINGS>.
192 The default is to use UTF-16LE, which should work with recent versions
199 GetOptions ("help|?" => \$help,
201 "merge|import" => \$merge,
202 "export" => \$export,
203 "prefix=s" => \$prefix,
204 "encoding=s" => \$encoding,
206 pod2usage (1) if $help;
208 if ($merge && $export) {
209 die "hivexregedit: cannot use --merge and --export at the same time\n"
212 unless ($merge || $export) {
213 die "hivexregedit: use --merge or --export, see the manpage for help\n"
216 if ($export && defined $encoding) {
217 die "hivexregedit: --encoding has no effect when used with --export\n"
220 if ($merge) { # --merge (reg_import)
222 die "hivexregedit --merge hivefile [input.reg ...]\n"
225 my $hivefile = shift @ARGV;
227 my $h = Win::Hivex->open ($hivefile, write => 1, debug => $debug);
229 # Read from stdin unless other files have been specified.
230 unshift (@ARGV, '-') unless @ARGV;
233 open FILE, $_ or die "$_: $!";
234 reg_import (\*FILE, sub {
236 # Remove prefix from the start of the path, matching
237 # case insensitively.
238 if (defined $prefix) {
239 my $len = length $prefix;
240 if (length $_ >= $len &&
241 lc (substr ($_, 0, $len)) eq lc ($prefix)) {
242 $_ = substr ($_, $len);
250 } else { # --export (reg_export)
252 die "hivexregedit --export hivefile key\n"
255 my $hivefile = shift @ARGV;
256 my $key = shift @ARGV;
258 my $h = Win::Hivex->open ($hivefile, debug => $debug);
260 print "Windows Registry Editor Version 5.00\n\n";
262 reg_export ($h, $key, \*STDOUT, prefix => $prefix);
268 L<Win::Hivex::Regedit(3)>,
274 L<http://libguestfs.org/>.
278 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
282 Copyright (C) 2010 Red Hat Inc.
284 This program is free software; you can redistribute it and/or modify
285 it under the terms of the GNU General Public License as published by
286 the Free Software Foundation; either version 2 of the License, or
287 (at your option) any later version.
289 This program is distributed in the hope that it will be useful,
290 but WITHOUT ANY WARRANTY; without even the implied warranty of
291 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
292 GNU General Public License for more details.
294 You should have received a copy of the GNU General Public License
295 along with this program; if not, write to the Free Software
296 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.