CentOS 7.4とfail2ban
いきなりだが、CentOS 7.4ではepelのfail2banは動作しない。
/var/log/fail2ban.logを見るとエラーが出ているはずだ。
出ていないならsystemctl restart fail2banすれば出てくる。
|
1 2 3 4 5 6 7 |
[27446]: ERROR ipset create fail2ban-example hash:ip timeout 1209600 firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp -m multiport --dports http,https -m set --match-set fail2ban-example src -j DROP -- stdout: '' 2018-01-24 16:05:50,337 fail2ban.action [27446]: ERROR ipset create fail2ban-example hash:ip timeout 1209600 firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp -m multiport --dports http,https -m set --match-set fail2ban-example src -j DROP -- stderr: '\x1b[91mError: COMMAND_FAILED\x1b[00m\n' 2018-01-24 16:05:50,338 fail2ban.action [27446]: ERROR ipset create fail2ban-example hash:ip timeout 1209600 firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp -m multiport --dports http,https -m set --match-set fail2ban-example src -j DROP -- returned 13 2018-01-24 16:05:50,339 fail2ban.actions [27446]: ERROR Failed to start jail 'example' action 'firewallcmd-ipset': Error starting action |
どうやらfirewallcmd-ipsetが今のfirewalldに合っていないらしく、actionbanをiptables-allportsなどに変更することで一応動くようになる。
|
1 |
banaction = iptables-allports |
しかし、この解決策は万全ではない。
firewalldもiptablesのコマンドが使えるが、設定はメモリ上に保存されるだけでfirewalldを再起動したりするとリセットされる。
systemctl restart firewalldするとfail2banが再起動するが、reloadだと再起動しない。
そんなことで動作しなくなるようではfail2banを導入する意味が無い。
やはり、firewalldとipsetの組み合わせで正しく動作する設定ファイルを作らないとダメらしい。
/etc/fail2ban/action.d/firewallcmd-ipset-new.confを新しく作成した。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[INCLUDES] before = [Definition] actionstart = firewall-cmd --permanent --new-ipset=fail2ban-<name> --type=hash:ip --option=timeout=<bantime> firewall-cmd --permanent --zone=<blocktype> --add-source=ipset:fail2ban-<name> firewall-cmd --reload actionstop = firewall-cmd --permanent --zone=<blocktype> --remove-source=ipset:fail2ban-<name> firewall-cmd --permanent --delete-ipset=fail2ban-<name> firewall-cmd --reload ipset flush fail2ban-<name> ipset destroy fail2ban-<name> actionban = ipset add fail2ban-<name> <ip> timeout <bantime> -exist actionunban = ipset del fail2ban-<name> <ip> -exist [Init] |
/etc/fail2ban/jail.localも変更した。
|
1 |
actionban = firewallcmd-ipset-new |
最後に設定ファイルの再読込した。
|
1 |
systemctl reload fail2ban |