| 1 | package Monitoring::Sneck::Checks::auditd; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Digest::SHA qw(sha256_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Monitoring::Sneck::Checks::auditd - checks Linux auditing is on, with the rules it should have | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 22 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Where auditctl is. Set by tests. Undef means the first of AUDITCTL_PATHS | ||||||||||||||||||||||||||||
| 23 | # that is there. | ||||||||||||||||||||||||||||
| 24 | 22 | our $AUDITCTL; | |||||||||||||||||||||||||||
| 25 | 22 | our @AUDITCTL_PATHS = ( '/sbin/auditctl', '/usr/sbin/auditctl' ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | auditd|perl:auditd | ||||||||||||||||||||||||||||
| 30 | auditd_cis|perl:auditd -I | ||||||||||||||||||||||||||||
| 31 | auditd_hash|perl:auditd -H 3b8d6f0e5a2c4b19d7e8f6a5c3b2a1908f7e6d5c4b3a29180f7e6d5c4b3a2918 | ||||||||||||||||||||||||||||
| 32 | auditd_rules|perl:auditd -r /etc/sneck/audit.rules.loaded | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Checks auditd is running, auditing is enabled, and what rules are | ||||||||||||||||||||||||||||
| 37 | loaded, through auditctl -s and auditctl -l. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | - CRITICAL :: auditd not running. Auditing disabled. Rules missing from, | ||||||||||||||||||||||||||||
| 40 | or not in, --rules-file. The rules not matching --hash. Lost events | ||||||||||||||||||||||||||||
| 41 | over --lost-critical. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | - WARNING :: The rules not immutable, with --immutable. Lost events | ||||||||||||||||||||||||||||
| 44 | over --lost-warning. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - UNKNOWN :: Not being Linux, auditctl not being there, or failing, as | ||||||||||||||||||||||||||||
| 47 | it does when not run as root. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | The file for --rules-file is as auditctl -l prints them, so can be made | ||||||||||||||||||||||||||||
| 50 | with auditctl -l > $file once the rules are as wanted. Blank lines and | ||||||||||||||||||||||||||||
| 51 | those starting with # are skipped. Order is not compared. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | The hash for --hash is the SHA-256 of what auditctl -l prints, the | ||||||||||||||||||||||||||||
| 54 | same as auditctl -l | sha256sum gives. The current one is on a line | ||||||||||||||||||||||||||||
| 55 | after the status line, so it can be taken from a run on a host with the | ||||||||||||||||||||||||||||
| 56 | rules as wanted. Use --rules-file instead to see which rules differ. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Lost is the events the kernel dropped since boot, such as when the | ||||||||||||||||||||||||||||
| 59 | backlog filled. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | - -r, --rules-file :: A file of the rules there must be, and no others. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | - -H, --hash :: The SHA-256 the rules must have, in hex. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | - -I, --immutable :: WARNING if the rules are not locked with -e 2. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | - --lost-warning :: Range for lost events. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | - --lost-critical :: Range for lost events. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 128 | ▶ | sub option_spec { | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 128 | return ( 'rules-file|r=s', 'hash|H=s', 'immutable|I', 'lost-warning=s', 'lost-critical=s' ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 128 | ▶ | sub ranges { return ( 'lost-warning', 'lost-critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 128 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 128 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | return ('--hash must be a SHA-256, 64 hex digits') | ||||||||||||||||||||||||||||
| 85 | 128 | ▶ | if defined( $options->{hash} ) && $options->{hash} !~ /\A[0-9a-fA-F]{64}\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"hash"} and not $options->{"hash"} =~ /\A[0-9a-fA-F]{64}\z/u
MC/DC100%2/2
defined $options->{"hash"} and not $options->{"hash"} =~ /\A[0-9a-fA-F]{64}\z/u
defined $options->{"hash"}
not $options->{"hash"} =~ /\A[0-9a-fA-F]{64}\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{"hash"}B= not $options->{"hash"} =~ /\A[0-9a-fA-F]{64}\z/u
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 120 | return (); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 METHODS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 check | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Does the check. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 112 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=28 SCAR=34.7
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 112 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 112 | ▶ | die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $auditctl = $AUDITCTL // ( grep { -x $_ } @AUDITCTL_PATHS )[0]; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Monitoring::Sneck::Checks::auditd::AUDITCTL // (grep {-x $_;} @Monitoring::Sneck::Checks::auditd::AUDITCTL_PATHS)[0]
MC/DC0%0/2
$Monitoring::Sneck::Checks::auditd::AUDITCTL // (grep {-x $_;} @Monitoring::Sneck::Checks::auditd::AUDITCTL_PATHS)[0]
$Monitoring::Sneck::Checks::auditd::AUDITCTL
(grep {-x $_;} @Monitoring::Sneck::Checks::auditd::AUDITCTL_PATHS)[0]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::auditd::AUDITCTLB= (grep {-x $_;} @Monitoring::Sneck::Checks::auditd::AUDITCTL_PATHS)[0]
| |||||||||||||||||||||||||||||
| 103 | 111 | ▶ | return $self->finish( $self->UNKNOWN, 'auditctl is not there' ) if !defined($auditctl); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 111 | ▶ | return $self->finish( $self->UNKNOWN, $auditctl . ' is not there' ) if !-x $auditctl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # lines such as "enabled 1", "pid 1234", and "lost 0" | ||||||||||||||||||||||||||||
| 107 | 111 | my ( $status_exit, $status_output ) = $self->run_command( $auditctl, '-s' ); | |||||||||||||||||||||||||||
| 108 | 101 | my %status; | |||||||||||||||||||||||||||
| 109 | 101 | foreach my $line ( split( /\n/, $status_output ) ) { | |||||||||||||||||||||||||||
| 110 | 983 | ▶ | $status{$1} = $2 if $line =~ /\A(\S+) (\S+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 101 | ▶ | if ( $status_exit || !defined( $status{enabled} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status_exit or not defined $status{"enabled"}
MC/DC50%1/2
$status_exit or not defined $status{"enabled"}
$status_exit
not defined $status{"enabled"}
Truth table66%2/3
A or not(B)
A= $status_exitB= not defined $status{"enabled"}
| |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | my $first = ( grep { /\S/ } split( /\n/, $status_output ) )[0] // 'saying nothing'; | ||||||||||||||||||||||||||
|
Condition50%1/2
(grep {/\S/u;} split(/\n/u, $status_output, 0))[0] // "saying nothing"
MC/DC0%0/1
(grep {/\S/u;} split(/\n/u, $status_output, 0))[0] // "saying nothing"
(grep {/\S/u;} split(/\n/u, $status_output, 0))[0]
Truth table50%1/2
A
A= (grep {/\S/u;} split(/\n/u, $status_output, 0))[0]
| |||||||||||||||||||||||||||||
| 114 | 3 | return $self->finish( $self->UNKNOWN, 'auditctl -s exited ' . $status_exit . ', ' . $first ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 98 | my ( $rules_exit, $rules_output ) = $self->run_command( $auditctl, '-l' ); | |||||||||||||||||||||||||||
| 117 | 89 | ▶ | if ($rules_exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $first = ( grep { /\S/ } split( /\n/, $rules_output ) )[0] // 'saying nothing'; | ||||||||||||||||||||||||||
|
Condition0%0/2
(grep {/\S/u;} split(/\n/u, $rules_output, 0))[0] // "saying nothing"
MC/DC0%0/1
(grep {/\S/u;} split(/\n/u, $rules_output, 0))[0] // "saying nothing"
(grep {/\S/u;} split(/\n/u, $rules_output, 0))[0]
Truth table0%0/2
A
A= (grep {/\S/u;} split(/\n/u, $rules_output, 0))[0]
| |||||||||||||||||||||||||||||
| 119 | 0 | return $self->finish( $self->UNKNOWN, 'auditctl -l exited ' . $rules_exit . ', ' . $first ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 89 | ▶ | my @rules = grep { /\S/ && $_ ne 'No rules' } split( /\n/, $rules_output ); | ||||||||||||||||||||||||||
|
Condition66%2/3
/\S/u && $_ ne 'No rules'
MC/DC50%1/2
/\S/u && $_ ne 'No rules'
/\S/u
$_ ne 'No rules'
Truth table66%2/3
A && B
A= /\S/uB= $_ ne 'No rules'
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 89 | my @codes = ( $self->OK ); | |||||||||||||||||||||||||||
| 124 | 89 | my @parts; | |||||||||||||||||||||||||||
| 125 | my @details; | ||||||||||||||||||||||||||||
| 126 | 89 | ▶ | if ( !$status{pid} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 4 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 128 | 4 | push( @parts, 'auditd not running' ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | # 0 is disabled, 1 enabled, and 2 enabled with the rules locked until reboot | ||||||||||||||||||||||||||||
| 131 | 89 | ▶ | if ( $status{enabled} eq '0' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | 4 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 133 | 4 | push( @parts, 'auditing disabled' ); | |||||||||||||||||||||||||||
| 134 | } elsif ( $status{enabled} eq '2' ) { | ||||||||||||||||||||||||||||
| 135 | 6 | push( @parts, 'auditing enabled and immutable' ); | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 79 | push( @parts, 'auditing enabled' ); | |||||||||||||||||||||||||||
| 138 | 79 | ▶ | if ( $self->opt('immutable') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 16 | push( @codes, $self->WARNING ); | |||||||||||||||||||||||||||
| 140 | 16 | push( @parts, 'rules not immutable' ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 89 | ▶ | push( @parts, scalar(@rules) . ' rule' . ( @rules == 1 ? '' : 's' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 89 | ▶ | if ( defined( $self->opt('rules-file') ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 15 | my $wanted_text = read_file( $self->opt('rules-file'), err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 148 | 15 | ▶ | return $self->finish( $self->UNKNOWN, 'can not read ' . $self->opt('rules-file') . ', ' . $! ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | if !defined($wanted_text); | ||||||||||||||||||||||||||||
| 150 | 14 | ▶ | my @wanted = grep { /\S/ && !/\A\s*#/ } split( /\n/, $wanted_text ); | ||||||||||||||||||||||||||
|
Condition66%2/3
/\S/u && !/\A\s*#/u
MC/DC50%1/2
/\S/u && !/\A\s*#/u
/\S/u
!/\A\s*#/u
Truth table66%2/3
A && not(B)
A= /\S/uB= !/\A\s*#/u
| |||||||||||||||||||||||||||||
| 151 | 14 | my %loaded = map { $_ => 1 } @rules; | |||||||||||||||||||||||||||
| 152 | 14 | my %wanted = map { $_ => 1 } @wanted; | |||||||||||||||||||||||||||
| 153 | 14 | my @missing = grep { !$loaded{$_} } @wanted; | |||||||||||||||||||||||||||
| 154 | 14 | my @unexpected = grep { !$wanted{$_} } @rules; | |||||||||||||||||||||||||||
| 155 | 14 | ▶ | if ( @missing || @unexpected ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@missing or @unexpected
MC/DC0%0/2
@missing or @unexpected
@missing
@unexpected
Truth table33%1/3
A or B
A= @missingB= @unexpected
| |||||||||||||||||||||||||||||
| 156 | 14 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 157 | 14 | push( @parts, scalar(@missing) . ' missing, ' . scalar(@unexpected) . ' unexpected' ); | |||||||||||||||||||||||||||
| 158 | 14 | push( @details, map { 'missing: ' . $_ } @missing ); | |||||||||||||||||||||||||||
| 159 | 14 | push( @details, map { 'unexpected: ' . $_ } @unexpected ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } ## end if ( defined( $self->opt('rules-file') ) ) | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # of the output as is, so it matches auditctl -l | sha256sum | ||||||||||||||||||||||||||||
| 164 | 88 | my $hash = sha256_hex($rules_output); | |||||||||||||||||||||||||||
| 165 | 88 | ▶ | if ( defined( $self->opt('hash') ) && lc( $self->opt('hash') ) ne $hash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("hash") and lc $self->opt("hash") ne $hash
MC/DC100%2/2
defined $self->opt("hash") and lc $self->opt("hash") ne $hash
defined $self->opt("hash")
lc $self->opt("hash") ne $hash
Truth table100%3/3
A and B
A= defined $self->opt("hash")B= lc $self->opt("hash") ne $hash
| |||||||||||||||||||||||||||||
| 166 | 8 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 167 | 8 | push( @parts, 'rules hash differs' ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 88 | push( @details, 'rules sha256 ' . $hash ); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 88 | ▶ | my $lost = $status{lost} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$status{"lost"} // 0
MC/DC0%0/1
$status{"lost"} // 0
$status{"lost"}
Truth table50%1/2
A
A= $status{"lost"}
| |||||||||||||||||||||||||||||
| 172 | 88 | my $lost_code = $self->threshold_status( $lost, $self->opt('lost-warning'), $self->opt('lost-critical') ); | |||||||||||||||||||||||||||
| 173 | 88 | push( @codes, $lost_code ); | |||||||||||||||||||||||||||
| 174 | 88 | ▶ | push( @parts, $lost . ' lost' ) if $lost || $lost_code != $self->OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lost or $lost_code != $self->OK
MC/DC50%1/2
$lost or $lost_code != $self->OK
$lost
$lost_code != $self->OK
Truth table66%2/3
A or B
A= $lostB= $lost_code != $self->OK
| |||||||||||||||||||||||||||||
| 175 | 88 | $self->perfdata( label => 'rules', value => scalar(@rules), min => 0 ); | |||||||||||||||||||||||||||
| 176 | 88 | $self->perfdata( | |||||||||||||||||||||||||||
| 177 | label => 'lost', | ||||||||||||||||||||||||||||
| 178 | value => $lost, | ||||||||||||||||||||||||||||
| 179 | uom => 'c', | ||||||||||||||||||||||||||||
| 180 | warning => $self->opt('lost-warning'), | ||||||||||||||||||||||||||||
| 181 | critical => $self->opt('lost-critical'), | ||||||||||||||||||||||||||||
| 182 | min => 0, | ||||||||||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 88 | return $self->finish( $self->worst(@codes), join( ', ', @parts ), @details ); | |||||||||||||||||||||||||||
| 186 | } ## end sub check | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 22 | 1; | |||||||||||||||||||||||||||