lib/eris/log/context/pfSense/filterlog.pm

filter: Statementstmt 33.3% 17 / 51 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 4 / 8 Podpod 50.0% 1 / 2 totaltotal 25.5% 22 / 86 CC 10 SCAR 41.8
CC
10
Cov
24%
contextualize_message
110.0
_build_parser
2.0
_build_matcher
2.0
CRAP 65.3
« lib/eris/role/pluggable.pm ↑ Summary script/eris-eris-client.pl »
1 package eris::log::context::pfSense::filterlog;
2 # ABSTRACT: Parse the pfSense filterlog
3  
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 with qw(
6 eris::role::context
7 );
8 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.008'; # VERSION
13  
14  
15 1 has 'parser' => (
16 is => 'ro',
17 isa => InstanceOf['Text::CSV_XS'],
18 lazy => 1,
19 builder => '_build_parser',
20 handles => {
21 csv_parse => 'parse',
22 csv_fields => 'fields',
23 },
24 );
25 sub _build_parser {
26 0 ▶ return Text::CSV_XS->new();
Subroutine _build_parser: not called CC=1 SCAR=6.9
27 }
28  
29  
30 0 ▶sub _build_matcher { 'filterlog' }
Subroutine _build_matcher: not called CC=1 SCAR=6.9
31  
32  
33 sub sample_messages {
34 0 ▶ my @msgs = split /\r?\n/, <<'EOF';
Subroutine sample_messages: not called CC=1 SCAR=6.9
Pod undocumented
35 2017-07-05T03:39:15+02:00 pfsense.home.db0.us filterlog: 99,16777216,,1770009028,igb1,match,pass,in,4,0x0,,64,55471,0,DF,17,udp,67,10.0.1.10,10.0.1.1,53515,53,47
36 2017-07-05T03:40:01+02:00 pfsense.home.db0.us filterlog: 5,16777216,,1000000103,igb0,match,block,in,4,0x0,,114,31734,0,none,17,udp,145,87.110.147.77,99.46.177.250,1024,26818,125
37 2017-07-05T03:40:38+02:00 pfsense.home.db0.us filterlog: 57,16777216,,11000,igb0,match,block,in,6,0x00,0x00000,64,UDP,17,158,fe80::de7f:a4ff:fe04:bac9,fe80::208:a2ff:fe0b:76ac,41442,546,158
38 EOF
39 0 return @msgs;
40 }
41  
42  
43 1 my %fields;
44  
45 sub contextualize_message {
46 0 ▶ my ($self,$log) = @_;
Subroutine contextualize_message: not called CC=10 SCAR=47.0
Pod documented
47 0 my $str = $log->context->{message};
48  
49 0 ▶ %fields = (
Branch: 1 branch
BranchTrueFalseTotal
unless keys %fields 0 0 0
50 base => [qw( rule_id subrule_id anchor rec_id dev proc action direction ipver )],
51 ipv4 => [qw( TOS ECN TTL id offset flags proto_id proto_app )],
52 ipv6 => [qw( class label TTL proto_app proto_id )],
53 ip => [qw(length src_ip dst_ip)],
54 tcp_or_udp => [qw(src_port dst_port proto_bytes)],
55 ) unless keys %fields;
56  
57 0 eval {
58 0 $self->csv_parse($str);
59 };
60  
61 0 my @fields = $self->csv_fields;
62 0 ▶ if( @fields > @{ $fields{base} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields > @{$fields{"base"};}) 0 0 0
63 0 my %ctxt = ( service => 'firewall' );
64 0 $log->add_tags(qw(security firewall));
65 0 @ctxt{@{ $fields{base} }} = splice @fields, 0, scalar(@{ $fields{base} });
66 0 ▶ my $ipv = sprintf "ipv%d", $ctxt{ipver} || 0;
Condition0%0/2
$ctxt{'ipver'} || 0
l!l
00
MC/DC0%0/1
$ctxt{'ipver'} || 0
$ctxt{'ipver'}
Truth table0%0/2
A
A= $ctxt{'ipver'}
Aresult
11
00
67 0 ▶ if ( exists $fields{$ipv} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields{$ipv}) 0 0 0
68 0 @ctxt{@{ $fields{$ipv} }} = splice @fields, 0, scalar(@{ $fields{$ipv} });
69 0 @ctxt{ @{ $fields{ip} } } = splice @fields, 0, scalar(@{ $fields{ip} });
70 0 ▶ if( $ctxt{proto_app} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctxt{"proto_app"}) 0 0 0
71 0 $ctxt{proto_app} = lc $ctxt{proto_app};
72 0 ▶ if( ( $ctxt{proto_app} eq 'udp' || $ctxt{proto_app} eq 'tcp' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctxt{"proto_app"} eq "udp" or $ctxt{"proto_app"} eq "tcp") 0 0 0
Condition0%0/3
$ctxt{"proto_app"} eq "udp" or $ctxt{"proto_app"} eq "tcp"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctxt{"proto_app"} eq "udp" or $ctxt{"proto_app"} eq "tcp"
$ctxt{"proto_app"} eq "udp" $ctxt{"proto_app"} eq "tcp"
Truth table0%0/3
A or B
A= $ctxt{"proto_app"} eq "udp"B= $ctxt{"proto_app"} eq "tcp"
ABresult
1X1
011
000
73 0 @ctxt{ @{ $fields{tcp_or_udp} } } = splice @fields, 0, scalar(@{ $fields{tcp_or_udp} });
74 }
75 }
76 0 ▶ if( $ctxt{direction} and $ctxt{length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctxt{"direction"} and $ctxt{"length"}) 0 0 0
Condition0%0/3
$ctxt{"direction"} and $ctxt{"length"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ctxt{"direction"} and $ctxt{"length"}
$ctxt{"direction"} $ctxt{"length"}
Truth table0%0/3
A and B
A= $ctxt{"direction"}B= $ctxt{"length"}
ABresult
0X0
100
111
77 0 $ctxt{"$ctxt{direction}_bytes"} = $ctxt{length};
78 }
79 }
80 0 $log->add_context($self->name,\%ctxt);
81 }
82 }
83  
84  
85 1 1;
86  
87 __END__
88  
89 =pod
90  
91 =encoding UTF-8
92  
93 =head1 NAME
94  
95 eris::log::context::pfSense::filterlog - Parse the pfSense filterlog
96  
97 =head1 VERSION
98  
99 version 0.008
100  
101 =head1 SYNOPSIS
102  
103 Parses the pfSense/*BSD filterlog generated by the OpenBSD (p)acket(f)filter in CSV
104 format.
105  
106 =head1 ATTRIBUTES
107  
108 =head2 parser
109  
110 An instance of Text::CSV_XS to parse the log line. The default will be
111 sufficient for most purposes.
112  
113 =head2 matcher
114  
115 Matches only the literal string 'filterlog'
116  
117 =head1 METHODS
118  
119 =head2 contextualize_message
120  
121 Parses the raw filterlog CSV data into a structured document
122  
123 Tags messages with 'security', and 'firewall'.
124  
125 =for Pod::Coverage sample_messages
126  
127 =head1 SEE ALSO
128  
129 L<eris::log::contextualizer>, L<eris::role::context>
130  
131 =head1 AUTHOR
132  
133 Brad Lhotsky <brad@divisionbyzero.net>
134  
135 =head1 COPYRIGHT AND LICENSE
136  
137 This software is Copyright (c) 2015 by Brad Lhotsky.
138  
139 This is free software, licensed under:
140  
141 The (three-clause) BSD License
142  
143 =cut
« lib/eris/role/pluggable.pm ↑ Summary script/eris-eris-client.pl »