dru145 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w ################################################################### # # fwanalysis - Program that uses fwlogsum's daily summary report to # parse the exported log file for the top 15 dropped ip's # and generate our standard Flagged Activity email for each # ip with a sample of 15 entries from the fw log file # as "evidence". An email will be sent to the sysadmin # of each network and to abuse@theirdomain.com. The email # address is obtained via a whois query. # IS Security will also receive a cc: of each email sent # out. # ################################################################## use strict; # Assign Variables my $today=`date +%d%b%y`; chomp($today); my $reportfile = "/exported/analysis/$today.logsum"; my $topten = 0; my $logfile = "/exported/$today.elog"; my @ips; my $ip; my $from_addr="IS Security<IS.Security\@mydomain.com>"; my $root="Root<root\@mydomain.com>"; my $arin="/usr/bin/whois -h whois.arin.net"; my $ripe="/usr/bin/whois -h whois.ripe.net"; my $apnic="/usr/bin/whois -h whois.apnic.net"; my $krnic="/usr/bin/whois -h whois.krnic.net"; my $email; my $domain; my $result; # Get the ten ip addresses from the daily fwlogsum report and # store them in an anonymous array @ips open REPORT, "$reportfile" or die "Can't open FWLOGSUM File: $!\n"; while (<REPORT>){ chomp; $topten = 1 if m!^Users/Source Addresses!; next unless $topten; push @ips, [split /\s+/] if /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; } close REPORT; # Use the evidence and whois subroutines for the first ip my $ip1 = $ips[0][0]; my $times = $ips[0][1]; whois($ip1); evidence($ip1,$times); # Second ip my $ip2 = $ips[1][0]; my $times2 = $ips[1][1]; whois($ip2); evidence($ip2,$times2); # Third ip my $ip3 = $ips[2][0]; my $times3 = $ips[2][1]; whois($ip3); evidence($ip3,$times3); # Fourth ip my $ip4 = $ips[3][0]; my $times4 = $ips[3][1]; whois($ip4); evidence($ip4,$times4); # Fifth ip my $ip5 = $ips[4][0]; my $times5 = $ips[4][1]; whois($ip5); evidence($ip5,$times5); # Repeats for 10 more ip's ############################################ # SUBROUTINES # ############################################ ##################################################### # evidence: Open up the exported log file, search for # the ip, add to array, split array into 15 # lines,replace firewall ip addresses, send # email ##################################################### sub evidence { my $ip = $_[0]; my $times = $_[1]; my @fwlog; open LOG, "$logfile" or die "Can't open $logfile: $!\n"; while (<LOG>){ if ($_ =~ $ip){ push (@fwlog, $_); } #end if } #end while close LOG; splice(@fwlog, 15); foreach (@fwlog){ s/192\.168\.14\.2/x.x.x.x/; s/192\.168\.2\.2/x.x.x.x/; s/192\.37\.222\.46/x.x.x.x/; s/192\.215\.150\.2/x.x.x.x/; } #end foreach open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Can't fork for sendmail: $!\n"; print SENDMAIL <<EOM; From: $from_addr To: $email$domain, abuse$domain CC: $from_addr Subject: Flagged Activity from $ip System Administrator, Cease or decease email. Here are the details: Source IP Address: $ip Number of times: $times Sample from the firewall logs (*note date/time are Eastern Daylight Ti +me -4 GMT): Explanation of Log Fields: (Line Number ; Date; Time; N/A; Type<log,al +ert>; Action; N/A; Direction; Protocol; Source IP; Destination IP; De +sti nation Port; Source Port; N/A; N/A) @fwlog Thank You, Security Team My Company EOM close(SENDMAIL) or warn "sendmail didn't close nicely"; } #close evidence sub #################################################### # whois: Obtain an email address from a whois query #################################################### sub whois{ my $ip = $_[0]; $result = `$arin $ip`; # If the whois matches a ripe address, then perform whois against # RIPE database and match email address. if ($result =~ m/European Regional Internet Registry/){ $result = `$ripe $ip`; if (match()){ } #close if match } #close if result # If the whois matches a apnic address, then perform whois against # apnic database and match single email address. If the apnic whois # matches a krnic address, then perform whois against the krnic databa +se. elsif ($result =~ m/Asia Pacific Network Information Center/){ $result = `$apnic $ip`; if ($result =~ m/Allocated to KRNIC Member/){ $result = `$krnic $ip`; } #end KRNIC match if (match()){ } #end if match } #end elsif # If the whois returns 2 handles, then perform whois against 2nd handl +e and # match email address. elsif (handles($arin)){ if (match()) { } #end if match } #end elsif # If whois does not return a emailbox, send email to IS Security notif +ying them elseif ($result =~ m/No mailbox/) { open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Can't fork for sendmail: $!\n"; print SENDMAIL <<EOM; From: $root To: $from_addr Subject: Can't perform a whois query for the ip $ip EOM close(SENDMAIL) or warn "sendmail didn't close nicely"; exit } #end else } #close whois sub # If the whois returns just a normal arin response, then match the ema +il. elsif (($email, $domain) = $result =~ m/([-.\w]+)(\@[-.\w]+)/) { print "The 1st email address is: $email$domain\n"; print "Email will also be sent to: abuse$domain\n"; } #end elsif ################################################### # match: Obtain an email address from a whois query ################################################### sub match { ($email, $domain) = $result =~ m/([-.\w]+)(\@[-.\w]+)/; } #end sub match ################################################# # handles: Obtain the handles from a whois query ################################################# sub handles { my $registry = $_[0]; $result =~ m/xxx/; my @handle = $result =~ m/\((.*?)\)/g; $result = `$registry $handle[1]`; } #end handles
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Constructive Criticism for My First Perl Program
by wog (Curate) on Sep 13, 2001 at 02:28 UTC | |
by dru145 (Friar) on Sep 13, 2001 at 19:15 UTC |