This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
corsaro_main.c
718 lines (611 loc) · 17 KB
/
corsaro_main.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* corsaro
*
* Alistair King, CAIDA, UC San Diego
*
* Copyright (C) 2012 The Regents of the University of California.
*
* This file is part of corsaro.
*
* corsaro is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* corsaro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with corsaro. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libtrace.h"
#include "utils.h"
#include "corsaro.h"
#include "corsaro_log.h"
#ifdef WITH_PLUGIN_SIXT
#include "corsaro_flowtuple.h"
#endif
/** @file
*
* @brief Code which uses libcorsaro to process a trace file and generate output
*
* @author Alistair King
*
*/
/** The number of intervals in CAIDA's legacy flowtuple files */
#define LEGACY_INTERVAL_CNT 60
/** Indicates that Corsaro is waiting to shutdown */
volatile sig_atomic_t corsaro_shutdown = 0;
/** The number of SIGINTs to catch before aborting */
#define HARD_SHUTDOWN 3
/* for when we are reading trace files */
/** A pointer to a libtrace object */
static libtrace_t *trace = NULL;
/** A pointer to a libtrace packet */
static libtrace_packet_t *packet = NULL;
/** A pointer to a libtrace BPF filter */
static libtrace_filter_t *filter = NULL;
#ifdef WITH_PLUGIN_SIXT
/* for when we are reading flowtuple files */
/** A pointer to a corsaro_in object for use when reading flowtuple files */
static corsaro_in_t *corsaro_in = NULL;
/** A pointer to a corsaro record */
static corsaro_in_record_t *record = NULL;
#endif
/** A pointer to the instance of corsaro that we will drive */
static corsaro_t *corsaro = NULL;
/** Should a live interface be set to promiscuous mode? */
static int promisc = 0;
/** The number of legacy intervals we have processed */
static int legacy_intervals = 0;
/* default gap limit */
#define GAP_LIMIT_DEFAULT 0
/** Maximum allowed packet inter-arrival time */
static int gap_limit = GAP_LIMIT_DEFAULT;
/** Handles SIGINT gracefully and shuts down */
static void catch_sigint(int sig)
{
corsaro_shutdown++;
if(corsaro_shutdown == HARD_SHUTDOWN)
{
fprintf(stderr, "caught %d SIGINT's. shutting down NOW\n",
HARD_SHUTDOWN);
exit(-1);
}
fprintf(stderr, "caught SIGINT, shutting down at the next opportunity\n");
/* this is a global flag for libtrace, so probably doesn't hurt to call it but
lets be a little careful */
if(trace != NULL)
{
trace_interrupt();
}
signal(sig, catch_sigint);
}
/** Clean up all state before exit */
static void clean()
{
if(packet != NULL)
{
trace_destroy_packet(packet);
packet = NULL;
}
#ifdef WITH_PLUGIN_SIXT
if(record != NULL)
{
corsaro_in_free_record(record);
record = NULL;
}
#endif
if(corsaro != NULL)
{
corsaro_finalize_output(corsaro);
}
}
/** Prepare a new trace file for reading */
static int init_trace(char *tracefile)
{
/* create a packet buffer */
if (packet == NULL &&
(packet = trace_create_packet()) == NULL) {
perror("Creating libtrace packet");
return -1;
}
trace = trace_create(tracefile);
if (trace_is_err(trace)) {
trace_perror(trace,"Opening trace file");
return -1;
}
/* just in case someone is being dumb */
if(legacy_intervals == 1)
{
fprintf(stderr, "WARNING: -l makes no sense when used with a pcap file\n");
}
/* enable promisc mode on the input if desired by the user */
if(promisc == 1)
{
corsaro_log(__func__, corsaro,
"switching input to promiscuous mode");
if(trace_config(trace,TRACE_OPTION_PROMISC,&promisc)) {
trace_perror(trace,"ignoring: ");
}
}
if (trace_start(trace) == -1) {
trace_perror(trace,"Starting trace");
return -1;
}
return 0;
}
/** Close a trace file */
static void close_trace()
{
if(trace != NULL)
{
trace_destroy(trace);
trace = NULL;
}
}
/** Process a trace file */
static int process_trace(char *traceuri)
{
double this_time = 0;
double last_time = 0;
if(init_trace(traceuri) != 0)
{
corsaro_log(__func__, corsaro,
"could not init trace for reading %s",
traceuri);
return -1;
}
/* let corsaro have the trace pointer */
corsaro_set_trace(corsaro, trace);
while (corsaro_shutdown == 0 && trace_read_packet(trace, packet)>0) {
this_time = trace_get_seconds(packet);
if(gap_limit > 0 && /* gap limit is enabled */
last_time > 0 && /* this is not the first packet */
((this_time-last_time) > 0) && /* packet doesn't go backward */
(this_time - last_time) > gap_limit) /* packet exceeds gap */
{
corsaro_log(__func__, corsaro,
"gap limit exceeded (prev: %f this: %f diff: %f)",
last_time, this_time, (this_time - last_time));
return -1;
}
last_time = this_time;
if((filter == NULL || trace_apply_filter(filter, packet) > 0) &&
corsaro_per_packet(corsaro, packet) != 0)
{
corsaro_log(__func__, corsaro, "corsaro_per_packet failed");
return -1;
}
}
if (trace_is_err(trace)) {
trace_perror(trace,"Reading packets");
corsaro_log(__func__, corsaro, "libtrace had an error reading packets");
return 1;
}
if(trace_get_dropped_packets(trace) != UINT64_MAX)
{
corsaro_log(__func__, corsaro, "dropped pkt cnt: %"PRIu64"\n",
trace_get_dropped_packets(trace));
}
return 0;
}
#ifdef WITH_PLUGIN_SIXT
/** Prepare for processing a FlowTuple file */
static int init_flowtuple(const char *tuplefile)
{
/* get the corsaro_in object we need to use to read the tuple file */
if((corsaro_in = corsaro_alloc_input(tuplefile)) == NULL)
{
corsaro_log(__func__, corsaro,
"could not alloc corsaro_in to read %s",
tuplefile);
return -1;
}
/* get a record buffer */
if((record = corsaro_in_alloc_record(corsaro_in)) == NULL)
{
corsaro_log(__func__, corsaro, "could not alloc record");
return -1;
}
/* start corsaro */
if(corsaro_start_input(corsaro_in) != 0)
{
corsaro_log(__func__, corsaro, "could not start corsaro");
return -1;
}
return 0;
}
/** Close a flowtuple input file */
static void close_flowtuple()
{
if(record != NULL)
{
corsaro_in_free_record(record);
record = NULL;
}
if(corsaro_in != NULL)
{
corsaro_finalize_input(corsaro_in);
corsaro_in = NULL;
}
}
/** Process a FlowTuple input file */
static int process_corsaro(const char *corsuri)
{
off_t len = 0;
corsaro_in_record_type_t type = CORSARO_IN_RECORD_TYPE_NULL;
corsaro_interval_t *int_end = NULL;
if(init_flowtuple(corsuri) != 0)
{
corsaro_log(__func__, corsaro,
"could not init flowtuple reading for %s", corsuri);
return -1;
}
while (corsaro_shutdown == 0 &&
(len = corsaro_in_read_record(corsaro_in, &type, record)) > 0) {
/* if we are in legacy interval mode, and this is an interval end
record, then subtract one from the time, unless it is the last
interval in the input file */
/* because only CAIDA has legacy flowtuple files, we make an assumption
that every 60 intervals we will see a 'last interval' style interval */
if(legacy_intervals == 1 &&
type == CORSARO_IN_RECORD_TYPE_IO_INTERVAL_END)
{
int_end = (corsaro_interval_t*)corsaro_in_get_record_data(record);
assert(int_end->number <= LEGACY_INTERVAL_CNT);
if(int_end->number < (LEGACY_INTERVAL_CNT - 1))
{
int_end->time--;
}
}
corsaro_per_record(corsaro, type, record);
/* reset the type to NULL to indicate we don't care */
type = CORSARO_IN_RECORD_TYPE_NULL;
}
if(len < 0)
{
corsaro_log(__func__, corsaro,
"corsaro_in_read_record failed to read record\n");
return -1;
}
close_flowtuple();
return 0;
}
#endif
/** Print usage information to stderr */
static void usage(const char *name)
{
int i;
char **plugin_names;
int plugin_cnt;
if((plugin_cnt = corsaro_get_plugin_names(&plugin_names)) < 0)
{
/* not much we can do */
fprintf(stderr, "corsaro_get_plugin_names failed\n");
return;
}
fprintf(stderr,
"usage: %s [-alP] -o outfile [-i interval] [-m mode] [-n name]\n"
" [-p plugin] [-f filter] [-r intervals]"
" trace_uri [trace_uri...]\n"
" -a align the end time of the first interval\n"
" -o <outfile> use <outfile> as a template for file names.\n"
" - %%P => plugin name\n"
" - %%N => monitor name\n"
" - see man strftime(3) for more options\n"
" -f <filter> BPF filter to apply to packets\n"
" -g <gap-limit> maximum allowed gap between packets (0 is no limit) (default: %d)\n"
" -G disable the global metadata output file\n"
" -i <interval> distribution interval in seconds (default: %d)\n"
" -l the input file has legacy intervals (FlowTuple only)\n"
" -L disable logging to a file\n"
" -m <mode> output in 'ascii' or 'binary'. (default: binary)\n"
" -n <name> monitor name (default: "
STR(CORSARO_MONITOR_NAME)")\n"
" -p <plugin> enable the given plugin, -p can be used "
"multiple times (default: all)\n"
" available plugins:\n",
name,
GAP_LIMIT_DEFAULT,
CORSARO_INTERVAL_DEFAULT);
for(i = 0; i < plugin_cnt; i++)
{
fprintf(stderr, " - %s\n", plugin_names[i]);
}
fprintf(stderr,
" use -p \"<plugin_name> -?\" to see plugin options\n"
" -P enable promiscuous mode on the input"
" (if supported)\n"
" -r rotate output files after n intervals\n"
" -R rotate corsaro meta files after n intervals\n"
);
corsaro_free_plugin_names(plugin_names, plugin_cnt);
}
/** Entry point for the Corsaro tool */
int main(int argc, char *argv[])
{
int opt;
int prevoptind;
/* we MUST not use any of the getopt global vars outside of arg parsing */
/* this is because the plugins can use get opt to parse their config */
int lastopt;
char *tmpl = NULL;
char *name = NULL;
char *bpf_filter = NULL;
int i = -1000;
int mode = CORSARO_FILE_MODE_BINARY;
char *plugins[CORSARO_PLUGIN_ID_MAX];
int plugin_cnt = 0;
char *plugin_arg_ptr = NULL;
int tracefile_cnt = 0;
char *traceuri = "Multiple Traces";
int align = 0;
int rotate = 0;
int meta_rotate = -1;
int logfile_disable = 0;
int global_file_disable = 0;
signal(SIGINT, catch_sigint);
while(prevoptind = optind,
(opt = getopt(argc, argv, ":f:g:i:m:n:o:p:r:R:aGlLPv?")) >= 0)
{
if (optind == prevoptind + 2 && *optarg == '-' ) {
opt = ':';
-- optind;
}
switch(opt)
{
case 'g':
gap_limit = atoi(optarg);
break;
case 'G':
global_file_disable = 1;
break;
case 'i':
i = atoi(optarg);
break;
case 'l':
legacy_intervals = 1;
break;
case 'L':
logfile_disable = 1;
break;
case 'm':
if(strcmp(optarg, "ascii") == 0)
{
/* ascii output format */
mode = CORSARO_FILE_MODE_ASCII;
}
else if(strcmp(optarg, "binary") == 0)
{
mode = CORSARO_FILE_MODE_BINARY;
}
else
{
fprintf(stderr,
"ERROR: mode parameter must be 'ascii' or 'binary'\n");
usage(argv[0]);
exit(-1);
}
break;
case 'n':
name = strdup(optarg);
break;
case 'o':
tmpl = strdup(optarg);
break;
case 'p':
plugins[plugin_cnt++] = strdup(optarg);
break;
case 'f':
bpf_filter = strdup(optarg);
break;
case 'a':
align = 1;
break;
case 'P':
promisc = 1;
break;
case 'r':
rotate = atoi(optarg);
break;
case 'R':
meta_rotate = atoi(optarg);
break;
case ':':
fprintf(stderr, "ERROR: Missing option argument for -%c\n", optopt);
usage(argv[0]);
exit(-1);
break;
case '?':
case 'v':
fprintf(stderr, "corsaro version %d.%d.%d\n", CORSARO_MAJOR_VERSION,
CORSARO_MID_VERSION, CORSARO_MINOR_VERSION);
usage(argv[0]);
exit(0);
break;
default:
usage(argv[0]);
exit(-1);
}
}
/* store the value of the last index*/
lastopt = optind;
/* reset getopt for others */
optind = 1;
/* -- call NO library functions which may use getopt before here -- */
/* this ESPECIALLY means corsaro_enable_plugin */
/* ensure that there is at least one trace file given */
if(lastopt > argc - 1)
{
fprintf(stderr, "ERROR: At least one trace file must be specified\n");
usage(argv[0]);
goto err;
}
tracefile_cnt = argc-lastopt;
/* argv[lastopt] is the pcap file */
/* if there is only one trace file, we want to tell corsaro what the uri was
rather than the silly "Multiple Traces" default */
if(tracefile_cnt == 1)
{
traceuri = argv[lastopt];
}
if(tmpl == NULL)
{
fprintf(stderr,
"ERROR: An output file template must be specified using -o\n");
usage(argv[0]);
goto err;
}
/* alloc corsaro */
if((corsaro = corsaro_alloc_output(tmpl, mode)) == NULL)
{
usage(argv[0]);
goto err;
}
/* create the bpf filter if specified */
if(bpf_filter != NULL)
{
corsaro_log(__func__, corsaro, "compiling filter: \"%s\"",
bpf_filter);
filter = trace_create_filter(bpf_filter);
}
/* keep a record of what this file was called */
if(corsaro_set_traceuri(corsaro, traceuri) != 0)
{
corsaro_log(__func__, corsaro, "failed to set trace uri");
goto err;
}
if(name != NULL && corsaro_set_monitorname(corsaro, name) != 0)
{
corsaro_log(__func__, corsaro, "failed to set monitor name");
goto err;
}
if(i > -1000)
{
corsaro_set_interval(corsaro, i);
}
if(align == 1)
{
corsaro_set_interval_alignment(corsaro, CORSARO_INTERVAL_ALIGN_YES);
}
if(rotate > 0)
{
corsaro_set_output_rotation(corsaro, rotate);
}
if(meta_rotate >= 0)
{
corsaro_set_meta_output_rotation(corsaro, meta_rotate);
}
for(i=0;i<plugin_cnt;i++)
{
/* the string at plugins[i] will contain the name of the plugin,
optionally followed by a space and then the arguments to pass
to the plugin */
if((plugin_arg_ptr = strchr(plugins[i], ' ')) != NULL)
{
/* set the space to a nul, which allows plugins[i] to be used
for the plugin name, and then increment plugin_arg_ptr to
point to the next character, which will be the start of the
arg string (or at worst case, the terminating \0 */
*plugin_arg_ptr = '\0';
plugin_arg_ptr++;
}
if(corsaro_enable_plugin(corsaro, plugins[i], plugin_arg_ptr) != 0)
{
fprintf(stderr, "ERROR: Could not enable plugin %s\n",
plugins[i]);
usage(argv[0]);
goto err;
}
}
if(logfile_disable != 0)
{
corsaro_disable_logfile(corsaro);
}
if(global_file_disable != 0)
{
corsaro_disable_globalfile(corsaro);
}
if(corsaro_start_output(corsaro) != 0)
{
/* 02/25/13 - ak comments debug message */
/*
corsaro_log(__func__, corsaro, "failed to start corsaro");
*/
usage(argv[0]);
goto err;
}
for(i = lastopt; i < argc && corsaro_shutdown == 0; i++)
{
/* this should be a new file we're dealing with */
assert(trace == NULL);
#ifdef WITH_PLUGIN_SIXT
assert(corsaro_in == NULL);
#endif
corsaro_log(__func__, corsaro, "processing %s", argv[i]);
#ifdef WITH_PLUGIN_SIXT
/* are we dealing with a flowtuple file ? */
/* @todo replace this with a corsaro_flowtuple call to check the magic */
if(corsaro_flowtuple_probe_file(NULL, argv[i]) == 1)
{
if(process_corsaro(argv[i]) != 0)
{
/* let process_corsaro log the error */
goto err;
}
}
else
{
#endif
if(process_trace(argv[i]) != 0)
{
/* let process_trace log the error */
goto err;
}
/* close the trace unless this is the last trace */
if(i < argc-1)
{
close_trace();
}
#ifdef WITH_PLUGIN_SIXT
}
#endif
}
/* free the plugin strings */
for(i=0;i<plugin_cnt;i++)
{
if(plugins[i] != NULL)
free(plugins[i]);
}
/* free the template string */
if(tmpl != NULL)
free(tmpl);
corsaro_finalize_output(corsaro);
close_trace();
corsaro = NULL;
clean();
return 0;
err:
/* if we bail early, let us be responsible and up the memory we alloc'd */
for(i=0;i<plugin_cnt;i++)
{
if(plugins[i] != NULL)
free(plugins[i]);
}
if(tmpl != NULL)
free(tmpl);
clean();
return -1;
}