MrCBofBCinTX
Technical User
After seeing more and more bandwidth on my server getting eaten up by more and more spam, etc.
I cobbled together a shell script with tidbits from the apache error log for a few weeks.
It got too long for my tastes so I did this perl script to do the work.
Works fine, but seems to me that it may have too many variables for such a simple script.
I'm not a real wiz with complex data structures, so I may not be seeing an obvious way to improve on this.
I cobbled together a shell script with tidbits from the apache error log for a few weeks.
It got too long for my tastes so I did this perl script to do the work.
Works fine, but seems to me that it may have too many variables for such a simple script.
I'm not a real wiz with complex data structures, so I may not be seeing an obvious way to improve on this.
Code:
#!/bin/perl
my @list;
my @log;
my @fields;
my @logged;
my @crap;
my $doodoo;
my @matches = ('soapCaller', 'phpmyadmin', 'source\/mod\/', 'a\.asp', 'xmlrpc', 'viewitem', 'manager', ' ___', 'noexist', 'thisdoesnotexistahaha.php', '/mwf\
/index.php', 'cacti', 'admin.*js');
my $runs = $ARGV[0] ||= 1; #Allows debug and one email per day from cronjob
do {
open LOG, "<", "/var/[URL unfurl="true"]www/logs/error_log";[/URL]
@fields = <LOG>;
close (LOG);
foreach my $match (@matches) {
@logged = grep(/$match/, @fields);
push (@log, @logged);
}
foreach my $crap (@log) {
my @duh = split (/ /, $crap);
my $duh = $duh[7];
$duh =~ s/]//; #Removes ] from IP entry in log
push (@list, $duh);
}
my @uniq = keys %{{ map { $_ => 1 } @list }};
foreach my $poop (@uniq) {
$doodoo .= "$poop ";
}
print "$doodoo\n";
system "pfctl -t badhosts -T add $doodoo";
undef $doodoo;
sleep 10; # Using perl to run more often than 1 minute from cron
} while ($runs > 0);