-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammer.pl
60 lines (51 loc) · 1.55 KB
/
hammer.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Extract IP's from apache access logs for the last hour and matches with forum spam bot list.
# The fun work of Daniel Pearson
use strict;
use warnings;
use Socket;
# Declarations
my ($file,$list,@files,$match,$path,$sort,$line);
my $timestamp = localtime(time);
# Check to see if matching file exists
$list ='/root/support/listed_ip_7.txt';
if (-e $list) {
# Delete the file so we can download a new one if it exists
print "File Exists!";
print "Deleting File $list\n";
unlink($list);
}
sleep(5);
system ("wget -P /root/support http://www.danielpearson.com/listed_ip_7.txt");
my $dir = $ARGV[0] or die "Need to specify the log file directory\n";
$path='/var/log/messages';
open $path, "-|", "/usr/bin/tail", "-1000", "$path" or die "could not start tail on $path: $!";
while (my $line = <$path>) {
chomp $line;
if ($line =~
m/(?!0+\.0+\.0+\.0+$)(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))/g)
{
my $ip = $1;
$ips{$ip} = $ip;
}
}
}
open ("files","$list");
while (my $sort = <files>) {
chomp $sort;
foreach my $key (sort keys %ips) {
if ($key =~ $sort) {
my $match =qx(iptables -nL | grep $key 2>&1);
chomp $match;
if ($match =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
print "Already banned $key\n";
}
else {
system ("iptables -A INPUT -s $key -j DROP");
open my $fh, '>>', '/root/support/banned.out';
print "Match Found we need to block it $key\n";
print $fh "$key:$timestamp\n";
close $fh;
}
}
}
}