まずは、インストールします。
# yum install --enablerepo=epel -y fail2ban fail2ban-systemd
次に、/etc/fail2ban/jail.localを作成します。このファイルはjail.confをオーバーライドするものです。
jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 172.16.1.0/24
maxretry = 5
findtime = 3600
bantime = 86400
backend = systemd
usedns = yes
[postfix-sasl]
enabled = true
findtime = 86400
bantime = -1
port = smtp,465,submission
logpath = %(postfix_log)s
bantimeにマイナスを設定すると永久banになるようです。
早速、起動と行きたいところですが、このままでは上手く動きません。
ipsetのtimeoutにbantimeを入れる動作をするためにエラーになってしまいます。0が永久ですが、bantimeに0入れるとfail2banがban & unbanを繰り返します。
また、ログが更新されると再起動した時にログ落ちしたIPはbanされませんので、追加されるたびに別のファイル(下記では、permanent_bans.ip)に残しておき、起動時にそのファイルから再度、読み込みします。
これを変更するには/etc/fail2ban/action.d/firewallcmd-ipset.confのactionstartとactionbanを編集します。
firewallcmd-ipset.conf(抜粋)
#actionstart = ipset create fail2ban-<name> hash:ip timeout <bantime> # firewall-cmd --direct --add-rule ipv4 filter <chain> 0 -p <protocol> -m multiport --dports <port> -m set --match-set fail2ban-<name> src -j <blocktype> actionstart = ipset create fail2ban-<name> hash:ip timeout 0 firewall-cmd --direct --add-rule ipv4 filter <chain> 0 -p <protocol> -m multiport --dports <port> -m set --match-set fail2ban-<name> src -j <blocktype> cat /etc/fail2ban/permanent_bans.ip | awk '/^fail2ban-<name> / {print $2}' | while read IP; do ipset add fail2ban-<name> $IP timeout 0 -exist; done actionstop = firewall-cmd --direct --remove-rule ipv4 filter <chain> 0 -p <protocol> -m multiport --dports <port> -m set --match-set fail2ban-<name> src -j <blocktype> ipset flush fail2ban-<name> ipset destroy fail2ban-<name> #actionban = ipset add fail2ban-<name> <ip> timeout <bantime> -exist actionban = ipset add fail2ban-<name> <ip> timeout 0 -exist echo "fail2ban-<name> <ip>" >> /etc/fail2ban/permanent_bans.ip
また、blocktypeは相手に応答しないdropにします。
iptables-common.conf(抜粋)
#blocktype = REJECT --reject-with icmp-port-unreachable blocktype = DROP
後は起動するだけです。
# systemctl start fail2ban # systemctl enable fail2ban
banのリストを見るには、
# ipset list
# fail2ban-client set postfix-sasl unbunip <ip_addr>
0 件のコメント:
コメントを投稿