iptables - Automatically ban IPs that request certain pages - Ask Ubuntu
i want ban ips request pages e. g. example.com/weird.php
, 1 send post
request example.com/weirder.php
.
i have searched lot, , learned several iptables
, seems instructions centos. worry being locked out. i'm looking detailed , safe answer.
i use aws ec2, ubuntu , nginx.
first try on machine have physical access to.
ip tables
first, let's figure out ip tables rules block ip address. detailed instructions, check out guide.let's drop incoming connections particular ip:
iptables -a input -s a.b.c.d -j drop
after this, restart iptables regular. try out command check if works fine you, familiar syntax , proceed further.
bash script
let's create bash script block ip. (warning: might need play user permissions , sudo work). create new file, say, blockip.sh.
#! /usr/bin/env bash iptables -a input -s $1 -d drop <other stuff want when block ip> <note: ip address in variable $1>
run chmod +x blockip.sh
make script executable. can run /path/to/file/blockip.sh 1.2.3.4
.
sudo permissions
if script needs have root user permissions, need configure sudo provide passwordless access. referring this stack overflow question, run sudo visudo
, add following line:
nobody = nopasswd: /your/script
save file , exit. security warning, make sure can modify script file or else, can potentially run harmful code root user modifying script.
php code
run little gem of code whenever want block someone.
exec("sudo /path/to/file/blockip.sh ".$_server['remote_addr']);
conclusion
first, test code on machine have physical access to. also, aware multiple users behind lan share same external ip , blocking 1 of them result in blocking of them. careful. upvote if answer helped you.
Comments
Post a Comment