Tuesday, June 11, 2013

PHP-SHELL script to whitelist IP on CSF

I needed a fast way to whitelist my IP on a linux server to be able to login to secure administration page...

I set it up by using a php file + shell file + cronjob

Now I easily open a public website address "mydomain.com/whatis.php" and it automatically whitelist my IP.

"whatis.php" PHP file code :
echo file_put_contents("/tmp/iplog",$_SERVER["REMOTE_ADDR"]);

"/script/ip" Shell file code :
while [ $i -le 10 ]
        status=`cat /tmp/iplog`
        if [ "$status" != "0" ]; then
                echo $status >> /etc/csf/csf.allow
                echo $status >> /etc/csf/csf.ignore
#                echo "allow from $status" >> /home/DOMAIN/public_html/admin/.htaccess
                echo "0" > /tmp/iplog
                `/etc/csf/csf.pl -r > /tmp/csf.log`
        sleep 5
        (( i++ ))

"/script/ip" Cronjob :
* * * * * /script/ip

Note : I tried to run `csf -r` to reset CSF firewall in shell script but it didn't work ! I don't know why. I found here that we should use `/etc/csf/csf.pl -r` instead. Thanks to that guy !!

No comments:

Post a Comment