blib/lib/CAPE/Utils.pm

filter: Statementstmt 8.2% 49 / 592 Branchbran 0.0% 0 / 280 Conditioncond 0.0% 0 / 161 MC/DCmcdc 0.0% 0 / 85 Subroutinesub 44.4% 16 / 36 Podpod 95.0% 19 / 20 totaltotal 7.1% 84 / 1174 CC 221 SCAR 60.6
CC
221
Cov
5%
get_tasks_table
870.0
munge
756.0
submit
756.0
CRAP 426.7
↑ Summary
1 package CAPE::Utils;
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 JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Slurp qw(append_file write_file read_file write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IPC::Cmd qw[ run ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::ANSITable;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::Cmd qw(run);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::Subnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Sys::Syslog;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 CAPE::Utils - A helpful library for with CAPE.
23  
24 =head1 VERSION
25  
26 Version 4.0.0
27  
28 =cut
29  
30 1 our $VERSION = '4.0.0';
31  
32 =head1 SYNOPSIS
33  
34 Quick summary of what the module does.
35  
36 Perhaps a little code snippet.
37  
38 use CAPE::Utils;
39  
40 my $cape_util=CAPE::Utils->new();
41  
42 my $sub_results=$cape_util->submit(items=>@to_detonate,unique=>0, quiet=>1);
43 use JSON;
44 print encode_json($sub_results)."\n";
45  
46 =head1 METHODS
47  
48 =head2 new
49  
50 Initiates the object. One argument is taken and that is the
51 path to the INI config file. The default is '/usr/local/etc/cape_utils.ini'
52 and if not found, the defaults will be used.
53  
54 my $cape_util=CAPE::Utils->new('/path/to/some/config.ini');
55  
56 =cut
57  
58 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
59 0 my $ini = $_[1];
60  
61 0 ▶ if ( !defined($ini) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ini) 0 0 0
62 0 $ini = '/usr/local/etc/cape_utils.ini';
63 }
64  
65 0 my $base_config = {
66 '_' => {
67 dsn => 'dbi:Pg:dbname=cape',
68 user => 'cape',
69 pass => '',
70 base => '/opt/CAPEv2/',
71 eve => '/opt/CAPEv2/log/eve.json',
72 poetry => 1,
73 fail_all => 0,
74 pending_columns => 'id,target,package,timeout,ET,route,options,clock,added_on',
75 running_columns => 'id,target,package,timeout,ET,route,options,clock,added_on,started_on,machine',
76 task_columns => 'id,target,package,timeout,ET,route,options,clock,added_on,latest,machine,status',
77 running_target_clip => 1,
78 running_time_clip => 1,
79 pending_target_clip => 1,
80 pending_time_clip => 1,
81 task_target_clip => 1,
82 task_time_clip => 1,
83 table_color => 'Text::ANSITable::Standard::NoGradation',
84 table_border => 'ASCII::None',
85 set_clock_to_now => 1,
86 timeout => 200,
87 enforce_timeout => 0,
88 subnets => '192.168.0.0/16,127.0.0.1/8,::1/128,172.16.0.0/12,10.0.0.0/8',
89 apikey => '',
90 auth => 'ip',
91 incoming => '/malware/client-incoming',
92 incoming_json => '/malware/incoming-json',
93 eve_look_back => 360,
94 malscore => 0,
95 poetry_path => '/etc/poetry/bin/poetry',
96 },
97 };
98  
99 0 my $config = Config::Tiny->read( $ini, 'utf8' );
100 0 ▶ if ( !defined($config) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $config) { } 0 0 0
101 0 $config = $base_config;
102 } else {
103 0 my @to_merge = keys( %{ $base_config->{_} } );
104 0 foreach my $item (@to_merge) {
105 0 ▶ if ( !defined( $config->{_}->{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"_"}{$item}) 0 0 0
106 0 $config->{_}->{$item} = $base_config->{_}->{$item};
107 }
108 }
109 }
110  
111 # init the object
112 0 my $self = { config => $config, };
113 0 bless $self;
114  
115 0 return $self;
116 } ## end sub new
117  
118 =head2 connect
119  
120 Return a DBH from DBI->connect for the CAPE SQL server.
121  
122 This will die with the output from $DBI::errstr if it fails.
123  
124 my $dbh = $cape->connect;
125  
126 =cut
127  
128 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = $_[0];
130  
131 my $dbh = DBI->connect( $self->{config}->{_}->{dsn}, $self->{config}->{_}->{user}, $self->{config}->{_}->{pass} )
132 0 ▶ || die($DBI::errstr);
Condition0%0/2
'DBI'->connect($self->{'config'}{'_'}{'dsn'}, $self->{'config'}{'_'}{'user'}, $self->{'config'}{'_'}{'pass'}) || die($DBI::errstr)
l!l
00
MC/DC0%0/1
'DBI'->connect($self->{'config'}{'_'}{'dsn'}, $self->{'config'}{'_'}{'user'}, $self->{'config'}{'_'}{'pass'}) || die($DBI::errstr)
'DBI'->connect($self->{'config'}{'_'}{'dsn'}, $self->{'config'}{'_'}{'user'}, $self->{'config'}{'_'}{'pass'})
Truth table0%0/2
A
A= 'DBI'->connect($self->{'config'}{'_'}{'dsn'}, $self->{'config'}{'_'}{'user'}, $self->{'config'}{'_'}{'pass'})
Aresult
11
00
133  
134 0 return $dbh;
135 }
136  
137 =head2 fail
138  
139 Set one or more pending tasks to failed as below.
140  
141 UPDATE tasks SET status = 'failed_processing' WHERE status = 'pending'
142  
143 The following options are also supported.
144  
145 - where :: Additional SQL where statements to add. Something must
146 be specified for this, unless fail_all in the config is
147 true. Otherwise this method will die.
148  
149 my $rows=$cape_util->fail( where=>"target like '%foo%'");
150  
151 =cut
152  
153 0 ▶sub fail {
Subroutine fail: not called CC=6 SCAR=37.4
Pod documented
154 0 my ( $self, %opts ) = @_;
155  
156 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
157 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
158 }
159  
160 0 ▶ if ( !defined( $opts{where} ) && !$self->{config}->{_}->{fail_all} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"where"} or $self->{"config"}{"_"}{"fail_all"}) 0 0 0
Condition0%0/3
defined $opts{"where"} or $self->{"config"}{"_"}{"fail_all"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $opts{"where"} or $self->{"config"}{"_"}{"fail_all"}
defined $opts{"where"} $self->{"config"}{"_"}{"fail_all"}
Truth table0%0/3
A or B
A= defined $opts{"where"}B= $self->{"config"}{"_"}{"fail_all"}
ABresult
1X1
011
000
161 0 die "fail_all is disabled and nothing specified for where";
162 }
163  
164 0 my $dbh = $self->connect;
165  
166 0 my $statement = "UPDATE tasks SET status = 'failed_processing' WHERE status = 'pending'";
167  
168 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
169 0 $statement = $statement . ' AND ' . $opts{where};
170 }
171  
172 0 $statement = $statement . ';';
173  
174 0 my $sth = $dbh->prepare($statement);
175  
176 0 $sth->execute;
177  
178 0 my $rows = $sth->rows;
179  
180 0 $sth->finish;
181 0 $dbh->disconnect;
182  
183 0 return $rows;
184 } ## end sub fail
185  
186 =head2 get_pending_count
187  
188 Get pending count pending tasks.
189  
190 - where :: And additional where part of the SQL statement. "and" will
191 automatically be used to join it with the rest of the
192 statement. May not contain a ';'.
193 - Default :: undef
194  
195 my $count=$cape_util->get_pending_count;
196  
197 =cut
198  
199 0 ▶sub get_pending_count {
Subroutine get_pending_count: not called CC=4 SCAR=30.0
Pod documented
200 0 my ( $self, %opts ) = @_;
201  
202 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
203 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
204 }
205  
206 0 my $dbh = $self->connect;
207  
208 0 my $statement = "select * from tasks where status = 'pending'";
209 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
210 0 $statement = $statement . ' AND ' . $opts{where};
211 }
212  
213 0 my $sth = $dbh->prepare($statement);
214 0 $sth->execute;
215  
216 0 my $rows = $sth->rows;
217  
218 0 $sth->finish;
219 0 $dbh->disconnect;
220  
221 0 return $rows;
222 } ## end sub get_pending_count
223  
224 =head2 get_pending
225  
226 Returns a arrah ref of hash refs of rows from the tasks table where the
227 status is set to pending via "select * from tasks where status = 'pending'"
228  
229 - where :: And additional where part of the SQL statement. "and" will
230 automatically be used to join it with the rest of the
231 statement. May not contain a ';'.
232 - Default :: undef
233  
234 - where :: Additional SQL where statements to add.
235  
236 =cut
237  
238 0 ▶sub get_pending {
Subroutine get_pending: not called CC=5 SCAR=34.0
Pod documented
239 0 my ( $self, %opts ) = @_;
240  
241 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
242 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
243 }
244  
245 0 my $dbh = $self->connect;
246  
247 0 my $statement = "select * from tasks where status = 'pending'";
248 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
249 0 $statement = $statement . ' AND ' . $opts{where};
250 }
251  
252 0 my $sth = $dbh->prepare($statement);
253 0 $sth->execute;
254  
255 0 my $row;
256 my @rows;
257 0 ▶ while ( $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_hashref) 0 0 0
258 0 push( @rows, $row );
259 }
260  
261 0 $sth->finish;
262 0 $dbh->disconnect;
263  
264 0 return \@rows;
265 } ## end sub get_pending
266  
267 =head2 get_pending_table
268  
269 Generates a ASCII table for pending.
270  
271 The following config variables can are relevant to this and
272 may be overriden.
273  
274 table_border
275 table_color
276 pending_columns
277 pending_target_clip
278 pending_time_clip
279  
280 The following options are also supported.
281  
282 - where :: And additional where part of the SQL statement. "and" will
283 automatically be used to join it with the rest of the
284 statement. May not contain a ';'.
285 - Default :: undef
286  
287 print $cape_util->get_pending_table( pending_columns=>'id,package');
288  
289 =cut
290  
291 0 ▶sub get_pending_table {
Subroutine get_pending_table: not called CC=15 SCAR=54.8
Pod documented
292 0 my ( $self, %opts ) = @_;
293  
294 0 my @overrides = ( 'table_border', 'table_color', 'pending_columns', 'pending_target_clip', 'pending_time_clip' );
295 0 foreach my $override (@overrides) {
296 0 ▶ if ( !defined( $opts{$override} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{$override}) 0 0 0
297 0 $opts{$override} = $self->{config}->{_}->{$override};
298 }
299 }
300  
301 0 my $rows = $self->get_pending( where => $opts{where} );
302  
303 0 my $tb = Text::ANSITable->new;
304 0 $tb->border_style( $opts{table_border} );
305 0 $tb->color_theme( $opts{table_color} );
306  
307 0 my @columns = split( /,/, $opts{pending_columns} );
308 0 my $header_int = 0;
309 0 my $padding = 0;
310 0 foreach my $header (@columns) {
311 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
312 0 else { $padding = 0; }
313  
314 0 $tb->set_column_style( $header_int, pad => $padding );
315  
316 0 $header_int++;
317 }
318  
319 0 $tb->columns( \@columns );
320  
321 0 my @td;
322 0 foreach my $row ( @{$rows} ) {
323 0 my @new_line;
324 0 foreach my $column (@columns) {
325 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
326 0 $row->{ET} = $row->{enforce_timeout};
327 }
328  
329 0 ▶ if ( defined( $row->{$column} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{$column}) { } 0 0 0
330 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
331 0 $row->{ET} = $row->{enforce_timeout};
332 }
333  
334 0 ▶ if ( ( $column eq 'clock' || $column eq 'added_on' ) && $opts{pending_time_clip} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($column eq 'clock' || $column eq 'added_on' and $opts{'pending_time_clip'}) { } 0 0 0
elsif ($column eq 'target' and $opts{'pending_target_clip'}) { } 0 0 0
Condition0%0/6
$column eq "clock" || $column eq "added_on" and $opts{"pending_time_clip"}
!ll&&!rl&&r
000
$column eq "clock" or $column eq "added_on"
l!l&&r!l&&!r
000
MC/DC0%0/3
$column eq "clock" || $column eq "added_on" and $opts{"pending_time_clip"}
$column eq "clock" $column eq "added_on" $opts{"pending_time_clip"}
Truth table0%0/5
A or B and C
A= $column eq "clock"B= $column eq "added_on"C= $opts{"pending_time_clip"}
ABCresult
00X0
1X00
0100
1X11
0111
335 0 $row->{$column} =~ s/\.[0-9]+$//;
336 ▶ } elsif ( $column eq 'target' && $opts{pending_target_clip} ) {
Condition0%0/3
$column eq "target" and $opts{"pending_target_clip"}
!ll&&!rl&&r
000
MC/DC0%0/2
$column eq "target" and $opts{"pending_target_clip"}
$column eq "target" $opts{"pending_target_clip"}
Truth table0%0/3
A and B
A= $column eq "target"B= $opts{"pending_target_clip"}
ABresult
0X0
100
111
337 0 $row->{target} =~ s/^.*\///;
338 }
339 0 push( @new_line, $row->{$column} );
340 } else {
341 0 push( @new_line, '' );
342 }
343 } ## end foreach my $column (@columns)
344  
345 0 push( @td, \@new_line );
346 } ## end foreach my $row ( @{$rows} )
347  
348 0 $tb->add_rows( \@td );
349  
350 0 return $tb->draw;
351 } ## end sub get_pending_table
352  
353 =head2 get_running
354  
355 Returns a array ref of hash refs of rows from the tasks table where the
356 status is set to pending.
357  
358 select * from tasks where status = 'running'
359  
360 The statement above is used to find running tasks.
361  
362 - where :: And additional where part of the SQL statement. "and" will
363 automatically be used to join it with the rest of the
364 statement. May not contain a ';'.
365 - Default :: undef
366  
367 use Data::Dumper;
368  
369 my $running=$cape_utils->get_running;
370 print Dumper($running);
371  
372 =cut
373  
374 0 ▶sub get_running {
Subroutine get_running: not called CC=5 SCAR=34.0
Pod documented
375 0 my ( $self, %opts ) = @_;
376  
377 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
378 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
379 }
380  
381 0 my $dbh = $self->connect;
382  
383 0 my $statement = "select * from tasks where status = 'running'";
384 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
385 0 $statement = $statement . ' AND ' . $opts{where};
386 }
387  
388 0 my $sth = $dbh->prepare($statement);
389 0 $sth->execute;
390  
391 0 my $row;
392 my @rows;
393 0 ▶ while ( $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_hashref) 0 0 0
394 0 push( @rows, $row );
395 }
396  
397 0 $sth->finish;
398 0 $dbh->disconnect;
399  
400 0 return \@rows;
401 } ## end sub get_running
402  
403 =head2 get_running_count
404  
405 Get pending count running tasks.
406  
407 select * from tasks where status = 'running'
408  
409 The statement above is used to find running tasks.
410  
411 - where :: And additional where part of the SQL statement. "and" will
412 automatically be used to join it with the rest of the
413 statement. May not contain a ';'.
414 - Default :: undef
415  
416 my $count=$cape_util->get_running_count;
417  
418 =cut
419  
420 0 ▶sub get_running_count {
Subroutine get_running_count: not called CC=4 SCAR=30.0
Pod documented
421 0 my ( $self, %opts ) = @_;
422  
423 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
424 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
425 }
426  
427 0 my $dbh = $self->connect;
428  
429 0 my $statement = "select * from tasks where status = 'running'";
430 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
431 0 $statement = $statement . ' AND ' . $opts{where};
432 }
433  
434 0 my $sth = $dbh->prepare($statement);
435 0 $sth->execute;
436  
437 0 my $rows = $sth->rows;
438  
439 0 $sth->finish;
440 0 $dbh->disconnect;
441  
442 0 return $rows;
443 } ## end sub get_running_count
444  
445 =head2 get_running_table
446  
447 Generates a ASCII table for pending.
448  
449 The following config variables can are relevant to this and
450 may be overriden.
451  
452 table_border
453 table_color
454 running_columns
455 running_target_clip
456 running_time_clip
457  
458 The statement below is used to find running tasks.
459  
460 select * from tasks where status = 'running'
461  
462 The following options are also supported.
463  
464 - where :: And additional where part of the SQL statement. "and" will
465 automatically be used to join it with the rest of the
466 statement. May not contain a ';'.
467 - Default :: undef
468  
469 print $cape_util->get_pending_table( pending_columns=>'id,package');
470  
471 =cut
472  
473 0 ▶sub get_running_table {
Subroutine get_running_table: not called CC=16 SCAR=56.1
Pod documented
474 0 my ( $self, %opts ) = @_;
475  
476 0 my @overrides = ( 'table_border', 'table_color', 'running_columns', 'running_target_clip', 'running_time_clip' );
477 0 foreach my $override (@overrides) {
478 0 ▶ if ( !defined( $opts{$override} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{$override}) 0 0 0
479 0 $opts{$override} = $self->{config}->{_}->{$override};
480 }
481 }
482  
483 0 my $rows = $self->get_running( where => $opts{where} );
484  
485 0 my $tb = Text::ANSITable->new;
486 0 $tb->border_style( $opts{table_border} );
487 0 $tb->color_theme( $opts{table_color} );
488  
489 0 my @columns = split( /,/, $opts{running_columns} );
490 0 my $header_int = 0;
491 0 my $padding = 0;
492 0 foreach my $header (@columns) {
493 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
494 0 else { $padding = 0; }
495  
496 0 $tb->set_column_style( $header_int, pad => $padding );
497  
498 0 $header_int++;
499 }
500  
501 0 $tb->columns( \@columns );
502  
503 0 my @td;
504 0 foreach my $row ( @{$rows} ) {
505 0 my @new_line;
506 0 foreach my $column (@columns) {
507 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
508 0 $row->{ET} = $row->{enforce_timeout};
509 }
510  
511 0 ▶ if ( defined( $row->{$column} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{$column}) { } 0 0 0
512 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
513 0 $row->{ET} = $row->{enforce_timeout};
514 }
515  
516 0 ▶ if ( ( $column eq 'clock' || $column eq 'added_on' || $column eq 'started_on' )
Branch: 2 branches
BranchTrueFalseTotal
if ($column eq 'clock' || $column eq 'added_on' || $column eq 'started_on' and $opts{'running_time_clip'}) { } 0 0 0
elsif ($column eq 'target' and $opts{'running_target_clip'}) { } 0 0 0
Condition0%0/9
$column eq "clock" || $column eq "added_on" || $column eq "started_on" and $opts{"running_time_clip"}
!ll&&!rl&&r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on"
l!l&&r!l&&!r
000
MC/DC0%0/4
$column eq "clock" || $column eq "added_on" || $column eq "started_on" and $opts{"running_time_clip"}
$column eq "clock" $column eq "added_on" $column eq "started_on" $opts{"running_time_clip"}
Truth table0%0/7
A or B or C and D
A= $column eq "clock"B= $column eq "added_on"C= $column eq "started_on"D= $opts{"running_time_clip"}
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
517 && $opts{running_time_clip} )
518 {
519 0 $row->{$column} =~ s/\.[0-9]+$//;
520 ▶ } elsif ( $column eq 'target' && $opts{running_target_clip} ) {
Condition0%0/3
$column eq "target" and $opts{"running_target_clip"}
!ll&&!rl&&r
000
MC/DC0%0/2
$column eq "target" and $opts{"running_target_clip"}
$column eq "target" $opts{"running_target_clip"}
Truth table0%0/3
A and B
A= $column eq "target"B= $opts{"running_target_clip"}
ABresult
0X0
100
111
521 0 $row->{target} =~ s/^.*\///;
522 }
523 0 push( @new_line, $row->{$column} );
524 } else {
525 0 push( @new_line, '' );
526 }
527 } ## end foreach my $column (@columns)
528  
529 0 push( @td, \@new_line );
530 } ## end foreach my $row ( @{$rows} )
531  
532 0 $tb->add_rows( \@td );
533  
534 0 return $tb->draw;
535 } ## end sub get_running_table
536  
537 =head2 get_tasks
538  
539 Returns a array ref of hash refs of rows from the tasks table where the
540 status is set to pending.
541  
542 - where :: The where part of the SQL statement. May not contain a ';'.
543 - Default :: undef
544  
545 - order :: Column to order by.
546 - Default :: id
547  
548 - limit :: Number of items to return.
549 - Default :: 100
550  
551 - direction :: Direction to order in.
552 - Default :: desc
553  
554 use Data::Dumper;
555  
556 A small example showing getting running, ordering by category, and limiting to 20.
557  
558 my $tasks=$cape_utils->get_tasks(where=>"status = 'running'", limit=>20, order=>"category", direction=>'desc');
559 print Dumper($running);
560  
561 =cut
562  
563 0 ▶sub get_tasks {
Subroutine get_tasks: not called CC=18 SCAR=58.3
Pod documented
564 0 my ( $self, %opts ) = @_;
565  
566 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
567 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
568 }
569  
570 0 ▶ if ( defined( $opts{order} ) && $opts{order} !~ /^[0-9a-zA-Z]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'order'} and not $opts{'order'} =~ /^[0-9a-zA-Z]+$/) { } 0 0 0
Condition0%0/3
defined $opts{"order"} and not $opts{"order"} =~ /^[0-9a-zA-Z]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"order"} and not $opts{"order"} =~ /^[0-9a-zA-Z]+$/
defined $opts{"order"} not $opts{"order"} =~ /^[0-9a-zA-Z]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"order"}B= not $opts{"order"} =~ /^[0-9a-zA-Z]+$/
ABresult
0X0
100
111
571 0 die '$opts{order},"' . $opts{order} . '", does not match /^[0-9a-zA-Z]+$/';
572 } else {
573 0 $opts{order} = 'id';
574 }
575  
576 0 ▶ if ( defined( $opts{limit} ) && $opts{limit} !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'limit'} and not $opts{'limit'} =~ /^[0-9]+$/) { } 0 0 0
Condition0%0/3
defined $opts{"limit"} and not $opts{"limit"} =~ /^[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"limit"} and not $opts{"limit"} =~ /^[0-9]+$/
defined $opts{"limit"} not $opts{"limit"} =~ /^[0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"limit"}B= not $opts{"limit"} =~ /^[0-9]+$/
ABresult
0X0
100
111
577 0 die '$opts{limit},"' . $opts{limit} . '", does not match /^[0-9]+$/';
578 } else {
579 0 $opts{limit} = '100';
580 }
581  
582 0 ▶ if ( defined( $opts{direction} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"direction"}) 0 0 0
583 0 $opts{direction} = lc( $opts{direction} );
584 }
585 0 ▶ if ( defined( $opts{direction} ) && ( $opts{direction} ne 'desc' || $opts{direction} ne 'asc' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'direction'} and $opts{'direction'} ne 'desc' || $opts{'direction'} ne 'asc') { } 0 0 0
Condition0%0/6
defined $opts{"direction"} and $opts{"direction"} ne "desc" || $opts{"direction"} ne "asc"
!ll&&!rl&&r
000
$opts{"direction"} ne "desc" or $opts{"direction"} ne "asc"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $opts{"direction"} and $opts{"direction"} ne "desc" || $opts{"direction"} ne "asc"
defined $opts{"direction"} $opts{"direction"} ne "desc" $opts{"direction"} ne "asc"
Truth table0%0/4
A and B or C
A= defined $opts{"direction"}B= $opts{"direction"} ne "desc"C= $opts{"direction"} ne "asc"
ABCresult
0XX0
1000
11X1
1011
586 0 die '$opts{diirection},"' . $opts{direction} . '", does not match desc or asc';
587 } else {
588 0 $opts{direction} = 'desc';
589 }
590  
591 0 my $dbh;
592 0 ▶ eval { $dbh = $self->connect or die $DBI::errstr };
Branch: 1 branch
BranchTrueFalseTotal
$dbh = $self->connect or die $DBI::errstr 0 0 0
593 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
594 0 die( 'Failed to connect to the DB... ' . $@ );
595 }
596  
597 0 my $statement = "select * from tasks";
598 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
599 0 $statement = $statement . ' where ' . $opts{where};
600 }
601  
602 0 $statement = $statement . ' order by ' . $opts{order} . ' ' . $opts{direction} . ' limit ' . $opts{limit} . ';';
603  
604 0 my $sth;
605 0 eval {
606 0 ▶ $sth = $dbh->prepare($statement) or die $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $dbh->prepare($statement) or die $DBI::errstr 0 0 0
607 0 ▶ $sth->execute or die $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $DBI::errstr 0 0 0
608 };
609 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
610 0 die( 'Failed to connect to run the search... ' . $@ );
611 }
612  
613 0 my $row;
614 my @rows;
615 0 ▶ while ( $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_hashref) 0 0 0
616 0 push( @rows, $row );
617 }
618  
619 0 $sth->finish;
620 0 $dbh->disconnect;
621  
622 0 return \@rows;
623 } ## end sub get_tasks
624  
625 =head2 get_tasks_count
626  
627 Gets a count of tasks.
628  
629 - where :: The where part of the SQL statement. May not contain a ';'.
630 - Default :: undef
631  
632 use Data::Dumper;
633  
634 A small example showing getting running, ordering by category, and limiting to 20.
635  
636 my $count=$cape_util->get_tasks_count(where=>"status = 'running'", limit=>20, order=>"category", direction=>'desc');
637  
638 =cut
639  
640 0 ▶sub get_tasks_count {
Subroutine get_tasks_count: not called CC=4 SCAR=30.0
Pod documented
641 0 my ( $self, %opts ) = @_;
642  
643 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
644 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
645 }
646  
647 0 my $dbh = $self->connect;
648  
649 0 my $statement = "select * from tasks";
650 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
651 0 $statement = $statement . ' ' . $opts{where};
652 }
653  
654 0 my $sth = $dbh->prepare($statement);
655 0 $sth->execute;
656  
657 0 my $rows = $sth->rows;
658  
659 0 $sth->finish;
660 0 $dbh->disconnect;
661  
662 0 return $rows;
663 } ## end sub get_tasks_count
664  
665 =head2 get_tasks_table
666  
667 Generates a ASCII table for tasks.
668  
669 The following config variables can are relevant to this and
670 may be overriden.
671  
672 table_border
673 table_color
674 task_columns
675 task_target_clip
676 task_time_clip
677  
678 The following options are also supported.
679  
680 - where :: Additional SQL where statements to add.
681 - Default :: undef
682  
683 - order :: Column to order by.
684 - Default :: id
685  
686 - limit :: Number of items to return.
687 - Default :: 100
688  
689 - direction :: Direction to order in.
690 - Default :: desc
691  
692 print $cape_util->get_tasks_table( where => "status = 'reported'");
693  
694 =cut
695  
696 0 ▶sub get_tasks_table {
Subroutine get_tasks_table: not called CC=29 SCAR=67.7
Pod documented
697 0 my ( $self, %opts ) = @_;
698  
699 0 my @overrides = ( 'table_border', 'table_color', 'task_columns', 'task_target_clip', 'task_time_clip' );
700 0 foreach my $override (@overrides) {
701 0 ▶ if ( !defined( $opts{$override} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{$override}) 0 0 0
702 0 $opts{$override} = $self->{config}->{_}->{$override};
703 }
704 }
705  
706 my $rows = $self->get_tasks(
707 where => $opts{where},
708 order => $opts{order},
709 limit => $opts{limit},
710 direction => $opts{direction}
711 0 );
712  
713 0 my $tb = Text::ANSITable->new;
714 0 $tb->border_style( $opts{table_border} );
715 0 $tb->color_theme( $opts{table_color} );
716  
717 0 my @columns = split( /,/, $opts{task_columns} );
718 0 my $header_int = 0;
719 0 my $padding = 0;
720 0 foreach my $header (@columns) {
721 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
722 0 else { $padding = 0; }
723  
724 0 $tb->set_column_style( $header_int, pad => $padding );
725  
726 0 $header_int++;
727 }
728  
729 0 $tb->columns( \@columns );
730  
731 0 my @td;
732 0 my @latest_check = (
733 'started_on', 'analysis_started_on', 'analysis_finished_on', 'analysis_finished_on',
734 'processing_finished_on', 'signatures_started_on', 'signatures_finished_on', 'reporting_started_on',
735 'reporting_finished_on', 'completed_on'
736 );
737 0 foreach my $row ( @{$rows} ) {
738 0 my @new_line;
739 0 foreach my $column (@columns) {
740 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
741 0 $row->{ET} = $row->{enforce_timeout};
742 }
743  
744 0 ▶ if ( $column eq 'latest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "latest") 0 0 0
745 0 $row->{latest} = '';
746 0 foreach my $item (@latest_check) {
747 0 ▶ if ( defined( $row->{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{$item}) 0 0 0
748 0 $row->{latest} = $row->{$item};
749 }
750 }
751 }
752  
753 0 ▶ if ( defined( $row->{$column} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{$column}) { } 0 0 0
754 0 ▶ if ( $column eq 'ET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column eq "ET") 0 0 0
755 0 $row->{ET} = $row->{enforce_timeout};
756 }
757  
758 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($column eq 'clock' || $column eq 'added_on' || $column eq 'started_on' || $column eq 'completed_on' || $column eq 'analysis_started_on' || $column eq 'analysis_finished_on' || $column eq 'processing_started_on' || $column eq 'processing_finished_on' || $column eq 'signatures_started_on' || $column eq 'signatures_finished_on' || $column eq 'reporting_started_on' || $column eq 'reporting_finished_on' || $column eq 'latest' and $opts{'task_time_clip'}) { } 0 0 0
elsif ($column eq 'target' and $opts{'task_target_clip'}) { } 0 0 0
Condition0%0/39
$column eq "clock" || $column eq "added_on" || $column eq "started_on" || $column eq "completed_on" || $column eq "analysis_started_on" || $column eq "analysis_finished_on" || $column eq "processing_started_on" || $column eq "processing_finished_on" || $column eq "signatures_started_on" || $column eq "signatures_finished_on" || $column eq "reporting_started_on" || $column eq "reporting_finished_on" || $column eq "latest" and $opts{"task_time_clip"}
!ll&&!rl&&r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on" or $column eq "signatures_started_on" or $column eq "signatures_finished_on" or $column eq "reporting_started_on" or $column eq "reporting_finished_on" or $column eq "latest"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on" or $column eq "signatures_started_on" or $column eq "signatures_finished_on" or $column eq "reporting_started_on" or $column eq "reporting_finished_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on" or $column eq "signatures_started_on" or $column eq "signatures_finished_on" or $column eq "reporting_started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on" or $column eq "signatures_started_on" or $column eq "signatures_finished_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on" or $column eq "signatures_started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on" or $column eq "processing_finished_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on" or $column eq "processing_started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on" or $column eq "analysis_finished_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on" or $column eq "analysis_started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on" or $column eq "completed_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on" or $column eq "started_on"
l!l&&r!l&&!r
000
$column eq "clock" or $column eq "added_on"
l!l&&r!l&&!r
000
MC/DC0%0/14
$column eq "clock" || $column eq "added_on" || $column eq "started_on" || $column eq "completed_on" || $column eq "analysis_started_on" || $column eq "analysis_finished_on" || $column eq "processing_started_on" || $column eq "processing_finished_on" || $column eq "signatures_started_on" || $column eq "signatures_finished_on" || $column eq "reporting_started_on" || $column eq "reporting_finished_on" || $column eq "latest" and $opts{"task_time_clip"}
$column eq "clock" $column eq "added_on" $column eq "started_on" $column eq "completed_on" $column eq "analysis_started_on" $column eq "analysis_finished_on" $column eq "processing_started_on" $column eq "processing_finished_on" $column eq "signatures_started_on" $column eq "signatures_finished_on" $column eq "reporting_started_on" $column eq "reporting_finished_on" $column eq "latest" $opts{"task_time_clip"}
Truth table0%0/27
A or B or C or D or E or F or G or H or I or J or K or L or M and N
A= $column eq "clock"B= $column eq "added_on"C= $column eq "started_on"D= $column eq "completed_on"E= $column eq "analysis_started_on"F= $column eq "analysis_finished_on"G= $column eq "processing_started_on"H= $column eq "processing_finished_on"I= $column eq "signatures_started_on"J= $column eq "signatures_finished_on"K= $column eq "reporting_started_on"L= $column eq "reporting_finished_on"M= $column eq "latest"N= $opts{"task_time_clip"}
ABCDEFGHIJKLMNresult
0000000000000X0
1XXXXXXXXXXXX00
01XXXXXXXXXXX00
001XXXXXXXXXX00
0001XXXXXXXXX00
00001XXXXXXXX00
000001XXXXXXX00
0000001XXXXXX00
00000001XXXXX00
000000001XXXX00
0000000001XXX00
00000000001XX00
000000000001X00
000000000000100
1XXXXXXXXXXXX11
01XXXXXXXXXXX11
001XXXXXXXXXX11
0001XXXXXXXXX11
00001XXXXXXXX11
000001XXXXXXX11
0000001XXXXXX11
00000001XXXXX11
000000001XXXX11
0000000001XXX11
00000000001XX11
000000000001X11
000000000000111
759 (
760 $column eq 'clock'
761 || $column eq 'added_on'
762 || $column eq 'started_on'
763 || $column eq 'completed_on'
764 || $column eq 'analysis_started_on'
765 || $column eq 'analysis_finished_on'
766 || $column eq 'processing_started_on'
767 || $column eq 'processing_finished_on'
768 || $column eq 'signatures_started_on'
769 || $column eq 'signatures_finished_on'
770 || $column eq 'reporting_started_on'
771 || $column eq 'reporting_finished_on'
772 || $column eq 'latest'
773 )
774 && $opts{task_time_clip}
775 )
776 {
777 0 $row->{$column} =~ s/\.[0-9]+$//;
778 ▶ } elsif ( $column eq 'target' && $opts{task_target_clip} ) {
Condition0%0/3
$column eq "target" and $opts{"task_target_clip"}
!ll&&!rl&&r
000
MC/DC0%0/2
$column eq "target" and $opts{"task_target_clip"}
$column eq "target" $opts{"task_target_clip"}
Truth table0%0/3
A and B
A= $column eq "target"B= $opts{"task_target_clip"}
ABresult
0X0
100
111
779 0 $row->{target} =~ s/^.*\///;
780 }
781 0 push( @new_line, $row->{$column} );
782 } else {
783 0 push( @new_line, '' );
784 }
785 } ## end foreach my $column (@columns)
786  
787 0 push( @td, \@new_line );
788 } ## end foreach my $row ( @{$rows} )
789  
790 0 $tb->add_rows( \@td );
791  
792 0 return $tb->draw;
793 } ## end sub get_tasks_table
794  
795 =head2 munge
796  
797 Munges the specified report file.
798  
799 $cape_utils->munge(file=>$report_file);
800  
801 =cut
802  
803 0 ▶sub munge {
Subroutine munge: not called CC=27 SCAR=66.3
Pod documented
804 0 my ( $self, %opts ) = @_;
805  
806 0 ▶ if ( !defined( $opts{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"file"}) 0 0 0
807 0 die('No file specified via $opts{file}');
808 }
809  
810 0 ▶ if ( !-f $opts{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $opts{"file"}) 0 0 0
811 0 die( '"' . $opts{file} . '" is not a file' );
812 }
813  
814 # create a backup copy prior to munging
815 # also only create it if it does not exist
816 0 my $pre_munge_file = $opts{file} . '.pre-cape_utils_munge';
817 0 ▶ if ( !-f $pre_munge_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $pre_munge_file) { } 0 0 0
818 copy( $opts{file}, $pre_munge_file )
819 0 ▶ || die( 'Creating pre-munge file for "' . $opts{file} . '" failed... ' . $! );
Branch: 1 branch
BranchTrueFalseTotal
copy($opts{"file"}, $pre_munge_file) or die 'Creating pre-munge file for "' . $opts{"file"} . '" failed... ' . $! 0 0 0
820 } else {
821 0 warn( 'Pre-munge file, "' . $pre_munge_file . '", already exists, skippying coppying' );
822 }
823  
824 # read the file on in
825 0 my $report;
826 0 eval { $report = decode_json( read_file( $opts{file} ) ); };
827 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
828 0 die( 'Failed to parse "' . $opts{file} . '"... ' . $@ );
829 }
830  
831 # find the munge keys
832 0 my @sections = sort( keys( %{ $self->{config} } ) );
833 0 my @munges;
834 0 foreach my $item (@sections) {
835 0 ▶ if ( $item =~ /^munge\_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^munge\_/) 0 0 0
836 0 push( @munges, $item );
837 }
838 }
839  
840 # should be set by a munge if it made a change
841 0 my $changed = 0;
842 # scratch space for between all munges
843 0 my %all_scratch;
844 0 foreach my $item (@munges) {
845 # scratch space for the munges to use
846 0 my %scratch;
847  
848 # only process the specified munge if we have both keys
849 0 ▶ if ( defined( $self->{config}{$item}{check} ) && defined( $self->{config}{$item}{munge} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'config'}{$item}{'check'} and defined $self->{'config'}{$item}{'munge'}) { } 0 0 0
Condition0%0/3
defined $self->{"config"}{$item}{"check"} and defined $self->{"config"}{$item}{"munge"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"config"}{$item}{"check"} and defined $self->{"config"}{$item}{"munge"}
defined $self->{"config"}{$item}{"check"} defined $self->{"config"}{$item}{"munge"}
Truth table0%0/3
A and B
A= defined $self->{"config"}{$item}{"check"}B= defined $self->{"config"}{$item}{"munge"}
ABresult
0X0
100
111
850 # now that we know we have the keys, get the full file path if needed
851 0 my $check_file = $self->{config}{$item}{check};
852 0 my $munge_file = $self->{config}{$item}{munge};
853 0 ▶ if ( $check_file !~ /^\// && $check_file !~ /^.\// && $check_file !~ /^..\// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check_file =~ m[^/] or $check_file =~ m[^./] or $check_file =~ m[^../]) 0 0 0
Condition0%0/6
$check_file =~ m[^/] or $check_file =~ m[^./] or $check_file =~ m[^../]
l!l&&r!l&&!r
000
$check_file =~ m[^/] or $check_file =~ m[^./]
l!l&&r!l&&!r
000
MC/DC0%0/3
$check_file =~ m[^/] or $check_file =~ m[^./] or $check_file =~ m[^../]
$check_file =~ m[^/] $check_file =~ m[^./] $check_file =~ m[^../]
Truth table0%0/4
A or B or C
A= $check_file =~ m[^/]B= $check_file =~ m[^./]C= $check_file =~ m[^../]
ABCresult
1XX1
01X1
0011
0000
854 0 $check_file = '/usr/local/etc/cape_utils_munge/' . $check_file;
855 }
856 0 ▶ if ( $munge_file !~ /^\// && $munge_file !~ /^.\// && $munge_file !~ /^..\// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($munge_file =~ m[^/] or $munge_file =~ m[^./] or $munge_file =~ m[^../]) 0 0 0
Condition0%0/6
$munge_file =~ m[^/] or $munge_file =~ m[^./] or $munge_file =~ m[^../]
l!l&&r!l&&!r
000
$munge_file =~ m[^/] or $munge_file =~ m[^./]
l!l&&r!l&&!r
000
MC/DC0%0/3
$munge_file =~ m[^/] or $munge_file =~ m[^./] or $munge_file =~ m[^../]
$munge_file =~ m[^/] $munge_file =~ m[^./] $munge_file =~ m[^../]
Truth table0%0/4
A or B or C
A= $munge_file =~ m[^/]B= $munge_file =~ m[^./]C= $munge_file =~ m[^../]
ABCresult
1XX1
01X1
0011
0000
857 0 $munge_file = '/usr/local/etc/cape_utils_munge/' . $munge_file;
858 }
859  
860 # figure out if we need to munge it or not
861 0 my $munge_it = 0;
862 0 eval {
863 0 my $check_code = read_file($check_file);
864 0 eval($check_code);
865 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
866 0 die($@);
867 }
868 };
869 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
870 0 warn( 'Munge "' . $item . '" errored during the check... ' . $@ );
871  
872 # override this even if set before dying
873 0 $munge_it = 0;
874 }
875  
876 # if so, try to munge it
877 0 ▶ if ($munge_it) {
Branch: 1 branch
BranchTrueFalseTotal
if ($munge_it) 0 0 0
878 0 eval {
879 0 my $munge_code = read_file($munge_file);
880 0 eval($munge_code);
881 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
882 0 die($@);
883 }
884 };
885 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
886 0 warn( 'Munge "' . $item . '" errored during the munge... ' . $@ );
887 }
888 } ## end if ($munge_it)
889 } else {
890 0 warn( 'Section "' . $item . '" missing either the key "check" or munge"' );
891 }
892 } ## end foreach my $item (@munges)
893  
894 # save the file if it changed
895 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 0 0 0
896 # if changed, update the malscore
897 0 my $malscore = 0.0;
898 0 my $sig_int = 0;
899 0 ▶ while ( defined( $report->{signatures}[$sig_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $report->{"signatures"}[$sig_int]) 0 0 0
900 0 ▶ if ( $report->{signatures}[$sig_int]{severity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report->{'signatures'}[$sig_int]{'severity'}) { } 0 0 0
901 $malscore += $report->{signatures}[$sig_int]{weight} * 0.5
902 0 * ( $report->{signatures}[$sig_int]{confidence} / 100 );
903 } else {
904 $malscore
905 += $report->{signatures}[$sig_int]{weight}
906 * ( $report->{signatures}[$sig_int]{weight} - 1 )
907 0 * ( $report->{signatures}[$sig_int]{confidence} / 100 );
908 }
909  
910 0 $sig_int++;
911 } ## end while ( defined( $report->{signatures}[$sig_int...]))
912 0 ▶ if ( $malscore > 10.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($malscore > 10) 0 0 0
913 0 $malscore = 10.0;
914 }
915 0 $report->{malscore} = $malscore;
916  
917 0 eval { write_file( $opts{file}, encode_json($report) ); };
918 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
919 0 die( 'Failed to encode updated report post munging and write it to "' . $opts{file} . '"... ' . $@ );
920 }
921 } ## end if ($changed)
922  
923 0 return 1;
924 } ## end sub munge
925  
926 =head2 search
927  
928 Searches the list of tasks. By default everything will be return ed.
929  
930 - where :: Additional SQL where statements to use for searching.
931 May not contain a ';'.
932 - Default :: undef
933  
934 Addtionally there are also helpers for searching. These may not contain either a /\'/
935 or a /\\/. They will be joined via and.
936  
937 The following are handled as a simple equality.
938  
939 - timeout
940 - memory
941 - enforce_timeout
942 - timedout
943  
944 The following are numeric. Each one may accept multiple
945 comma sperated values. The equalities =, >,>=, <=, and !
946 are supported. If no equality is specified, then = is used.
947  
948 - id
949 - timeout
950 - priority
951 - dropped_files
952 - running_processes
953 - api_calls
954 - domains
955 - signatures_total
956 - signatures_alert
957 - files_written
958 - registry_keys_modified
959 - crash_issues
960 - anti_issues
961 - sample_id
962 - machine_id
963  
964 # will result in id >= 3 and id < 44
965 id => '>=3,<44'
966  
967 # either of these will be id = 4
968 id => '=4'
969 id => '4'
970  
971 The following are string items. As is, they
972 are evaluated as a simple equality. If ending with
973 ending in '_like', they will be evaluated as a like.
974  
975 - target
976 - category
977 - custom
978 - machine
979 - package
980 - route
981 - tags_tasks
982 - options
983 - platform
984  
985 # becomes... target = 'foo'
986 target => 'foo'
987  
988 # becomes... target like 'foo%'
989 target_like => 'foo%'
990  
991 =cut
992  
993 0 ▶sub search {
Subroutine search: not called CC=24 SCAR=64.0
Pod documented
994 0 my ( $self, %opts ) = @_;
995  
996 0 ▶ if ( defined( $opts{where} ) && $opts{where} =~ /\;/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"} and $opts{"where"} =~ /\;/) 0 0 0
Condition0%0/3
defined $opts{"where"} and $opts{"where"} =~ /\;/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"where"} and $opts{"where"} =~ /\;/
defined $opts{"where"} $opts{"where"} =~ /\;/
Truth table0%0/3
A and B
A= defined $opts{"where"}B= $opts{"where"} =~ /\;/
ABresult
0X0
100
111
997 0 die '$opts{where},"' . $opts{where} . '", contains a ";"';
998 }
999  
1000 #
1001 # make sure all the set variables are not dangerous or potentially dangerous
1002 #
1003  
1004 0 my @to_check = (
1005 'id', 'target', 'route', 'machine',
1006 'timeout', 'priority', 'route', 'tags_tasks',
1007 'options', 'clock', 'added_on', 'started_on',
1008 'completed_on', 'status', 'dropped_files', 'running_processes',
1009 'api_calls', 'domains', 'signatures_total', 'signatures_alert',
1010 'files_written', 'registry_keys_modified', 'crash_issues', 'anti_issues',
1011 'timedout', 'sample_id', 'machine_id', 'parent_id',
1012 'tlp', 'category', 'package'
1013 );
1014  
1015 0 foreach my $var_to_check (@to_check) {
1016 0 ▶ if ( defined( $opts{$var_to_check} ) && $opts{$var_to_check} =~ /[\\\']/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$var_to_check} and $opts{$var_to_check} =~ /[\\\']/) 0 0 0
Condition0%0/3
defined $opts{$var_to_check} and $opts{$var_to_check} =~ /[\\\']/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{$var_to_check} and $opts{$var_to_check} =~ /[\\\']/
defined $opts{$var_to_check} $opts{$var_to_check} =~ /[\\\']/
Truth table0%0/3
A and B
A= defined $opts{$var_to_check}B= $opts{$var_to_check} =~ /[\\\']/
ABresult
0X0
100
111
1017 0 die( '"' . $opts{$var_to_check} . '" for "' . $var_to_check . '" matched /[\\\']/' );
1018 }
1019 }
1020  
1021 #
1022 # init the SQL statement
1023 #
1024  
1025 0 my $sql = "select * from tasks where id >= 0";
1026  
1027 0 ▶ if ( defined( $opts{where} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"where"}) 0 0 0
1028 0 $sql = $sql . ' AND ' . $opts{where};
1029 }
1030  
1031 #
1032 # add simple items
1033 #
1034  
1035 0 my @simple = ( 'timeout', 'memory', 'enforce_timeout', 'timedout' );
1036  
1037 0 foreach my $item (@simple) {
1038 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
1039 0 $sql = $sql . " and " . $item . " = '" . $opts{$item} . "'";
1040 }
1041 }
1042  
1043 #
1044 # add numeric items
1045 #
1046  
1047 0 my @numeric = (
1048 'id', 'timeout', 'priority', 'dropped_files',
1049 'running_processes', 'api_calls', 'domains', 'signatures_total',
1050 'signatures_alert', 'files_written', 'registry_keys_modified', 'crash_issues',
1051 'anti_issues', 'sample_id', 'machine_id'
1052 );
1053  
1054 0 foreach my $item (@numeric) {
1055 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
1056  
1057 # remove and tabs or spaces
1058 0 $opts{$item} =~ s/[\ \t]//g;
1059 0 my @arg_split = split( /\,/, $opts{$item} );
1060  
1061 # process each item
1062 0 foreach my $arg (@arg_split) {
1063  
1064 # match the start of the item
1065 0 ▶ if ( $arg =~ /^[0-9]+$/ ) {
Branch: 8 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-9]+$/) { } 0 0 0
elsif ($arg =~ /^$/) { } 0 0 0
1066 0 $sql = $sql . " and " . $item . " = '" . $arg . "'";
1067 } elsif ( $arg =~ /^\=[0-9]+$/ ) {
1068 0 $arg =~ s/^\=//;
1069 0 $sql = $sql . " and " . $item . " <= '" . $arg . "'";
1070 } elsif ( $arg =~ /^\<\=[0-9]+$/ ) {
1071 0 $arg =~ s/^\<\=//;
1072 0 $sql = $sql . " and " . $item . " <= '" . $arg . "'";
1073 } elsif ( $arg =~ /^\<[0-9]+$/ ) {
1074 0 $arg =~ s/^\<//;
1075 0 $sql = $sql . " and " . $item . " < '" . $arg . "'";
1076 } elsif ( $arg =~ /^\>\=[0-9]+$/ ) {
1077 0 $arg =~ s/^\>\=//;
1078 0 $sql = $sql . " and " . $item . " >= '" . $arg . "'";
1079 } elsif ( $arg =~ /^\>[0-9]+$/ ) {
1080 0 $arg =~ s/^\>\=//;
1081 0 $sql = $sql . " and " . $item . " > '" . $arg . "'";
1082 } elsif ( $arg =~ /^\![0-9]+$/ ) {
1083 0 $arg =~ s/^\!//;
1084 0 $sql = $sql . " and " . $item . " != '" . $arg . "'";
1085 } elsif ( $arg =~ /^$/ ) {
1086  
1087 # only exists for skipping when some one has passes something starting
1088 # with a ,, ending with a,, or with ,, in it.
1089 } else {
1090 # if we get here, it means we don't have a valid use case for what ever was passed and should error
1091 0 die( '"' . $arg . '" does not appear to be a valid item for a numeric search for the ' . $item );
1092 }
1093 } ## end foreach my $arg (@arg_split)
1094 } ## end if ( defined( $opts{$item} ) )
1095 } ## end foreach my $item (@numeric)
1096  
1097 #
1098 # handle string items
1099 #
1100  
1101 my @strings
1102 0 = ( 'target', 'category', 'custom', 'machine', 'package', 'route', 'tags_tasks', 'options', 'platform', );
1103  
1104 0 foreach my $item (@strings) {
1105 0 ▶ if ( defined( $opts{$item} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item}) 0 0 0
1106 0 ▶ if ( defined( $opts{ $item . '_like' } ) && $opts{ $item . '_like' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$item . '_like'} and $opts{$item . '_like'}) { } 0 0 0
Condition0%0/3
defined $opts{$item . '_like'} and $opts{$item . '_like'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{$item . '_like'} and $opts{$item . '_like'}
defined $opts{$item . '_like'} $opts{$item . '_like'}
Truth table0%0/3
A and B
A= defined $opts{$item . '_like'}B= $opts{$item . '_like'}
ABresult
0X0
100
111
1107 0 $sql = $sql . " and host like '" . $opts{$item} . "'";
1108 } else {
1109 0 $sql = $sql . " and " . $item . " = '" . $opts{$item} . "'";
1110 }
1111 }
1112 }
1113  
1114 #
1115 # finalize it and search
1116 #
1117  
1118 0 $sql = $sql . ';';
1119  
1120 0 my $dbh = $self->connect;
1121 0 my $sth = $dbh->prepare($sql);
1122  
1123 0 $sth->execute;
1124  
1125 0 my $rows;
1126  
1127 0 return $rows;
1128 } ## end sub search
1129  
1130 =head2 submit
1131  
1132 Submits files to CAPE.
1133  
1134 - clock :: Timestamp to use for setting the clock to of the VM for
1135 when executing the item. If left undefined, it will be
1136 autogenerated.
1137 - Format :: mm-dd-yyy HH:MM:ss
1138  
1139 - items :: A array ref of items to submit. If a directory is listed in
1140 here, it will be read, but subdirectories will not be recursed. They
1141 will be ignored.
1142  
1143 - name_regex :: Regex to use for matching items in a submitted dir.
1144 Only used if the a submitted item is a dir.
1145 - Default :: undef
1146  
1147 - mime_regex :: Array ref of desired mime types to match via
1148 regex. Only used if the a submitted item is a dir.
1149 - Default :: undef
1150  
1151 - timeout :: Value to use for timeout. Set to 0 to not enforce.
1152 - Default :: 200
1153  
1154 - machine :: The machine to use for this. If not defined, first
1155 available will be used.
1156 - Default :: undef
1157  
1158 - package :: Package to use, if not letting CAPE decide.
1159 - Default :: undef
1160  
1161 - options :: Option string to be passed via --options.
1162 - Default :: undef
1163  
1164 - random :: If it should randomize the order of submission.
1165 - Default :: 1
1166  
1167 - tags :: Tags to be passed to the script via --tags.
1168 - Default :: undef
1169  
1170 - platform :: What to pass to --platform.
1171 - Default :: undef
1172  
1173 - custom :: Any custom values to pass via --custom.
1174 - Default :: undef
1175  
1176 - enforce_timeout :: Force it to run the entire period.
1177 - Default :: 0
1178  
1179 - unique :: Only submit it if it is unique.
1180 - Default :: 0
1181  
1182 -quiet :: Do not print the results.
1183 - Default :: 0
1184  
1185 The retuned value is a hash ref where the keys are successfully submitted files
1186 and values of those keys is the task ID.
1187  
1188 my $sub_results=$cape_util->submit(items=>@to_detonate,unique=>0, quiet=>1);
1189 use JSON;
1190 print encode_json($sub_results)."\n";
1191  
1192 =cut
1193  
1194 0 ▶sub submit {
Subroutine submit: not called CC=27 SCAR=66.3
Pod documented
1195 0 my ( $self, %opts ) = @_;
1196  
1197 0 ▶ if ( !defined( $opts{items}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"items"}[0]) 0 0 0
1198 0 die 'No items to submit passed';
1199 }
1200  
1201 0 ▶ if ( !defined( $opts{clock} ) && $self->{config}->{_}->{set_clock_to_now} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{"clock"} and $self->{"config"}{"_"}{"set_clock_to_now"}) 0 0 0
Condition0%0/3
not defined $opts{"clock"} and $self->{"config"}{"_"}{"set_clock_to_now"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $opts{"clock"} and $self->{"config"}{"_"}{"set_clock_to_now"}
not defined $opts{"clock"} $self->{"config"}{"_"}{"set_clock_to_now"}
Truth table0%0/3
not(A) and B
A= not defined $opts{"clock"}B= $self->{"config"}{"_"}{"set_clock_to_now"}
ABresult
0X0
100
111
1202 0 $opts{clock} = $self->timestamp;
1203 }
1204  
1205 0 ▶ if ( !defined( $opts{timeout} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"timeout"}) 0 0 0
1206 0 $opts{timeout} = $self->{config}->{_}->{timeout};
1207 }
1208  
1209 0 ▶ if ( !defined( $opts{enforce_timeout} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"enforce_timeout"}) 0 0 0
1210 0 $opts{enforce_timeout} = $self->{config}->{_}->{enforce_timeout};
1211 }
1212  
1213 0 my @to_submit;
1214  
1215 0 foreach my $item ( @{ $opts{items} } ) {
1216 0 ▶ if ( -f $item ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $item) { } 0 0 0
elsif (-d $item) { } 0 0 0
1217 0 push( @to_submit, File::Spec->rel2abs($item) );
1218 } elsif ( -d $item ) {
1219 0 opendir( my $dh, $item );
1220 0 ▶ while ( readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readdir $dh)) 0 0 0
1221 0 ▶ if ( -f $item . '/' . $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $item . '/' . $_) 0 0 0
1222 0 push( @to_submit, File::Spec->rel2abs( $item . '/' . $_ ) );
1223 }
1224 }
1225 0 closedir($dh);
1226 }
1227 } ## end foreach my $item ( @{ $opts{items} } )
1228  
1229 0 ▶ chdir( $self->{config}->{_}->{base} ) || die( 'Unable to CD to "' . $self->{config}->{_}->{base} . '"' );
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"config"}{"_"}{"base"} or die 'Unable to CD to "' . $self->{"config"}{"_"}{"base"} . '"' 0 0 0
1230  
1231 0 my @to_run = ();
1232  
1233 0 ▶ if ( $self->{config}->{_}->{poetry} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"_"}{"poetry"}) 0 0 0
1234 0 push( @to_run, $self->{config}->{_}->{poetry_path}, 'run' );
1235 }
1236  
1237 0 push( @to_run, 'python3', $self->{config}->{_}->{base} . '/utils/submit.py' );
1238  
1239 0 ▶ if ( defined( $opts{clock} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"clock"}) 0 0 0
1240 0 push( @to_run, '--clock', $opts{clock} );
1241 }
1242  
1243 0 ▶ if ( defined( $opts{unique} ) && $opts{unique} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"unique"} and $opts{"unique"}) 0 0 0
Condition0%0/3
defined $opts{"unique"} and $opts{"unique"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"unique"} and $opts{"unique"}
defined $opts{"unique"} $opts{"unique"}
Truth table0%0/3
A and B
A= defined $opts{"unique"}B= $opts{"unique"}
ABresult
0X0
100
111
1244 0 push( @to_run, '--unique' );
1245 }
1246  
1247 0 ▶ if ( defined( $opts{timeout} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"timeout"}) 0 0 0
1248 0 push( @to_run, '--timeout', $opts{timeout} );
1249 }
1250  
1251 0 ▶ if ( $opts{enforce_timeout} && $opts{enforce_timeout} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"enforce_timeout"} and $opts{"enforce_timeout"}) 0 0 0
Condition0%0/3
$opts{"enforce_timeout"} and $opts{"enforce_timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"enforce_timeout"} and $opts{"enforce_timeout"}
$opts{"enforce_timeout"} $opts{"enforce_timeout"}
Truth table0%0/3
A and B
A= $opts{"enforce_timeout"}B= $opts{"enforce_timeout"}
ABresult
0X0
100
111
1252 0 push( @to_run, '--enforce-timeout' );
1253 }
1254  
1255 0 ▶ if ( defined( $opts{package} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"package"}) 0 0 0
1256 0 push( @to_run, '--package', $opts{package} );
1257 }
1258  
1259 0 ▶ if ( defined( $opts{machine} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"machine"}) 0 0 0
1260 0 push( @to_run, '--machine', $opts{machine} );
1261 }
1262  
1263 0 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 0 0 0
1264 0 push( @to_run, '--options', $opts{options} );
1265 }
1266  
1267 0 ▶ if ( defined( $opts{tags} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"tags"}) 0 0 0
1268 0 push( @to_run, '--tags', $opts{tags} );
1269 }
1270  
1271 0 my $added = {};
1272 0 foreach (@to_submit) {
1273 0 my @tmp_to_run = @to_run;
1274 0 push( @tmp_to_run, $_ );
1275 0 my ( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = run(
1276 command => \@tmp_to_run,
1277 verbose => 0
1278 );
1279 0 my $results = join( '', @{$full_buf} );
1280 0 ▶ if ( !$opts{quiet} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"quiet"}) 0 0 0
1281 0 print $results;
1282 }
1283  
1284 0 my @results_split = split( /\n/, $results );
1285 0 foreach my $item (@results_split) {
1286 0 $item =~ s/\e\[[0-9;]*m(?:\e\[K)?//g;
1287 0 chomp($item);
1288 0 ▶ if ( $item =~ /^Success\:\ File\ \".*\"\ added\ as\ task\ with\ ID\ \d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /^Success\:\ File\ \".*\"\ added\ as\ task\ with\ ID\ \d+$/) 0 0 0
1289 0 $item =~ s/^Success\:\ File\ \"//;
1290 0 my ( $file, $task ) = split( /\"\ added\ as\ task\ with\ ID\ /, $item );
1291 0 $added->{$file} = $task;
1292 }
1293 }
1294 } ## end foreach (@to_submit)
1295  
1296 0 return $added;
1297 } ## end sub submit
1298  
1299 =head2 timestamp
1300  
1301 Creates a timestamp to be used with utils/submit. localtime
1302 is used to get the current time.
1303  
1304 print $cape_util->timestamp."\n";
1305  
1306 =cut
1307  
1308 0 ▶sub timestamp {
Subroutine timestamp: not called CC=6 SCAR=37.4
Pod documented
1309 0 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;
1310 0 $year += 1900;
1311 0 $mon++;
1312 0 ▶ if ( $sec < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec < 10) 0 0 0
1313 0 $sec = '0' . $sec;
1314 }
1315 0 ▶ if ( $min < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min < 10) 0 0 0
1316 0 $min = '0' . $min;
1317 }
1318 0 ▶ if ( $hour < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour < 10) 0 0 0
1319 0 $hour = '0' . $hour;
1320 }
1321 0 ▶ if ( $mon < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon < 10) 0 0 0
1322 0 $mon = '0' . $mon;
1323 }
1324 0 ▶ if ( $mday < 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mday < 10) 0 0 0
1325 0 $mday = '0' . $mday;
1326 }
1327  
1328 0 return $mon . '-' . $mday . '-' . $year . ' ' . $hour . ':' . $min . ':' . $sec;
1329 } ## end sub timestamp
1330  
1331 =head2 shuffle
1332  
1333 Performa a Fisher Yates shuffle on the passed array ref.
1334  
1335 =cut
1336  
1337 0 ▶sub shuffle {
Subroutine shuffle: not called CC=3 SCAR=24.8
Pod documented
1338 0 my $self = shift;
1339 0 my $array = shift;
1340 0 my $i;
1341 0 ▶ for ( $i = @$array; --$i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 0 0 0
1342 0 my $j = int rand( $i + 1 );
1343 0 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 0 0 0
1344 0 @$array[ $i, $j ] = @$array[ $j, $i ];
1345 }
1346 0 return $array;
1347 } ## end sub shuffle
1348  
1349 =head2 check_remote
1350  
1351 Checks the remote connection.
1352  
1353 Two variablesare required, API key and IP.
1354  
1355 $results=$cape_utils->check_remote(apikey=>$apikey, remote=>$remote_ip);
1356 if (!$results){
1357 print "unauthed\n";
1358 return;
1359 }
1360  
1361 =cut
1362  
1363 0 ▶sub check_remote {
Subroutine check_remote: not called CC=19 SCAR=59.4
Pod documented
1364 0 my ( $self, %opts ) = @_;
1365  
1366 # if we don't have a API key, we can only auth via IP
1367 0 ▶ if ( ( $self->{config}->{_}->{auth} ne 'ip' || $self->{config}->{_}->{auth} ne 'either' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"_"}{"auth"} ne "ip" || $self->{"config"}{"_"}{"auth"} ne "either" and not defined $opts{"apikey"}) 0 0 0
Condition0%0/6
$self->{"config"}{"_"}{"auth"} ne "ip" || $self->{"config"}{"_"}{"auth"} ne "either" and not defined $opts{"apikey"}
!ll&&!rl&&r
000
$self->{"config"}{"_"}{"auth"} ne "ip" or $self->{"config"}{"_"}{"auth"} ne "either"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"config"}{"_"}{"auth"} ne "ip" || $self->{"config"}{"_"}{"auth"} ne "either" and not defined $opts{"apikey"}
$self->{"config"}{"_"}{"auth"} ne "ip" $self->{"config"}{"_"}{"auth"} ne "either" not defined $opts{"apikey"}
Truth table0%0/5
A or B and not(C)
A= $self->{"config"}{"_"}{"auth"} ne "ip"B= $self->{"config"}{"_"}{"auth"} ne "either"C= not defined $opts{"apikey"}
ABCresult
00X0
1X00
0100
1X11
0111
1368 && !defined( $opts{apikey} ) )
1369 {
1370 0 return 0;
1371 }
1372  
1373 # make sure the API key is what it is expecting if we are not using IP only
1374 0 ▶ if ( $self->{config}->{_}->{auth} ne 'ip'
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"_"}{"auth"} ne "ip" and defined $opts{"apikey"} and $opts{"apikey"} ne $self->{"config"}{"_"}{"apikey"}) 0 0 0
Condition0%0/6
$self->{"config"}{"_"}{"auth"} ne "ip" and defined $opts{"apikey"} and $opts{"apikey"} ne $self->{"config"}{"_"}{"apikey"}
!ll&&!rl&&r
000
$self->{"config"}{"_"}{"auth"} ne "ip" and defined $opts{"apikey"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"config"}{"_"}{"auth"} ne "ip" and defined $opts{"apikey"} and $opts{"apikey"} ne $self->{"config"}{"_"}{"apikey"}
$self->{"config"}{"_"}{"auth"} ne "ip" defined $opts{"apikey"} $opts{"apikey"} ne $self->{"config"}{"_"}{"apikey"}
Truth table0%0/4
A and B and C
A= $self->{"config"}{"_"}{"auth"} ne "ip"B= defined $opts{"apikey"}C= $opts{"apikey"} ne $self->{"config"}{"_"}{"apikey"}
ABCresult
0XX0
10X0
1100
1111
1375 && defined( $opts{apikey} )
1376 && $opts{apikey} ne $self->{config}->{_}->{apikey} )
1377 {
1378 # don't return if it is either as IP may still go off
1379 0 ▶ if ( $self->{config}->{_}->{auth} ne 'either' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"_"}{"auth"} ne "either") 0 0 0
1380 0 return 0;
1381 }
1382 }
1383  
1384 # if we have a apikey and method is set to apikey or either, we are good to return true
1385 0 ▶ if ( defined( $opts{apikey} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"apikey"} and $self->{"config"}{"_"}{"auth"} ne "apikey" || $self->{"config"}{"_"}{"auth"} ne "either") 0 0 0
Condition0%0/6
defined $opts{"apikey"} and $self->{"config"}{"_"}{"auth"} ne "apikey" || $self->{"config"}{"_"}{"auth"} ne "either"
!ll&&!rl&&r
000
$self->{"config"}{"_"}{"auth"} ne "apikey" or $self->{"config"}{"_"}{"auth"} ne "either"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $opts{"apikey"} and $self->{"config"}{"_"}{"auth"} ne "apikey" || $self->{"config"}{"_"}{"auth"} ne "either"
defined $opts{"apikey"} $self->{"config"}{"_"}{"auth"} ne "apikey" $self->{"config"}{"_"}{"auth"} ne "either"
Truth table0%0/4
A and B or C
A= defined $opts{"apikey"}B= $self->{"config"}{"_"}{"auth"} ne "apikey"C= $self->{"config"}{"_"}{"auth"} ne "either"
ABCresult
0XX0
1000
11X1
1011
1386 && ( $self->{config}->{_}->{auth} ne 'apikey' || $self->{config}->{_}->{auth} ne 'either' ) )
1387 {
1388 0 return 1;
1389 }
1390  
1391 # can't do anything else with out a IP
1392 0 ▶ if ( !defined( $opts{ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"ip"}) 0 0 0
1393 0 return 0;
1394 }
1395  
1396 0 my $subnets_string = $self->{config}->{_}->{subnets};
1397 0 $subnets_string =~ s/[\ \t]+//g;
1398 0 $subnets_string =~ s/\,+/,/g;
1399 0 my @subnets_split = split( /,/, $subnets_string );
1400 0 my @subnets;
1401 0 foreach my $item (@subnets_split) {
1402 0 ▶ if ( $item =~ /^[\:A-Fa-f0-9]+$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($item =~ /^[\:A-Fa-f0-9]+$/) { } 0 0 0
elsif ($item =~ m[^[\:A-Fa-f0-9]+/[0-9]+$]) { } 0 0 0
elsif ($item =~ /^[\.0-9]+$/) { } 0 0 0
elsif ($item =~ m[^[\.0-9]+/[0-9]+$]) { } 0 0 0
1403 0 push( @subnets, $item . '/128' );
1404 } elsif ( $item =~ /^[\:A-Fa-f0-9]+\/[0-9]+$/ ) {
1405 0 push( @subnets, $item );
1406 } elsif ( $item =~ /^[\.0-9]+$/ ) {
1407 0 push( @subnets, $item . '/32' );
1408 } elsif ( $item =~ /^[\.0-9]+\/[0-9]+$/ ) {
1409 0 push( @subnets, $item );
1410 }
1411 } ## end foreach my $item (@subnets_split)
1412 0 my $allowed_subnets;
1413 0 eval { $allowed_subnets = subnet_matcher(@subnets); };
1414 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1415 0 die( 'Failed it init subnet matcher... ' . $@ );
1416 }
1417  
1418 0 ▶ if ( $allowed_subnets->( $opts{ip} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$allowed_subnets($opts{"ip"})) 0 0 0
1419 0 return 1;
1420 }
1421  
1422 0 return 0;
1423 } ## end sub check_remote
1424  
1425 =head2 eve_process
1426  
1427 Process the finished tasks for CAPEv2.
1428  
1429 $cape_utils->eve_process;
1430  
1431 =cut
1432  
1433 0 ▶sub eve_process {
Subroutine eve_process: not called CC=18 SCAR=58.3
Pod documented
1434 0 my ( $self, %opts ) = @_;
1435  
1436 0 my $dbh;
1437 0 ▶ eval { $dbh = $self->connect or die $DBI::errstr };
Branch: 1 branch
BranchTrueFalseTotal
$dbh = $self->connect or die $DBI::errstr 0 0 0
1438 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1439 0 die( 'Failed to connect to the DB... ' . $@ );
1440 }
1441  
1442 my $statement
1443 = "select * from tasks where ( status = 'reported' ) AND ( completed_on >= CURRENT_TIMESTAMP - interval '"
1444 . $self->{config}{_}{eve_look_back}
1445 0 . " seconds' )";
1446  
1447 0 my $sth = $dbh->prepare($statement);
1448 0 $sth->execute;
1449  
1450 0 my $row;
1451 my @rows;
1452 0 ▶ while ( $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_hashref) 0 0 0
1453 0 push( @rows, $row );
1454 }
1455  
1456 0 $sth->finish;
1457 0 $dbh->disconnect;
1458  
1459 0 my $main_eve = $self->{config}{_}{eve};
1460  
1461 0 foreach my $row (@rows) {
1462 0 my $report = $self->{config}{_}{base} . '/storage/analyses/' . $row->{id} . '/reports/lite.json';
1463 0 my $id_eve = $self->{config}{_}{incoming_json} . '/' . $row->{id} . '.eve.json';
1464 0 my $incoming_json = $self->{config}{_}{incoming_json} . '/' . $row->{id} . '.json';
1465  
1466 # make sure we have the required files and they are accessible
1467 # id_eve is being used as a lock file to make sure we don't reprocess it
1468 0 ▶ if ( -f $report && -r $report && !-f $id_eve ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $report and -r $report and not -f $id_eve) { } 0 0 0
Condition0%0/6
-f $report and -r $report and not -f $id_eve
!ll&&!rl&&r
000
-f $report and -r $report
!ll&&!rl&&r
000
MC/DC0%0/3
-f $report and -r $report and not -f $id_eve
-f $report -r $report not -f $id_eve
Truth table0%0/4
A and B and not(C)
A= -f $reportB= -r $reportC= not -f $id_eve
ABCresult
0XX0
10X0
1100
1111
1469 0 my $eve_json;
1470 # the incoming json needs to exist if the following is to work
1471 # if it does not, just a mostly empty one
1472 0 ▶ if ( -f $incoming_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $incoming_json) { } 0 0 0
1473 0 eval {
1474 0 $eve_json = decode_json( read_file($incoming_json) );
1475 0 $eve_json->{cape_eve_process} = { incoming_json_error => undef, };
1476 };
1477 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1478 0 my $error_message = 'Failed to decode incoming JSON for ' . $row->{id} . ' ... ' . $@;
1479 0 $self->log_drek( 'cape_eve_process', 'err', $error_message );
1480 0 $eve_json = {
1481 cape_eve_process => {
1482 incoming_json_error => $error_message,
1483 },
1484 };
1485 }
1486 } else {
1487 0 $eve_json = { cape_eve_process => {}, };
1488 }
1489  
1490 # sets various common items so they don't need to be dealt with more than once
1491 # hash creation
1492 0 $eve_json->{cape_eve_process}{time} = time;
1493 0 $eve_json->{cape_eve_process}{host} = hostname;
1494 0 $eve_json->{row} = $row;
1495 0 $eve_json->{event_type} = 'potential_malware_detonation';
1496 0 ▶ if ( !defined( $eve_json->{cape_eve_process}{incoming_json_error} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $eve_json->{"cape_eve_process"}{"incoming_json_error"}) 0 0 0
1497 0 $eve_json->{cape_eve_process}{incoming_json_error} = undef,;
1498 }
1499  
1500 0 my $lite_json;
1501 0 eval {
1502 0 $lite_json = decode_json( read_file($report) );
1503  
1504 0 ▶ if ( defined( $lite_json->{signatures} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lite_json->{"signatures"}) 0 0 0
1505 0 $eve_json->{signatures} = $lite_json->{signatures};
1506 }
1507  
1508 0 ▶ if ( defined( $lite_json->{malscore} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lite_json->{"malscore"}) 0 0 0
1509 0 $eve_json->{malscore} = $lite_json->{malscore};
1510  
1511 0 ▶ if ( $lite_json->{malscore} >= $self->{config}{_}{malscore} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lite_json->{"malscore"} >= $self->{"config"}{"_"}{"malscore"}) 0 0 0
1512 0 $eve_json->{event_type} = 'alert';
1513 }
1514 }
1515 };
1516 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1517 0 my $error_message = 'Failed to decode lite.json for ' . $row->{id} . ' ... ' . $@;
1518 0 $self->log_drek( 'cape_eve_process', 'err', $error_message );
1519 0 $eve_json->{cape_eve_process}{lite_json_error} = $error_message,;
1520 }
1521  
1522 # new line is needed as encode_json does not add one and this prevents the eve file
1523 # from being one long line when appended to
1524 0 my $raw_eve_json = encode_json($eve_json) . "\n";
1525  
1526 0 eval { write_file( $id_eve, $raw_eve_json ); };
1527 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1528 0 my $error_message = 'Failed to write out ID EVE for ' . $row->{id} . ' at ' . $id_eve . ' ... ' . $@;
1529 0 $self->log_drek( 'cape_eve_process', 'err', $error_message );
1530 }
1531  
1532 0 eval { append_file( $self->{config}{_}{eve}, $raw_eve_json ); };
1533 } else {
1534 0 ▶ if ( !-f $report || !-r $report ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $report and -r $report) 0 0 0
Condition0%0/3
-f $report and -r $report
!ll&&!rl&&r
000
MC/DC0%0/2
-f $report and -r $report
-f $report -r $report
Truth table0%0/3
A and B
A= -f $reportB= -r $report
ABresult
0X0
100
111
1535 0 warn( $row->{id} . ' reported, but lite.json does not exist for it or it is not readable' );
1536 }
1537 }
1538 } ## end foreach my $row (@rows)
1539  
1540 } ## end sub eve_process
1541  
1542 # sends stuff to syslog
1543 0 ▶sub log_drek {
Subroutine log_drek: not called CC=3 SCAR=24.8
Pod undocumented
1544 0 my ( $self, $sender, $level, $message ) = @_;
1545  
1546 0 ▶ if ( !defined($level) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $level) 0 0 0
1547 0 $level = 'info';
1548 }
1549  
1550 0 ▶ if ( !defined($sender) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sender) 0 0 0
1551 0 $sender = 'CAPE::Utils';
1552 }
1553  
1554 0 openlog( $sender, 'cons,pid', 'daemon' );
1555 0 syslog( $level, '%s', $message );
1556 0 closelog();
1557 } ## end sub log_drek
1558  
1559 =head1 CONFIG FILE
1560  
1561 The default config file is '/usr/local/etc/cape_utils.ini'.
1562  
1563 The defaults are as below, which out of the box, it will work by
1564 default with CAPEv2 in it's default config.
1565  
1566 # The DBI dsn to use
1567 dsn=dbi:Pg:dbname=cape
1568 # DB user
1569 user=cape
1570 # DB password
1571 pass=
1572 # the install base for CAPEv2
1573 base=/opt/CAPEv2/
1574 # 0/1 if poetry should be used
1575 poetry=1
1576 # the path for poetry... point this at where poetry is setup for your CAPEv2 install
1577 # defaults to /etc/poetry/bin/poetry to be compatible with new CAPEv2 installs
1578 poetry_path=/etc/poetry/bin/poetry
1579 # 0/1 if fail should be allowed to run with out a where statement
1580 fail_all=0
1581 # colums to use for pending table show
1582 pending_columns=id,target,package,timeout,ET,route,options,clock,added_on
1583 # colums to use for runniong table show
1584 running_columns=id,target,package,timeout,ET,route,options,clock,added_on,started_on,machine
1585 # colums to use for tasks table
1586 task_columns=id,target,package,timeout,ET,route,options,clock,added_on,latest,machine,status
1587 # if the target column for running table display should be clipped to the filename
1588 running_target_clip=1
1589 # if microseconds should be clipped from time for running table display
1590 running_time_clip=1
1591 # if the target column for pending table display should be clipped to the filename
1592 pending_target_clip=1
1593 # if microseconds should be clipped from time for pending table display
1594 pending_time_clip=1
1595 # if the target column for task table display should be clipped to the filename
1596 task_target_clip=1
1597 # if microseconds should be clipped from time for task table display
1598 task_time_clip=1
1599 # default table color
1600 table_color=Text::ANSITable::Standard::NoGradation
1601 # default table border
1602 table_border=ASCII::None
1603 # when submitting use now for the current time
1604 set_clock_to_now=1
1605 # default timeout value for submit
1606 timeout=200
1607 # default value for enforce timeout for submit
1608 enforce_timeout=0
1609 # how to auth for mojo_cape_submit
1610 # ip = match against subnets
1611 # apikey = use apikey
1612 # both = require both to match
1613 # either = either may work
1614 auth=ip
1615 # the api key to for with mojo_cape_submit
1616 #apikey=
1617 # comma seperated list of allowed subnets for mojo_cape_submit
1618 subnets=192.168.0.0/16,127.0.0.1/8,::1/128,172.16.0.0/12,10.0.0.0/8
1619 # incoming dir to use for mojo_cape_submit
1620 incoming=/malware/client-incoming
1621 # directory to store json data files for submissions recieved by mojo_cape_submit
1622 # this directory is also used for storing run specific eves
1623 incoming_json=/malware/incoming-json
1624 # Location to write the eve log to.
1625 eve=/opt/CAPEv2/log/eve.json
1626 # how far to go back for processing eve
1627 eve_look_back=360
1628 # malscore for changing the event_type for eve from potential_malware_detonation to alert
1629 malscore=0
1630  
1631 =head2 Report Munge Section
1632  
1633 INI sections matching /^munge\_/ will be used for report munging. This requires two values for that sections,
1634 'check' and 'munge'.
1635  
1636 'check' is a path to a Perl script that will wrapped in a eval and require to check if the file should be
1637 munged or not.
1638  
1639 'munge' is a path to a Perl script that will wrapped in a eval and require to do the munging.
1640  
1641 Below is a example showing the setup for a single script.
1642  
1643 [munge_pdf]
1644 check=/usr/local/etc/cape_utils_munge/pdf_check
1645 munge=/usr/local/etc/cape_utils_munge/pdf_munge
1646  
1647 If more than one munge section exists, they are ran in sorted order.
1648  
1649 If the paths specied do not start with a '/', './', or '../', then '/usr/local/etc/cape_utils_munge/' is
1650 applied to the start.
1651  
1652 The scripts are read as evaled strings.
1653  
1654 The relevant variables are as below.
1655  
1656 - $munge_it :: Perl boolean for if it should be munged or not. Should be set by the check script.
1657  
1658 - $report :: The hash ref containing the parsed JSON report.
1659  
1660 - $changed :: Perl boolean for if it changed or not.
1661  
1662 For some examples see the directory 'munge_examples'.
1663  
1664 =head1 CAPEv2 lite.json to EVE handling
1665  
1666 Tasks are found by looking back X number of seconds in the tasks table for tasks that have reported.
1667 The amount of time is determined by the config value 'eve_look_back'.
1668  
1669 It will check if a task has been processed already or not be seeing if a task specified EVE JSON
1670 has been created under the 'incoming_json' directory. This is in the format $task_id.'eve.json'.
1671 If not, it will proceed.
1672  
1673 It reads the 'lite.json' report for task as well as the incoming JSON. It then copies the keys
1674 'signatures' and 'malscore' into the hash for the incoming JSON and writes it out to
1675 $task_id.'eve.json' and appending it to the file specified via the config value 'eve'.
1676  
1677 The are two possible values for 'event_type', 'potential_malware_detonation' and 'alert'.
1678 'potential_malware_detonation' is changed to alert when 'malscore' goves over the value
1679 specified via config value 'malscore'.
1680  
1681 'row' is the full row for the task in question from the task table as a hash.
1682  
1683 'signatures' is copied from '.signature' in the report JSON.
1684  
1685 =head1 AUTHOR
1686  
1687 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
1688  
1689 =head1 BUGS
1690  
1691 Please report any bugs or feature requests to C<bug-cape-utils at rt.cpan.org>, or through
1692 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=CAPE-Utils>. I will be notified, and then you'll
1693 automatically be notified of progress on your bug as I make changes.
1694  
1695 =head1 SUPPORT
1696  
1697 You can find documentation for this module with the perldoc command.
1698  
1699 perldoc CAPE::Utils
1700  
1701  
1702 You can also look for information at:
1703  
1704 =over 4
1705  
1706 =item * RT: CPAN's request tracker (report bugs here)
1707  
1708 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=CAPE-Utils>
1709  
1710 =item * Search CPAN
1711  
1712 L<https://metacpan.org/release/CAPE-Utils>
1713  
1714 =item * Git
1715  
1716 L<git@github.com:VVelox/CAPE-Utils.git>
1717  
1718 =item * Web
1719  
1720 L<https://github.com/VVelox/CAPE-Utils>
1721  
1722 =back
1723  
1724  
1725 =head1 ACKNOWLEDGEMENTS
1726  
1727  
1728 =head1 LICENSE AND COPYRIGHT
1729  
1730 This software is Copyright (c) 2022 by Zane C. Bowers-Hadley.
1731  
1732 This is free software, licensed under:
1733  
1734 The Artistic License 2.0 (GPL Compatible)
1735  
1736  
1737 =cut
1738  
1739 1 1; # End of CAPE::Utils
↑ Summary