fail2ban continua tentando banir novamente

fail2ban continua tentando banir novamente

Tenho um problema para fazer com que o fail2ban faça o que quero. Eu utilizo uma ação que proíbe o IP no CLoudFlare, isso significa que ainda muitas solicitações passam até que tudo seja atualizado no Cloudflare (e portanto o usuário não consegue mais acessar o site).

Até que isso acontecesse, o fail2ban tenta banir o IP repetidamente, fazendo com que ele envie milhares (no caso de um ddos) de solicitações para o cloudflare, o que leva muito tempo e, portanto, não banirá nenhum outro invasor até que seja feito. os anteriores (já banidos).

Então, existe uma maneira de impedir que o fail2ban tente bani-los novamente e simplesmente ignorá-los?

#!/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

Responder1

Considere criar uma lista de IPs banidos e adicionar os IPs a ela ao emitir a solicitação de banimento. No script de ação, ignore as solicitações de banimento se o IP estiver na lista. Você também precisará alterar a ação de cancelar o banimento para remover o IP da lista de IP banidos. Crie um script como o seguinte:

#!/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

Suas ações seriam:

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

informação relacionada