On Ubuntu you can install fail2ban like this:
Customize it by putting things in /etc/fail2ban/jail.local. For example:
[postfix-sasl] enabled = true filter = postfix-sasl logpath = /var/log/mail.log
[sshd] port = 22,2777
Then in /etc/fail2ban/filter.d/postfix-sasl.conf you can have:
[Definition] _daemon = postfix(-\w+)?/\w+(?:/smtp[ds])? # For example, match this: # Apr 4 14:52:59 www postfix/submission/smtpd[993587]: warning: unknown[91.224.92.22]: SASL LOGIN authentication failed: UGFzc3dvcmQ6 failregex = ha%(__prefix_line)swarning: [-._\w]+\[<HOST>\]: SASL ((?i)LOGIN|PLAIN|(?:CRAM|DIGEST)-MD5) authentication failed(: [ A-Za-z0-9+/:]*={0,2})?\s*$
You can make sure it's working like this: