jueves, 10 de septiembre de 2015

iptables: Banear ips automaticamente con lista

Esta es una solución sencilla para tener una lista de ips baneadas que podemos administrar fácilmente.
Hay soluciones más complejas y avanzadas como esta. Yo he optado por la más simple ya que de momento el volumen de ataques no es demasiado alto.
Esta solución la he sacado de este enlace el script esta explicado muy bien. Tiene algún fallito. falta declarar la variable TEMP y falta poner el parametro "-f" en el comando cp de la función unban. Este es el script tal y como yo lo estoy utilizando.

Creamos la carpeta /etc/myOwnFirewall y el script myOwnFirewall dentro de ella

mkdir /etc/myOwnFirewall
vi /etc/myOwnFirewall/myOwnFirewall


El contenido del script sería el siguiente

#!/bin/bash

# myOwnFirewall v0.1
#
# Script para banear IPs
#
# Author: Rolando Caldas
# http://rolandocaldas.com
#
# https://github.com/rolando-caldas/myOwnFirewall

BANNED_IPS="/etc/myOwnFirewall/banList.txt"
LOCK="/etc/myOwnFirewall/.lock"
TEMP="/etc/myOwnFirewall/temp"

function banIP {
        if [ -f ${LOCK} ]
        then
                if [ $# -eq 0 ]
                then
                        echo "You must enter an ip address"
                else
                        ip=$1
                        exists=false
                        for i in `cat $BANNED_IPS`; do
                                if [ $i = $ip ]
                                then
                                        exists=true
                                fi
                        done

                        if [ $exists = false ]
                        then
                                echo $ip >> $BANNED_IPS
                                iptables -I INPUT -s $ip -j DROP
                                iptables -I OUTPUT -s $ip -j DROP
                                echo "IP ${ip} banned"
                        else
                                echo "IP ${ip} already in the list"
                        fi
                fi
        else
                echo "myOwnFirewall is not running"
        fi
}

function restartScript {
        stopScript
        startScript
}

function showHelp {
        echo "Usage: myOwnFirewall {start|stop|restart}"
}

function startScript {

        if [ -f ${LOCK} ]
        then
                echo "myOwnFirewall is already running"
                exit
        else
                ` > ${LOCK}`

                for i in `cat $BANNED_IPS`; do
                        iptables -I INPUT -s $i -j DROP
                        iptables -I OUTPUT -s $i -j DROP
                done

                echo "myOwnFirewall started"
        fi

}

function stopScript {
        if [ -f ${LOCK} ]
        then
                for i in `cat $BANNED_IPS`; do
                        iptables -D INPUT -s $i -j DROP
                        iptables -D OUTPUT -s $i -j DROP
                done
                rm ${LOCK}

                echo "myOwnFirewall stopped"
        else
                echo "myOwnFirewall is not running"
        fi
}

function unbanIP {
        if [ -f ${LOCK} ]
        then
                if [ $# -eq 0 ]
                then
                        echo "You must enter an ip address*"
                else
                        ip=$1
                        exists=false

                        for i in `cat $BANNED_IPS`; do
                                if [ $i = $ip ]
                                then
                                        exists=true
                                else
                                        echo $i >> $TEMP
                                fi
                        done

                        if [ $exists = false ]
                        then
                                echo "IP ${ip} doesn't in the list"
                        else
                                cp -f $TEMP $BANNED_IPS && rm $TEMP
                                iptables -D INPUT -s $ip -j DROP
                                iptables -D OUTPUT -s $ip -j DROP
                                echo "IP ${ip} unbanned"
                        fi
                fi
        else
                echo "myOwnFirewall is not running"
        fi
}

case "$1" in
        ban)
                banIP $2
                exit
                ;;
        restart)
                restartScript
                exit
                ;;
        start)
                startScript
                exit
                ;;
        stop)
                stopScript
                exit
                ;;
        unban)
                unbanIP $2
                exit
                ;;
        *)
                showHelp
                exit
                ;;
esac


En centos y redhat es algo distinto a debian el funcionamiento de iptables los cambios en las reglas no se guardan a no ser que lo forcemos con "service iptables save".
Yo no me he complicado mucho y ejecuto el script con el parámetro start en el inicio del sistema en el rc.local y borro el fichero .lock en el shutdown. He aprovechado el script de iptables en el init.d y he añadido la línea rm -f /etc/myOwnFirewall/.lock en la función stop(). De este modo cuando se detiene el servicio iptables borra el fichero .lock que le sirve al script para saber si el baneado de ips está activo.
El funcionamiento del script está muy bien explicado en este enlace

No hay comentarios:

Publicar un comentario