blib/lib/Monitoring/Sneck/Checks/checksums.pm

filter: Statementstmt 100% 86 / 86 Branchbran 100% 34 / 34 Conditioncond 87.5% 21 / 24 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 96.5% 166 / 172 CC 32 SCAR 22.9
CC
32
Cov
98%
check
17.0
validate
7.0
_parse
6.0
CRAP 9.9
« blib/lib/Monitoring/Sneck/Checks/dns.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_content.pm »
1 package Monitoring::Sneck::Checks::checksums;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::checksums - checks files against stored checksums
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 # Algorithms, by the length of their hex digests.
22 3 our %BY_LENGTH = ( 32 => 'md5', 40 => 'sha1', 56 => 'sha224', 64 => 'sha256', 96 => 'sha384', 128 => 'sha512' );
23  
24 =head1 SYNOPSIS
25  
26 !setuid_sums||perl:checksums -l /usr/local/etc/sneck/setuid.sha256
27 !sshd_sum||perl:checksums -F /usr/sbin/sshd=5ea6737a5debfd2195a09cb910d8c0e8d6fa3dda458b6de2ac400a17febd276a
28  
29 To make a list, on Linux or FreeBSD.
30  
31 sha256sum /usr/bin/sudo /usr/bin/passwd > /usr/local/etc/sneck/setuid.sha256
32 sha256 /usr/local/bin/sudo /usr/bin/passwd > /usr/local/etc/sneck/setuid.sha256
33  
34 =head1 DESCRIPTION
35  
36 Hashes each file and compares it against what is stored for it, through
37 Digest::MD5 and Digest::SHA. A file that differs, is not there, or can
38 not be read is CRITICAL. A list that can not be read, a line in one
39 that is not understood, or nothing to check, is UNKNOWN.
40  
41 Lists may be in the format of sha256sum and the like, "hash path", or
42 that of FreeBSD's sha256 and the like, and sha256sum --tag,
43 "SHA256 (path) = hash". Blank lines and those starting with # are
44 skipped. md5, sha1, sha224, sha256, sha384, and sha512 are known by the
45 length of the hash.
46  
47 Anyone who can change the files can most likely change the list too,
48 so keep it somewhere they can not, and check it with
49 L<Monitoring::Sneck::Checks::file_mode>.
50  
51 =head1 OPTIONS
52  
53 - -l, --list :: A checksum list. May be given more than once.
54  
55 - -F, --file :: "path=hash", a file and its hash. May be given more
56 than once.
57  
58 One of --list or --file must be given.
59  
60 =cut
61  
62 8 ▶sub option_spec { return ( 'list|l=s@', 'file|F=s@' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
63  
64 8 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod documented
65 8 my ( $self, $options ) = @_;
66  
67 8 my @problems;
68 8 ▶ foreach my $file ( @{ $options->{file} // [] } ) {
Condition100%2/2
$options->{"file"} // []
l!l
35
MC/DC100%1/1
$options->{"file"} // []
$options->{"file"}
Truth table100%2/2
A
A= $options->{"file"}
Aresult
11
00
69 3 my ( $path, $hash ) = $file =~ /\A(.+)=([0-9A-Fa-f]+)\z/;
70 3 ▶ push( @problems, '--file "' . $file . '" is not path=hash' ) if !defined($hash) || !$BY_LENGTH{ length($hash) };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash and $Monitoring::Sneck::Checks::checksums::BY_LENGTH{length $hash} 1 2 3
Condition66%2/3
defined $hash and $Monitoring::Sneck::Checks::checksums::BY_LENGTH{length $hash}
!ll&&!rl&&r
012
MC/DC50%1/2
defined $hash and $Monitoring::Sneck::Checks::checksums::BY_LENGTH{length $hash}
defined $hash $Monitoring::Sneck::Checks::checksums::BY_LENGTH{length $hash}
Truth table66%2/3
A and B
A= defined $hashB= $Monitoring::Sneck::Checks::checksums::BY_LENGTH{length $hash}
ABresult
0X0
100
111
71 }
72 push( @problems, 'nothing to check, give --list or --file' )
73 8 ▶ if !defined( $options->{list} ) && !defined( $options->{file} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"list"} or defined $options->{"file"} 1 7 8
Condition100%3/3
defined $options->{"list"} or defined $options->{"file"}
l!l&&r!l&&!r
521
MC/DC100%2/2
defined $options->{"list"} or defined $options->{"file"}
defined $options->{"list"} defined $options->{"file"}
Truth table100%3/3
A or B
A= defined $options->{"list"}B= defined $options->{"file"}
ABresult
1X1
011
000
74  
75 8 return @problems;
76 } ## end sub validate
77  
78 =head1 METHODS
79  
80 =head2 check
81  
82 Does the check.
83  
84 =cut
85  
86 6 ▶sub check {
Subroutine check: called CC=17 SCAR=28.3
Pod documented
87 6 my ($self) = @_;
88  
89 6 my @entries;
90 my @unknown;
91 6 ▶ foreach my $list ( @{ $self->opt('list') // [] } ) {
Condition100%2/2
$self->opt("list") // []
l!l
51
MC/DC100%1/1
$self->opt("list") // []
$self->opt("list")
Truth table100%2/2
A
A= $self->opt("list")
Aresult
11
00
92 6 my $content = read_file( $list, err_mode => 'quiet' );
93 6 ▶ if ( !defined($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 1 5 6
94 1 push( @unknown, 'can not read ' . $list . ', ' . $! );
95 1 next;
96 }
97 5 my $line_number = 0;
98 5 foreach my $line ( split( /\r?\n/, $content ) ) {
99 12 $line_number++;
100 12 ▶ next if $line =~ /\A\s*(?:#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*(?:#|\z)/u 4 8 12
101 8 my $entry = _parse($line);
102 8 ▶ if ( !defined($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 2 6 8
103 2 push( @unknown, $list . ' line ' . $line_number . ' is not understood' );
104 2 next;
105 }
106 6 push( @entries, $entry );
107 } ## end foreach my $line ( split( /\r?\n/, $content ) )
108 } ## end foreach my $list ( @{ $self->opt('list') // [] ...})
109 6 ▶ foreach my $file ( @{ $self->opt('file') // [] } ) {
Condition100%2/2
$self->opt("file") // []
l!l
24
MC/DC100%1/1
$self->opt("file") // []
$self->opt("file")
Truth table100%2/2
A
A= $self->opt("file")
Aresult
11
00
110 2 my ( $path, $hash ) = $file =~ /\A(.+)=([0-9A-Fa-f]+)\z/;
111 2 push( @entries, { path => $path, hash => lc($hash), algorithm => $BY_LENGTH{ length($hash) } } );
112 }
113  
114 6 my @problems;
115 6 my $matched = 0;
116 6 foreach my $entry (@entries) {
117 8 my $hash = eval { $self->hash_file( $entry->{path}, $entry->{algorithm} ) };
118 8 ▶ if ( !defined($hash) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $hash) { } 2 6 8
elsif ($hash ne $entry->{'hash'}) { } 1 5 6
119 2 my $why = $@;
120 2 $why =~ s/\s+\z//;
121 2 push( @problems, $why );
122 } elsif ( $hash ne $entry->{hash} ) {
123 1 push( @problems, $entry->{path} . ' does not match its ' . $entry->{algorithm} );
124 } else {
125 5 $matched++;
126 }
127 } ## end foreach my $entry (@entries)
128  
129 6 ▶ my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK );
Branch: 1 branch
BranchTrueFalseTotal
defined $problems[0] ? : 2 4 6
130 6 ▶ push( @codes, $self->UNKNOWN ) if defined( $unknown[0] ) || !defined( $entries[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown[0] or not defined $entries[0] 3 3 6
Condition66%2/3
defined $unknown[0] or not defined $entries[0]
l!l&&r!l&&!r
303
MC/DC50%1/2
defined $unknown[0] or not defined $entries[0]
defined $unknown[0] not defined $entries[0]
Truth table66%2/3
A or not(B)
A= defined $unknown[0]B= not defined $entries[0]
ABresult
1X1
011
000
131 6 $self->perfdata( label => 'matched', value => $matched, min => 0, max => scalar(@entries) );
132 6 my $message = $matched . ' of ' . scalar(@entries) . ' files match';
133 6 ▶ $message = $message . ', ' . scalar(@problems) . ' do not' if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 2 4 6
134 6 ▶ $message = $message . ', ' . join( ', ', @unknown ) if defined( $unknown[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown[0] 3 3 6
135  
136 6 return $self->finish( $self->worst(@codes), $message, @problems );
137 } ## end sub check
138  
139 =head2 hash_file
140  
141 Returns the hex digest of a file. Dies if it can not be read.
142  
143 - path :: The file.
144  
145 - algorithm :: md5, sha1, sha224, sha256, sha384, or sha512.
146  
147 my $hash = $check->hash_file( '/usr/sbin/sshd', 'sha256' );
148  
149 =cut
150  
151 8 ▶sub hash_file {
Subroutine hash_file: called CC=5 SCAR=16.1
Pod documented
152 8 my ( $self, $path, $algorithm ) = @_;
153  
154 8 ▶ die( $path . ' is not a file' . "\n" ) if -e $path && !-f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path and not -f $path 1 7 8
Condition100%3/3
-e $path and not -f $path
!ll&&!rl&&r
161
MC/DC100%2/2
-e $path and not -f $path
-e $path not -f $path
Truth table100%3/3
A and not(B)
A= -e $pathB= not -f $path
ABresult
0X0
100
111
155 7 ▶ open( my $fh, '<:raw', $path ) or die( 'can not read ' . $path . ', ' . $! . "\n" );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die 'can not read ' . $path . ', ' . $! . "\n" 6 1 7
156 6 my $digest;
157 6 ▶ if ( $algorithm eq 'md5' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algorithm eq 'md5') { } 1 5 6
158 1 require Digest::MD5;
159 1 $digest = Digest::MD5->new;
160 } else {
161 5 require Digest::SHA;
162 5 $digest = Digest::SHA->new( $algorithm =~ s/\Asha//r );
163 }
164 6 $digest->addfile($fh);
165 6 close($fh);
166  
167 6 return $digest->hexdigest;
168 } ## end sub hash_file
169  
170 # Parses a line of a checksum list.
171 #
172 # Args...
173 #
174 # - line :: The line, such as 'e3b0...b855 /etc/motd' or
175 # 'SHA256 (/etc/motd) = e3b0...b855'.
176 #
177 # Returns a hash ref of path, hash, lower cased, and algorithm, or undef
178 # if it is not understood.
179 #
180 # Example...
181 #
182 # _parse('SHA256 (/etc/motd) = E3B0...B855');
183 # # { path => '/etc/motd', hash => 'e3b0...b855', algorithm => 'sha256' }
184 8 ▶sub _parse {
Subroutine _parse: called CC=6 SCAR=17.9
185 8 my ($line) = @_;
186  
187 8 my ( $tag, $path, $hash );
188 8 ▶ if ( $line =~ /\A([A-Z0-9]+) \((.+)\) = ([0-9A-Fa-f]+)\s*\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /\A([A-Z0-9]+) \((.+)\) = ([0-9A-Fa-f]+)\s*\z/u) { } 3 5 8
elsif ($line =~ /\A([0-9A-Fa-f]+) [ *](.+?)\s*\z/u) { } 4 1 5
189 3 ( $tag, $path, $hash ) = ( lc($1), $2, $3 );
190 } elsif ( $line =~ /\A([0-9A-Fa-f]+) [ *](.+?)\s*\z/ ) {
191 4 ( $hash, $path ) = ( $1, $2 );
192 } else {
193 1 return undef;
194 }
195 7 my $algorithm = $BY_LENGTH{ length($hash) };
196 7 ▶ return undef if !defined($algorithm) || ( defined($tag) && $tag ne $algorithm );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $algorithm or defined $tag and $tag ne $algorithm 1 6 7
Condition83%5/6
not defined $algorithm or defined $tag and $tag ne $algorithm
l!l&&r!l&&!r
016
defined $tag and $tag ne $algorithm
!ll&&!rl&&r
421
MC/DC66%2/3
not defined $algorithm or defined $tag and $tag ne $algorithm
not defined $algorithm defined $tag $tag ne $algorithm
Truth table75%3/4
not(A) or B and C
A= not defined $algorithmB= defined $tagC= $tag ne $algorithm
ABCresult
1XX1
0111
00X0
0100
197  
198 6 return { path => $path, hash => lc($hash), algorithm => $algorithm };
199 } ## end sub _parse
200  
201 3 1;
« blib/lib/Monitoring/Sneck/Checks/dns.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/file_content.pm »