I'm trying to get mod_evasive to fire off a script to add an iptables rule to deny the offending host. I've tried the suggestions from both answers here but I still can't get it working. Apart from the post that's linked, I'm trying to run a script as outlined in this article.
My Apache config has this
DOSSystemCommand "sudo -u root /root/scripts/ban_ip.sh %s"
The script has this
#!/bin/sh
IP=$1
IPTABLES=/sbin/iptables
$IPTABLES -A banned -s $IP -p TCP -j DROP
echo "$IPTABLES -D banned -s $IP -p TCP -j DROP" | at now + 5 minutes
I've created a 'banned' chain (I've also just tried to add it to the INPUT chain to no avail)
My /etc/sudoers looks like this:
apache ALL=(root) NOPASSWD: /root/scripts/ban_ip.sh *
I've disabled SELinux to make sure it's not getting in the way. I can su apache --shell=/bin/bash
and run sudo /root/scripts/ban_ip.sh 10.10.10.10
and it works just fine.
But when a source gets flagged as malicious in mod_evasion, it denies the host with 403s but it never runs the script, so I'm not really gaining any advantage here.
What else can I try to get this working?
My system works! :)
Requeriments:
NOTE: You can use other mail agent and modify the script.
Now my configs:
mod_evasive (/etc/apache2/mods-enabled/mod-evasive.conf)
sudoers
ddos_system.sh (copy to /usr/local/bin)
MINI FAQ
Q: What about the last line? (rm -f ...)
A: When mod_evasive detect some attacks It create file (lock file) in "DOSLogDir" with the name "dos-[sourceip]" (ex. dos-8.8.8.8) and execute the "DOSSystemCommand" once until that file disappear. So when you execute "iptables" you should remove the lock file for the next check.
Tested in Debian 7.
Good luck, regards.
I tried the approach from Beast response (thanks!!) and had to change this fragment to make it work:
Basically I had to add sudo to the commands /sbin/iptables and at inside the script:
It took me a while to notice this so I hope posting here can help others trying this solution.
I managed to use the above answer by Beast to get it to work, but some tweaks were needed.
The sudoers file (/etc/sudoers) should have this line added, otherwise the script was not running:
These commands are needed as well to give proper permissions and safeguard the script.
and I did not have to modify the script to add any sudo command.
I used
And also updated the script to use this path.
I was working on a project recently when I came upon this question about mod_evasive. While the most voted answer was the one I needed which partially solved my problem, I wouldn't recommend it to anybody as it has a serious security loophole. While it is not recommended to allow www-data user to run sudo, no user should be allowed to run the
at
command with sudo privileges with NOPASSWD! Theat
command can be used to escalate privileges to root with just a simple command if it is allowed to run with sudo without password. For e.g, take a look at this image:- privilege escalation with at commandYes, its as simple and dangerous as that! So, how to solve the problem without using the
at
command? This is how I did it./etc/sudoers
www-data ALL=NOPASSWD: /sbin/iptables *
anti-ddos.sh
Here, I used the
sleep
command to delete theiptables
IP block rule after 60 seconds which has the same effect as runningat now + 1 minute
. For sake of brevity I'm not giving all my configs. You can refer other answers as they have made a pretty good explanation of it. Stay secure, stay safe.Note:- I don't have enough privileges to post images. Also, I don't like posting online like this but I couldn't withstand the serious security issue here and so, I created an account. :)