LogFormat "%a" problem CustomLog "|/path/to/this/script.pl" problem #### #!/usr/bin/perl -w $|=1; use strict; use Fcntl qw(:DEFAULT); use Net::Netmask; use Net::Whois::IANA; use Proc::Fork; use SDBM_File; my $iana = Net::Whois::IANA->new; my $bye = join '|', ( 'strings known to occur', 'in custname and orgname', ); tie (my %seen, 'SDBM_File', '/some/sdbm', O_RDWR|O_CREAT, 0666) or die "$!"; open my $fh, "< /dev/stdin" or die "$!"; while (<$fh>) { chomp; next if exists $seen{$_}; $seen{$_} = 1; my $ip = $_; child { my $problem = 0; my $host = `host $ip`; $problem = 1 if $host =~ /some host name regex/; if ($problem or $host =~ /not found/) { $iana->whois_query(-ip=>$ip); my $inetnum = $iana->inetnum || ''; my $custname = $iana->{QUERY}->{custname} || ''; my $orgname = $iana->{QUERY}->{orgname} || ''; next unless $inetnum and ($custname or $orgname); if ($custname =~ /$bye/i or $orgname =~ /$bye/i){ my $mask = Net::Netmask->new($inetnum); `iptables -A INPUT -s $mask -j DROP`; } } } } close $fh or die "$!"; untie %seen;