タグ「シェルスクリプト」が付けられているもの

Fail2banでalready banと出たときの対処

fail2ban+iptable(debian)の環境。
fail2banからiptableでDROP済(already banned)なのに、繰り返し攻撃してくる。
相手は変造パケットを投げてきているのであろうか。返事をしてしまっている。

1.png

そこで、下記の iptable コマンド;

iptables -A INPUT -s IPAddress -j DROP

をfail2banとは別に発行すると、しつこい攻撃を撃退できることが分かった。
正当な流儀としては、fail2banのaction.dとかを弄るべきであろう。
しかしながらとりあえず(対症療法で)、シェルスクリプト。

#/bin/sh

if [ -e /tmp/banlist.txt ]; then
# echo "/tmp/reban exist"
true
else
echo "create /tmp/banlist.txt"
touch /tmp/banlist.txt
exit 0
fi

REBAN=`tail /var/log/fail2ban.log|grep -m1 already|awk {'print $7'}`

if [ "${REBAN}" = "" ]; then
# echo "ipaddress is null"
exit 0
else
while read LINE
do
if [ "${LINE}" = "${REBAN}" ]; then
# echo "already banned"
exit 0
else
# echo ${REBAN} >> /tmp/banlist.txt
# `iptables -A INPUT -s ${REBAN} -j DROP`
# echo " ${REBAN} is reBANNED !"
true
fi
done < /tmp/banlist.txt
fi

`iptables -A INPUT -s ${REBAN} -j DROP`
echo ${REBAN} >> /tmp/banlist.txt
echo "${REBAN} is reBANNED !"

exit 0

crontabにて、5分に1回、回している。

2.png

無事撃退できた模様。

→後日談としてiptable-all-portで遮断すれば良さそうだ。

(2022/05/14 記)

----
追記

fail2banのactionに着目した。
デフォルトのTCPで拒否しているところ、UDPやICMPも含む、全プロトコルをDROPさせる、"all"の設定にしてみた。scriptが不要か悩む。

# cat /etc/fail2ban/action.d/iptables-common.local
[Init]
blocktype = DROP
protocol = all
#

しばらくscriptを併用して様子をうかがう。

(2022/05/17)

---
さらに追記

fail2banで"protocol=all"としただけでは撃退できない。
シェルスクリプトが効いて、遮断した様子。

fail2ban_sh.png

本スクリプトでiptableコマンドの送出が必要のようだ。
(iptableのchain INPUTで当該IPアドレスをDROP追加するルール。)

(2022/05/18)


さらに追記

jail.d/*.confの設定で、

banaction = iptables-allports

を追加した。シェルスクリプトを回す必要は無くなった。

sshd.conf

[sshd]
mode = aggressive
enabled = true
backend = %(sshd_backend)s
logpath = %(sshd_log)s
findtime = 600
maxretry = 3
bantime = 2d
banaction = iptables-allports

postfix.conf

[postfix]
mode = aggressive
enabled = true
backend = %(postfix_backend)s
logpath = %(postfix_log)s
findtime = 3600
maxretry = 3
bantime = 2d
banaction = iptables-allports

dovecot.conf

[dovecot]
mode = aggressive
enabled = true
backend = %(dovecot_backend)s
logpath = %(dovecot_log)s
findtime = 600
maxretry = 5
bantime = 2d
banaction = iptables-allports

今回新たに追加のrecidive.conf

[recidive]
enabled = true
bantime = 3mo
findtime = 9d
maxretry = 10
banaction = iptables-allports

すべてのポートで拒否とした。

crontabのエントリからスクリプトを除外した。

(2022/05/27)