Albeit most of modern firewalls have its own embedded functions for blocking bots, but sometimes it isn't enough.
So here is my basic script for analyzing apache logs and block(or something else) suspicious addresses.
Feedback is appreciated.
#!/usr/bin/perl -w use strict; use warnings; use POSIX qw(strftime); my $pattern = "\"GET \/ HTTP\/"; # request index page pattern my $httpd_log = "/var/log/httpd-access.log"; # log file my $ok = "1000"; # allowed connections per ip for $check_period my $check_period = 1; # check period in hours my $date = strftime("%d/%b/%Y:%H", localtime(time-$check_period*3600)) +; # date minus $check_period hours my (%ips, $ip, $start); open (LOG, $httpd_log) or die $!; while (<LOG>) { next unless m/$date/ || $start; # skipping old records $start=1; if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*$pattern/go) { # g +etting ips $ips{$1}++; } } close LOG; foreach $ip (keys %ips) { if ($ips{$ip} >= $ok) { # print "$ip = $ips{$ip}\n"; next; # comment out this li +ne if you want to modify firewall rules and uncomment one of the foll +owing #system("/sbin/pfctl -t bots -T add $ip"); # adding ad +dress to table <bots> #system("/sbin/ipfw table 5 add $ip"); # adding addres +s to table 5 #system("/sbin/iptables -A INPUT -s $ip -j REJECT"); # + adding denying rule } }

In reply to Getting bots ips from apache logs. by idle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.