/
Snort.pm
235 lines (179 loc) · 5.04 KB
/
Snort.pm
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
=head1 NAME
POE::Filter::Snort - a POE stream filter that parses Snort logs into hashes
=head1 SYNOPSIS
#!/usr/bin/env perl
use warnings; use strict;
use POE qw(Filter::Snort Wheel::FollowTail);
POE::Session->create(
inline_states => {
_start => \&start_log,
got_rec => \&display_record,
},
);
POE::Kernel->run();
exit;
sub start_log {
$_[HEAP]->{watcher} = POE::Wheel::FollowTail->new(
Filename => "/var/log/snort/alert",
Filter => POE::Filter::Snort->new(),
InputEvent => "got_rec",
);
}
sub display_record {
my $rec = $_[ARG0];
print "Got a snort record:\n";
print "\tComment: $rec->{comment}\n" if exists $rec->{comment};
print "\tClass : $rec->{class}\n" if exists $rec->{class};
print "\tPrio : $rec->{priority}\n" if exists $rec->{priority};
if (exists $rec->{src_ip}) {
if (exists $rec->{src_port}) {
print "\tSource : $rec->{src_ip} $rec->{src_port}\n";
print "\tDest : $rec->{dst_ip} $rec->{dst_port}\n";
}
else {
print "\tSource : $rec->{src_ip}\n";
print "\tDest : $rec->{dst_ip}\n";
}
foreach my $xref (@{$rec->{xref}}) {
print "\tXref : $xref\n";
}
}
}
=head1 DESCRIPTION
POE::Filter::Snort parses streams containing Snort alerts. Each alert
is returned as a hash containing the following fields: comment, class,
priority, src_ip, dst_ip, src_port, dst_port, xref, raw.
Most fields are optional. For example, some snort alerts don't
contain a source and destination IP address. Those that do aren't
always accompanied by a source and destination port.
The xref field refers to an array of URLs describing the alert in more
detail. It will always exist, but it may be empty if no URLs appear
in snort's configuration file.
The raw field is an arrayref containing each original line of the
snort alert.
=cut
package POE::Filter::Snort;
use warnings;
use strict;
use vars qw($VERSION @ISA);
$VERSION = '0.031';
@ISA = qw(POE::Filter);
use Carp qw(carp croak);
use POE::Filter::Line;
sub FRAMING_BUFFER () { 0 }
sub PARSER_STATE () { 1 }
sub PARSED_RECORD () { 2 }
sub LINE_FILTER () { 3 }
sub STATE_OUTSIDE () { 0x02 }
sub STATE_INSIDE () { 0x04 }
sub new {
my $class = shift;
# We use a POE::Filter::Line internally.
my $line_filter = POE::Filter::Line->new();
my $self = bless [
"", # FRAMING_BUFFER
STATE_OUTSIDE, # PARSER_STATE
{ }, # PARSED_RECORD
$line_filter, # LINE_FILTER
], $class;
}
sub get_one_start {
my ($self, $stream) = @_;
$self->[LINE_FILTER]->get_one_start($stream);
}
sub get_one {
my $self = shift;
while (1) {
my $line = $self->[LINE_FILTER]->get_one();
return [ ] unless @$line;
$line = $line->[0];
if ($self->[PARSER_STATE] & STATE_OUTSIDE) {
next unless $line =~
/^\[\*\*\]\s*\[(\d+):(\d+):(\d+)\]\s*(.*?)\s*\[\*\*\]/;
$self->[PARSED_RECORD] = {
sid => $2,
rev => $3,
comment => $4,
xref => [ ],
raw => [ $line ],
};
$self->[PARSER_STATE] = STATE_INSIDE;
next;
}
push @{ $self->[PARSED_RECORD]{raw} }, $line;
if ($line =~ /^\s*$/) {
$self->[PARSER_STATE] = STATE_OUTSIDE;
return [ $self->[PARSED_RECORD] ];
}
if ($line =~ /\[Classification:\s*(.+?)\s*\]/) {
$self->[PARSED_RECORD]{class} = $1;
}
if ($line =~ /\[Priority:\s*(.+?)\s*\]/) {
$self->[PARSED_RECORD]{priority} = $1;
}
if (
$line =~ m{
^\d+\/\d+-\d+:\d+:\d+\.\d+\s*
(\d+\.\d+\.\d+\.\d+):(\d+) # src ipv4 : port
\s*->\s*
(\d+\.\d+\.\d+\.\d+):(\d+) # dst ipv4 : port
}x
) {
$self->[PARSED_RECORD]{src_ip} = $1;
$self->[PARSED_RECORD]{src_port} = $2;
$self->[PARSED_RECORD]{dst_ip} = $3;
$self->[PARSED_RECORD]{dst_port} = $4;
}
elsif (
$line =~ m{
^\d+\/\d+-\d+:\d+:\d+\.\d+\s*
(\d+\.\d+\.\d+\.\d+) # src ipv4
\s*->\s*
(\d+\.\d+\.\d+\.\d+) # dst ipv4
}x
) {
$self->[PARSED_RECORD]{src_ip} = $1;
$self->[PARSED_RECORD]{dst_ip} = $2;
}
while ($line =~ /\[Xref\s*=>\s*(.*?)\s*\]/g) {
push @{$self->[PARSED_RECORD]{xref}}, $1;
}
die $line if $line =~ /<-/;
}
}
sub put {
my $self = shift;
croak ref($self) . " doesn't implement put()";
}
sub get_pending {
my $self = shift;
return $self->[LINE_FILTER]->get_pending();
}
sub get {
my ($self, $stream) = @_;
my @return;
$self->get_one_start($stream);
while (1) {
my $next = $self->get_one();
last unless @$next;
push @return, @$next;
}
return \@return;
}
1;
=head1 SEE ALSO
Snort - "the de facto standard for intrusion detection/prevention"
http://www.snort.org/
L<POE>
=head1 BUG TRACKER
https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=POE-Filter-Snort
=head1 REPOSITORY
http://github.com/rcaputo/poe-filter-snort
http://gitorious.org/poe-filter-snort
=head1 OTHER RESOURCES
http://search.cpan.org/dist/POE-Filter-Snort/
=head1 COPYRIGHT
Copyright 2005-2010, Rocco Caputo. All rights are reserved.
POE::Filter::Snort is free software; you may use, redistribute, and/or
modify it under the same terms as Perl itself.
=cut