fail2ban は再度禁止を試み続ける

fail2ban は再度禁止を試み続ける

fail2ban で必要な処理が実行できないという問題があります。CLoudFlare で IP を禁止するアクションを使用しているため、Cloudflare 側ですべてが更新されるまで、依然として多くのリクエストが通過します (したがって、ユーザーはサイトにアクセスできなくなります)。

それが起こるまで、fail2ban は IP を何度も禁止しようとし、何千もの (DDoS の場合) リクエストを cloudflare に送信します。これには非常に長い時間がかかるため、以前の (すでに禁止されている) 攻撃者が終了しないと、他の攻撃者は禁止されません。

では、fail2ban が再度禁止しようとするのを止めて、無視する方法はあるのでしょうか?

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

答え1

禁止 IP リストを作成し、禁止リクエストを発行するときに IP を追加することを検討してください。アクション スクリプトでは、IP がリストに含まれている場合は禁止リクエストを無視します。また、禁止 IP リストから IP を削除するには、禁止解除アクションを変更する必要もあります。次のようなスクリプトを作成します。

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

あなたの行動は次のようになります:

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

関連情報