lib/Parse/Win32Registry/WinNT/File.pm

filter: Statementstmt 91.9% 159 / 173 Branchbran 75.0% 36 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.3% 28 / 31 Podpod 0.0% 0 / 7 totaltotal 86.1% 223 / 259 CC 27 SCAR 14.9
CC
27
Cov
88%
_dump_security_cache
12.0
get_virtual_root_key
9.1
new
7.0
CRAP 4.4
« lib/Parse/Win32Registry/Win95/Key.pm ↑ Summary lib/Parse/Win32Registry/Win95/File.pm »
1 package Parse::Win32Registry::WinNT::File;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use base qw(Parse::Win32Registry::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Parse::Win32Registry::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Parse::Win32Registry::WinNT::Key;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 ▶use constant REGF_HEADER_LENGTH => 0x200;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use constant OFFSET_TO_FIRST_HBIN => 0x1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod undocumented
18 40 my $class = shift;
19 40 ▶ my $filename = shift or croak "No filename specified";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or croak("No filename specified") 39 1 40
20  
21 39 ▶ open my $fh, '<', $filename or croak "Unable to open '$filename': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("Unable to open '${filename}': $!") 38 1 39
22  
23 # 0x00 dword = 'regf' signature
24 # 0x04 dword = seq1
25 # 0x08 dword = seq2
26 # 0x0c qword = timestamp
27 # 0x14 dword = major version
28 # 0x18 dword = minor version
29 # 0x1c dword = type (0 = registry file, 1 = log file)
30 # 0x20 dword = (1)
31 # 0x24 dword = offset to root key
32 # 0x28 dword = total length of all hbins (excludes header)
33 # 0x2c dword = (1)
34 # 0x30 = embedded filename
35  
36 # Extracted offsets are always relative to first hbin
37  
38 38 my $bytes_read = sysread($fh, my $regf_header, REGF_HEADER_LENGTH);
39 38 ▶ if ($bytes_read != REGF_HEADER_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 512) 1 37 38
40 1 warnf('Could not read registry file header');
41 1 return;
42 }
43  
44 37 my ($regf_sig,
45 $seq1,
46 $seq2,
47 $timestamp,
48 $major_version,
49 $minor_version,
50 $type,
51 $offset_to_root_key,
52 $total_hbin_length,
53 $embedded_filename,
54 ) = unpack('a4VVa8VVVx4VVx4a64', $regf_header);
55  
56 37 $offset_to_root_key += OFFSET_TO_FIRST_HBIN;
57  
58 37 ▶ if ($regf_sig ne 'regf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($regf_sig ne "regf") 1 36 37
59 1 warnf('Invalid registry file signature');
60 1 return;
61 }
62  
63 36 $embedded_filename = unpack('Z*', decode('UCS-2LE', $embedded_filename));
64  
65 # The header checksum is the xor of the first 127 dwords.
66 # The checksum is stored in the 128th dword, at offset 0x1fc (508).
67 36 my $checksum = 0;
68 36 foreach my $x (unpack('V127', $regf_header)) {
69 4572 $checksum ^= $x;
70 }
71 36 my $embedded_checksum = unpack('x508V', $regf_header);
72 36 ▶ if ($checksum != $embedded_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($checksum != $embedded_checksum) 1 35 36
73 1 warnf('Invalid checksum for registry file header');
74 }
75  
76 36 my $self = {};
77 36 $self->{_filehandle} = $fh;
78 36 $self->{_filename} = $filename;
79 36 $self->{_length} = (stat $fh)[7];
80 36 $self->{_offset_to_root_key} = $offset_to_root_key;
81 36 $self->{_timestamp} = unpack_windows_time($timestamp);
82 36 $self->{_embedded_filename} = $embedded_filename;
83 36 $self->{_seq1} = $seq1;
84 36 $self->{_seq2} = $seq2;
85 36 $self->{_version} = "$major_version.$minor_version";
86 36 $self->{_type} = $type;
87 36 $self->{_total_hbin_length} = $total_hbin_length;
88 36 $self->{_embedded_checksum} = $embedded_checksum;
89 36 $self->{_security_cache} = {}; # comment out to disable cache
90 36 bless $self, $class;
91  
92 36 return $self;
93 }
94  
95 14 ▶sub get_root_key {
Subroutine get_root_key: called CC=1 SCAR=0.0
Pod undocumented
96 14 my $self = shift;
97  
98 14 my $offset_to_root_key = $self->{_offset_to_root_key};
99  
100 14 my $root_key = Parse::Win32Registry::WinNT::Key->new($self,
101 $offset_to_root_key);
102 14 return $root_key;
103 }
104  
105 6 ▶sub get_virtual_root_key {
Subroutine get_virtual_root_key: called CC=9 SCAR=22.1
Pod undocumented
106 6 my $self = shift;
107 6 my $fake_root = shift;
108  
109 6 my $root_key = $self->get_root_key;
110 6 ▶ return if !defined $root_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root_key 0 6 6
111  
112 6 ▶ if (!defined $fake_root) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fake_root) 6 0 6
113 # guess virtual root from filename
114 6 my $filename = basename $self->{_filename};
115  
116 6 ▶ if ($filename =~ /NTUSER/i) {
Branch: 6 branches
BranchTrueFalseTotal
if ($filename =~ /NTUSER/i) { } 1 5 6
elsif ($filename =~ /USRCLASS/i) { } 1 4 5
elsif ($filename =~ /SOFTWARE/i) { } 1 3 4
elsif ($filename =~ /SYSTEM/i) { } 1 2 3
elsif ($filename =~ /SAM/i) { } 1 1 2
elsif ($filename =~ /SECURITY/i) { } 1 0 1
117 1 $fake_root = 'HKEY_CURRENT_USER';
118 }
119 elsif ($filename =~ /USRCLASS/i) {
120 1 $fake_root = 'HKEY_CLASSES_ROOT';
121 }
122 elsif ($filename =~ /SOFTWARE/i) {
123 1 $fake_root = 'HKEY_LOCAL_MACHINE\SOFTWARE';
124 }
125 elsif ($filename =~ /SYSTEM/i) {
126 1 $fake_root = 'HKEY_LOCAL_MACHINE\SYSTEM';
127 }
128 elsif ($filename =~ /SAM/i) {
129 1 $fake_root = 'HKEY_LOCAL_MACHINE\SAM';
130 }
131 elsif ($filename =~ /SECURITY/i) {
132 1 $fake_root = 'HKEY_LOCAL_MACHINE\SECURITY';
133 }
134 else {
135 0 $fake_root = 'HKEY_UNKNOWN';
136 }
137 }
138  
139 6 $root_key->{_name} = $fake_root;
140 6 $root_key->{_key_path} = $fake_root;
141  
142 6 return $root_key;
143 }
144  
145 1 ▶sub get_timestamp {
Subroutine get_timestamp: called CC=1 SCAR=0.0
Pod undocumented
146 1 my $self = shift;
147  
148 1 return $self->{_timestamp};
149 }
150  
151 1 ▶sub get_timestamp_as_string {
Subroutine get_timestamp_as_string: called CC=1 SCAR=0.0
Pod undocumented
152 1 my $self = shift;
153  
154 1 return iso8601($self->{_timestamp});
155 }
156  
157 2 ▶sub get_embedded_filename {
Subroutine get_embedded_filename: called CC=1 SCAR=0.0
Pod undocumented
158 2 my $self = shift;
159  
160 2 return $self->{_embedded_filename};
161 }
162  
163 2 ▶sub get_block_iterator {
Subroutine get_block_iterator: called CC=1 SCAR=0.1
Pod undocumented
164 2 my $self = shift;
165  
166 2 my $offset_to_next_hbin = OFFSET_TO_FIRST_HBIN;
167 2 my $end_of_file = $self->{_length};
168  
169 4 ▶ return Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
170 4 ▶ if ($offset_to_next_hbin > $end_of_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_next_hbin > $end_of_file) 0 4 4
171 0 return; # no more hbins
172 }
173 4 ▶ if (my $hbin = Parse::Win32Registry::WinNT::Hbin->new($self,
Branch: 1 branch
BranchTrueFalseTotal
if (my $hbin = 'Parse::Win32Registry::WinNT::Hbin'->new($self, $offset_to_next_hbin)) { } 2 2 4
174 $offset_to_next_hbin))
175 {
176 2 ▶ return unless $hbin->get_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $hbin->get_length > 0 0 2 2
177 2 $offset_to_next_hbin += $hbin->get_length;
178 2 return $hbin;
179 }
180 else {
181 2 return; # no more hbins
182 }
183 2 });
184 }
185  
186 13 *get_hbin_iterator = \&get_block_iterator;
187  
188 0 ▶sub _dump_security_cache {
Subroutine _dump_security_cache: not called CC=3 SCAR=24.8
189 0 my $self = shift;
190  
191 0 ▶ if (defined(my $cache = $self->{_security_cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cache = $self->{"_security_cache"})) 0 0 0
192 0 foreach my $offset (sort { $a <=> $b } keys %$cache) {
193 0 my $security = $cache->{$offset};
194 0 printf '0x%x %s\n', $offset, $security->as_string;
195 }
196 }
197 }
198  
199  
200 package Parse::Win32Registry::WinNT::Hbin;
201  
202 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
204  
205 13 ▶use base qw(Parse::Win32Registry::Entry);
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 13 ▶use Parse::Win32Registry::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
209 13 ▶use Parse::Win32Registry::WinNT::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 13 ▶use constant HBIN_HEADER_LENGTH => 0x20;
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
214 4 my $class = shift;
215 4 my $regfile = shift;
216 4 my $offset = shift;
217  
218 4 ▶ croak 'Missing registry file' if !defined $regfile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regfile 0 4 4
219 4 ▶ croak 'Missing offset' if !defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 4 4
220  
221 4 my $fh = $regfile->get_filehandle;
222  
223 # 0x00 dword = 'hbin' signature
224 # 0x04 dword = offset from first hbin to this hbin
225 # 0x08 dword = length of this hbin / relative offset to next hbin
226 # 0x14 qword = timestamp (first hbin only)
227  
228 # Extracted offsets are always relative to first hbin
229  
230 4 sysseek($fh, $offset, 0);
231 4 my $bytes_read = sysread($fh, my $hbin_header, HBIN_HEADER_LENGTH);
232 4 ▶ if ($bytes_read != HBIN_HEADER_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read != 32) 2 2 4
233 2 return;
234 }
235  
236 2 my ($sig,
237 $offset_to_hbin,
238 $length,
239 $timestamp) = unpack('a4VVx8a8x4', $hbin_header);
240  
241 2 ▶ if ($sig ne 'hbin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig ne "hbin") 0 2 2
242 0 return;
243 }
244  
245 2 my $self = {};
246 2 $self->{_regfile} = $regfile;
247 2 $self->{_offset} = $offset;
248 2 $self->{_length} = $length;
249 2 $self->{_header_length} = HBIN_HEADER_LENGTH;
250 2 $self->{_allocated} = 1;
251 2 $self->{_tag} = $sig;
252 2 $self->{_timestamp} = unpack_windows_time($timestamp);
253 2 bless $self, $class;
254  
255 2 return $self;
256 }
257  
258 0 ▶sub get_timestamp {
Subroutine get_timestamp: not called CC=1 SCAR=6.9
259 0 my $self = shift;
260  
261 0 return $self->{_timestamp};
262 }
263  
264 0 ▶sub get_timestamp_as_string {
Subroutine get_timestamp_as_string: not called CC=1 SCAR=6.9
265 0 my $self = shift;
266  
267 0 return iso8601($self->{_timestamp});
268 }
269  
270 2 ▶sub get_entry_iterator {
Subroutine get_entry_iterator: called CC=1 SCAR=0.0
271 2 my $self = shift;
272  
273 2 my $regfile = $self->{_regfile};
274 2 my $offset = $self->{_offset};
275 2 my $length = $self->{_length};
276  
277 2 my $offset_to_next_entry = $offset + HBIN_HEADER_LENGTH;
278 2 my $end_of_hbin = $offset + $length;
279  
280 72 ▶ return Parse::Win32Registry::Iterator->new(sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
281 72 ▶ if ($offset_to_next_entry >= $end_of_hbin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_to_next_entry >= $end_of_hbin) 2 70 72
282 2 return; # no more entries
283 }
284 70 ▶ if (my $entry = Parse::Win32Registry::WinNT::Entry->new($regfile,
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = 'Parse::Win32Registry::WinNT::Entry'->new($regfile, $offset_to_next_entry)) { } 70 0 70
285 $offset_to_next_entry))
286 {
287 70 ▶ return unless $entry->get_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->get_length > 0 0 70 70
288 70 $offset_to_next_entry += $entry->get_length;
289 70 return $entry;
290 }
291 else {
292 0 return; # no more entries
293 }
294 2 });
295 }
296  
297 13 1;
« lib/Parse/Win32Registry/Win95/Key.pm ↑ Summary lib/Parse/Win32Registry/Win95/File.pm »