-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: ban_abusive_ip.pl script (taken from off1) (#8474)
* build: ban_abusive_ip.pl script (taken from off1) * fix: linting
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/perl -w | ||
|
||
use strict; | ||
|
||
use Storable; | ||
|
||
use ProductOpener::PerlStandards; | ||
|
||
my $blocked_ips_file = "/srv/off/logs/blocked_ips.txt"; | ||
|
||
my %blocked_ips = (); | ||
|
||
my $in; | ||
|
||
if (open($in, "<", $blocked_ips_file)) { | ||
|
||
while (<$in>) { | ||
my $ip = $_; | ||
chomp($ip); | ||
print STDERR "loaded ip: $ip\n"; | ||
$blocked_ips{$ip} = 1; | ||
} | ||
} | ||
close($in); | ||
|
||
my %ip = (); | ||
|
||
while (<STDIN>) { | ||
if ($_ =~ /(^\S+) /) { | ||
my $ip = $1; | ||
next if $ip !~ /^[0-9\.]+$/; | ||
$ip{$ip}++; | ||
} | ||
} | ||
|
||
foreach my $ip (sort {$ip{$a} <=> $ip{$b}} keys %ip) { | ||
next if exists $blocked_ips{$ip}; | ||
if ($ip{$ip} > 100) { | ||
print STDERR "detected abusive $ip : $ip{$ip}\n"; | ||
my $command1 = "iptables -A INPUT -s $ip -p tcp -m state --state NEW -m tcp --dport 80 -j DROP"; | ||
my $command2 = "iptables -A INPUT -s $ip -p tcp -m state --state NEW -m tcp --dport 443 -j DROP"; | ||
print STDERR $command1 . "\n"; | ||
print STDERR $command2 . "\n"; | ||
system($command1); | ||
system($command2); | ||
my $out; | ||
open($out, ">>", $blocked_ips_file); | ||
print $out $ip . "\n"; | ||
close $out; | ||
} | ||
} | ||
|