Last active
February 20, 2020 18:13
-
-
Save allolex/52f7db6336e473283d5b to your computer and use it in GitHub Desktop.
Script to scour Apache logs for IP's performing brute-force password attacks on WordPress sites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my %config = ( | |
'limit' => 20000, | |
'name' => '/var/log/apache2/blog.access.log' | |
); | |
my $lines = get_lines(\%config); | |
my $ip = count_lines($lines); | |
print_attackers($ip, 0); | |
sub print_attackers { | |
my ($ip_address, $max_logins) = @_; | |
foreach my $key (sort { $ip_address->{$b} <=> $ip_address->{$a} } keys %$ip_address) { | |
if ($ip_address->{$key} > $max_logins) { | |
print "$key : $ip_address->{$key} attempts\n"; | |
} | |
} | |
} | |
sub get_lines { | |
my $args = shift; | |
my $pattern = 'POST /wp-login.php HTTP/1.0'; | |
my $tail = 'tail -' . $args->{'limit'} . q( ") . $args->{'name'} . q("); | |
my $grep = qq(grep "$pattern"); | |
my $cut = qq(cut -d' ' -f1); | |
my $command = "$tail | $grep | $cut"; | |
my @lines = `$command`; | |
return \@lines; | |
} | |
sub count_lines { | |
my $lines = shift; | |
my %addresses; | |
foreach (@$lines) { | |
chomp; | |
s/\s+$//; | |
$addresses{$_}++; | |
} | |
return \%addresses; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment