forked from Merit-Research/AMON
-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge.pl
134 lines (130 loc) · 2.7 KB
/
merge.pl
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
# Merge alerts so that we have fewer to compare with peakflow
# Specify alerts.txt file on the command line
$THRESH = 600;
$DELAY = 300;
$usage="$0 alerts-file\n";
%alerts = ();
if ($#ARGV < 0)
{
print $usage;
exit(1);
}
$fh = new IO::File($ARGV[0]);
while(<$fh>)
{
@items = split /\s+/, $_;
$id = $items[0];
$time = $items[2];
$bytes = $items[5];
$pkts = $items[6];
if ($_ =~ m/(.*)(dst ip )(\d+\.\d+\.\d+\.\d+)(.*)/)
{
$target = $3;
$text = $4;
}
else
{
next;
}
if ($_ =~ m/(.*)(src ip )(\d+\.\d+\.\d+\.\d+)(.*)(dst ip )(\d+\.\d+\.\d+\.\d+)(.*)/)
{
next;
}
if ($_ =~ m/(.*)(proto tcp and flags 0)/)
{
next;
}
else
{
$alerts{$target}{$id}{'text'} = $_;
$alerts{$target}{$id}{'time'} = $time;
$type = 0;
if ($_ =~ /proto udp/ && $_ =~ /src port 53 /)
{
$type = 1;
}
elsif ($_ =~ /proto icmp/)
{
$type = 2;
}
elsif ($_ =~ /proto udp/ && $_ =~ /src port 0/)
{
$type = 8;
}
elsif ($_ =~ /proto udp/ && $_ =~ /src port 389 /)
{
$type = 16;
}
elsif ($_ =~ /proto udp/ && $_ =~ /src port 123 /)
{
$type = 256;
}
elsif ($_ =~ /proto tcp/ && $_ =~ /flags 2\n/)
{
$type = 1024;
}
elsif ($_ =~ /proto tcp/ && $_ =~ /flags 16/)
{
$type = 512;
}
elsif ($_ =~ /proto tcp/ && $_ =~ /flags 18/)
{
$type = 32;
}
elsif ($_ =~ /proto tcp/ && $_ =~ /flags 4\n/)
{
$type = 64;
}
$alerts{$target}{$id}{'type'} = $type;
$alerts{$target}{$id}{'bytes'} = $bytes;
$alerts{$target}{$id}{'pkts'} = $pkts;
}
}
for $target (keys %alerts)
{
$ptime = 0;
$p = "";
$bytes = 0;
$pkts = 0;
$type = 0;
$pid = 0;
for $id (sort {$a <=> $b} keys %{$alerts{$target}})
{
$diff = $alerts{$target}{$id}{'time'} - $ptime;
#print "target $target time $alerts{$target}{$id}{'time'} ptime $ptime p $p diff $diff\n";
if ($ptime == 0 || ($alerts{$target}{$id}{'time'} - $ptime > $THRESH))
{
if ($p != "")
{
if ($type > 0)
{
print "$pid $start $end $target $bytes $pkts $type\n";
}
}
$type = $alerts{$target}{$id}{'type'};
$start = $alerts{$target}{$id}{'time'};
$bytes = $alerts{$target}{$id}{'bytes'};
$pkts = $alerts{$target}{$id}{'pkts'};
$p = $alerts{$target}{$id}{'text'};
$pid = $id;
}
else
{
$type |= $alerts{$target}{$id}{'type'};
}
$end = $alerts{$target}{$id}{'time'} + $DELAY;
$ptime = $alerts{$target}{$id}{'time'};
if ($bytes < abs($alerts{$target}{$id}{'bytes'}))
{
$bytes = abs($alerts{$target}{$id}{'bytes'});
}
if ($pkts < abs($alerts{$target}{$id}{'pkts'}))
{
$pkts = abs($alerts{$target}{$id}{'pkts'});
}
}
if ($type > 0)
{
print "$pid $start $end $target $bytes $pkts $type\n";
}
}