4 # Copyright 1999, Codetalker Communications, Inc.
6 # This script takes a firewall log and breaks it into several
7 # different files. Each file is named based on the service that
8 # runs on the port that was recognized in log line. After
9 # this script has run, you should end up with several files.
10 # Of course you will have the original log file and then files
11 # such as web.log, telnet.log, pop3.log, imap.log, backorifice.log,
12 # netbus.log, and unknown.log.
14 # The number of entries in unknown.log should be minimal. The
15 # mappings of the port numbers and file names are stored in the bottom
16 # of this file in the data section. Simply look at the ports being hit,
17 # find out what these ports do, and add them to the data section.
19 # You may be wondering why I haven't simply parsed RFC1700 to come up
20 # with a list of port numbers and files. The reason is that I don't
21 # believe reading firewall logs should be all that automated. You
22 # should be familiar with what probes are hitting your system. By
23 # manually adding entries to the data section this ensures that I
24 # have at least educated myself about what this protocol is, what
25 # the potential exposure is, and why you might be seeing this traffic.
31 $TIDBITSFILE = "unknown.log";
33 # Read the ports data from the end of this file and build the three hashes
35 chomp; # trim the newline
36 s/#.*//; # no comments
37 s/^\s+//; # no leading white
38 s/\s+$//; # no trailing white
39 next unless length; # anything left?
40 $_ = lc; # switch to lowercase
41 ($proto, $identifier, $filename) = m/(\S+)\s+(\S+)\s+(\S+)/;
43 if ($proto =~ m/^icmp$/) { $icmp{$identifier} = $filename; last SWITCH
; };
44 if ($proto =~ m/^udp$/) { $udp{$identifier} = $filename; last SWITCH
; };
45 if ($proto =~ m/^tcp$/) { $tcp{$identifier} = $filename; last SWITCH
; };
46 die "An unknown protocol listed in the proto defs\n$_\n";
51 unless (defined($filename)) { die "Usage: logfilter.pl <log file>\n"; }
52 open(LOGFILE
, $filename) || die "Could not open the firewall log file.\n";
53 $openfiles{$filename} = "LOGFILE";
56 while($line = <LOGFILE
>) {
61 # determine the protocol - send to unknown.log if not found
64 ($line =~ m
/\sicmp\s/) && do {
69 # Extract the icmp packet information specifying the type.
71 # Note: Must check for ICMP first because this may be an ICMP reply
72 # to a TCP or UDP connection (eg Port Unreachable).
74 ($icmptype) = $line =~ m/icmp (\d+)\/\d
+/;
76 $filename = $TIDBITSFILE;
77 $filename = $icmp{$icmptype} if (defined($icmp{$icmptype}));
82 ($line =~ m
/\stcp\s/) && do {
87 # extract the source and destination ports and compare them to
88 # known ports in the tcp hash. For the first match, place this
89 # line in the file specified by the tcp hash. Ignore one of the
90 # port matches if both ports happen to be known services.
92 ($sport, $dport) = $line =~ m/\d+\.\d+\.\d+\.\d+,(\d+) -> \d+\.\d+\.\d+\.\d+,(\d+)/;
93 #print "$line\n" unless (defined($sport) && defined($dport));
95 $filename = $TIDBITSFILE;
96 $filename = $tcp{$sport} if (defined($tcp{$sport}));
97 $filename = $tcp{$dport} if (defined($tcp{$dport}));
102 ($line =~ m
/\sudp\s/) && do {
105 # UDP Protocol - same procedure as with TCP, different hash
108 ($sport, $dport) = $line =~ m/\d+\.\d+\.\d+\.\d+,(\d+) -> \d+\.\d+\.\d+\.\d+,(\d+)/;
110 $filename = $TIDBITSFILE;
111 $filename = $udp{$sport} if (defined($udp{$sport}));
112 $filename = $udp{$dport} if (defined($udp{$dport}));
118 # The default case is that the protocol was unknown
120 $filename = $TIDBITSFILE;
124 # write the line to the appropriate file as determined above
126 # check for filename in the openfiles hash. if it exists then write
127 # to the given handle. otherwise open a handle to the file and add
128 # it to the hash of open files.
130 if (defined($openfiles{$filename})) {
131 $handle = $openfiles{$filename};
133 $handle = "HANDLE" . keys %openfiles;
134 open ($handle, ">>".$filename) || die "Couldn't open|create the file $filename";
135 $openfiles{$filename} = $handle;
137 print $handle "#$linenum\t $line\n";
141 # close all open file handles
143 foreach $key (keys %openfiles) {
144 close($openfiles{$key});
150 icmp
3 destunreach
.log
172 tcp
635 nfs
.log # NFS mount services
173 udp
635 nfs
.log # NFS mount services
176 tcp
6112 games
.log # Battle net
178 tcp
7070 realaudio
.log
181 udp
31337 backorifice
.log