Lilith.pm

filter: Statementstmt 25.0% 87 / 347 Branchbran 10.5% 19 / 180 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 73.6% 14 / 19 Podpod 100% 7 / 7 totaltotal 18.8% 127 / 674 CC 125 SCAR 71.2
CC
125
Cov
18%
__ANON__
2162.0
search
1722.0
extend
342.0
CRAP 1238.8
↑ Summary Lilith/Schema.pm »
1 package Lilith;
2  
3 3 ▶use 5.006;
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 POE qw(Wheel::FollowTail);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Digest::SHA qw(sha256_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sys::Syslog qw( closelog openlog syslog );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Lilith::Schema ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Lilith - Work with Suricata/Sagan EVE logs and PostgreSQL.
17  
18 =head1 VERSION
19  
20 Version 3.0.0
21  
22 =cut
23  
24 3 our $VERSION = '3.0.0';
25  
26 =head1 SYNOPSIS
27  
28 my $toml_raw = read_file($config_file) or die 'Failed to read "' . $config_file . '"';
29 my ( $toml, $err ) = from_toml($toml_raw);
30 unless ($toml) {
31 die "Error parsing toml,'" . $config_file . "'" . $err;
32 }
33  
34 my $lilith=Lilith->new(
35 dsn=>$toml->{dsn},
36 user=>$toml->{user},
37 pass=>$toml->{pass},
38 );
39  
40  
41 $lilith->create_table(
42 dsn=>$toml->{dsn},
43 user=>$toml->{user},
44 pass=>$toml->{pass},
45 );
46  
47 my %files;
48 my @toml_keys = keys( %{$toml} );
49 my $int = 0;
50 while ( defined( $toml_keys[$int] ) ) {
51 my $item = $toml_keys[$int];
52  
53 if ( ref( $toml->{$item} ) eq "HASH" ) {
54 # add the file in question
55 $files{$item} = $toml->{$item};
56 }
57  
58 $int++;
59 }
60  
61 $ilith->run(
62 files=>\%files,
63 );
64  
65 =head1 FUNCTIONS
66  
67 =head2 new
68  
69 Initiates it.
70  
71 my $lilith=Lilith->run(
72 dsn=>$toml->{dsn},
73 user=>$toml->{user},
74 pass=>$toml->{pass},
75 );
76  
77 The args taken by this are as below.
78  
79 - dsn :: The DSN to use for with DBI.
80  
81 - suricata :: Name of the table for Suricata alerts.
82 Default :: suricata_alerts
83  
84 - user :: Name for use with DBI for the DB connection.
85 Default :: lilith
86  
87 - pass :: pass for use with DBI for the DB connection.
88 Default :: undef
89  
90 - sid_ignore :: Array of SIDs to ignore for Suricata and Sagan
91 for the extend.
92 Default :: undef
93  
94 - class_ignore :: Array of classes to ignore for the
95 extend for Suricata and Sagan
96 Default :: undef
97  
98 - suricata_sid_ignore :: Array of SIDs to ignore for Suricata
99 for the extend.
100 Default :: undef
101  
102 - suricata_class_ignore :: Array of classes to ignore for the
103 extend for Suricata.
104 Default :: undef
105  
106 - sagan_sid_ignore :: Array of SIDs to ignore for Sagan for
107 the extend.
108 Default :: undef
109  
110 - sagan_class_ignore :: Array of classes to ignore for the
111 extend for Sagan.
112 Default :: undef
113  
114 =cut
115  
116 6 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
Pod documented
117 6 my ( $blank, %opts ) = @_;
118  
119 6 ▶ if ( !defined( $opts{dsn} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"dsn"}) 2 4 6
120 2 die('"dsn" is not defined');
121 }
122  
123 4 ▶ if ( !defined( $opts{user} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"user"}) 3 1 4
124 3 $opts{user} = 'lilith';
125 }
126  
127 4 ▶ if ( !defined( $opts{sid_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"sid_ignore"}) 3 1 4
128 3 my @empty_array;
129 3 $opts{sid_ignore} = \@empty_array;
130 }
131  
132 4 ▶ if ( !defined( $opts{class_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"class_ignore"}) 4 0 4
133 4 my @empty_array;
134 4 $opts{class_ignore} = \@empty_array;
135 }
136  
137 4 ▶ if ( !defined( $opts{suricata_sid_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"suricata_sid_ignore"}) 4 0 4
138 4 my @empty_array;
139 4 $opts{suricata_sid_ignore} = \@empty_array;
140 }
141  
142 4 ▶ if ( !defined( $opts{suricata_class_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"suricata_class_ignore"}) 4 0 4
143 4 my @empty_array;
144 4 $opts{suricata_class_ignore} = \@empty_array;
145 }
146  
147 4 ▶ if ( !defined( $opts{sagan_sid_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"sagan_sid_ignore"}) 4 0 4
148 4 my @empty_array;
149 4 $opts{sagan_sid_ignore} = \@empty_array;
150 }
151  
152 4 ▶ if ( !defined( $opts{sagan_class_ignore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"sagan_class_ignore"}) 4 0 4
153 4 my @empty_array;
154 4 $opts{sagan_class_ignore} = \@empty_array;
155 }
156  
157 my $self = {
158 sid_ignore => $opts{sid_ignore},
159 suricata_sid_ignore => $opts{suricata_sid_ignore},
160 sagan_sid_ignore => $opts{sagan_sid_ignore},
161 class_ignore => $opts{class_ignore},
162 suricata_class_ignore => $opts{suricata_class_ignore},
163 sagan_class_ignore => $opts{sagan_class_ignore},
164 dsn => $opts{dsn},
165 user => $opts{user},
166 pass => $opts{pass},
167 debug => $opts{debug},
168 4 class_map => {
169 'Not Suspicious Traffic' => '!SusT',
170 'Unknown Traffic' => 'UnknownT',
171 'Attempted Information Leak' => '!IL',
172 'Information Leak' => 'IL',
173 'Large Scale Information Leak' => 'LrgSclIL',
174 'Attempted Denial of Service' => 'ADoS',
175 'Denial of Service' => 'DoS',
176 'Attempted User Privilege Gain' => 'AUPG',
177 'Unsuccessful User Privilege Gain' => '!SucUsrPG',
178 'Successful User Privilege Gain' => 'SucUsrPG',
179 'Attempted Administrator Privilege Gain' => '!SucAdmPG',
180 'Successful Administrator Privilege Gain' => 'SucAdmPG',
181 'Decode of an RPC Query' => 'DRPCQ',
182 'Executable code was detected' => 'ExeCode',
183 'A suspicious string was detected' => 'SusString',
184 'A suspicious filename was detected' => 'SusFilename',
185 'An attempted login using a suspicious username was detected' => '!LoginUser',
186 'A system call was detected' => 'Syscall',
187 'A TCP connection was detected' => 'TCPconn',
188 'A Network Trojan was detected' => 'NetTrojan',
189 'A client was using an unusual port' => 'OddClntPrt',
190 'Detection of a Network Scan' => 'NetScan',
191 'Detection of a Denial of Service Attack' => 'DOS',
192 'Detection of a non-standard protocol or event' => 'NS PoE',
193 'Generic Protocol Command Decode' => 'GPCD',
194 'access to a potentially vulnerable web application' => 'PotVulWebApp',
195 'Web Application Attack' => 'WebAppAtk',
196 'Misc activity' => 'MiscActivity',
197 'Misc Attack' => 'MiscAtk',
198 'Generic ICMP event' => 'GenICMP',
199 'Inappropriate Content was Detected' => '!AppCont',
200 'Potential Corporate Privacy Violation' => 'PotCorpPriVio',
201 'Attempt to login by a default username and password' => '!DefUserPass',
202 'Targeted Malicious Activity was Detected' => 'TargetedMalAct',
203 'Exploit Kit Activity Detected' => 'ExpKit',
204 'Device Retrieving External IP Address Detected' => 'RetrExtIP',
205 'Domain Observed Used for C2 Detected' => 'C2domain',
206 'Possibly Unwanted Program Detected' => 'PotUnwantedProg',
207 'Successful Credential Theft Detected' => 'CredTheft',
208 'Possible Social Engineering Attempted' => 'PosSocEng',
209 'Crypto Currency Mining Activity Detected' => 'Mining',
210 'Malware Command and Control Activity Detected' => 'MalC2act',
211 'Potentially Bad Traffic' => 'PotBadTraf',
212 'Unsuccessful Admin Privilege' => 'SucAdmPG',
213 'Exploit Attempt' => 'ExpAtmp',
214 'Program Error' => 'ProgErr',
215 'Suspicious Command Execution' => 'SusProgExec',
216 'Network event' => 'NetEvent',
217 'System event' => 'SysEvent',
218 'Configuration Change' => 'ConfChg',
219 'Spam' => 'Spam',
220 'Attempted Access To File or Directory' => 'FoDAccAtmp',
221 'Suspicious Traffic' => 'SusT',
222 'Configuration Error' => 'ConfErr',
223 'Hardware Event' => 'HWevent',
224 '' => 'blankC',
225 },
226 lc_class_map => {},
227 rev_class_map => {},
228 lc_rev_class_map => {},
229 snmp_class_map => {},
230 };
231 4 bless $self;
232  
233 4 my @keys = keys( %{ $self->{class_map} } );
234 4 foreach my $key (@keys) {
235 224 my $lc_key = lc($key);
236 224 $self->{lc_class_map}{$lc_key} = $self->{class_map}{$key};
237 224 $self->{rev_class_map}{ $self->{class_map}{$key} } = $key;
238 224 $self->{lc_rev_class_map}{ lc( $self->{class_map}{$key} ) } = $key;
239 224 $self->{snmp_class_map}{$lc_key} = $self->{class_map}{$key};
240 224 $self->{snmp_class_map}{$lc_key} = $self->{class_map}{$key};
241 224 $self->{snmp_class_map}{$lc_key} =~ s/^\!/not\_/;
242 224 $self->{snmp_class_map}{$lc_key} =~ s/\ /\_/;
243 } ## end foreach my $key (@keys)
244  
245 4 return $self;
246 } ## end sub new
247  
248 =head2 run
249  
250 Start processing. This method is not expected to return.
251  
252 $lilith->run(
253 files=>{
254 foo=>{
255 type=>'suricata',
256 instance=>'foo-pie',
257 eve=>'/var/log/suricata/alerts-pie.json',
258 },
259 'foo-lae'=>{
260 type=>'sagan',
261 eve=>'/var/log/sagan/alerts-lae.json',
262 },
263 },
264 );
265  
266 One argument named 'files' is taken and it is hash of
267 hashes. The keys are below.
268  
269 - type :: Either 'suricata', 'sagan', or 'cape', depending
270 on the type it is.
271  
272 - eve :: Path to the EVE file to read.
273  
274 - instance :: Instance name. If not specified the key
275 is used.
276  
277 =cut
278  
279 0 ▶sub run {
Subroutine run: not called CC=9 SCAR=45.0
Pod documented
280 0 my ( $self, %opts ) = @_;
281  
282 0 my $dbh;
283 0 eval { $dbh = DBI->connect_cached( $self->{dsn}, $self->{user}, $self->{pass} ); };
284 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
285 0 warn($@);
286 0 openlog( 'lilith', undef, 'daemon' );
287 0 syslog( 'LOG_ERR', $@ );
288 0 closelog;
289 }
290  
291 # process each file
292 0 my $file_count = 0;
293 0 foreach my $item_key ( keys( %{ $opts{files} } ) ) {
294 0 my $item = $opts{files}->{$item_key};
295 0 ▶ if ( !defined( $item->{instance} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"instance"}) 0 0 0
296 0 warn( 'No instance name specified for ' . $item_key . ' so using that as the instance name' );
297 0 $item->{instance} = $item_key;
298 }
299  
300 0 ▶ if ( !defined( $item->{type} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $item->{'type'}) { } 0 0 0
elsif ($item->{'type'} ne 'suricata' and $item->{'type'} ne 'sagan' and $item->{'type'} ne 'cape') { } 0 0 0
301 0 die( 'No type specified for ' . $item->{instance} );
302 ▶ } elsif ( $item->{type} ne 'suricata' && $item->{type} ne 'sagan' && $item->{type} ne 'cape' ) {
Condition0%0/6
$item->{"type"} ne "suricata" and $item->{"type"} ne "sagan" and $item->{"type"} ne "cape"
!ll&&!rl&&r
000
$item->{"type"} ne "suricata" and $item->{"type"} ne "sagan"
!ll&&!rl&&r
000
MC/DC0%0/3
$item->{"type"} ne "suricata" and $item->{"type"} ne "sagan" and $item->{"type"} ne "cape"
$item->{"type"} ne "suricata" $item->{"type"} ne "sagan" $item->{"type"} ne "cape"
Truth table0%0/4
A and B and C
A= $item->{"type"} ne "suricata"B= $item->{"type"} ne "sagan"C= $item->{"type"} ne "cape"
ABCresult
0XX0
10X0
1100
1111
303 0 die( 'Type, ' . $item->{type} . ', for instance ' . $item->{instance} . ' is not a known type' );
304 }
305  
306 0 ▶ if ( !defined( $item->{eve} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"eve"}) 0 0 0
307 0 die( 'No file specified for ' . $item->{instance} );
308 }
309  
310 # create each POE session out for each EVE file we are following
311 POE::Session->create(
312 inline_states => {
313 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
315 Filename => $_[HEAP]{eve},
316 0 InputEvent => "got_log_line",
317 );
318 },
319 0 ▶ got_log_line => sub {
Subroutine __ANON__: not called CC=46 SCAR=76.8
320 0 my $self = $_[HEAP]{self};
321 0 my $json;
322 0 eval { $json = decode_json( $_[ARG0] ) };
323 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
324 0 return;
325 }
326  
327 0 my $dbh;
328 0 eval { $dbh = DBI->connect_cached( $self->{dsn}, $self->{user}, $self->{pass} ); };
329 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
330 0 warn($@);
331 0 openlog( 'lilith', undef, 'daemon' );
332 0 syslog( 'LOG_ERR', $@ );
333 0 closelog;
334 }
335  
336 0 eval {
337 0 ▶ if ( defined($json)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json and defined $json->{"event_type"} and $json->{"event_type"} eq "alert") 0 0 0
Condition0%0/6
defined $json and defined $json->{"event_type"} and $json->{"event_type"} eq "alert"
!ll&&!rl&&r
000
defined $json and defined $json->{"event_type"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $json and defined $json->{"event_type"} and $json->{"event_type"} eq "alert"
defined $json defined $json->{"event_type"} $json->{"event_type"} eq "alert"
Truth table0%0/4
A and B and C
A= defined $jsonB= defined $json->{"event_type"}C= $json->{"event_type"} eq "alert"
ABCresult
0XX0
10X0
1100
1111
338 && defined( $json->{event_type} )
339 && $json->{event_type} eq 'alert' )
340 {
341 # put the event ID together
342 my $event_id
343 = sha256_base64( $_[HEAP]{instance}
344 . $_[HEAP]{host}
345 . $json->{timestamp}
346 . $json->{flow_id}
347 0 . $json->{in_iface} );
348  
349 # handle if suricata
350 0 ▶ if ( $_[HEAP]{type} eq 'suricata' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[3]{'type'} eq 'suricata') { } 0 0 0
elsif ($_[3]{'type'} eq 'sagan') { } 0 0 0
elsif ($_[3]{'type'} eq 'cape') { } 0 0 0
351 0 my $sth
352 = $dbh->prepare( 'insert into suricata_alerts'
353 . ' ( instance, host, timestamp, flow_id, event_id, in_iface, src_ip, src_port, dest_ip, dest_port, proto, app_proto, flow_pkts_toserver, flow_bytes_toserver, flow_pkts_toclient, flow_bytes_toclient, flow_start, classification, signature, gid, sid, rev, raw ) '
354 . ' VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
355 );
356 $sth->execute(
357 $_[HEAP]{instance}, $_[HEAP]{host},
358 $json->{timestamp}, $json->{flow_id},
359 $event_id, $json->{in_iface},
360 $json->{src_ip}, $json->{src_port},
361 $json->{dest_ip}, $json->{dest_port},
362 $json->{proto}, $json->{app_proto},
363 $json->{flow}{pkts_toserver}, $json->{flow}{bytes_toserver},
364 $json->{flow}{pkts_toclient}, $json->{flow}{bytes_toclient},
365 $json->{flow}{start}, $json->{alert}{category},
366 $json->{alert}{signature}, $json->{alert}{gid},
367 $json->{alert}{signature_id}, $json->{alert}{rev},
368 0 $_[ARG0]
369 );
370 } ## end if ( $_[HEAP]{type} eq 'suricata' )
371  
372 #handle if sagan
373 elsif ( $_[HEAP]{type} eq 'sagan' ) {
374 0 my $sth
375 = $dbh->prepare( 'insert into sagan_alerts'
376 . ' ( instance, instance_host, timestamp, event_id, flow_id, in_iface, src_ip, src_port, dest_ip, dest_port, proto, facility, host, level, priority, program, proto, xff, stream, classification, signature, gid, sid, rev, raw) '
377 . ' VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
378 );
379 $sth->execute(
380 $_[HEAP]{instance}, $_[HEAP]{host},
381 $json->{timestamp}, $event_id,
382 $json->{flow_id}, $json->{in_iface},
383 $json->{src_ip}, $json->{src_port},
384 $json->{dest_ip}, $json->{dest_port},
385 $json->{proto}, $json->{facility},
386 $json->{host}, $json->{level},
387 $json->{priority}, $json->{program},
388 $json->{proto}, $json->{xff},
389 $json->{stream}, $json->{alert}{category},
390 $json->{alert}{signature}, $json->{alert}{gid},
391 $json->{alert}{signature_id}, $json->{alert}{rev},
392 0 $_[ARG0],
393 );
394 } elsif ( $_[HEAP]{type} eq 'cape' ) {
395 0 my $sth
396 = $dbh->prepare( 'insert into cape_alerts'
397 . ' ( instance, target, instance_host, task, start, stop, malscore, subbed_from_ip, subbed_from_host, pkg, md5, sha1, sha256, slug, url, url_hostname, proto, src_ip, src_port, dest_ip, dest_port, size, raw ) '
398 . ' VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );'
399 );
400  
401 0 my $url;
402 0 ▶ if ( defined( $json->{http} ) && defined( $json->{http}{url} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"http"} and defined $json->{"http"}{"url"}) 0 0 0
Condition0%0/3
defined $json->{"http"} and defined $json->{"http"}{"url"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"http"} and defined $json->{"http"}{"url"}
defined $json->{"http"} defined $json->{"http"}{"url"}
Truth table0%0/3
A and B
A= defined $json->{"http"}B= defined $json->{"http"}{"url"}
ABresult
0X0
100
111
403 0 $url = $json->{http}{url};
404 }
405  
406 0 my $url_hostname;
407 0 ▶ if ( defined( $json->{http} ) && defined( $json->{http}{hostname} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"http"} and defined $json->{"http"}{"hostname"}) 0 0 0
Condition0%0/3
defined $json->{"http"} and defined $json->{"http"}{"hostname"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"http"} and defined $json->{"http"}{"hostname"}
defined $json->{"http"} defined $json->{"http"}{"hostname"}
Truth table0%0/3
A and B
A= defined $json->{"http"}B= defined $json->{"http"}{"hostname"}
ABresult
0X0
100
111
408 0 $url_hostname = $json->{http}{hostname};
409 }
410  
411 0 my $proto;
412 0 ▶ if ( defined( $json->{proto} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"proto"}) 0 0 0
413 0 $proto = $json->{proto};
414 }
415  
416 0 my $src_ip;
417 0 ▶ if ( defined( $json->{src_ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"src_ip"}) 0 0 0
418 0 $src_ip = $json->{src_ip};
419 }
420  
421 0 my $src_port;
422 0 ▶ if ( defined( $json->{src_port} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"src_port"}) 0 0 0
423 0 $src_port = $json->{src_port};
424 }
425  
426 0 my $dest_ip;
427 0 ▶ if ( defined( $json->{dest_ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"dest_ip"}) 0 0 0
428 0 $dest_ip = $json->{dest_ip};
429 }
430  
431 0 my $dest_port;
432 0 ▶ if ( defined( $json->{dest_port} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"dest_port"}) 0 0 0
433 0 $dest_port = $json->{dest_port};
434 }
435  
436 0 my $size;
437 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{size} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'size'}) { } 0 0 0
elsif (defined $json->{'fileinfo'} and defined $json->{'fileinfo'}{'size'}) { } 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"size"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"size"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"size"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"size"}
ABresult
0X0
100
111
438 0 $size = $json->{cape_submit}{size};
439 ▶ } elsif ( defined( $json->{fileinfo} ) && defined( $json->{fileinfo}{size} ) ) {
Condition0%0/3
defined $json->{"fileinfo"} and defined $json->{"fileinfo"}{"size"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"fileinfo"} and defined $json->{"fileinfo"}{"size"}
defined $json->{"fileinfo"} defined $json->{"fileinfo"}{"size"}
Truth table0%0/3
A and B
A= defined $json->{"fileinfo"}B= defined $json->{"fileinfo"}{"size"}
ABresult
0X0
100
111
440 0 $size = $json->{fileinfo}{size};
441 }
442  
443 # figure out what to use for the target
444 0 my $target;
445 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'name'}) { } 0 0 0
elsif (defined $json->{'suricata_extract_submit'} and defined $json->{'suricata_extract_submit'}{'name'}) { } 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"name"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"name"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"name"}
ABresult
0X0
100
111
446 0 $target = $json->{cape_submit}{name};
447 ▶ } elsif ( defined( $json->{suricata_extract_submit} )
Condition0%0/3
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"name"}
defined $json->{"suricata_extract_submit"} defined $json->{"suricata_extract_submit"}{"name"}
Truth table0%0/3
A and B
A= defined $json->{"suricata_extract_submit"}B= defined $json->{"suricata_extract_submit"}{"name"}
ABresult
0X0
100
111
448 && defined( $json->{suricata_extract_submit}{name} ) )
449 {
450 0 $target = $json->{suricata_extract_submit}{name};
451 } else {
452 0 $target = $json->{row}{target};
453 }
454  
455 0 my $subbed_from_ip;
456 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{remote_ip} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"remote_ip"}) 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"remote_ip"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"remote_ip"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"remote_ip"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"remote_ip"}
ABresult
0X0
100
111
457 {
458 0 $subbed_from_ip = $json->{cape_submit}{remote_ip};
459 }
460  
461 0 my $subbed_from_host;
462 0 ▶ if ( defined( $json->{suricata_extract_submit} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"host"}) 0 0 0
Condition0%0/3
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"host"}
defined $json->{"suricata_extract_submit"} defined $json->{"suricata_extract_submit"}{"host"}
Truth table0%0/3
A and B
A= defined $json->{"suricata_extract_submit"}B= defined $json->{"suricata_extract_submit"}{"host"}
ABresult
0X0
100
111
463 && defined( $json->{suricata_extract_submit}{host} ) )
464 {
465 0 $subbed_from_host = $json->{suricata_extract_submit}{host};
466 }
467  
468 0 my $md5;
469 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{md5} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'md5'}) { } 0 0 0
elsif (defined $json->{'suricata_extract_submit'} and defined $json->{'suricata_extract_submit'}{'md5'}) { } 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"md5"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"md5"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"md5"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"md5"}
ABresult
0X0
100
111
470 0 $md5 = $json->{cape_submit}{md5};
471 ▶ } elsif ( defined( $json->{suricata_extract_submit} )
Condition0%0/3
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"md5"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"md5"}
defined $json->{"suricata_extract_submit"} defined $json->{"suricata_extract_submit"}{"md5"}
Truth table0%0/3
A and B
A= defined $json->{"suricata_extract_submit"}B= defined $json->{"suricata_extract_submit"}{"md5"}
ABresult
0X0
100
111
472 && defined( $json->{suricata_extract_submit}{md5} ) )
473 {
474 0 $md5 = $json->{suricata_extract_submit}{md5};
475 }
476  
477 0 my $sha1;
478 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{sha1} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'sha1'}) { } 0 0 0
elsif (defined $json->{'suricata_extract_submit'} and defined $json->{'suricata_extract_submit'}{'sha1'}) { } 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"sha1"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"sha1"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"sha1"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"sha1"}
ABresult
0X0
100
111
479 0 $sha1 = $json->{cape_submit}{sha1};
480 ▶ } elsif ( defined( $json->{suricata_extract_submit} )
Condition0%0/3
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"sha1"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"sha1"}
defined $json->{"suricata_extract_submit"} defined $json->{"suricata_extract_submit"}{"sha1"}
Truth table0%0/3
A and B
A= defined $json->{"suricata_extract_submit"}B= defined $json->{"suricata_extract_submit"}{"sha1"}
ABresult
0X0
100
111
481 && defined( $json->{suricata_extract_submit}{sha1} ) )
482 {
483 0 $sha1 = $json->{suricata_extract_submit}{sha1};
484 }
485  
486 0 my $sha256;
487 0 ▶ if ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{sha256} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'sha256'}) { } 0 0 0
elsif (defined $json->{'suricata_extract_submit'} and defined $json->{'suricata_extract_submit'}{'sha256'}) { } 0 0 0
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"sha256"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"sha256"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"sha256"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"sha256"}
ABresult
0X0
100
111
488 0 $sha256 = $json->{cape_submit}{sha256};
489 ▶ } elsif ( defined( $json->{suricata_extract_submit} )
Condition0%0/3
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"sha256"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"suricata_extract_submit"} and defined $json->{"suricata_extract_submit"}{"sha256"}
defined $json->{"suricata_extract_submit"} defined $json->{"suricata_extract_submit"}{"sha256"}
Truth table0%0/3
A and B
A= defined $json->{"suricata_extract_submit"}B= defined $json->{"suricata_extract_submit"}{"sha256"}
ABresult
0X0
100
111
490 && defined( $json->{suricata_extract_submit}{sha256} ) )
491 {
492 0 $sha256 = $json->{suricata_extract_submit}{sha256};
493 }
494  
495 0 my $slug;
496 0 ▶ if ( defined( $json->{suricata_extract_submit}{slug} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $json->{'suricata_extract_submit'}{'slug'}) { } 0 0 0
elsif (defined $json->{'cape_submit'} and defined $json->{'cape_submit'}{'slug'}) { } 0 0 0
497 0 $slug = $json->{suricata_extract_submit}{slug};
498 ▶ } elsif ( defined( $json->{cape_submit} ) && defined( $json->{cape_submit}{slug} ) )
Condition0%0/3
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"slug"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $json->{"cape_submit"} and defined $json->{"cape_submit"}{"slug"}
defined $json->{"cape_submit"} defined $json->{"cape_submit"}{"slug"}
Truth table0%0/3
A and B
A= defined $json->{"cape_submit"}B= defined $json->{"cape_submit"}{"slug"}
ABresult
0X0
100
111
499 {
500 0 $slug = $json->{cape_submit}{slug};
501 }
502  
503 0 $target =~ s/^.*\///g;
504 $sth->execute(
505 $_[HEAP]{instance}, $target,
506 $_[HEAP]{host}, $json->{row}{id},
507 $json->{row}{started_on}, $json->{row}{completed_on},
508 $json->{malscore}, $subbed_from_ip,
509 $subbed_from_host, $json->{row}{package},
510 0 $md5, $sha1,
511 $sha256, $slug,
512 $url, $url_hostname,
513 $proto, $src_ip,
514 $src_port, $dest_ip,
515 $dest_port, $size,
516 $_[ARG0],
517 );
518 } ## end elsif ( $_[HEAP]{type} eq 'cape' )
519 } ## end if ( defined($json) && defined( $json->{event_type...}))
520 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
521 0 warn( 'SQL INSERT issue... ' . $@ );
522 0 openlog( 'lilith', undef, 'daemon' );
523 0 syslog( 'LOG_ERR', 'SQL INSERT issue... ' . $@ );
524 0 closelog;
525 }
526 } ## end eval
527  
528 },
529 },
530 heap => {
531 eve => $item->{eve},
532 type => $item->{type},
533 host => hostname,
534 instance => $item->{instance},
535 0 self => $self,
536 },
537 );
538  
539 } ## end foreach my $item_key ( keys( %{ $opts{files} } ...))
540  
541 0 POE::Kernel->run;
542 } ## end sub run
543  
544 =head2 extend
545  
546 my $return=$lilith->extend(
547 go_back_minutes=>5,
548 );
549  
550 =cut
551  
552 0 ▶sub extend {
Subroutine extend: not called CC=18 SCAR=58.3
Pod documented
553 0 my ( $self, %opts ) = @_;
554  
555 0 ▶ if ( !defined( $opts{go_back_minutes} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"go_back_minutes"}) 0 0 0
556 0 $opts{go_back_minutes} = 5;
557 }
558  
559 #
560 # basic initial stuff
561 #
562  
563 # librenms return hash
564 0 my $to_return = {
565 data => {
566 totals => { total => 0, },
567 sagan_instances => {},
568 suricata_instances => {},
569 sagan_totals => { total => 0, },
570 suricata_totals => { total => 0, },
571 },
572 version => 1,
573 error => '0',
574 errorString => '',
575 };
576  
577 #
578 # Do the search in eval incase of failure
579 #
580  
581 0 my $sagan_found = ();
582 0 my $suricata_found = ();
583 0 eval {
584 0 my $dbh;
585 0 eval { $dbh = DBI->connect_cached( $self->{dsn}, $self->{user}, $self->{pass} ); };
586 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
587 0 die( 'DBI->connect_cached failure.. ' . $@ );
588 }
589  
590 0 my $hostname = hostname;
591  
592 #
593 # suricata SQL bit
594 #
595  
596 my $sql
597 = 'select * from suricata_alerts'
598 . " where timestamp >= CURRENT_TIMESTAMP - interval '"
599 . $opts{go_back_minutes}
600 0 . " minutes' and host ='"
601 . $hostname . "'";
602  
603 0 $sql = $sql . ';';
604 0 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
605 0 warn( 'SQL search "' . $sql . '"' );
606 }
607 0 my $sth = $dbh->prepare($sql);
608 0 $sth->execute();
609  
610 0 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
611 0 push( @{$suricata_found}, $row );
612 }
613  
614 #
615 # Sagan SQL bit
616 #
617  
618 $sql
619 = 'select * from sagan_alerts'
620 . " where timestamp >= CURRENT_TIMESTAMP - interval '"
621 . $opts{go_back_minutes}
622 0 . " minutes' and instance_host = '"
623 . $hostname . "'";
624  
625 0 $sql = $sql . ';';
626 0 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
627 0 warn( 'SQL search "' . $sql . '"' );
628 }
629 0 $sth = $dbh->prepare($sql);
630 0 $sth->execute();
631  
632 0 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
633 0 push( @{$sagan_found}, $row );
634 }
635  
636 };
637 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
638 0 $to_return->{error} = 1;
639 0 $to_return->{errorString} = $@;
640 }
641  
642 0 foreach my $row ( @{$suricata_found} ) {
643 0 $to_return->{data}{totals}{total}++;
644 0 $to_return->{data}{suricata_totals}{total}++;
645 0 my $snmp_class = $self->get_short_class_snmp( $row->{classification} );
646 0 ▶ if ( !defined( $to_return->{data}{totals}{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'totals'}{$snmp_class}) { } 0 0 0
647 0 $to_return->{data}{totals}{$snmp_class} = 1;
648 } else {
649 0 $to_return->{data}{totals}{$snmp_class}++;
650 }
651 0 ▶ if ( !defined( $to_return->{data}{suricata_totals}{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'suricata_totals'}{$snmp_class}) { } 0 0 0
652 0 $to_return->{data}{suricata_totals}{$snmp_class} = 1;
653 } else {
654 0 $to_return->{data}{suricata_totals}{$snmp_class}++;
655 }
656 0 ▶ if ( !defined( $to_return->{data}{suricata_instances}{ $row->{instance} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_return->{"data"}{"suricata_instances"}{$row->{"instance"}}) 0 0 0
657 0 $to_return->{data}{suricata_instances}{ $row->{instance} } = { total => 0 };
658 }
659 0 $to_return->{data}{suricata_instances}{ $row->{instance} }{total}++;
660 0 ▶ if ( !defined( $to_return->{data}{suricata_instances}{ $row->{instance} }{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'suricata_instances'}{$row->{'instance'}}{$snmp_class}) { } 0 0 0
661 0 $to_return->{data}{suricata_instances}{ $row->{instance} }{$snmp_class} = 1;
662 } else {
663 0 $to_return->{data}{suricata_instances}{ $row->{instance} }{$snmp_class}++;
664 }
665 } ## end foreach my $row ( @{$suricata_found} )
666  
667 0 foreach my $row ( @{$sagan_found} ) {
668 0 $to_return->{data}{totals}{total}++;
669 0 $to_return->{data}{sagan_totals}{total}++;
670 0 my $snmp_class = $self->get_short_class_snmp( $row->{classification} );
671 0 ▶ if ( !defined( $to_return->{data}{totals}{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'totals'}{$snmp_class}) { } 0 0 0
672 0 $to_return->{data}{totals}{$snmp_class} = 1;
673 } else {
674 0 $to_return->{data}{totals}{$snmp_class}++;
675 }
676 0 ▶ if ( !defined( $to_return->{data}{sagan_totals}{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'sagan_totals'}{$snmp_class}) { } 0 0 0
677 0 $to_return->{data}{sagan_totals}{$snmp_class} = 1;
678 } else {
679 0 $to_return->{data}{sagan_totals}{$snmp_class}++;
680 }
681 0 ▶ if ( !defined( $to_return->{data}{sagan_instances}{ $row->{instance} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_return->{"data"}{"sagan_instances"}{$row->{"instance"}}) 0 0 0
682 0 $to_return->{data}{sagan_instances}{ $row->{instance} } = { total => 0 };
683 }
684 0 $to_return->{data}{sagan_instances}{ $row->{instance} }{total}++;
685 0 ▶ if ( !defined( $to_return->{data}{sagan_instances}{ $row->{instance} }{$snmp_class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $to_return->{'data'}{'sagan_instances'}{$row->{'instance'}}{$snmp_class}) { } 0 0 0
686 0 $to_return->{data}{sagan_instances}{ $row->{instance} }{$snmp_class} = 1;
687 } else {
688 0 $to_return->{data}{sagan_instances}{ $row->{instance} }{$snmp_class}++;
689 }
690 } ## end foreach my $row ( @{$sagan_found} )
691  
692 0 return $to_return;
693 } ## end sub extend
694  
695 =head2 get_short_class
696  
697 Get SNMP short class name for a class.
698  
699 my $short_class_name=$lilith->get_short_class($class);
700  
701 =cut
702  
703 12 ▶sub get_short_class {
Subroutine get_short_class: called CC=3 SCAR=11.0
Pod documented
704 12 my ( $self, $class ) = @_;
705  
706 12 ▶ if ( !defined($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 2 10 12
707 2 return ('undefC');
708 }
709  
710 10 ▶ if ( defined( $self->{lc_class_map}->{ lc($class) } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"lc_class_map"}{lc $class}) 9 1 10
711 9 return $self->{lc_class_map}->{ lc($class) };
712 }
713  
714 1 return ('unknownC');
715 } ## end sub get_short_class
716  
717 =head2 get_short_class_snmp
718  
719 Get SNMP short class name for a class. This
720 is the same as the short class name, but with /^\!/
721 replaced with 'not_'.
722  
723 my $snmp_class_name=$lilith->get_short_class_snmp($class);
724  
725 =cut
726  
727 8 ▶sub get_short_class_snmp {
Subroutine get_short_class_snmp: called CC=3 SCAR=11.0
Pod documented
728 8 my ( $self, $class ) = @_;
729  
730 8 ▶ if ( !defined($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 2 6 8
731 2 return ('undefC');
732 }
733  
734 6 ▶ if ( defined( $self->{snmp_class_map}->{ lc($class) } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"snmp_class_map"}{lc $class}) 5 1 6
735 5 return $self->{snmp_class_map}->{ lc($class) };
736 }
737  
738 1 return ('unknownC');
739 } ## end sub get_short_class_snmp
740  
741 =head2 get_short_class_snmp_list
742  
743 Gets a list of short SNMP class names.
744  
745 my $snmp_classes=$lilith->get_short_class_snmp_list;
746  
747 foreach my $item (@{ $snmp_classes }){
748 print $item."\n";
749 }
750  
751 =cut
752  
753 1 ▶sub get_short_class_snmp_list {
Subroutine get_short_class_snmp_list: called CC=2 SCAR=6.9
Pod documented
754 1 my ($self) = @_;
755  
756 1 my $snmp_classes = [ 'undefC', 'unknownC' ];
757 1 foreach my $item ( keys( %{ $self->{snmp_class_map} } ) ) {
758 56 push( @{$snmp_classes}, $self->{snmp_class_map}{$item} );
759 }
760  
761 1 return $snmp_classes;
762 } ## end sub get_short_class_snmp_list
763  
764 =head2 search
765  
766 Searches the specified table and returns a array of found rows. This is a wrapper around
767 Lilith::Schema. If you are looking for something more complex, read L<DBIx::Class>,
768 L<SQL::Abstract::Classic>, and L<Lilith::Schema>.
769  
770 - table :: 'suricata', 'cape', 'sagan' depending on the desired table to
771 use. Will die if something other is specified. The table
772 name used is based on what was passed to new(if not the
773 default).
774 Default :: suricata
775  
776 - go_back_minutes :: How far back to search in minutes.
777 Default :: 1440
778  
779 - limit :: Limit on how many to return.
780 Default :: undef
781  
782 - offset :: Offset for when using limit.
783 Default :: undef
784  
785 - order_by :: Column to order by.
786 Default :: timetamp
787 Cape Default :: id
788  
789 - order_dir :: Direction to order.
790 Default :: ASC
791  
792 Below are simple search items that if given will be matched via a basic equality.
793  
794 - src_ip
795 - dest_ip
796 - event_id
797 - md5
798 - sha1
799 - sha256
800 - subbed_from_ip
801  
802 # will become "and src_ip = '192.168.1.2'"
803 src_ip => '192.168.1.2',
804  
805 Below are a list of numeric items. The value taken is a array and anything
806 prefixed '!' with add as a and not equal.
807  
808 - src_port
809 - dest_port
810 - gid
811 - sid
812 - rev
813 - id
814 - size
815 - malscore
816 - task
817  
818 # will become "and src_port = '22' and src_port != ''512'"
819 src_port => ['22', '!512'],
820  
821 Below are a list of string items.
822  
823 - host
824 - instance_host
825 - instance
826 - class
827 - signature
828 - app_proto
829 - in_iface
830 - url
831 - url_hostname
832 - slug
833 - pkg
834  
835 # will become "and host = 'foo.bar'"
836 host => 'foo.bar',
837  
838 # will become "and class != 'foo'"
839 class => '!foo',
840  
841 # will become "and instance like '%foo'"
842 instance => '%foo',
843  
844 # will become "and instance not like '%foo'"
845 instance => '!%foo',
846  
847 Below are complex items.
848  
849 - ip
850 - port
851  
852 # will become "and ( src_ip != '192.168.1.2' or dest_ip != '192.168.1.2' )"
853 ip => '192.16.1.2'
854  
855 # will become "and ( src_port != '22' or dest_port != '22' )"
856 port => '22'
857  
858 =cut
859  
860 0 ▶sub search {
Subroutine search: not called CC=41 SCAR=74.5
Pod documented
861 0 my ( $self, %opts ) = @_;
862  
863 0 ▶ if ( defined( $opts{table} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"table"}) 0 0 0
864 0 ▶ if ( $opts{table} ne 'suricata' && $opts{table} ne 'sagan' && $opts{table} ne 'cape' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"table"} ne "suricata" and $opts{"table"} ne "sagan" and $opts{"table"} ne "cape") 0 0 0
Condition0%0/6
$opts{"table"} ne "suricata" and $opts{"table"} ne "sagan" and $opts{"table"} ne "cape"
!ll&&!rl&&r
000
$opts{"table"} ne "suricata" and $opts{"table"} ne "sagan"
!ll&&!rl&&r
000
MC/DC0%0/3
$opts{"table"} ne "suricata" and $opts{"table"} ne "sagan" and $opts{"table"} ne "cape"
$opts{"table"} ne "suricata" $opts{"table"} ne "sagan" $opts{"table"} ne "cape"
Truth table0%0/4
A and B and C
A= $opts{"table"} ne "suricata"B= $opts{"table"} ne "sagan"C= $opts{"table"} ne "cape"
ABCresult
0XX0
10X0
1100
1111
865 0 die( '"' . $opts{table} . '" is not a known table type' );
866 }
867 }
868 0 my $table = 'SuricataAlert';
869 0 my $default_order_by = 'timestamp';
870 0 ▶ if ( $opts{table} eq 'sagan' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'table'} eq 'sagan') { } 0 0 0
elsif ($opts{'table'} eq 'cape') { } 0 0 0
871 0 $table = 'SaganAlert';
872 } elsif ( $opts{table} eq 'cape' ) {
873 0 $table = 'CapeAlert';
874 0 $default_order_by = 'id';
875 }
876  
877 0 ▶ if ( !defined( $opts{order_by} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"order_by"}) 0 0 0
878 0 $opts{order_by} = $default_order_by;
879 }
880  
881 0 ▶ if ( !defined( $opts{go_back_minutes} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'go_back_minutes'}) { } 0 0 0
882 0 $opts{go_back_minutes} = '1440';
883 } else {
884 0 ▶ if ( $opts{go_back_minutes} !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"go_back_minutes"} =~ /^[0-9]+$/) 0 0 0
885 0 die( '"' . $opts{go_back_minutes} . '" for go_back_minutes is not numeric' );
886 }
887 }
888 0 my $go_back_time = 'CURRENT_TIMESTAMP - interval \'' . $opts{go_back_minutes} . ' minutes\'';
889  
890 0 ▶ if ( defined( $opts{order_dir} ) && $opts{order_dir} ne 'ASC' && $opts{order_dir} ne 'DESC' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'order_dir'} and $opts{'order_dir'} ne 'ASC' and $opts{'order_dir'} ne 'DESC') { } 0 0 0
elsif (not defined $opts{'order_dir'}) { } 0 0 0
Condition0%0/6
defined $opts{"order_dir"} and $opts{"order_dir"} ne "ASC" and $opts{"order_dir"} ne "DESC"
!ll&&!rl&&r
000
defined $opts{"order_dir"} and $opts{"order_dir"} ne "ASC"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $opts{"order_dir"} and $opts{"order_dir"} ne "ASC" and $opts{"order_dir"} ne "DESC"
defined $opts{"order_dir"} $opts{"order_dir"} ne "ASC" $opts{"order_dir"} ne "DESC"
Truth table0%0/4
A and B and C
A= defined $opts{"order_dir"}B= $opts{"order_dir"} ne "ASC"C= $opts{"order_dir"} ne "DESC"
ABCresult
0XX0
10X0
1100
1111
891 0 die( '"' . $opts{order_dir} . '" for order_dir must by either ASC or DESC' );
892 } elsif ( !defined( $opts{order_dir} ) ) {
893 0 $opts{order_dir} = 'ASC';
894 }
895  
896 0 my $go_back_column = 'timestamp';
897 0 ▶ if ( $opts{table} eq 'cape' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"table"} eq "cape") 0 0 0
898 0 $go_back_column = 'stop';
899 }
900  
901 0 my $schema = Lilith::Schema->connect( $self->{dsn}, $self->{user}, $self->{pass}, );
902  
903 0 my $search = { $go_back_column => { '>=', \$go_back_time } };
904  
905 #
906 # add simple items
907 #
908  
909 0 my @simple = ( 'src_ip', 'dest_ip', 'proto', 'event_id', 'md5', 'sha1', 'sha256', 'subbed_from_ip' );
910  
911 0 foreach my $item (@simple) {
912 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
913 0 $search->{$item} = $opts{$item};
914 }
915 }
916  
917 #
918 # add numeric items
919 #
920  
921 0 my @numeric = ( 'src_port', 'dest_port', 'gid', 'sid', 'rev', 'id', 'size', 'malscore', 'task' );
922  
923 0 foreach my $item (@numeric) {
924 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
925 # process each item
926 0 my @found;
927 0 foreach my $arg ( @{ $opts{$item} } ) {
928 0 $arg =~ s/[\ \t]//g;
929  
930 0 my $equality = '=';
931 0 my $number;
932  
933 # match the start of the item
934 0 ▶ if ( $arg =~ /^[0-9]+$/ ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($arg =~ /^[0-9]+$/) { } 0 0 0
elsif ($arg =~ /^\<\=[0-9]+$/) { } 0 0 0
elsif ($arg =~ /^\<[0-9]+$/) { } 0 0 0
elsif ($arg =~ /^\>\=[0-9]+$/) { } 0 0 0
elsif ($arg =~ /^\>[0-9]+$/) { } 0 0 0
elsif ($arg =~ /^\![0-9]+$/) { } 0 0 0
elsif ($arg =~ /^$/) { } 0 0 0
935 0 $number = $arg;
936 } elsif ( $arg =~ /^\<\=[0-9]+$/ ) {
937 0 $arg =~ s/^\<\=//;
938 0 $equality = '<=';
939 0 $number = $arg;
940 } elsif ( $arg =~ /^\<[0-9]+$/ ) {
941 0 $arg =~ s/^\<//;
942 0 $equality = '<';
943 0 $number = $arg;
944 } elsif ( $arg =~ /^\>\=[0-9]+$/ ) {
945 0 $arg =~ s/^\>\=//;
946 0 $equality = '>=';
947 0 $number = $arg;
948 } elsif ( $arg =~ /^\>[0-9]+$/ ) {
949 0 $arg =~ s/^\>\=//;
950 0 $equality = '>';
951 0 $number = $arg;
952 } elsif ( $arg =~ /^\![0-9]+$/ ) {
953 0 $arg =~ s/^\!//;
954 0 $equality = '!=';
955 0 $number = $arg;
956 } elsif ( $arg =~ /^$/ ) {
957  
958 # only exists for skipping when some one has passes something starting
959 # with a ,, ending with a,, or with ,, in it.
960 } else {
961 # if we get here, it means we don't have a valid use case for what ever was passed and should error
962 0 die( '"' . $arg . '" does not appear to be a valid item for a numeric search for the ' . $item );
963 }
964 0 ▶ if ( defined($number) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number) 0 0 0
965 0 print "test\n";
966 0 push( @found, { $equality, $number } );
967 }
968 } ## end foreach my $arg ( @{ $opts{$item} } )
969 0 ▶ if ( defined( $found[0] ) && !defined( $found[1] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $found[0] and not defined $found[1]) { } 0 0 0
elsif (defined $found[0] and defined $found[1]) { } 0 0 0
Condition0%0/3
defined $found[0] and not defined $found[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found[0] and not defined $found[1]
defined $found[0] not defined $found[1]
Truth table0%0/3
A and not(B)
A= defined $found[0]B= not defined $found[1]
ABresult
0X0
100
111
970 0 $search->{$item} = $found[0];
971 ▶ } elsif ( defined( $found[0] ) && defined( $found[1] ) ) {
Condition0%0/3
defined $found[0] and defined $found[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $found[0] and defined $found[1]
defined $found[0] defined $found[1]
Truth table0%0/3
A and B
A= defined $found[0]B= defined $found[1]
ABresult
0X0
100
111
972 0 $search->{$item} = [ '-and' => \@found ];
973 }
974 } ## end if ( defined( $opts{$item} ) )
975 } ## end foreach my $item (@numeric)
976  
977 #
978 # more complex items
979 #
980  
981 0 ▶ if ( defined( $opts{ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ip"}) 0 0 0
982 0 ▶ if ( !defined( $search->{'-and'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $search->{"-and"}) 0 0 0
983 0 $search->{'-and'} = [];
984 }
985 0 $search->{'-and'} = [ { '-or' => { src_ip => { '=', $opts{ip} }, dest_ip => { '=', $opts{ip} } } } ];
986 }
987  
988 0 ▶ if ( defined( $opts{port} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"port"}) 0 0 0
989 0 ▶ if ( !defined( $search->{'-and'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $search->{"-and"}) 0 0 0
990 0 $search->{'-and'} = [];
991 }
992 push(
993 0 @{ $search->{'-and'} },
994 0 [ { '-or' => { src_port => { '=', $opts{port} }, dest_port => { '=', $opts{port} } } } ]
995 );
996 }
997  
998 #
999 # handle string items
1000 #
1001  
1002 0 my @strings = (
1003 'host', 'instance_host', 'instance', 'class',
1004 'signature', 'app_proto', 'in_iface', 'url',
1005 'url_hostname', 'slug', 'pkg', 'subbed_from_host'
1006 );
1007 0 foreach my $item (@strings) {
1008 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
1009 0 ▶ if ( $opts{$item} =~ /\%/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$item} =~ /\%/) { } 0 0 0
1010 0 ▶ if ( $opts{$item} =~ /^\!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$item} =~ /^\!/) { } 0 0 0
1011 0 $opts{$item} =~ s/^\!//;
1012 0 $search->{$item} = { '-not_like', $opts{$item} };
1013 } else {
1014 0 $search->{$item} = { 'like', $opts{$item} };
1015 }
1016 } else {
1017 0 ▶ if ( $opts{$item} =~ /^\!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$item} =~ /^\!/) { } 0 0 0
1018 0 $opts{$item} =~ s/^\!//;
1019 0 $search->{$item} = { '!=', $opts{$item} };
1020 } else {
1021 0 $search->{$item} = { '=', $opts{$item} };
1022 }
1023 }
1024 } ## end if ( defined( $opts{$item} ) )
1025 } ## end foreach my $item (@strings)
1026  
1027 my @fetch_results = $schema->resultset($table)->search(
1028 $search,
1029 {
1030 order_by => $opts{order_by} . ' ' . $opts{order_dir},
1031 0 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
1032 }
1033 )->all;
1034  
1035 0 return \@fetch_results;
1036 } ## end sub search
1037  
1038 =head1 AUTHOR
1039  
1040 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
1041  
1042 =head1 BUGS
1043  
1044 Please report any bugs or feature requests to C<bug-lilith at rt.cpan.org>, or through
1045 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lilith>. I will be notified, and then you'll
1046 automatically be notified of progress on your bug as I make changes.
1047  
1048  
1049  
1050  
1051 =head1 SUPPORT
1052  
1053 You can find documentation for this module with the perldoc command.
1054  
1055 perldoc Lilith
1056  
1057  
1058 You can also look for information at:
1059  
1060 =over 4
1061  
1062 =item * RT: CPAN's request tracker (report bugs here)
1063  
1064 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Lilith>
1065  
1066 =item * CPAN Ratings
1067  
1068 L<https://cpanratings.perl.org/d/Lilith>
1069  
1070 =item * Search CPAN
1071  
1072 L<https://metacpan.org/release/Lilith>
1073  
1074 =back
1075  
1076  
1077 =head1 ACKNOWLEDGEMENTS
1078  
1079  
1080 =head1 LICENSE AND COPYRIGHT
1081  
1082 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
1083  
1084 This is free software, licensed under:
1085  
1086 The Artistic License 2.0 (GPL Compatible)
1087  
1088  
1089 =cut
1090  
1091 3 1; # End of Lilith
↑ Summary Lilith/Schema.pm »