Assuming there is a php website and I want to block an ip at the firewall level based on the site code execution. The site is run under non-root user.
I was going to pass IP from the site code to a script (writeable to root only) like
#!/bin/bash
function validate_ip()
{ ... code here ...}
if validate_ip $1; then
/usr/sbin/iptables -I INPUT -s $1 -j DROP
echo 'blocked';
else
echo 'bad IP $1';
fi
using suid bit. I want to add additional IP validation to avoid XSS and other bad things (consider it paranoia if you like), so do not want to allow the site to call iptables directly.
The script does not work can't initialize iptables table 'filter': Permission denied (you must be root)
because bash drops suid bit
There is workaround: allow iptables in sudo but I don't think it's secure. I have no time/possibility to develop/buy a binary which will do the task. One suggested binary wrapper around script but I hesitate, perhaps there is a better way?
So, the question is: how can I allow non-root app to block ip in iptables firewall in a secure way?
username ALL=(root) NOPASSWD: /usr/local....
, then switched to user's shell and triedsudo /usr/local/script 1.1.1.1
but sudo asked for password here. What am I doing wrong? – Putnik Sep 22 '17 at 13:12