fail2ban sigue intentando prohibir nuevamente

fail2ban sigue intentando prohibir nuevamente

Tengo problemas para que fail2ban haga lo que quiero. Utilizo una acción que prohíbe la IP en CLoudFlare, lo que significa que todavía llegan muchas solicitudes hasta que todo se haya actualizado por parte de Cloudflare (y por lo tanto el usuario ya no puede acceder al sitio).

Hasta que eso sucedió, fail2ban intenta prohibir la IP una y otra vez, lo que hace que envíe miles (en caso de un ddos) de solicitudes a Cloudflare, lo que lleva mucho tiempo y, por lo tanto, no prohibirá a ningún otro atacante hasta que termine. los anteriores (ya prohibidos).

Entonces, ¿hay alguna manera de evitar que fail2ban intente prohibirlos nuevamente y simplemente ignorarlos?

#!/bin/bash

# Make sure that the ip-blacklist file exists
# otherwise we go ahead and create it
if [ ! -e "/etc/fail2ban/ip.blacklist" ] ; then
    touch "/etc/fail2ban/ip.blacklist"
fi

if [[ $1 = ban ]]; then
    if ! grep -q "$2" /etc/fail2ban/ip.blacklist; then
        # Store the IP as we need it later to check if the user is already banned
        echo "$2" >> /etc/fail2ban/ip.blacklist

        # Submit the ban request
    fi 
elif [[ $1 = unban ]]; then
    # Remove the IP from the blacklist 
    perl -ni -e "print unless (/^$2$/);" /etc/fail2ban/ip.blacklist
    #sed -i '/^$2$/d' /etc/fail2ban/ip.blacklist

    # Submit the unban request
fi

Respuesta1

Considere crear una lista de IP prohibidas y agregarle las IP al emitir la solicitud de prohibición. En el script de acción, ignore las solicitudes de prohibición si la IP está en la lista. También deberá cambiar la acción de desbanificación para eliminar la IP de la lista de IP prohibidas. Cree un script como el siguiente:

#!/bin/bash

# Define ourbanfile
banFile=ip.blacklist

# Ensure we have a banFile will be created if missing
if [ ! -e ${banFile} ]; then
    touch ${banFile}
fi

# Ban or unban as desired
if [[ $1 = ban ]]; then
    if ! grep -q "$2" ${banFile}; then
        # Store the IP as we need it later to check if the user is already banned
        echo "$2" >> ${banFile}

        # Submit the ban request
    fi
elif [[ $1 = unban ]]; then
    # Remove the IP from the blacklist
    perl -ni -e "print unless (/^$2\$/);" ${banFile}
    #sed -i '/^$2$/d' ${banFile}

    # Submit the unban request
fi

# Cat banfile if running on terminal (testing)
tty -s  && cat ${banFile}

# EOF

Tus acciones serían:

actionban = /path/to/script ban <IP>
actionunban = /path/to/script unban <IP>

información relacionada