This repository has been archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
cif-smrt
executable file
·394 lines (324 loc) · 11.3 KB
/
cif-smrt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!perl
BEGIN {
use FindBin;
use local::lib "$FindBin::Bin/..";
}
use feature 'say';
use strict;
use warnings;
use AnyEvent;
use Daemon::Control;
use Getopt::Long;
use CIF qw/debug hash_create_random init_logging $Logger parse_rules parse_config/;
use CIF::Smrt;
use CIF::Rule;
use CIF::SDK::Client;
use Data::Dumper;
use Carp qw/croak/;
use Try::Tiny;
use constant CIF_REMOTE => 'http://localhost:5000';
my $help;
my $remote = CIF_REMOTE; # https://github.com/csirtgadgets/massive-octo-spice/issues/71
my $token;
my $rule = $CIF::SmrtRulesDefault;
my $feed = '';
my $limit;
my $refresh = 0;
my $clean = 0;
my $notbefore = 'today';
my $cache = $CIF::VarPath.'/smrt/cache';
my $meta = 0;
my $timeout = 900;
my $no_verify_ssl = 1; ##TODO alphas only!!!
my $ignore_journal;
my $proxy;
my $https_proxy;
my $test_observable;
# daemon
my $daemon = 0;
my $config = $CIF::EtcPath.'/cif-smrt.yml';
my $pidfile = $CIF::PidPath.'/smrt.pid';
my $user = $CIF::CIF_USER;
my $group = $CIF::CIF_GROUP;
my $interval = 60; #min
my $randomstart = 30; #min
my $testmode = 0;
# notification
my $notify;
my $notify_to = 'root@localhost';
my $notify_subj = '[cif-smrt] ERROR';
my $notify_from = 'cif';
my $notify_level = 'error';
# logging
my $debug = 0;
my $verbosity = 0;
my $logging = 0;
my $consolemode = 0;
my $logfile = $CIF::LogPath.'/cif-smrt.log';
my $today = DateTime->today()->ymd('');
Getopt::Long::Configure("bundling");
GetOptions(
# basic
'help|h' => \$help,
'config|C=s' => \$config,
# daemon opts
'daemon|D' => \$daemon,
'user|u=s' => \$user,
'group|g=s' => \$group,
'pidfile|p=s' => \$pidfile,
'--interval' => \$interval,
'--randomstart' => \$randomstart,
'--testmode' => \$testmode,
'--ignore-journal' => \$ignore_journal,
# logging
'logfile=s' => \$logfile,
'debug|d+' => \$debug,
'verbosity|v+' => \$verbosity,
'logging' => \$logging,
# advanced
'remote|R=s' => \$remote,
'timeout=i' => \$timeout,
'no-verify-ssl' => \$no_verify_ssl,
'token|T=s' => \$token,
'rule|r=s' => \$rule,
'feed|f=s' => \$feed,
'limit|L=i' => \$limit,
'refresh|Z' => \$refresh,
'meta|M' => \$meta,
'clean|c' => \$clean,
'not-before=s' => \$notbefore,
'cache|P=s' => \$cache,
'proxy=s' => \$proxy,
'https-proxy=s' => \$https_proxy,
'test-observable=s' => \$test_observable,
#notification
'notify' => \$notify,
'notify-to=s' => \$notify_to,
'notify-subj=s' => \$notify_subj,
'notify-from=s' => \$notify_from,
'notify-level=s'=> \$notify_level,
) or die(usage());
my $smrt_config;
my $client_config;
die(usage()) if($help);
die(usage()) unless($rule);
unless(-e $rule){
say (usage()."ERROR: $rule doesn't exist or cif-smrt doesn't have permission to access it");
exit(-1);
}
if($notbefore && $notbefore =~ /^\d+$/){
print usage();
print $0.' -N|--notbefore requires quotes around the value (eg: -N "3 days ago")'."\n\n";
exit(-1);
}
if(-e $config){
$config = parse_config($config);
$config = $config->{'client'};
$token = $token || $config->{'token'};
if($config->{'remote'} && ($remote eq CIF_REMOTE)){
$remote = $config->{'remote'};
}
$timeout = $timeout || $config->{'timeout'};
$no_verify_ssl = $config->{'no_verify_ssl'} if($config->{'no_verify_ssl'} && !$no_verify_ssl);
$proxy = $proxy || $config->{'proxy'};
$https_proxy = $https_proxy || $config->{'https_proxy'};
$notbefore = $config->{'notbefore'} || $notbefore;
}
sub usage {
return <<EOF;
Usage: $0 [OPTIONS] [-D status|start|stop|restart|reload]
Options:
-C, --config=FILE specify cofiguration file, default: $config
-d, --debug turn on debugging (max verbosity)
-v+, --verbosity turn up verbosity
-h, --help this message
-r, --rule=STRING specify a rule or a rules directory, default: $rule
-f, --feed=STRING specify a feed (within a rule)
-R, --remote=STRING specify a remote to connect to, default $remote
-T, --token=STRING specify a default token/apikey to use
--not-before=STRING specify a time to begin processing the data "[today|yesterday|X days ago]"
--limit=INT limit parsing to a subset of records (useful for debugging)
--proxy specify a proxy address for cif-smrt to use in fetching feeds
--https-proxy specify a proxy for cif-smrt to use for feeds hosted on https
Daemon Options:
-D, --daemon run as daemon
-u, --user run daemon as user, default: $user
-g, --group run daemon as group, default: $group
-p, --pid pidfile location, default: $pidfile
--randomstart random start delay, default: $randomstart min
--interval runtime interval, default: $interval min
--testmode run now, overrides randomstart
--logfile: logfile location, default: $logfile
--logging: turn on logging [to file]
Notification Options:
--notify: turn on notification, default: off.
--notify-to: default: $notify_to
--notify-from: default: $notify_from
--notify-subj: default: $notify_subj
--notify-level: default: $notify_level
Advanced Options:
-M, --meta apply metadata processors, default: $meta
-c, --clean clear cache
-P, --cache cache location, default $cache
Examples:
$0 -C $config
$0 -C $config -p $pidfile -D start
$0 -r $rule -D start
EOF
}
if($notify){
$notify = {
to => $notify_to,
from => $notify_from,
subject => $notify_subj,
min_level => lc($notify_level) }
}
my $loglevel = ($debug) ? 'DEBUG' : 'INFO';
init_logging(
{
level => $loglevel,
category => 'cif-smrt',
filename => ($logging) ? $logfile : 0,
},
$notify,
);
if($verbosity){
unless($debug){ # we're already at TRACE
$Logger->more_logging($verbosity);
}
}
unless($token){
croak('missing token');
}
$Logger->info('staring up...');
if($daemon){
$ARGV[0] = 'start';
my $rv = Daemon::Control->new(
name => 'cif-smrt',
prereq_no_process => 1,
program => sub { main() },
pid_file => $pidfile,
fork => 2,
user => $user,
group => $group,
)->run();
} elsif($testmode) {
_main();
} else {
$Logger->more_logging(2);
main();
}
$Logger->info('shutting down...');
sub main {
my $done = AnyEvent->condvar();
# play nice with feed providers, so we don't slam all the feed providers every 60min at once
$randomstart = ($testmode) ? 0 : int(rand($randomstart)); # sometime in the next $randomstart
$Logger->debug('random start set to: '.$randomstart);
$Logger->info('delaying start for: '.$randomstart.'min then running every '.$interval.'min there after...');
$Logger->info('to run immediately, set: --randomstart 0 or --testmode');
$Logger->info('to see the list of options, use -h');
$randomstart = (60 * $randomstart);
$interval = (60 * $interval);
my $w = AnyEvent->timer(
after => $randomstart,
interval => $interval,
cb => sub {
_main();
$done->send() if($testmode);
$Logger->info('sleeping: '.($interval/60).'min');
}
);
$done->recv();
}
sub _main {
my $child = fork();
if($child){
# parent
$Logger->debug('running pid: '.$child);
waitpid($child,0);
} else {
# child
my ($smrt,$err,$ret,$data,$rules,$cli);
_clean_tmp();
$rules = parse_rules($rule,$feed);
$rules = [ $rules ] unless(ref($rules) && ref($rules) eq 'ARRAY');
my $nowait = 1;
$nowait = 0 if($testmode);
$cli = CIF::SDK::Client->new({
remote => $remote,
token => $token,
timeout => $timeout,
verify_ssl => ($no_verify_ssl) ? 0 : 1,
nowait => $nowait,
});
$Logger->info('checking for router...');
($ret,$err) = $cli->ping_write();
unless($ret){
$err = 'unknown error...' unless($err);
$Logger->fatal($err);
exit 0;
}
foreach my $r (@$rules){
undef $err;
if($r->disabled && $r->disabled eq 'true'){
$Logger->info("disabled: $r->{'rule_path'} -f $r->{'feed'}");
next();
}
$Logger->debug($r->defaults->{'provider'}.' - '.$r->feed);
$Logger->info("processing: -r $r->{'rule_path'} -f $r->{'feed'}");
$r->defaults->{'group'} = 'everyone' unless($r->defaults->{'group'});
$r->defaults->{'altid'} = $r->defaults->{'remote'} unless($r->defaults->{'altid'});
$smrt = CIF::Smrt->new({
rule => $r,
tmp => $cache,
ignore_journal => $ignore_journal,
not_before => $notbefore,
limit => $limit,
proxy => $proxy,
https_proxy => $https_proxy,
test_observable => $test_observable,
});
try {
$data = $smrt->process();
} catch {
$err = shift;
};
if($err){
$Logger->error('failed to process data, skipping...');
$err =~ s/\n//g;
$Logger->error($err);
} else {
if($#{$data} >= 0){
$Logger->debug('sending '.($#{$data} + 1).' observables...');
($ret,$err) = $cli->submit($data);
if($err){
$Logger->error($err);
} else {
if(ref($ret) eq 'HASH' && $ret->{'message'}){
$Logger->debug($ret->{'message'});
}
$Logger->debug('done sending...');
}
} else {
$Logger->info('nothing new to send...');
}
}
}
$Logger->info('completed');
exit 0;
}
}
##TODO re-factor
sub _clean_tmp {
$Logger->debug('cleaning up tmp: '.$cache);
opendir(F,$cache) || return;
my $tmp = [ sort { $a cmp $b } grep (/^\d+\.log$/,readdir(F)) ];
foreach (@$tmp){
m/^(\d+)\.log$/;
if($clean || ($1 < $today)){
$Logger->info('removing: '.$cache.'/'.$_);
#todo -- clean this
system('rm -f '.$cache.'/'.$_);
}
}
}