summaryrefslogtreecommitdiffstats
path: root/tools/virt-win-reg
blob: 16688f4717e5d70609af1470e45735c9002d7270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/perl -w
# virt-win-reg
# Copyright (C) 2010 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use warnings;
use strict;

use Sys::Guestfs;
use Sys::Guestfs::Lib qw(open_guest get_partitions resolve_windows_path
  inspect_all_partitions inspect_partition
  inspect_operating_systems mount_operating_system);
use Win::Hivex;
use Win::Hivex::Regedit qw(reg_import reg_export);

use Pod::Usage;
use Getopt::Long;
use File::Temp qw/tempdir/;
use Locale::TextDomain 'libguestfs';

=encoding utf8

=head1 NAME

virt-win-reg - Export and merge Windows Registry entries from a Windows guest

=head1 SYNOPSIS

 virt-win-reg domname 'HKLM\Path\To\Subkey'

 virt-win-reg domname 'HKLM\Path\To\Subkey' name

 virt-win-reg domname 'HKLM\Path\To\Subkey' @

 virt-win-reg --merge domname [input.reg ...]

 virt-win-reg [--options] disk.img ... # instead of domname

=head1 WARNING

You must I<not> use C<virt-win-reg> with the C<--merge> option on live
virtual machines.  If you do this, you I<will> get irreversible disk
corruption in the VM.  C<virt-win-reg> tries to stop you from doing
this, but doesn't catch all cases.

Modifying the Windows Registry is an inherently risky operation.  The format
is deliberately obscure and undocumented, and Registry changes
can leave the system unbootable.  Therefore when using the C<--merge>
option, make sure you have a reliable backup first.

=head1 DESCRIPTION

This program can export and merge Windows Registry entries from a
Windows guest.

The first parameter is the libvirt guest name or the raw disk image of
a Windows guest.

If C<--merge> is I<not> specified, then the chosen registry
key is displayed/exported (recursively).  For example:

 $ virt-win-reg Windows7 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft'

You can also display single values from within registry keys,
for example:

 $ cvkey='HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
 $ virt-win-reg Windows7 $cvkey ProductName
 Windows 7 Enterprise

With C<--merge>, you can merge a textual regedit file into
the Windows Registry:

 $ virt-win-reg --merge Windows7 changes.reg

=head2 SUPPORTED SYSTEMS

The program currently supports Windows NT-derived guests starting with
Windows XP through to at least Windows 7.

Registry support is done for C<HKEY_LOCAL_MACHINE\SAM>,
C<HKEY_LOCAL_MACHINE\SECURITY>, C<HKEY_LOCAL_MACHINE\SOFTWARE>,
C<HKEY_LOCAL_MACHINE\SYSTEM> and C<HKEY_USERS\.DEFAULT>.

You can use C<HKLM> as a shorthand for C<HKEY_LOCAL_MACHINE>, and
C<HKU> for C<HKEY_USERS>.

C<HKEY_USERS\$SID> and C<HKEY_CURRENT_USER> are B<not> supported at
this time.

=head2 NOTE

This program is only meant for simple access to the registry.  If you
want to do complicated things with the registry, we suggest you
download the Registry hive files from the guest using L<libguestfs(3)>
or L<guestfish(1)> and access them locally, eg. using L<hivex(3)>,
L<hivexsh(1)> or L<hivexregedit(1)>.

=head2 ENCODING

C<virt-win-reg> expects that regedit files have already been reencoded
in the local encoding.  Usually on Linux hosts, this means UTF-8 with
Unix-style line endings.  Since Windows regedit files are often in
UTF-16LE with Windows-style line endings, you may need to reencode the
whole file before or after processing.

To reencode a file from Windows format to Linux (before processing it
with the C<--merge> option), you would do something like this:

 iconv -f utf-16le -t utf-8 < win.reg | dos2unix > linux.reg

To go in the opposite direction, after exporting and before sending
the file to a Windows user, do something like this:

 unix2dos linux.reg | iconv -f utf-8 -t utf-16le > win.reg

For more information about encoding, see L<Win::Hivex::Regedit(3)>.

If you are unsure about the current encoding, use the L<file(1)>
command.  Recent versions of Windows regedit.exe produce a UTF-16LE
file with Windows-style (CRLF) line endings, like this:

 $ file software.reg
 software.reg: Little-endian UTF-16 Unicode text, with very long lines,
 with CRLF line terminators

This file would need conversion before you could C<--merge> it.

=head2 SHELL QUOTING

Be careful when passing parameters containing C<\> (backslash) in the
shell.  Usually you will have to use 'single quotes' or double
backslashes (but not both) to protect them from the shell.

Paths and value names are case-insensitive.

=head2 CurrentControlSet etc.

Registry keys like C<CurrentControlSet> don't really exist in the
Windows Registry at the level of the hive file, and therefore you
cannot modify these.

C<CurrentControlSet> is usually an alias for C<ControlSet001>.  In
some circumstances it might refer to another control set.  The way
to find out is to look at the C<HKLM\SYSTEM\Select> key:

 # virt-win-reg WindowsGuest 'HKLM\SYSTEM\Select'
 [HKEY_LOCAL_MACHINE\SYSTEM\Select]
 "Current"=dword:00000001
 "Default"=dword:00000001
 "Failed"=dword:00000000
 "LastKnownGood"=dword:00000002

"Default" is the one which Windows will choose when it boots.

Similarly, other C<Current...> keys in the path may need to
be replaced.

=head1 OPTIONS

=over 4

=cut

my $help;

=item B<--help>

Display brief help.

=cut

my $version;

=item B<--version>

Display version number and exit.

=cut

my $debug;

=item B<--debug>

Enable debugging messages.

=cut

my $uri;

=item B<--connect URI> | B<-c URI>

If using libvirt, connect to the given I<URI>.  If omitted, then we
connect to the default libvirt hypervisor.

If you specify guest block devices directly, then libvirt is not used
at all.

=cut

my $merge;

=item B<--merge>

In merge mode, this merges a textual regedit file into the Windows
Registry of the virtual machine.  If this flag is I<not> given then
virt-win-reg displays or exports Registry entries instead.

Note that C<--merge> is I<unsafe> to use on live virtual machines, and
will result in disk corruption.  However exporting (without this flag)
is always safe.

=cut

my $encoding;

=item B<--encoding> UTF-16LE|ASCII

When merging (only), you may need to specify the encoding for strings
to be used in the hive file.  This is explained in detail in
L<Win::Hivex::Regedit(3)/ENCODING STRINGS>.

The default is to use UTF-16LE, which should work with recent versions
of Windows.

=back

=cut

GetOptions ("help|?" => \$help,
            "version" => \$version,
            "connect|c=s" => \$uri,
            "debug|d" => \$debug,
            "merge" => \$merge,
            "encoding=s" => \$encoding,
    ) or pod2usage (2);
pod2usage (1) if $help;
if ($version) {
    my $g = Sys::Guestfs->new ();
    my %h = $g->version ();
    print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
    exit
}

# virt-win-reg only takes a single disk image ...
die __"no libvirt domain name or disk image given\n" if @ARGV == 0;
my $domname_or_image = shift @ARGV;

warn "launching libguestfs ..." if $debug;

my @lib_args = ([$domname_or_image]);
push @lib_args, address => $uri if $uri;
push @lib_args, rw => 1 if $merge;
my $g = open_guest (@lib_args);
$g->launch ();

warn "inspecting guest ..." if $debug;

# List of possible filesystems.
my @partitions = get_partitions ($g);

# Now query each one to build up a picture of what's in it.
my %fses =
    inspect_all_partitions ($g, \@partitions,
      use_windows_registry => 0);

my $oses = inspect_operating_systems ($g, \%fses);

my @roots = keys %$oses;
die __"multiboot operating systems are not supported by virt-win-reg" if @roots > 1;
my $root_dev = $roots[0];

my $os = $oses->{$root_dev};
my $ro = $merge ? 0 : 1;
mount_operating_system ($g, $os, $ro);

# Create a working directory to store the downloaded registry files.
my $tmpdir = tempdir (CLEANUP => 1);

# Only used when merging to map downloaded hive names to hive handles.
my %hives;

if (!$merge) {                  # Export mode.
    die __"expecting 1 or 2 more parameters, subkey path and optionally the value to export\n"
        if @ARGV < 1 || @ARGV > 2;

    my $path = shift @ARGV;
    my $name = shift @ARGV; # or undef

    # Map this to the hive name.  This function dies on failure.
    my ($hivename, $prefix);
    ($hivename, $path, $prefix) = map_path_to_hive ($path);

    # Download the chosen hive.
    download_hive ($hivename);

    # Open it.
    my $h = Win::Hivex->open ("$tmpdir/$hivename", debug => $debug);

    unless ($name) {
        # Export it.
        warn "exporting $path from $hivename with prefix $prefix ..." if $debug;
        reg_export ($h, $path, \*STDOUT, prefix => $prefix);
    } else {
        # Export a single key using hivexget.
        my @args = ("hivexget", "$tmpdir/$hivename", $path, $name);
        warn "running ", join (" ", @args), " ..." if $debug;
        system (@args) == 0 or die "hivexget failed: $?"
    }
}
else {                          # Import mode.
    if (@ARGV == 0) {
        reg_import (\*STDIN, \&import_mapper, encoding => $encoding);
    } else {
        foreach (@ARGV) {
            open my $fh, $_ or die "open: $_: $!";
            reg_import ($fh, \&import_mapper, encoding => $encoding);
        }
    }

    # Now we've done importing, commit all the hive handles and
    # close them all.
    $_->commit (undef) foreach values %hives;
    %hives = ();

    # Look in the tmpdir for all the hive files which have been
    # downloaded / modified by the import mapper, and upload
    # each one.
    opendir my $dh, $tmpdir or die "$tmpdir: $!";
    foreach (readdir $dh) {
        unless (/^\./) {
            upload_hive ($_)
        }
    }

    # Sync everything.
    $g->umount_all ();
    $g->sync ();
}

exit 0;

# map function passed to reg_import.
sub import_mapper
{
    local $_ = shift;

    my ($hivename, $path, $prefix) = map_path_to_hive ($_);

    # Need to download this hive?
    unless (-f "$tmpdir/$hivename") {
        download_hive ($hivename);

        my $h = Win::Hivex->open ("$tmpdir/$hivename",
                                  write => 1, debug => $debug);
        $hives{$hivename} = $h;
    }

    return ($hives{$hivename}, $path);
}

# Given a path, map that to the name of the hive and the true path
# within that hive.
sub map_path_to_hive
{
    local $_ = shift;
    my ($hivename, $prefix);

    if (/^\\?(?:HKEY_LOCAL_MACHINE|HKLM)\\SAM(\\.*)?$/i) {
        $hivename = "sam";
        $_ = defined $1 ? $1 : "\\";
        $prefix = "HKEY_LOCAL_MACHINE\\SAM";
    }
    elsif (/^\\?(?:HKEY_LOCAL_MACHINE|HKLM)\\SECURITY(\\.*)?$/i) {
        $hivename = "security";
        $_ = defined $1 ? $1 : "\\";
        $prefix = "HKEY_LOCAL_MACHINE\\SECURITY";
    }
    elsif (/^\\?(?:HKEY_LOCAL_MACHINE|HKLM)\\SOFTWARE(\\.*)?$/i) {
        $hivename = "software";
        $_ = defined $1 ? $1 : "\\";
        $prefix = "HKEY_LOCAL_MACHINE\\SOFTWARE";
    }
    elsif (/^\\?(?:HKEY_LOCAL_MACHINE|HKLM)\\SYSTEM(\\.*)?$/i) {
        $hivename = "system";
        $_ = defined $1 ? $1 : "\\";
        $prefix = "HKEY_LOCAL_MACHINE\\SYSTEM";
    }
    elsif (/^\\?(?:HKEY_USERS|HKU)\\.DEFAULT(\\.*)?$/i) {
        $hivename = "default";
        $_ = defined $1 ? $1 : "\\";
        $prefix = "HKEY_LOCAL_MACHINE\\.DEFAULT";
    }
    else {
        die __x("virt-win-reg: {p}: not a supported Windows Registry path\n",
                p => $_)
    }

    return ($hivename, $_, $prefix);
}

# Download a named hive file.  Die on failure.
sub download_hive
{
    local $_;
    my $hivename = shift;

    my $systemroot = $os->{root}->{systemroot} || "/windows";
    my $winfile_before = "$systemroot/system32/config/$hivename";
    my $winfile;
    eval { $winfile = $g->case_sensitive_path ($winfile_before); };
    if ($@) {
        die __x("virt-win-reg: {p}: file not found in guest: {err}\n",
                p => $winfile_before, err => $@);
    }

    warn "downloading $winfile ..." if $debug;
    eval { $g->download ($winfile, "$tmpdir/$hivename"); };
    if ($@) {
        die __x("virt-win-reg: {p}: could not download registry file: {err}\n",
                p => $winfile, err => $@);
    }
}

# Upload a named hive file.  Die on failure.
sub upload_hive
{
    local $_;
    my $hivename = shift;

    my $systemroot = $os->{root}->{systemroot} || "/windows";
    my $winfile_before = "$systemroot/system32/config/$hivename";
    my $winfile;
    eval { $winfile = $g->case_sensitive_path ($winfile_before); };
    if ($@) {
        die __x("virt-win-reg: {p}: file not found in guest: {err}\n",
                p => $winfile_before, err => $@);
    }

    warn "uploading $winfile ..." if $debug;
    eval { $g->upload ("$tmpdir/$hivename", $winfile); };
    if ($@) {
        die __x("virt-win-reg: {p}: could not upload registry file: {err}\n",
                p => $winfile, err => $@);
    }
}

=head1 SHELL QUOTING

Libvirt guest names can contain arbitrary characters, some of which
have meaning to the shell such as C<#> and space.  You may need to
quote or escape these characters on the command line.  See the shell
manual page L<sh(1)> for details.

=head1 SEE ALSO

L<hivex(3)>,
L<hivexsh(1)>,
L<hivexregedit(1)>,
L<guestfs(3)>,
L<guestfish(1)>,
L<virt-cat(1)>,
L<Sys::Guestfs(3)>,
L<Sys::Guestfs::Lib(3)>,
L<Win::Hivex(3)>,
L<Win::Hivex::Regedit(3)>,
L<Sys::Virt(3)>,
L<http://libguestfs.org/>.

=head1 BUGS

When reporting bugs, please enable debugging and capture the
I<complete> output:

 export LIBGUESTFS_DEBUG=1
 virt-win-reg --debug [... rest ...] > /tmp/virt-win-reg.log 2>&1

Attach /tmp/virt-win-reg.log to a new bug report at
L<https://bugzilla.redhat.com/>

=head1 AUTHOR

Richard W.M. Jones L<http://people.redhat.com/~rjones/>

=head1 COPYRIGHT

Copyright (C) 2010 Red Hat Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.