File: ipmon2dlf.in

package info (click to toggle)
lire 20020214-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,180 kB
  • ctags: 1,245
  • sloc: perl: 11,637; xml: 5,725; sh: 3,458; makefile: 1,008
file content (218 lines) | stat: -rw-r--r-- 7,401 bytes parent folder | download
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
#! @PATHTOPERL@ -w

# vim:syntax=perl

use strict;
use lib '@LR_PERL5LIBDIR@';
use Lire::DlfSchema;
use Lire::Syslog;
use Lire::Firewall qw/ firewall_number2names /;
use Lire::Program qw( :msg :dlf );

my %action2cisco =
  (
   'b'      => "denied",
   'a'      => "permitted",
  );


init_dlf_converter( "firewall" );
my $schema = Lire::DlfSchema::load_schema( "firewall" );
my $dlf_maker = $schema->make_hashref2asciidlf_func(
   qw/time rule action protocol from_ip from_port rcv_intf to_ip to_port length count/
);

my ($lines, $dlflines, $errorlines) = (0, 0, 0);
my $parser = new Lire::Syslog;

while (<>) {
    chomp;

    $lines++;
    my $log = eval { $parser->parse($_) };
    if ($@) {
	lr_warn( $@ );
	lr_notice( qq{cannot convert line $. "$_" to firewall dlf, skipping} );
	$errorlines++;
	next;
    }

    my %dlf;
    if ($log->{'process'} ne "ipmon" ) {
	lr_warn("skipping line '$_': process should be ipmon, " .
                "not '" . $log->{'process'} . "'");
	$errorlines++;
	next;
    }

    $dlf{time} = $log->{timestamp};

    # process stuff like:
    # 07:42:28.585962 ep1 @0:8 b 192.168.26.5,53 -> 192.168.26.1,2926 PR udp 
    #    len 20 70  OUT 
    my @line = split ' ', $log->{'content'};
    defined $line[0] and $dlf{'iptime'} = $line[0];

    $dlf{count} = 1;
    if (defined $line[1] and $line[1] =~ /^(\d+)x$/) {
        # print as many dlf's as the number indicates
        $dlf{count} = $1;
        shift @line;
    }

    defined $line[1] and $dlf{'rcv_intf'} = $line[1];
    defined $line[2] and $dlf{'rule'} = $line[2];
    defined $line[3] and $dlf{'action'} = $line[3]; # b or p
    if (defined $line[4]) {
        ($dlf{'from_ip'}, my $port) = split /,/, $line[4];
        defined $port and $dlf{'from_port'} = $port;
    }
    defined $line[5] and ($line[5] eq '->' or 
        lr_warn("skipping line '$_': invalid format: fifth field " .
        "should be '->', not '". $line[5] ."'") and $errorlines++ and next);
    if (defined $line[6]) {
        ($dlf{'to_ip'}, my $port) = split /,/, $line[6];
        defined $port and $dlf{'to_port'} = $port;
    }
    defined $line[7] and ($line[7] eq 'PR' or 
        lr_warn("skipping line '$_': invalid format: seventh field " .
        "should be 'PR', not '". $line[7] ."'") and $errorlines++ and next);
    defined $line[8] and $dlf{'protocol'} = $line[8]; # one of tcp, udp, icmp
    defined $line[9] and ($line[9] eq 'len' or 
        lr_warn("skipping line '$_': invalid format: nineth field " .
        "should be 'len', not '". $line[9] ."'") and $errorlines++ and next);

    if (defined $line[10] and defined $line[11]) {
        # sum lenght of header and lenght of packet

        # Jul  6 08:13:15 rolle ipmon[2658]: 08:13:15.373154 ep1 @0:7 b 
        #    100.187.115.1 -> 100.0.0.1 PR igmp len 24 (32) IN
        $line[11] =~ s/^[^\d]*(\d+)[^\d]*$/$1/;

        $dlf{'length'} = $line[10] + $line[11];
	$dlf{'length'} *= $dlf{'count'};
    }

    if (defined $line[12]) {
        if ($line[12] =~ /^-\w+$/) {
            $dlf{'flags'} = $line[12];
            defined $line[13] and $dlf{'direction'} = $line[13];
        } elsif ($line[12] eq 'icmp') {
            splice(@line, 0, 13);

            if ($line[-1] eq 'OUT' or $line[-1] eq 'IN') {
                $dlf{'direction'} = $line[-1];
                pop @line;
            }

            $dlf{'flags'} = join('_', @line);
        } else {
            defined $line[12] and $dlf{'direction'} = $line[12];
        }
    }

    $dlf{action} = $action2cisco{$dlf{action}} || $dlf{action};

    firewall_number2names( \%dlf );
    my $dlf = $dlf_maker->( \%dlf );

    print join( " ", @$dlf), "\n";
    $dlflines++;
}

end_dlf_converter( $lines, $dlflines, $errorlines );

__END__

=pod

=head1 NAME

ipmon2dlf - convert ipf logs, as produced by ipmon, to dlf

=head1 SYNOPSIS

B<ipmon2ldf>

=head1 DESCRIPTION

B<ipmon2dlf> reads from stdin, writes dlf to stdout, and, in case of errors,
complains on stderr.  It expects an ipf logfile, as produced by Darren Reed's
IP Filter, on stdin.  IP Filter is shipped with FreeBSD, OpenBSD (up to 2.9)
and some other OS's.

=head1 EXAMPLE

A ipfilter logfile which looks like

 Oct 30 07:42:29 rolle ipmon[16747]: 07:42:28.585962              ie0 @0:9 
  b 192.168.48.1,45085 -> 192.168.48.2,22 PR tcp len 20 64 -S OUT 
 Oct 30 07:40:24 rolle ipmon[16747]: 07:40:23.631307              ep1 @0:6 
  b 192.168.26.5,113 -> 192.168.26.1,3717 PR tcp len 20 40 -AR OUT 
 Oct 30 07:42:29 rolle ipmon[16747]: 07:42:28.585962              ie0 @0:9 
  b 192.168.48.1,45085 -> 192.168.48.2,22 PR tcp len 20 64 -S OUT 
 Oct 30 07:44:11 rolle ipmon[16747]: 07:44:10.605416 2x              ep1 @0:15 
  b 192.168.26.1,138 -> 192.168.26.255,138 PR udp len 20 257  IN 
 Oct 30 07:44:34 rolle ipmon[16747]: 07:44:33.891869              ie0 @0:10 
  b 192.168.48.1,23406 -> 192.168.48.2,22 PR tcp len 20 64 -S OUT 
 Oct 30 07:49:13 rolle ipmon[16747]: 07:49:12.554420              ep1 @0:15 
  b 210.132.100.117 -> 192.168.26.5 PR icmp len 20 56 icmp 3/3 for 
  192.168.26.5,61915 - 210.132.100.117,53 PR udp len 20 23040 IN 
 Oct 30 07:50:23 rolle ipmon[16747]: 07:50:22.908107              ep1 @0:15 
  b 210.132.100.117 -> 192.168.26.5 PR icmp len 20 56 icmp 3/3 for 
  192.168.26.5,4480 - 210.132.100.117,53 PR udp len 20 19712 IN 
 Oct 30 07:56:11 rolle ipmon[16747]: 07:56:11.113029 2x              ep1 @0:15 
  b 192.168.26.1,138 -> 192.168.26.255,138 PR udp len 20 257  IN 

(that's: .... 'PR' protocol 'len' length_of_ip_headers_saved packetlength
direction) will get converted to something like

 994398737 denied igmp 100.187.115.1 - ep1 LIRE_NOTAVAIL 224.0.0.2 - 56
 994398861 denied igmp 100.187.115.1 - ep1 LIRE_NOTAVAIL 224.0.0.1 - 56
 994398862 denied igmp 100.187.115.1 - ep1 LIRE_NOTAVAIL 224.0.0.2 - 56
 994406849 denied udp 192.168.26.4 137 ie0 LIRE_NOTAVAIL 192.168.26.255 137 116
 994406850 denied udp 192.168.26.4 137 ie0 LIRE_NOTAVAIL 192.168.26.255 137 116
 994406866 denied udp 192.168.26.4 137 ie0 LIRE_NOTAVAIL 192.168.26.255 137 98

=head1 SEE ALSO

ipl(4) for description of log structure.

The ipmon.c source (e.g. on
http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/usr.sbin/ipmon/Attic/ipmon.c?rev=1.27&content-type=text/plain&hideattic=0
for the specification) of the log syntax.

The IP Filter webpage on http://coombs.anu.edu.au/~avalon/ip-filter.html

=head1 VERSION

$Id: ipmon2dlf.in,v 1.7 2002/01/22 23:38:49 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2000-2001 Stichting LogReport Foundation LogReport@LogReport.org
 
This program 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 2 of the License, or
(at your option) any later version.
 
This program 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 this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=head1 AUTHOR

Joost van Baal <joostvb@logreport.org>

=end

# Local Variables:
# mode: cperl
# End: