File: parsetest.pl

package info (click to toggle)
psad 1.4.8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,756 kB
  • ctags: 1,878
  • sloc: perl: 28,425; ansic: 8,323; makefile: 1,875; sh: 287
file content (345 lines) | stat: -rwxr-xr-x 11,463 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w
#
#####################################################################
#
# File: parsetest.pl
#
# Purpose: To test substr() vs. regex-based packet parser routine
#          in psad.
#
# Execution: perl -d:DProf ./parsetest.pl && dprofpp tmon.out
#
# Author: Michael Rash
#
# Sample output (it seems clear that the regex parser is faster):
#
#    $  perl -d:DProf ./parsetest.pl && dprofpp tmon.out
#    Sat Nov  1 09:16:53 2003 .. generating packet array.
#    Sat Nov  1 09:16:53 2003 .. parse1()
#    Sat Nov  1 09:16:56 2003 .. parse2()
#    icmp: 20002
#    Total Elapsed Time =  3.30997 Seconds
#      User+System Time =  3.30997 Seconds
#    Exclusive Times
#    %Time ExclSec CumulS #Calls sec/call Csec/c  Name
#     82.4   2.730  2.730      1   2.7300 2.7300  main::parse1
#     15.7   0.520  0.520      1   0.5200 0.5200  main::parse2
#     0.30   0.010  0.010      1   0.0100 0.0100  main::BEGIN
#     0.00   0.000 -0.000      1   0.0000      -  strict::import
#     0.00   0.000 -0.000      1   0.0000      -  strict::bits
#
#####################################################################
#
# $Id: parsetest.pl 907 2003-11-01 14:18:37Z mbr $
#

use strict;

my @arr;
my @err_pkts;
my $tcp_ctr;
my $udp_ctr;
my $icmp_ctr;
my $test_pkt = 'Oct 31 13:07:46 orthanc kernel: DROP IN=eth0 ' .
    'OUT= MAC=00:a0:cc:28:42:5a:00:01:5c:22:2e:42:08:00 ' .
    'SRC=208.205.78.126 DST=68.49.82.239 LEN=92 TOS=0x00 ' .
    'PREC=0x00 TTL=112 ID=39829 PROTO=ICMP TYPE=8 CODE=0 ' .
    'ID=59175 SEQ=21559';

print scalar localtime(), " .. generating packet array.\n";
for (my $i=0; $i<=10000; $i++) {
    push @arr, $test_pkt;
}

print scalar localtime(), " .. parse1()\n";
&parse1();
print scalar localtime(), " .. parse2()\n";
&parse2();

if ($tcp_ctr) {
    print "tcp: $tcp_ctr\n";
}
if ($udp_ctr) {
    print "udp: $udp_ctr\n";
}
if ($icmp_ctr) {
    print "icmp: $icmp_ctr\n";
}

exit 0;
#==================================================

sub parse1() {
    for my $pkt (@arr) {
        my $src = '';
        my $dst = '';
        my $len = -1;
        my $tos = '';
        my $ttl = -1;
        my $id  = -1;
        my $proto = '';
        my $sp    = -1;
        my $dp    = -1;
        my $win   = -1;
        my $type  = -1;
        my $code  = -1;
        my $seq   = -1;
        my $flags = '';
        my $sid   = 0;
        my $chain    = '';
        my $intf     = '';
        my $in_intf  = '';
        my $out_intf = '';
        my $dshield_str = '';
        my $index;

        my @pkt_fields = split /\s+/, $pkt;
        for my $arg (@pkt_fields) {
            $index = index($arg, 'SID');
            if ($index == 0) {
                $sid = substr($arg,
                    $index + length('SID'));
            }
            ### find all of the packet fields
            $index = index($arg, 'IN=');
            if ($index == 0) {
                $in_intf = substr($arg, $index+3);
                next;
            }
            $index = index($arg, 'OUT=');
            if ($index == 0) {
                $out_intf = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'SRC=');
            if ($index == 0) {
                $src = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'DST=');
            if ($index == 0) {
                $dst = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'LEN=');
            if ($index == 0) {
                $len = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'TOS=');
            if ($index == 0) {
                $tos = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'TTL=');
            if ($index == 0) {
                $ttl = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'ID=');
            if ($index == 0) {
                $id = substr($arg, $index+3);
                next;
            }
            $index = index($arg, 'PROTO=');
            if ($index == 0) {
                $proto = substr($arg, $index+6);
                next;
            }
            $index = index($arg, 'SPT=');
            if ($index == 0) {
                $sp = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'DPT=');
            if ($index == 0) {
                $dp = substr($arg, $index+4);
                next;
            }
            $index = index($arg, 'WINDOW=');
            if ($index == 0) {
                $win = substr($arg, $index+7);
                next;
            }
            $index = index($arg, 'TYPE=');
            if ($index == 0) {
                $type = substr($arg, $index+5);
                next;
            }
            $index = index($arg, 'CODE=');
            if ($index == 0) {
                $code = substr($arg, $index+5);
                next;
            }
            $index = index($arg, 'SEQ=');
            if ($index == 0) {
                $seq = substr($arg, $index+4);
                next;
            }
        }

        ### get the in/out interface and iptables chain
        if ($in_intf and not $out_intf) {
            $intf = $in_intf;
            $chain = 'input';
        } elsif ($in_intf and $out_intf) {
            $intf = $in_intf;
            $chain = 'forward';
        } elsif (not $in_intf and $out_intf) {
            $intf = $out_intf;
            $chain = 'output';
        }

        unless ($intf and $chain) {
            push @err_pkts, $pkt;
            next PKT;
        }

        ### May 18 22:21:26 orthanc kernel: DROP IN=eth2 OUT=
        ### MAC=00:60:1d:23:d0:01:00:60:1d:23:d3:0e:08:00 SRC=192.168.20.25
        ### DST=192.168.20.1 LEN=60 TOS=0x10 PREC=0x00 TTL=64 ID=47300 DF
        ### PROTO=TCP SPT=34111 DPT=6345 WINDOW=5840 RES=0x00 SYN URGP=0
        if ($proto eq 'TCP') {
            if ($pkt =~ /\sRES=\S+\s*(.*)\s+URGP=/) {
                    $flags = $1;
            }
            $proto = 'tcp';
            $flags = 'NULL' unless $flags;  ### default to NULL
            if (!$sid and ($flags =~ /ACK/ || $flags =~ /RST/)) {
                push @err_pkts, $pkt;
                next PKT;
            }
            unless ($flags !~ /WIN/ &&
                    $flags =~ /ACK/ ||
                    $flags =~ /SYN/ ||
                    $flags =~ /RST/ ||
                    $flags =~ /URG/ ||
                    $flags =~ /PSH/ ||
                    $flags =~ /FIN/ ||
                    $flags eq 'NULL') {
                push @err_pkts, $pkt;
                next PKT;
            }
            ### make sure we have a "reasonable" packet (note that nmap
            ### can scan port 0 and iptables can report this fact)
            unless ($src and $dst and $len >= 0 and $tos and $ttl >= 0
                    and $id >= 0 and $proto and $sp >= 0 and $dp >= 0
                    and $win >= 0 and $flags) {
                push @err_pkts, $pkt;
                next PKT;
            }
            $tcp_ctr++;
        ### May 18 22:21:26 orthanc kernel: DROP IN=eth2 OUT=
        ### MAC=00:60:1d:23:d0:01:00:60:1d:23:d3:0e:08:00
        ### SRC=192.168.20.25 DST=192.168.20.1 LEN=28 TOS=0x00 PREC=0x00
        ### TTL=40 ID=47523 PROTO=UDP SPT=57339 DPT=305 LEN=8
        } elsif ($proto eq 'UDP') {
            $proto = 'udp';
            ### make sure we have a "reasonable" packet (note that nmap
            ### can scan port 0 and iptables can report this fact)
            unless ($src and $dst and $len >= 0 and $tos and $ttl >= 0
                    and $id >= 0 and $proto and $sp >= 0 and $dp >= 0) {
                push @err_pkts, $pkt;
                next PKT;
            }
            $udp_ctr++;
        } elsif ($proto eq 'ICMP') {
            $proto = 'icmp';
            unless ($src and $dst and $len >= 0 and $ttl >= 0 and $proto
                    and $type >= 0 and $code >= 0 and $id >= 0
                    and $seq >= 0) {
                push @err_pkts, $pkt;
                next PKT;
            }
            $icmp_ctr++;
        } else {
            ### Sometimes the iptables log entry gets messed up due to
            ### buffering issues so we write it to the error log.
            push @err_pkts, $pkt;
            next PKT;
        }
    }
    return;
}

sub parse2() {
    for my $pkt (@arr) {
        my $src = '';
        my $dst = '';
        my $len = -1;
        my $tos = '';
        my $ttl = -1;
        my $id  = -1;
        my $proto = '';
        my $sp    = -1;
        my $dp    = -1;
        my $win   = -1;
        my $type  = -1;
        my $code  = -1;
        my $seq   = -1;
        my $flags = '';
        my $sid   = 0;
        my $chain    = '';
        my $intf     = '';
        my $in_intf  = '';
        my $out_intf = '';
        my $dshield_str = '';
        if ($pkt =~ /SRC=(\S+)\s+DST=(\S+)\s+LEN=(\d+)\s+TOS=(\S+)
                    \s*.*\s+TTL=(\d+)\s+ID=(\d+)\s*.*\s+PROTO=TCP\s+
                    SPT=(\d+)\s+DPT=(\d+)\s+WINDOW=(\d+)\s+
                    RES=\S+\s*(.*)\s+URGP=/x) {
            ($src, $dst, $len, $tos, $ttl, $id, $sp, $dp, $win, $flags) =
                ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);
            $proto = 'tcp';
            $flags = 'NULL' unless $flags;  ### default to NULL
            if (!$sid and ($flags =~ /ACK/ || $flags =~ /RST/)) {
                next PKT;
            }
            ### per page 595 of the Camel book, "if /blah1|blah2/"
            ### can be slower than "if /blah1/ || /blah2/
            unless ($flags !~ /WIN/ &&
                    $flags =~ /ACK/ ||
                    $flags =~ /SYN/ ||
                    $flags =~ /RST/ ||
                    $flags =~ /URG/ ||
                    $flags =~ /PSH/ ||
                    $flags =~ /FIN/ ||
                    $flags eq 'NULL') {
                push @err_pkts, $pkt;
                next PKT;
            }
            $tcp_ctr++;
        ### May 18 22:21:26 orthanc kernel: DROP IN=eth2 OUT=
        ### MAC=00:60:1d:23:d0:01:00:60:1d:23:d3:0e:08:00
        ### SRC=192.168.20.25 DST=192.168.20.1 LEN=28 TOS=0x00 PREC=0x00
        ### TTL=40 ID=47523 PROTO=UDP SPT=57339 DPT=305 LEN=8
        } elsif ($pkt =~ /SRC=(\S+)\s+DST=(\S+)\s+LEN=(\d+)\s+TOS=(\S+)
                          \s.*TTL=(\d+)\s+ID=(\d+)\s*.*\s+PROTO=UDP\s+
                          SPT=(\d+)\s+DPT=(\d+)/x) {
            ($src, $dst, $len, $tos, $ttl, $id, $sp, $dp) =
                ($1,$2,$3,$4,$5,$6,$7,$8);
            $proto = 'udp';
            $udp_ctr++;
        } elsif ($pkt =~ /SRC=(\S+)\s+DST=(\S+)\s+LEN=(\d+).*
                          TTL=(\d+).*PROTO=ICMP\s+TYPE=(\d+)\s+
                          CODE=(\d+)\s+ID=(\d+)\s+SEQ=(\d+)/x) {
            ($src, $dst, $len, $ttl, $type, $code, $id, $seq) =
                ($1,$2,$3,$4,$5,$6,$7,$8);
            $proto = 'icmp';
            $icmp_ctr++;
        } else {
            ### Sometimes the iptables log entry gets messed up due to
            ### buffering issues so we write it to the error log.
            push @err_pkts, $pkt;
            next PKT;
        }
        if ($pkt =~ /IN=(\S+)\s+OUT=\s/) {
            $intf = $1;
            $chain = 'input';
        } elsif ($pkt =~ /IN=(\S+)\s+OUT=\S/) {
            $intf = $1;
            $chain = 'forward';
        }
    }
    return;
}