lib/eris/log/context/sshd.pm

filter: Statementstmt 40.5% 15 / 37 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 50.0% 1 / 2 totaltotal 29.6% 19 / 64 CC 12 SCAR 47.6
CC
12
Cov
26%
contextualize_message
156.0
sample_messages
2.0
BEGIN
1.0
CRAP 117.3
« lib/eris/log/context/postfix.pm ↑ Summary lib/eris/log/context/GeoIP.pm »
1 package eris::log::context::sshd;
2 # ABSTRACT: Parse sshd logs into structured data
3  
4 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 with qw(
8 eris::role::context
9 );
10  
11 1 our $VERSION = '0.008'; # VERSION
12  
13  
14 sub sample_messages {
15 0 ▶ my @msgs = split /\r?\n/, <<EOF;
Subroutine sample_messages: not called CC=1 SCAR=6.9
Pod undocumented
16 Jul 26 15:47:32 ether sshd[30700]: Accepted password for canuck from 2.82.66.219 port 54085 ssh2
17 Jul 26 15:47:32 ether sshd[30700]: pam_unix(sshd:session): session opened for user canuck by (uid=0)
18 Jul 26 15:50:14 ether sshd[4291]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=43.229.53.60 user=root
19 Jul 26 15:50:16 ether sshd[4291]: Failed password for root from 43.229.53.60 port 57806 ssh2
20 Jul 26 15:50:18 ether sshd[4291]: Failed password for root from 43.229.53.60 port 57806 ssh2
21 Jul 26 15:50:21 ether sshd[4291]: Failed password for root from 43.229.53.60 port 57806 ssh2
22 Jul 26 15:50:21 ether sshd[4292]: Disconnecting: Too many authentication failures for root
23 Jul 26 15:50:21 ether sshd[4291]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=43.229.53.60 user=root
24 Jul 26 15:50:22 ether sshd[4663]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=43.229.53.60 user=root
25 Jul 26 15:50:21 ether sshd[4291]: Invalid user trudy from 43.229.53.60
26 EOF
27 0 return @msgs;
28 }
29  
30  
31 1 const my %RE => (
32 extract_details => qr/(?:Accepted|Failed) (\S+) for (\S+) from (\S+) port (\S+) (\S+)/,
33 IPv4 => qr/\d{1,3}(?:\.\d{1,3}){3}/,
34 );
35 1 const my %F => (
36 extract_details => [qw(driver acct src_ip src_port proto_app)],
37 );
38 1 const my %SDATA => qw(
39 user acct
40 );
41  
42 sub contextualize_message {
43 0 ▶ my ($self,$log) = @_;
Subroutine contextualize_message: not called CC=12 SCAR=50.5
Pod documented
44 0 my $c = $log->context;
45 0 my $str = $c->{message};
46  
47 0 my %ctxt = ();
48 0 ▶ $ctxt{status} = $str =~ /Accepted/ ? 'success'
Branch: 2 branches
BranchTrueFalseTotal
$str =~ /Accepted/ ? : 0 0 0
$str =~ /Failed/ ? : 0 0 0
49 : $str =~ /Failed/ ? 'failure'
50 : undef;
51 0 ▶ if( defined $ctxt{status} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ctxt{'status'}) { } 0 0 0
elsif ($str =~ /Invalid/) { } 0 0 0
52 0 $ctxt{action} = 'authentication';
53 0 ▶ if( my @data = ($str =~ /(?>$RE{extract_details})/o) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@data) = $str =~ /(?>$RE{"extract_details"})/o) 0 0 0
54 0 @ctxt{@{ $F{extract_details} }} = @data;
55 }
56 }
57 elsif( $str =~ /Invalid/ ) {
58 0 $ctxt{status} = 'invalid';
59 0 @ctxt{qw(acct src_ip)} = ($str =~ /Invalid user (\S+) from (\S+)/);
60 }
61 else {
62 0 delete $ctxt{status};
63 }
64 0 ▶ if( exists $c->{sdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->{"sdata"}) 0 0 0
65 0 foreach my $k (keys %SDATA) {
66 0 ▶ $ctxt{$SDATA{$k}} = $c->{sdata}{$k} if exists $c->{sdata}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{"sdata"}{$k} 0 0 0
67 }
68 0 ▶ if( exists $c->{sdata}{rhost} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->{"sdata"}{"rhost"}) 0 0 0
69 0 ▶ my $k = $c->{sdata}{rhost} =~ /^$RE{IPv4}$/o ? 'src_ip' : 'src_host';
Branch: 1 branch
BranchTrueFalseTotal
$c->{'sdata'}{'rhost'} =~ /^$RE{'IPv4'}$/o ? : 0 0 0
70 0 $ctxt{$k} = $c->{sdata}{rhost};
71 }
72 }
73  
74 0 ▶ $log->add_context($self->name,\%ctxt) if keys %ctxt;
Branch: 1 branch
BranchTrueFalseTotal
if keys %ctxt 0 0 0
75 }
76  
77  
78 1 1;
79  
80 __END__
81  
82 =pod
83  
84 =encoding UTF-8
85  
86 =head1 NAME
87  
88 eris::log::context::sshd - Parse sshd logs into structured data
89  
90 =head1 VERSION
91  
92 version 0.008
93  
94 =head1 SYNOPSIS
95  
96 Parse sshd logs into structured data
97  
98 =head1 METHODS
99  
100 =head2 contextualize_message
101  
102 Parses an sshd log and extracts the relevant details
103  
104 action => authentication/..
105 status => succes/failure/invalid
106 driver => keyboard/password/public key
107 acct => user in question
108 proto_app => sshv2 / sshv1
109  
110 And
111  
112 src_ip, src_port
113  
114 =for Pod::Coverage sample_messages
115  
116 =head1 SEE ALSO
117  
118 L<eris::log::contextualizer>, L<eris::role::context>
119  
120 =head1 AUTHOR
121  
122 Brad Lhotsky <brad@divisionbyzero.net>
123  
124 =head1 COPYRIGHT AND LICENSE
125  
126 This software is Copyright (c) 2015 by Brad Lhotsky.
127  
128 This is free software, licensed under:
129  
130 The (three-clause) BSD License
131  
132 =cut
« lib/eris/log/context/postfix.pm ↑ Summary lib/eris/log/context/GeoIP.pm »