Check/SuricataFlows.pm

filter: Statementstmt 16.4% 31 / 189 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 11.1% 43 / 385 CC 76 SCAR 73.7
CC
76
Cov
9%
run
2352.0
new
870.0
BEGIN
1.0
CRAP 1587.8
↑ Summary Monitoring/Sneck/Checks/suricata_flows.pm »
1 package Check::SuricataFlows;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::ReadBackwards ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::Piece qw( localtime );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use NetAddr::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Check::SuricataFlows - Make sure Suricata is seeing data via reading the Suricata flows json
17  
18 =head1 VERSION
19  
20 Version 0.3.0
21  
22 =cut
23  
24 1 our $VERSION = '0.3.0';
25  
26 =head1 SYNOPSIS
27  
28 This reads the Suricata EVE JSON flow data file.
29  
30 .timestamp :: Used for double checking to make sure we don't read farther
31 back than we need to.
32  
33 If the following is found, the entry is checked.
34  
35 .dest_ip
36 .src_ip
37 .flow.pkts_toclient
38 .flow.pkts_toserver
39  
40 Bi-directional is when .flow.pkts_toclient and .flow.pkts_toserver are both greater
41 than zero.
42  
43 Uni-directional is when only .flow.pkts_toclient or .flow.pkts_toserver is greater
44 than zero and the other is zero.
45  
46 If all entries found are uni-directional then it is safe to assume the monitored span
47 is misconfigured.
48  
49 If sensor_names is used, then each of the specified sensors is checked for. It is
50 checked from .host in the JSON and the variable for setting that in the Suricata config
51 is .sensor-name .
52  
53 Example...
54  
55 use Check::SuricataFlows;
56 use Data::Dumper;
57  
58 my $flow_checker;
59 eval {
60 $flow_checker = Check::SuricataFlows->new(
61 max_lines => $max_lines,
62 read_back_time => $read_back_time,
63 warn_count => $warn_count,
64 alert_count => $alert_count,
65 flow_file => $flow_file,
66 );
67 };
68 if ($@) {
69 print 'Failed to call Check::SuricataFlows->new... ' . $@ . "\n";
70 exit 3;
71 }
72  
73 my $results;
74 eval { $results = $flow_checker->run; };
75 if ($@) {
76 print 'Failed to call $flow_checker->run... ' . $@ . "\n";
77 exit 3;
78 }
79  
80 print $results->{status};
81 exit $results->{status_code};
82  
83 =head1 METHODS
84  
85 =head2 new
86  
87 Initiates the object.
88  
89 - max_lines :: Maximum distance to read back. The sizing of this should be large enough to
90 ensure you get enought data that has bidirectional flow info. Likely need to increase for
91 very noisy networks.
92 default :: 500
93  
94 - read_back_time :: How far back to read in seconds. Set to 0 to disable.
95 default :: 300
96  
97 - warn_count :: Warn if it is less then this for bidirectional traffic.
98 default :: 20
99  
100 - alert_count :: Alert if it is less than this for bidirectional traffic.
101 default :: 10
102  
103 - flow_file :: The location json file containing the flow data.
104 default :: /var/log/suricata/flows/current/flow.json
105  
106 - sensor_names :: A array of sensor names to check for. If specified each of these need to be above
107 warn/alert count. If empty or undef, only no checking is done for sensor names, just totals.
108 default :: []
109  
110 - ignore_IPs :: A array of IPs to ignore.
111 default :: []
112  
113 Example...
114  
115 my $flow_checker;
116 eval {
117 $flow_checker = Check::SuricataFlows->new(
118 max_lines => $max_lines,
119 read_back_time => $read_back_time,
120 warn_count => $warn_count,
121 alert_count => $alert_count,
122 flow_file => $flow_file,
123 );
124 };
125 if ($@) {
126 print 'Failed to call Check::SuricataFlows->new... ' . $@ . "\n";
127 exit 3;
128 }
129  
130 =cut
131  
132 0 ▶sub new {
Subroutine new: not called CC=29 SCAR=67.7
Pod documented
133 0 my ( $blank, %opts ) = @_;
134  
135 0 ▶ if ( !defined( $opts{max_lines} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opts{'max_lines'}) { } 0 0 0
elsif (ref $opts{'max_lines'} ne '') { } 0 0 0
elsif (not &looks_like_number($opts{'max_lines'})) { } 0 0 0
elsif ($opts{'max_lines'} < 1) { } 0 0 0
136 0 $opts{max_lines} = 500;
137 } elsif ( ref( $opts{max_lines} ) ne '' ) {
138 0 die( '$opts{max_lines} of ref type "' . ref( $opts{max_lines} ) . '" and not ""' );
139 } elsif ( !looks_like_number( $opts{max_lines} ) ) {
140 0 die( '$opts{max_lines}, "' . $opts{max_lines} . '", does not appear to be numeric' );
141 } elsif ( $opts{max_lines} < 1 ) {
142 0 die( '$opts{max_lines}, "' . $opts{max_lines} . '", must be 1 or greater' );
143 }
144  
145 0 ▶ if ( !defined( $opts{read_back_time} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opts{'read_back_time'}) { } 0 0 0
elsif (ref $opts{'read_back_time'} ne '') { } 0 0 0
elsif (not &looks_like_number($opts{'read_back_time'})) { } 0 0 0
elsif ($opts{'read_back_time'} < 1) { } 0 0 0
146 0 $opts{read_back_time} = 300;
147 } elsif ( ref( $opts{read_back_time} ) ne '' ) {
148 0 die( '$opts{read_back_time} of ref type "' . ref( $opts{read_back_time} ) . '" and not ""' );
149 } elsif ( !looks_like_number( $opts{read_back_time} ) ) {
150 0 die( '$opts{read_back_time}, "' . $opts{read_back_time} . '", does not appear to be numeric' );
151 } elsif ( $opts{read_back_time} < 1 ) {
152 0 die( '$opts{read_back_time}, "' . $opts{read_back_time} . '", must be 1 or greater' );
153 }
154  
155 0 ▶ if ( !defined( $opts{warn_count} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'warn_count'}) { } 0 0 0
elsif (ref $opts{'warn_count'} ne '') { } 0 0 0
elsif (not &looks_like_number($opts{'warn_count'})) { } 0 0 0
156 0 $opts{warn_count} = 20;
157 } elsif ( ref( $opts{warn_count} ) ne '' ) {
158 0 die( '$opts{warn_count} of ref type "' . ref( $opts{warn_count} ) . '" and not ""' );
159 } elsif ( !looks_like_number( $opts{warn_count} ) ) {
160 0 die( '$opts{warn_count}, "' . $opts{warn_count} . '", does not appear to be numeric' );
161 }
162  
163 0 ▶ if ( !defined( $opts{alert_count} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'alert_count'}) { } 0 0 0
elsif (ref $opts{'alert_count'} ne '') { } 0 0 0
elsif (not &looks_like_number($opts{'alert_count'})) { } 0 0 0
164 0 $opts{alert_count} = 10;
165 } elsif ( ref( $opts{alert_count} ) ne '' ) {
166 0 die( '$opts{alert_count} of ref type "' . ref( $opts{alert_count} ) . '" and not ""' );
167 } elsif ( !looks_like_number( $opts{alert_count} ) ) {
168 0 die( '$opts{alert_count}, "' . $opts{alert_count} . '", does not appear to be numeric' );
169 }
170  
171 0 ▶ if ( !defined( $opts{flow_file} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'flow_file'}) { } 0 0 0
elsif (ref $opts{'flow_file'} ne '') { } 0 0 0
172 0 $opts{flow_file} = '/var/log/suricata/flows/current/flow.json';
173 } elsif ( ref( $opts{flow_file} ) ne '' ) {
174 0 die( '$opts{flow_file} of ref type "' . ref( $opts{flow_file} ) . '" and not ""' );
175 }
176  
177 0 ▶ if ( !defined( $opts{sensor_names} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'sensor_names'}) { } 0 0 0
elsif (ref $opts{'sensor_names'} ne 'ARRAY') { } 0 0 0
178 0 $opts{sensor_names} = [];
179 } elsif ( ref( $opts{sensor_names} ) ne 'ARRAY' ) {
180 0 die( '$opts{sensor_names} of ref type "' . ref( $opts{sensor_names} ) . '" and not "ARRAY"' );
181 } else {
182 0 my $sensor_names_int = 0;
183 0 ▶ while ( defined( $opts{sensor_names}[$sensor_names_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"sensor_names"}[$sensor_names_int]) 0 0 0
184 0 ▶ if ( ref( $opts{sensor_names}[$sensor_names_int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"sensor_names"}[$sensor_names_int] ne "") 0 0 0
185 die( '$opts{sensor_names}['
186 . $sensor_names_int
187 . '] of ref type "'
188 0 . ref( $opts{sensor_names}[$sensor_names_int] )
189 . '" and not ""' );
190 }
191 0 $sensor_names_int++;
192 } ## end while ( defined( $opts{sensor_names}[$sensor_names_int...]))
193 } ## end else [ if ( !defined( $opts{sensor_names} ) ) ]
194  
195 0 my @ignore_IPs;
196 0 my $ignore_IPs_lookup = {};
197 0 ▶ if ( defined( $opts{ignore_IPs} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ignore_IPs"}) 0 0 0
198 0 ▶ if ( ref( $opts{ignore_IPs} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"ignore_IPs"} ne "ARRAY") 0 0 0
199 0 die( '$opts{ignore_IPs} of ref type "' . ref( $opts{ignore_IPs} ) . '" and not "ARRAY"' );
200 }
201  
202 0 my $ignore_IPs_int = 0;
203 0 ▶ while ( defined( $opts{ignore_IPs}[$ignore_IPs_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ignore_IPs"}[$ignore_IPs_int]) 0 0 0
204 0 ▶ if ( ref( $opts{ignore_IPs}[$ignore_IPs_int] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"ignore_IPs"}[$ignore_IPs_int] ne "") 0 0 0
205 die( '$opts{ignore_IPs}['
206 . $ignore_IPs_int
207 . '] of ref type "'
208 0 . ref( $opts{ignore_IPs}[$ignore_IPs_int] )
209 . '" and not ""' );
210 }
211  
212 0 ▶ if ( ( $opts{ignore_IPs}[$ignore_IPs_int] !~ /^$IPv6_re$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv6_re$/ or $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv4_re$/) 0 0 0
Condition0%0/3
$opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv6_re$/ or $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv4_re$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv6_re$/ or $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv4_re$/
$opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv6_re$/ $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv4_re$/
Truth table0%0/3
A or B
A= $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv6_re$/B= $opts{"ignore_IPs"}[$ignore_IPs_int] =~ /^$Check::SuricataFlows::IPv4_re$/
ABresult
1X1
011
000
213 && ( $opts{ignore_IPs}[$ignore_IPs_int] !~ /^$IPv4_re$/ ) )
214 {
215 die( '$opts{ignore_IPs}['
216 . $ignore_IPs_int . '], "'
217 0 . $opts{ignore_IPs}[$ignore_IPs_int]
218 . '", does not appear to be IPv4 or IPv6' );
219 }
220 0 eval {
221 0 my $ip = NetAddr::IP->new( $opts{ignore_IPs}[$ignore_IPs_int] );
222 0 push( @ignore_IPs, $ip->addr );
223 };
224 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
225 die( '$opts{ignore_IPs}['
226 . $ignore_IPs_int . '], "'
227 0 . $opts{ignore_IPs}[$ignore_IPs_int]
228 . '", could not be parsed... '
229 . $@ );
230 }
231  
232 0 $ignore_IPs_int++;
233 } ## end while ( defined( $opts{ignore_IPs}[$ignore_IPs_int...]))
234  
235 0 foreach my $ip (@ignore_IPs) {
236 0 $ignore_IPs_lookup->{$ip} = 1;
237 }
238 } ## end if ( defined( $opts{ignore_IPs} ) )
239  
240 my $self = {
241 flow_file => $opts{flow_file},
242 alert_count => $opts{alert_count},
243 warn_count => $opts{warn_count},
244 read_back_time => $opts{read_back_time},
245 max_lines => $opts{max_lines},
246 sensor_names => $opts{sensor_names},
247 0 ignore_IPs => \@ignore_IPs,
248 ignore_IPs_lookup => $ignore_IPs_lookup,
249 };
250 0 bless $self;
251  
252 0 return $self;
253 } ## end sub new
254  
255 =head2 run
256  
257 This method runs the check.
258  
259 Possible fatal errors such as the flow file not existing or being readable
260 results in a status_code of 3 being set and a description set in status.
261  
262 This returns a hash ref. The keys are as below.
263  
264 - status :: Status string for the results.
265  
266 - status_code :: Nagios style int. 0=OK, 1=WARN, 2=ALERT, 3=UNKNOWN/ERROR
267  
268 - lines_read :: Number of lines read.
269  
270 - lines_parsed :: Number of lines successfully parsed.
271  
272 - lines_get_errored :: Number of lines that resulted in fetch errors.
273  
274 - lines_get_errors :: A array of strings of errors encountered when getting the next flow entry to process.
275  
276 - bi_directional_count :: Count of bi-directional flows.
277  
278 - uni_directional_count :: Count of uni-directional flows.
279  
280 - ip_ignored_lines :: Lines ignored thanks to src/dest IP.
281  
282 - ip_parse_errored :: Lines in which the src/dest IP could not be parsed.
283  
284 - ip_parse_errors :: Array containing error info on src/dest IP c parsing issues.
285  
286 Example...
287  
288 my $results;
289 eval { $results = $flow_checker->run; };
290 if ($@) {
291 print 'Failed to call $flow_checker->run... ' . $@ . "\n";
292 exit 3;
293 }
294  
295 print $results->{status};
296 exit $results->{status_code};
297  
298 =cut
299  
300 0 ▶sub run {
Subroutine run: not called CC=48 SCAR=77.6
Pod documented
301 0 my $self = $_[0];
302  
303 0 my $to_return = {
304 status => '',
305 status_code => 0,
306 lines_read => 0,
307 lines_parsed => 0,
308 lines_get_errored => 0,
309 bi_directional_count => 0,
310 uni_directional_count => 0,
311 lines_get_errors => [],
312 by_sensor => {},
313 ip_ignored_lines => 0,
314 ip_parse_errored => 0,
315 ip_parse_errors => [],
316 };
317  
318 # can't go any further if we can't read the file
319 0 ▶ if ( !-e $self->{flow_file} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -e $self->{'flow_file'}) { } 0 0 0
elsif (not -r $self->{'flow_file'}) { } 0 0 0
320 0 $to_return->{status} = "flow file, '" . $self->{flow_file} . "', does not exist\n";
321 0 $to_return->{status_code} = 3;
322 0 return $to_return;
323 } elsif ( !-r $self->{flow_file} ) {
324 0 $to_return->{status} = "flow file, '" . $self->{flow_file} . "', not readable\n";
325 0 $to_return->{status_code} = 3;
326 0 return $to_return;
327 }
328  
329 # get the current time and figure when we should read back till
330 0 my $read_till = localtime;
331 0 $read_till = $read_till - $self->{read_back_time};
332  
333 # init readbackwards and if we can't we can't go any further
334 0 my $bw;
335 0 eval { $bw = File::ReadBackwards->new( $self->{flow_file} )
336 0 ▶ or die "can't read '" . $self->{flow_file} . "' $!"; };
Branch: 1 branch
BranchTrueFalseTotal
$bw = "File::ReadBackwards"->new($self->{"flow_file"}) or die q[can't read '] . $self->{"flow_file"} . "' $!" 0 0 0
337 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
338 $to_return->{status}
339 0 = "Failed to initiate File::ReadBackwards for '" . $self->{flow_file} . "' ... " . $@ . "\n";
340 0 $to_return->{status_code} = 3;
341 0 return $to_return;
342 }
343  
344 # as long as this is true, we should continue reading the file
345 0 my $process = 1;
346 # for keeping track of number of lines read
347 0 my $line_count;
348 0 ▶ while ($process) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process) 0 0 0
349 0 $line_count++;
350 0 ▶ if ( $line_count > $self->{max_lines} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line_count > $self->{'max_lines'}) { } 0 0 0
elsif ($bw->eof) { } 0 0 0
351 0 $process = 0;
352 } elsif ( $bw->eof ) {
353 0 $process = 0;
354 }
355  
356 0 ▶ if ($process) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process) 0 0 0
357 0 my $parsed_line;
358 0 my $process_line = 0;
359 0 eval {
360 0 my $line = $bw->readline;
361  
362 0 $to_return->{lines_read}++;
363  
364 0 $parsed_line = decode_json($line);
365  
366 0 $to_return->{lines_parsed}++;
367  
368 0 $process_line = 1;
369 };
370 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($process_line) { } 0 0 0
371 0 push( @{ $to_return->{lines_get_errors} }, $@ );
372 0 $to_return->{lines_get_errored}++;
373 } elsif ($process_line) {
374 0 my $time_good = 0;
375  
376 0 ▶ if ( defined( $parsed_line->{'timestamp'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parsed_line->{"timestamp"}) 0 0 0
377 0 $parsed_line->{'timestamp'} =~ s/\.[0-9]+//;
378  
379 0 my $time;
380 0 eval { $time = Time::Piece->strptime( $parsed_line->{'timestamp'}, '%Y-%m-%dT%T%z' ); };
381 0 ▶ if ( defined($time) && $time >= $read_till ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $time and $time >= $read_till) { } 0 0 0
elsif (defined $time and $time < $read_till) { } 0 0 0
Condition0%0/3
defined $time and $time >= $read_till
!ll&&!rl&&r
000
MC/DC0%0/2
defined $time and $time >= $read_till
defined $time $time >= $read_till
Truth table0%0/3
A and B
A= defined $timeB= $time >= $read_till
ABresult
0X0
100
111
382 0 $time_good = 1;
383 ▶ } elsif ( defined($time) && $time < $read_till ) {
Condition0%0/3
defined $time and $time < $read_till
!ll&&!rl&&r
000
MC/DC0%0/2
defined $time and $time < $read_till
defined $time $time < $read_till
Truth table0%0/3
A and B
A= defined $timeB= $time < $read_till
ABresult
0X0
100
111
384 0 $process = 0;
385 }
386 } ## end if ( defined( $parsed_line->{'timestamp'} ...))
387  
388 0 ▶ if ( $time_good
Branch: 1 branch
BranchTrueFalseTotal
if ($time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) and defined $parsed_line->{"flow"}{"pkts_toserver"} and ref $parsed_line->{"flow"}{"pkts_toserver"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toserver"})) 0 0 0
Condition0%0/36
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) and defined $parsed_line->{"flow"}{"pkts_toserver"} and ref $parsed_line->{"flow"}{"pkts_toserver"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toserver"})
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) and defined $parsed_line->{"flow"}{"pkts_toserver"} and ref $parsed_line->{"flow"}{"pkts_toserver"} eq ""
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) and defined $parsed_line->{"flow"}{"pkts_toserver"}
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"})
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq ""
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"}
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH"
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"}
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq ""
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"}
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq ""
!ll&&!rl&&r
000
$time_good and defined $parsed_line->{"dest_ip"}
!ll&&!rl&&r
000
MC/DC0%0/13
$time_good and defined $parsed_line->{"dest_ip"} and ref $parsed_line->{"dest_ip"} eq "" and defined $parsed_line->{"src_ip"} and ref $parsed_line->{"src_ip"} eq "" and defined $parsed_line->{"flow"} and ref $parsed_line->{"flow"} eq "HASH" and defined $parsed_line->{"flow"}{"pkts_toclient"} and ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) and defined $parsed_line->{"flow"}{"pkts_toserver"} and ref $parsed_line->{"flow"}{"pkts_toserver"} eq "" and &looks_like_number($parsed_line->{"flow"}{"pkts_toserver"})
$time_good defined $parsed_line->{"dest_ip"} ref $parsed_line->{"dest_ip"} eq "" defined $parsed_line->{"src_ip"} ref $parsed_line->{"src_ip"} eq "" defined $parsed_line->{"flow"} ref $parsed_line->{"flow"} eq "HASH" defined $parsed_line->{"flow"}{"pkts_toclient"} ref $parsed_line->{"flow"}{"pkts_toclient"} eq "" &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"}) defined $parsed_line->{"flow"}{"pkts_toserver"} ref $parsed_line->{"flow"}{"pkts_toserver"} eq "" &looks_like_number($parsed_line->{"flow"}{"pkts_toserver"})
Truth table0%0/14
A and B and C and D and E and F and G and H and I and J and K and L and M
A= $time_goodB= defined $parsed_line->{"dest_ip"}C= ref $parsed_line->{"dest_ip"} eq ""D= defined $parsed_line->{"src_ip"}E= ref $parsed_line->{"src_ip"} eq ""F= defined $parsed_line->{"flow"}G= ref $parsed_line->{"flow"} eq "HASH"H= defined $parsed_line->{"flow"}{"pkts_toclient"}I= ref $parsed_line->{"flow"}{"pkts_toclient"} eq ""J= &looks_like_number($parsed_line->{"flow"}{"pkts_toclient"})K= defined $parsed_line->{"flow"}{"pkts_toserver"}L= ref $parsed_line->{"flow"}{"pkts_toserver"} eq ""M= &looks_like_number($parsed_line->{"flow"}{"pkts_toserver"})
ABCDEFGHIJKLMresult
0XXXXXXXXXXXX0
10XXXXXXXXXXX0
110XXXXXXXXXX0
1110XXXXXXXXX0
11110XXXXXXXX0
111110XXXXXXX0
1111110XXXXXX0
11111110XXXXX0
111111110XXXX0
1111111110XXX0
11111111110XX0
111111111110X0
11111111111100
11111111111111
389 && defined( $parsed_line->{'dest_ip'} )
390 && ( ref( $parsed_line->{'dest_ip'} ) eq '' )
391 && defined( $parsed_line->{'src_ip'} )
392 && ( ref( $parsed_line->{'src_ip'} ) eq '' )
393 && defined( $parsed_line->{'flow'} )
394 && ( ref( $parsed_line->{'flow'} ) eq 'HASH' )
395 && defined( $parsed_line->{'flow'}{'pkts_toclient'} )
396 && ( ref( $parsed_line->{'flow'}{'pkts_toclient'} ) eq '' )
397 && looks_like_number( $parsed_line->{'flow'}{'pkts_toclient'} )
398 && defined( $parsed_line->{'flow'}{'pkts_toserver'} )
399 && ( ref( $parsed_line->{'flow'}{'pkts_toserver'} ) eq '' )
400 && looks_like_number( $parsed_line->{'flow'}{'pkts_toserver'} ) )
401 {
402 0 my $IP_ignore = 0;
403  
404 0 eval {
405 0 my $src_ip = NetAddr::IP->new( $parsed_line->{'src_ip'} );
406 0 ▶ if ( $self->{ignore_IPs_lookup}{ $src_ip->addr } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ignore_IPs_lookup"}{$src_ip->addr}) 0 0 0
407 0 $IP_ignore = 1;
408 }
409 0 ▶ if ( !$IP_ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IP_ignore) 0 0 0
410 0 my $dest_ip = NetAddr::IP->new( $parsed_line->{'dest_ip'} );
411 0 ▶ if ( $self->{ignore_IPs_lookup}{ $dest_ip->addr } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ignore_IPs_lookup"}{$dest_ip->addr}) 0 0 0
412 0 $IP_ignore = 1;
413 }
414 }
415 };
416 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
417 0 push( @{ $to_return->{ip_parse_errors} }, $@ );
418 0 $to_return->{ip_parse_errored}++;
419 }
420  
421 0 ▶ if ( !$IP_ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $IP_ignore) { } 0 0 0
422 # .host is not there unless sensor-name is set, and undef as a key warns
423 0 ▶ my $sensor = $parsed_line->{'host'} // '';
Condition0%0/2
$parsed_line->{"host"} // ""
l!l
00
MC/DC0%0/1
$parsed_line->{"host"} // ""
$parsed_line->{"host"}
Truth table0%0/2
A
A= $parsed_line->{"host"}
Aresult
11
00
424 0 ▶ if ( !defined( $to_return->{by_sensor}{$sensor} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_return->{"by_sensor"}{$sensor}) 0 0 0
425 0 $to_return->{by_sensor}{$sensor} = {
426 bi_directional_count => 0,
427 uni_directional_count => 0,
428 };
429 }
430 0 ▶ if ( ( $parsed_line->{'flow'}{'pkts_toserver'} > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed_line->{'flow'}{'pkts_toserver'} > 0 and $parsed_line->{'flow'}{'pkts_toclient'} > 0) { } 0 0 0
Condition0%0/3
$parsed_line->{"flow"}{"pkts_toserver"} > 0 and $parsed_line->{"flow"}{"pkts_toclient"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$parsed_line->{"flow"}{"pkts_toserver"} > 0 and $parsed_line->{"flow"}{"pkts_toclient"} > 0
$parsed_line->{"flow"}{"pkts_toserver"} > 0 $parsed_line->{"flow"}{"pkts_toclient"} > 0
Truth table0%0/3
A and B
A= $parsed_line->{"flow"}{"pkts_toserver"} > 0B= $parsed_line->{"flow"}{"pkts_toclient"} > 0
ABresult
0X0
100
111
431 && ( $parsed_line->{'flow'}{'pkts_toclient'} > 0 ) )
432 {
433 0 $to_return->{bi_directional_count}++;
434 0 $to_return->{by_sensor}{$sensor}{bi_directional_count}++;
435 } else {
436 0 $to_return->{uni_directional_count}++;
437 0 $to_return->{by_sensor}{$sensor}{uni_directional_count}++;
438 }
439 } else {
440 0 $to_return->{ip_ignored_lines}++;
441 }
442 } ## end if ( $time_good && defined( $parsed_line->...))
443 } ## end elsif ($process_line)
444 } ## end if ($process)
445 } ## end while ($process)
446  
447 0 ▶ if ( $to_return->{bi_directional_count} <= $self->{alert_count} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to_return->{'bi_directional_count'} <= $self->{'alert_count'}) { } 0 0 0
elsif ($to_return->{'bi_directional_count'} <= $self->{'warn_count'}) { } 0 0 0
448 $to_return->{status}
449 = 'ALERT: bi directional count of '
450 . $to_return->{bi_directional_count}
451 . ' is less than '
452 0 . $self->{alert_count} . "\n";
453 0 $to_return->{status_code} = 2;
454 } elsif ( $to_return->{bi_directional_count} <= $self->{warn_count} ) {
455 $to_return->{status}
456 = 'WARN: bi directional count of '
457 . $to_return->{bi_directional_count}
458 . ' is less than '
459 0 . $self->{warn_count} . "\n";
460 0 $to_return->{status_code} = 1;
461 } else {
462 0 $to_return->{status} = 'OK: bi directional count of ' . $to_return->{bi_directional_count} . "\n";
463 }
464  
465 0 ▶ if ( defined( $self->{sensor_names}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sensor_names"}[0]) 0 0 0
466 0 foreach my $sensor ( @{ $self->{sensor_names} } ) {
467 0 ▶ if ( defined( $to_return->{by_sensor}{$sensor} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to_return->{'by_sensor'}{$sensor}) { } 0 0 0
468 0 ▶ if ( $to_return->{by_sensor}{$sensor}{bi_directional_count} <= $self->{alert_count} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to_return->{'by_sensor'}{$sensor}{'bi_directional_count'} <= $self->{'alert_count'}) { } 0 0 0
elsif ($to_return->{'by_sensor'}{$sensor}{'bi_directional_count'} <= $self->{'warn_count'}) { } 0 0 0
469 $to_return->{status}
470 = $to_return->{status}
471 . 'ALERT, '
472 . $sensor
473 . ': bi directional count of '
474 . $to_return->{by_sensor}{$sensor}{bi_directional_count}
475 . ' is less than '
476 0 . $self->{alert_count} . "\n";
477 0 $to_return->{status_code} = 2;
478 } elsif ( $to_return->{by_sensor}{$sensor}{bi_directional_count} <= $self->{warn_count} ) {
479 $to_return->{status}
480 = $to_return->{status}
481 . 'WARN, '
482 . $sensor
483 . ': bi directional count of '
484 . $to_return->{by_sensor}{$sensor}{bi_directional_count}
485 . ' is less than '
486 0 . $self->{warn_count} . "\n";
487 0 ▶ if ( $to_return->{status_code} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_return->{"status_code"} < 1) 0 0 0
488 0 $to_return->{status_code} = 1;
489 }
490 } else {
491 $to_return->{status}
492 = $to_return->{status} . 'OK, '
493 . $sensor
494 . ': bi directional count of '
495 0 . $to_return->{by_sensor}{$sensor}{bi_directional_count} . "\n";
496 }
497 } else {
498 0 $to_return->{status} = $to_return->{status} . 'ALERT, ' . $sensor . ": sensor never seen\n";
499 0 $to_return->{status_code} = 2;
500 }
501 } ## end foreach my $sensor ( @{ $self->{sensor_names} })
502 } ## end if ( defined( $self->{sensor_names}[0] ) )
503  
504 0 $to_return->{status} = $to_return->{status} . 'uni directional: ' . $to_return->{uni_directional_count} . "\n";
505 0 foreach my $sensor ( @{ $self->{sensor_names} } ) {
506 0 ▶ if ( defined( $to_return->{by_sensor}{$sensor} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to_return->{'by_sensor'}{$sensor}) { } 0 0 0
507 $to_return->{status}
508 = $to_return->{status}
509 . 'uni directional, '
510 . $sensor . ': '
511 0 . $to_return->{by_sensor}{$sensor}{uni_directional_count} . "\n";
512 } else {
513 0 $to_return->{status} = $to_return->{status} . 'uni directional, ' . $sensor . ": 0\n";
514 }
515 } ## end foreach my $sensor ( @{ $self->{sensor_names} })
516 $to_return->{status}
517 0 = $to_return->{status} . 'seen sensors: ' . join( ', ', keys( %{ $to_return->{by_sensor} } ) ) . "\n";
518 $to_return->{status}
519 0 = $to_return->{status} . 'ignored IPs: ' . join( ', ', @{ $self->{ignore_IPs} } ) . "\n";
520 0 $to_return->{status} = $to_return->{status} . 'max lines to read: ' . $self->{max_lines} . "\n";
521 0 $to_return->{status} = $to_return->{status} . 'lines read: ' . $to_return->{lines_read} . "\n";
522 0 $to_return->{status} = $to_return->{status} . 'lines parsed: ' . $to_return->{lines_parsed} . "\n";
523 0 $to_return->{status} = $to_return->{status} . 'line gets errored: ' . $to_return->{lines_get_errored} . "\n";
524 0 $to_return->{status} = $to_return->{status} . 'IP parse errored: ' . $to_return->{ip_parse_errored} . "\n";
525  
526 0 return $to_return;
527 } ## end sub run
528  
529 =head1 AUTHOR
530  
531 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
532  
533 =head1 BUGS
534  
535 Please report any bugs or feature requests to C<bug-check-suricataflows at rt.cpan.org>, or through
536 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Check-SuricataFlows>. I will be notified, and then you'll
537 automatically be notified of progress on your bug as I make changes.
538  
539  
540  
541  
542 =head1 SUPPORT
543  
544 You can find documentation for this module with the perldoc command.
545  
546 perldoc Check::SuricataFlows
547  
548  
549 You can also look for information at:
550  
551 =over 4
552  
553 =item * RT: CPAN's request tracker (report bugs here)
554  
555 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Check-SuricataFlows>
556  
557 =item * CPAN Ratings
558  
559 L<https://cpanratings.perl.org/d/Check-SuricataFlows>
560  
561 =item * Search CPAN
562  
563 L<https://metacpan.org/release/Check-SuricataFlows>
564  
565 =back
566  
567  
568 =head1 ACKNOWLEDGEMENTS
569  
570  
571 =head1 LICENSE AND COPYRIGHT
572  
573 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
574  
575 This is free software, licensed under:
576  
577 The GNU General Public License, Version 2, June 1991
578  
579  
580 =cut
581  
582 1 1; # End of Check::SuricataFlows
↑ Summary Monitoring/Sneck/Checks/suricata_flows.pm »