fail2ban cannot set iptables rules

kinezana asked:

I have only a thin knowledge of both iptables and fail2ban, but my mail server has a lot of incoming connections that I want to get rid of.

I have this config block in /etc/fail2ban/jail.conf

[postfix-sasl]
enabled  = true
port     = smtp,465,submission,imap3,imaps,pop3,pop3s
logpath  = %(postfix_log)s
backend  = %(postfix_backend)s

But I think the ban, although correctly detected, isn’t effective at all, as I see the fail2ban logs reporting an iptables error, here is a piece of log following fail2ban startup and trying to restore a previously found ban :

2018-01-22 14:53:23,365 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- stdout: b''
2018-01-22 14:53:23,365 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- stderr: b"iptables v1.6.0: invalid port/service `smtp,465,submission,imap3,imaps,pop3,pop3s' specified\nTry `iptables -h' or 'iptables --help' for more information.\n"
2018-01-22 14:53:23,366 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- returned 2
2018-01-22 14:53:23,366 fail2ban.actions        [26127]: ERROR   Failed to start jail 'postfix-sasl' action 'iptables': Error starting action
2018-01-22 14:53:23,366 fail2ban.actions        [26127]: NOTICE  [postfix-sasl] Ban 80.82.70.210
2018-01-22 14:53:23,470 fail2ban.action         [26127]: ERROR   iptables -w -n -L INPUT | grep -q 'f2b-postfix-sasl[ \t]' -- stdout: b''
2018-01-22 14:53:23,470 fail2ban.action         [26127]: ERROR   iptables -w -n -L INPUT | grep -q 'f2b-postfix-sasl[ \t]' -- stderr: b''
2018-01-22 14:53:23,471 fail2ban.action         [26127]: ERROR   iptables -w -n -L INPUT | grep -q 'f2b-postfix-sasl[ \t]' -- returned 1
2018-01-22 14:53:23,471 fail2ban.CommandAction  [26127]: ERROR   Invariant check failed. Trying to restore a sane environment
2018-01-22 14:53:23,679 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- stdout: b''
2018-01-22 14:53:23,680 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- stderr: b"iptables v1.6.0: invalid port/service `smtp,465,submission,imap3,imaps,pop3,pop3s' specified\nTry `iptables -h' or 'iptables --help' for more information.\n"
2018-01-22 14:53:23,680 fail2ban.action         [26127]: ERROR   iptables -w -N f2b-postfix-sasl
iptables -w -A f2b-postfix-sasl -j RETURN
iptables -w -I INPUT -p tcp --dport smtp,465,submission,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl -- returned 2
2018-01-22 14:53:23,680 fail2ban.actions        [26127]: ERROR   Failed to execute ban jail 'postfix-sasl' action 'iptables' info 'CallingMap({'failures': 2, 'time': 1516629203.366475, 'matches': 'Jan 22 14:05:12 ysengr postfix/smtpd[25386]: warning: unknown[80.82.70.210]: SASL LOGIN authentication failed: UGFzc3dvcmQ6\nJan 22 14:05:59 ysengr postfix/smtpd[25386]: warning: unknown[80.82.70.210]: SASL LOGIN authentication failed: UGFzc3dvcmQ6', 'ip': '80.82.70.210', 'ipmatches': <function Actions.__checkBan.<locals>.<lambda> at 0xb6a5c344>, 'ipjailmatches': <function Actions.__checkBan.<locals>.<lambda> at 0xb6a5c38c>, 'ipjailfailures': <function Actions.__checkBan.<locals>.<lambda> at 0xb6a5c41c>, 'ipfailures': <function Actions.__checkBan.<locals>.<lambda> at 0xb6a5c3d4>})': Error starting action

I’ve tried reading this but I can’t get any valuable info from it.
Can anyone help me correctly setting it all up ?
Thank you very much

EDIT: here are the versions of the two software

root@ysengr:/etc/fail2ban# fail2ban-server -V
Fail2Ban v0.9.6

Copyright (c) 2004-2008 Cyril Jaquier, 2008- Fail2Ban Contributors
Copyright of modifications held by their respective authors.
Licensed under the GNU General Public License v2 (GPL).

Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>.
Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>.
root@ysengr:/etc/fail2ban# iptables -V
iptables v1.6.0

As instructed I reset the banaction to iptables multiport but the logs shows the same error.
This time in ran the iptables command line reported in the logs in a separate terminal and it gaves me this :

root@ysengr:/etc/fail2ban# iptables -w -I INPUT -p tcp -m multiport --dports 25,465,587,imap3,imaps,pop3,pop3s -j f2b-postfix-sasl
iptables v1.6.0: invalid port/service `imap3' specified

Indeed, imap3 was absent from /etc/services so I added it like so

root@ysengr:/etc/fail2ban# grep imap3 /etc/services 
imap2       143/tcp     imap imap3      # Interim Mail Access P 2 and 4

My answer:


In order to ban multiple ports, your banaction needs to be set to iptables-multiport. This is the default, but you apparently have it set to something else based on the log output you’ve shown.

Try resetting banaction = iptables-multiport.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.