|
NAMEParse::Win32Registry - Parse Windows Registry Files SYNOPSIS use strict;
use Parse::Win32Registry qw( :REG_
unpack_windows_time
unpack_unicode_string );
my $filename = shift or die "Filename?";
my $registry = Parse::Win32Registry->new($filename)
or die "'$filename' is not a registry file\n";
my $root_key = $registry->get_root_key
or die "Could not get root key of '$filename'\n";
# The following code works on USER.DAT or NTUSER.DAT files
my $software_key = $root_key->get_subkey(".DEFAULT\\Software")
|| $root_key->get_subkey("Software");
if (defined($software_key)) {
my @user_key_names = (
"Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
"Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
);
foreach my $name (@user_key_names) {
if (my $key = $software_key->get_subkey($name)) {
print "\n", $key->as_string, "\n";
foreach my $value ($key->get_list_of_values) {
print $value->as_string, "\n";
}
}
}
# This demonstrates how you can deal with a binary value
# that contains a Unicode string
foreach my $ver (qw(8.0 9.0 10.0 11.0)) {
my $key_name = "Microsoft\\Office\\$ver\\Common\\UserInfo";
if (my $key = $software_key->get_subkey($key_name)) {
print "\n", $key->as_string, "\n";
my @value_names = qw(UserName UserInitials Company);
foreach my $value_name (@value_names) {
if (my $value = $key->get_value($value_name)) {
print $value->as_string, "\n";
my $data = $value->get_data;
my $string = unpack_unicode_string($data);
print "$value_name = '$string'\n";
}
}
}
}
}
# The following code works on SYSTEM.DAT or SOFTWARE files
my $software_key = $root_key->get_subkey("Software") || $root_key;
if (defined($software_key)) {
my @software_key_names = (
"Microsoft\\Windows\\CurrentVersion",
"Microsoft\\Windows NT\\CurrentVersion",
);
foreach my $name (@software_key_names) {
if (my $key = $software_key->get_subkey($name)) {
print "\n", $key->as_string, "\n";
foreach my $value ($key->get_list_of_values) {
print $value->as_string, "\n";
}
}
}
# This demonstrates how you can deal with a Unix date
# found in a registry value
my $key_name = "Microsoft\\Windows NT\\CurrentVersion";
if (my $curver_key = $software_key->get_subkey($key_name)) {
print "\n", $curver_key->as_string, "\n";
if (my $value = $curver_key->get_value("InstallDate")) {
print $value->as_string, "\n";
my $time = $value->get_data;
print "InstallDate = ",
scalar gmtime $time, " GMT\n";
print "InstallDate = ",
scalar localtime $time, " Local\n";
}
}
}
# The following code works on SYSTEM.DAT or SYSTEM files
my $system_key = $root_key->get_subkey("System") || $root_key;
my $ccs_name = "CurrentControlSet"; # default for Win95
if (my $key = $system_key->get_subkey("Select")) {
my $current_value = $key->get_value("Current");
$ccs_name = sprintf("ControlSet%03d", $current_value->get_data);
print "CurrentControlSet = $ccs_name\n";
}
my $ccs_key = $system_key->get_subkey($ccs_name);
if (defined($ccs_key)) {
my @system_key_names = (
"Control\\ComputerName\\ComputerName",
"Control\\TimeZoneInformation",
);
foreach my $name (@system_key_names) {
if (my $key = $ccs_key->get_subkey($name)) {
print "\n", $key->as_string, "\n";
foreach my $value ($key->get_list_of_values) {
print $value->as_string, "\n";
}
}
}
# This demonstrates how you can deal with a Windows date
# found in a registry value
my $key_name = "Control\\Windows";
if (my $windows_key = $ccs_key->get_subkey($key_name)) {
print "\n", $windows_key->as_string, "\n";
if (my $value = $windows_key->get_value("ShutdownTime")) {
print $value->as_string, "\n";
my $data = $value->get_data;
my $time = unpack_windows_time($data);
print "ShutdownTime = ",
scalar gmtime $time, " GMT\n";
print "ShutdownTime = ",
scalar localtime $time, " Local\n";
}
}
}
DESCRIPTIONParse::Win32Registry is a module for parsing Windows Registry files, allowing you to read the keys and values of a registry file without going through the Windows API. It provides an object-oriented interface to the keys and values in a registry file. Registry files are structured as trees of keys, with each key containing further subkeys or values. The module is intended to be cross-platform, and run on those platforms where Perl will run. It supports both Windows NT registry files (Windows NT, 2000, XP, 2003, Vista, 7) and Windows 95 registry files (Windows 95, 98, Millennium Edition). It is intended to be used to parse offline registry files. If a registry file is currently in use, you will not be able to open it. However, you can save part or all of a currently loaded registry file using the Windows reg command if you have the appropriate administrative access. DEPENDENCIESRequires Perl 5.8.1. All required modules are standard modules. METHODSStart by creating a Registry object from a valid registry file. Use the Registry object's get_root_key method to obtain the root key of that registry file. This root key is your first Key object. From this key, you can explore the Key and Value objects that comprise the registry file using the methods described below. Data is read directly from a registry file when a Key or Value object is created, and discarded when the Key or Value object is destroyed. This avoids any delay in parsing an entire registry file to obtain a Key or Value object as most code only looks at a subset of the keys and values contained in a registry file. Registry Object Methods
Key Object Methods
Value Object Methods
Security Object MethodsOnly Windows NT registry files contain security information to control access to the registry keys. This information is stored in security entries which are distributed through the registry file separately from the keys that they apply to. This allows the registry to share security information amongst a large number of keys whilst unnecessary duplication. Security entries link to other security entries in a circular chain, each entry linking to the one that precedes it and the one that follows it.
Security Descriptor Object MethodsA Security Descriptor object represents a security descriptor which contains an owner SID, a primary group SID, a System ACL, and a Discretionary ACL.
ACL Object MethodsAn ACL object represents an Access Control List, which comprises a list of Access Control Entries.
ACE Object MethodsAn ACE object represents an Access Control Entry. An ACE describes the permissions assigned (the access mask) to a Security Identifier (the trustee).
SID Object MethodsA SID object represents a Security Identifier.
EXPORTSConstantsOn request, Parse::Win32Registry will export the registry type constants: use Parse::Win32Registry qw( :REG_ ); The :REG_ tag exports all of the following constants: REG_NONE
REG_SZ
REG_EXPAND_SZ
REG_BINARY
REG_DWORD
REG_DWORD_BIG_ENDIAN
REG_LINK
REG_MULTI_SZ
REG_RESOURCE_LIST
REG_FULL_RESOURCE_DESCRIPTOR
REG_RESOURCE_REQUIREMENTS_LIST
REG_QWORD
You can import individual types by specifying them, for example: use Parse::Win32Registry qw( REG_SZ REG_DWORD ); SUPPORT FUNCTIONSParse::Win32Registry provides a number of support functions, which are exported on request. All of the support functions can be imported with: use Parse::Win32Registry qw( :functions ); Unpacking Binary DataThere are a number of functions for assisting in unpacking binary data found in registry values. These functions are exported on request: use Parse::Win32Registry qw( unpack_windows_time
unpack_unicode_string
unpack_sid
unpack_ace
unpack_acl
unpack_security_descriptor );
These unpack functions also return the length of the packed object when called in a list context. For example, to extract one SID: my $sid = unpack_sid($data); To extract a series of SIDs: my $pos = 0;
while ($pos < length($data)) {
my ($sid, $packed_len) = unpack_sid(substr($data, $pos));
last if !defined $sid; # abort if SID not defined
# ...do something with $sid...
$pos += $packed_len; # move past the packed SID
}
Formatting DataThese functions are exported on request: use Parse::Win32Registry qw( iso8601 hexdump );
Processing Multiple Registry Files SimultaneouslyThere are three support functions that create iterators for simultaneously processing the keys and values of multiple registry files. These functions are exported on request: use Parse::Win32Registry qw( make_multiple_subkey_iterator
make_multiple_value_iterator
make_multiple_subtree_iterator );
Handling lists of subkeys or values should be done with a little care as some of the processed registry files might not contain the subkey or value being examined and the list will contain missing entries: ($key1, $key2, undef, $key4) One way of handling this is to use map to check that a key is defined and return undef if the subkey or value is not present. @subkeys = map { defined $_ && $_->get_subkey('subkey') || undef } @keys;
@values = map { defined $_ && $_->get_value('value') || undef } @keys;
Comparing Keys and ValuesThese functions are exported on request: use Parse::Win32Registry qw( compare_multiple_keys
compare_multiple_values );
HANDLING INVALID DATAThe Parse::Win32Registry module will skip keys or values that cannot be successfully parsed. If keys or values cannot be parsed, then the get_subkey and get_value methods of Key objects will return nothing. The get_list_of_subkeys and get_list_of_values methods of Key objects will skip those keys or values that cannot be parsed. If none of the keys or values can be parsed successfully, an empty list will be returned. Additionally, values (in Windows NT registry files) often store data in a separate area of the registry file. If the value can be parsed, but the data cannot, a Value object will be created, but it will have no data. The get_data method will return nothing. The most robust way of handling keys or values or data is therefore to check that they are defined before processing them. For example: my $key = $root_key->get_subkey( "Software\\Perl" );
if ( defined $key ) {
print $key->as_string, "\n";
my $value = $key->get_value( "Version" );
if ( defined $value ) {
print $value->as_string, "\n";
my $data = $value->get_data;
if ( defined $data ) {
# process $data in some way...
}
}
}
You might not feel this robustness is necessary for your scripts. You can be alerted when there are problems parsing registry keys or values by switching on warnings with: Parse::Win32Registry->enable_warnings; They can be switched off again with: Parse::Win32Registry->disable_warnings; LOW-LEVEL METHODSThese methods are intended for those who want to look at the structure of a registry file, but with something a little more helpful than a hex editor. They are not designed for pulling data out of keys and values: they are designed to make it easier to look at the underlying components of a registry file. Windows NT registry files are composed of one or more Hbin blocks. Hbin blocks can contain a series of entries, such as key, value, and security entries, but also includes subkey lists, value lists, key class names, and value data. Windows 95 registry files are composed of an RGKN block, followed by one or more RGDB blocks. RGKN blocks contain the entries which link the registry keys in the form of a tree. RGDB blocks contain a corresponding entry for each key in the RGKN block. This RGDB entry includes the name of the key and any associated values. For convenience, when iterating the entries in an RGDB block, each will be returned as a key entry followed by zero or more value entries. To see demonstrations of how these methods can be used, look at the regscan.pl, gtkregscope.pl, and wxregscope.pl scripts. Registry Object Methods
Block Object Methods
Entry Object MethodsIn addition to the basic methods provided by all entries, if an entry is a key, value, or security entry, it will also provide the methods available to Key, Value, or Security objects. You might therefore find it useful to check what methods are available so that you can use them: # use Entry object methods...
...
if ($entry->can('get_subkey')) {
# use Key object methods...
}
elsif ($entry->can('get_data')) {
# use Value object methods...
}
elsif ($entry->can('get_security_descriptor')) {
# use Security object methods...
}
SCRIPTSAll of the supplied scripts are intended to be used either as tools or as examples for you to modify and develop. Try regdump.pl or regshell.pl to look at a registry file from the command line, or gtkregview.pl or wxregview.pl if you want a GUI. If you want to compare registry files, try regmultidiff.pl from the command line or gtkregcompare.pl or wxregcompare.pl if you want a GUI. You can edit the scripts to customize them for your own requirements. If you specify subkeys on the command line, note that you need to quote the subkey on Windows if it contains spaces: regdump.pl ntuser.dat "software\microsoft\windows nt" You will also need to quote backslashes and spaces in Unix shells: regdump.pl ntuser.dat software\\microsoft\\windows\ nt or use single quotes: regdump.pl ntuser.dat 'software\microsoft\windows nt' gtkregcompare.plgtkregcompare.pl is a GTK+ program for comparing multiple registry files. It displays a tree of the registry keys and values highlighting those that have changed. It requires Gtk2-Perl to be installed. Filenames of registry files to compare can be supplied on the command line: gtkregcompare.pl <filename1> <filename2> <filename3> ... You can of course use wildcards when running from a Unix shell. gtkregscope.plgtkregscope.pl is a GTK+ registry scanner. It presents all the entries in a registry file returned by the get_block_iterator and get_entry_iterator methods. It uses color to highlight key, value, security, and subkey list entries, and presents the block as a colored map. It requires Gtk2-Perl to be installed. A filename can also be supplied on the command line: gtkregscope.pl <filename> gtkregview.plgtkregview.pl is a GTK+ registry viewer. It displays a tree of registry keys on the left hand side, a list of values on the right, and a hex dump of the selected value data at the bottom. It requires Gtk2-Perl to be installed. A filename can also be supplied on the command line: gtkregview.pl <filename> regclassnames.plregclassnames.pl will display registry keys that have class names. Only a very few Windows NT registry key have class names. Type regclassnames.pl on its own to see the help: regclassnames.pl <filename> [subkey] regdump.plregdump.pl is used to display the keys and values of a registry file. Type regdump.pl on its own to see the help: regdump.pl <filename> [subkey] [-r] [-v] [-x] [-c] [-s] [-o]
-r or --recurse traverse all child keys from the root key
or the subkey specified
-v or --values display values
-x or --hexdump display value data as a hex dump
-c or --class-name display the class name for the key (if present)
-s or --security display the security information for the key,
including the owner and group SIDs,
and the system and discretionary ACLs (if present)
-o or --owner display the owner SID for the key (if present)
The contents of the root key will be displayed unless a subkey is specified. Paths to subkeys are always specified relative to the root key. By default, only the subkeys and values immediately underneath the specified key will be displayed. To display all keys and values beneath a key, use the -r or --recurse option. For example, regdump.pl ntuser.dat might display the following: $$$PROTO.HIV [2005-01-01T09:00:00Z]
..\AppEvents
..\Console
..\Control Panel
..\Environment
..\Identities
..\Keyboard Layout
..\Printers
..\Software
..\UNICODE Program Groups
From here, you can explore the subkeys to find those keys or values you are interested in: regdump.pl ntuser.dat software
regdump.pl ntuser.dat software\microsoft
...
regexport.plregexport.pl will display registry keys and values in the Windows Registry Editor Version 5.00 format used by REGEDIT on Windows 2000 and later. Type regexport.pl on its own to see the help: regexport.pl <filename> [subkey] [-r]
-r or --recurse traverse all child keys from the root key
or the subkey specified
Values are always shown for each key displayed. Subkeys are displayed as comments when not recursing. (Comments are preceded by the ';' character.) regfind.plregfind.pl is used to search the keys, values, data, or types of a registry file for a matching string. Type regfind.pl on its own to see the help: regfind.pl <filename> <search-string> [-k] [-v] [-d] [-t] [-x]
-k or --key search key names for a match
-v or --value search value names for a match
-d or --data search value data for a match
-t or --type search value types for a match
-x or --hexdump display value data as a hex dump
To search for the string "recent" in the names of any keys or values: regfind.pl ntuser.dat recent -kv To search for the string "administrator" in the data of any values: regfind.pl ntuser.dat administrator -d To list all REG_MULTI_SZ values: regfind.pl ntuser.dat -t multi_sz Search strings are not case-sensitive. regml.plregml.pl will display those keys with explicit System Mandatory Label ACEs set in the System ACL. This feature was introduced with Windows Vista, and is used by applications such as Internet Explorer running in Protected Mode. Note that if a key does not have an explicit System Mandatory Label ACE, it has Medium Integrity Level. Only Windows NT registry files can contain System Mandatory Label ACEs. Type regml.pl on its own to see the help: regml.pl <filename> regmultidiff.plregmultidiff.pl can be used to compare multiple registry files and identify the differences between them. Type regmultidiff.pl on its own to see the help: regmultidiff.pl <file1> <file2> <file3> ... [<subkey>] [-v] [-x] [-l] [-a]
-v or --values display values
-x or --hexdump display value data as a hex dump
-l or --long show each changed key or value instead of a summary
-a or --all show all keys and values before and after a change
You can limit the comparison by specifying an initial subkey. regscan.plregscan.pl dumps all the entries in a registry file. This will include defunct keys and values that are no longer part of the current active registry. Type regscan.pl on its own to see the help: regscan.pl <filename> [-k] [-v] [-s] [-a] [-p] [-u] [-w]
-k or --keys list only 'key' entries
-v or --values list only 'value' entries
-s or --security list only 'security' entries
-a or --allocated list only 'allocated' entries
-p or --parse-info show the technical information for an entry
instead of the string representation
-u or --unparsed show the unparsed on-disk entries as a hex dump
regsecurity.plregsecurity.pl will display the security information contained in a registry files. Only Windows NT registry files contain security information. Type regsecurity.pl on its own to see the help: regsecurity.pl <filename> regshell.plProvides an interactive command shell where you navigate through the keys using 'cd' to change the current key and 'ls' or 'dir' to list the contents of the current key. Tab completion of subkey and value names is available. Names containing spaces are supported by quoting names with " characters. Note that names are case sensitive. A filename should be supplied on the command line: regshell.pl <filename> Once regshell.pl is running, type help to see the available commands. It requires Term::ReadLine to be installed. regstats.plregstats.pl counts the number of keys and values in a registry file. It will also provide a count of each value type if requested. Type regstats.pl on its own to see the help: regstats.pl <filename> [-t]
-t or --types count value types
regtimeline.plregtimeline.pl displays keys and values in date order. As only Windows NT based registry keys provide timestamps, this script only works on Windows NT registry files. You can limit the display to a given number of days (counting back from the timestamp of the last key). Type regtimeline.pl on its own to see the help: regtimeline.pl <filename> [subkey] [-l <number>] [-v] [-x]
-l or --last display only the last <number> days
of registry activity
-v or --values display values
-x or --hexdump display value data as a hex dump
regtree.plregtree.pl simply displays the registry as an indented tree, optionally displaying the values of each key. Type regtree.pl on its own to see the help: regtree.pl <filename> [subkey] [-v]
-v or --values display values
wxregcompare.plwxregcompare.pl is a wxWidgets program for comparing multiple registry files. It displays a tree of the registry keys and values, highlighting those that have changed. It requires wxPerl to be installed. Filenames of registry files to compare can be supplied on the command line: wxregcompare.pl <filename1> <filename2> <filename3> ... You can of course use wildcards when running from a Unix shell. wxregscope.plwxregscope.pl is a wxWidgets registry scanner. It presents all the entries in a registry file returned by the get_block_iterator and get_entry_iterator methods. It uses color to highlight key, value, security, and subkey list entries. It requires wxPerl to be installed. A filename can also be supplied on the command line: wxregscope.pl <filename> wxregview.plwxregview.pl is a wxWidgets registry viewer. It displays a tree of registry keys on the left hand side, a list of values on the right, and a hex dump of the selected value data at the bottom. It can also provide a timeline view of all of the registry keys, which can be used to navigate the main tree view by clicking or double-clicking on a timeline key. It requires wxPerl to be installed. A filename can also be supplied on the command line: wxregview.pl <filename> ACKNOWLEDGEMENTSThis would not have been possible without the work of those people who have analysed and shared their knowledge of the structure of Windows Registry files, primarily: B.D. (WinReg.txt), Petter Nordahl-Hagen (chntpw), and Richard Sharpe and Jerry Carter (Samba 3). AUTHORJames Macfarlane, <jmacfarla@cpan.org> COPYRIGHT AND LICENSECopyright (C) 2006-2012 by James Macfarlane This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|