Fail2ban is a brute-force detection system that analyzes the log files on your system to figure out failed login attempts to various services and block them. By default, it uses iptables to block attacking IP addresses.
When trying to set up Fail2ban with Asterisk, the Asterisk jail file that comes with your installed Fail2ban version may not always be compatible with your Asterisk version and you may face issues such as attacks not getting blocked or error messages like:
2018-04-0815:53:26,494 fail2ban.filter [10340]: ERROR No 'host' groupin'SECURITY.* SecurityEvent="FailedACL".*RemoteAddress=".+?/.+?//.+?".*' 2018-04-0815:53:26,494 fail2ban.transmitter [10340]: WARNING Command ['set', 'asterisk', 'addfailregex', 'SECURITY.* SecurityEvent="FailedACL".*RemoteAddress=".+?/.+?//.+?".*'] has failed. Received RegexException('No \'host\'groupin \'SECURITY.* SecurityEvent="FailedACL".*RemoteAddress=".+?/.+?//.+?".*\'',) 2018-04-0815:53:26,496 fail2ban.filter [10340]: ERROR No 'host' groupin'SECURITY.* SecurityEvent="InvalidAccountID".*RemoteAddress=".+?/.+?//.+?".*' 2018-04-0815:53:26,497 fail2ban.transmitter [10340]: WARNING Command ['set', 'asterisk', 'addfailregex', 'SECURITY.* SecurityEvent="InvalidAccountID".*RemoteAddress=".+?/.+?//.+?".*'] has failed. Received RegexException('No \'host\'groupin \'SECURITY.* SecurityEvent="InvalidAccountID".*RemoteAddress=".+?/.+?//.+?".*\'',) 2018-04-0815:53:26,497 fail2ban.filter [10340]: ERROR No 'host' groupin'SECURITY.* SecurityEvent="ChallengeResponseFailed".*RemoteAddress=".+?/.+?//.+?".*' 2018-04-0815:53:26,498 fail2ban.transmitter [10340]: WARNING Command ['set', 'asterisk', 'addfailregex', 'SECURITY.* SecurityEvent="ChallengeResponseFailed".*RemoteAddress=".+?/.+?//.+?".*'] has failed. Received RegexException('No \'host\'groupin \'SECURITY.* SecurityEvent="ChallengeResponseFailed".*RemoteAddress=".+?/.+?//.+?".*\'',) 2018-04-0815:53:26,499 fail2ban.filter [10340]: ERROR No 'host' groupin'SECURITY.* SecurityEvent="InvalidPassword".*RemoteAddress=".+?/.+?//.+?".*' 2018-04-0815:53:26,499 fail2ban.transmitter [10340]: WARNING Command ['set', 'asterisk', 'addfailregex', 'SECURITY.* SecurityEvent="InvalidPassword".*RemoteAddress=".+?/.+?//.+?".*'] has failed. Received RegexException('No \'host\'groupin \'SECURITY.* SecurityEvent="InvalidPassword".*RemoteAddress=".+?/.+?//.+?".*\'',)
I will share a working configuration tested with:
Ubuntu 16.04
Asterisk 13.19.0
Fail2Ban 0.9.3
Configuration
/etc/fail2ban/filter.d/asterisk.conf:
# Fail2Ban filter for asterisk authentication failures #
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from # common.local before = common.conf
failregex = ^Registration from'[^']*' failed for '<HOST>(:\d+)?' - (?:Wrong password|Username/auth name mismatch|No matching peer found|Not a local domain|Device does not match ACL|Peer isnot supposed to register|ACL error \(permit/deny\)|Not a local domain)$ ^Call from'[^']*' \(<HOST>:\d+\) to extension '[^']*' rejected because extension not found in context ^(?:Host )?<HOST> (?:failed (?:to authenticate\b|MD5 authentication\b)|triedto authenticate with nonexistent user\b) ^No registration for peer '[^']*' \(from <HOST>\)$ ^hacking attempt detected '<HOST>'$ ^SecurityEvent="(?:FailedACL|InvalidAccountID|ChallengeResponseFailed|InvalidPassword)"(?:(?:,(?!RemoteAddress=)\w+="[^"]*")*|.*?),RemoteAddress="IPV[46]/(UDP|TCP|WS)/<HOST>/\d+"(?:,(?!RemoteAddress=)\w+="[^"]*")*$ ^"Rejecting unknown SIP connection from <HOST>"$ ^Request (?:'[^']*' )?from'(?:[^']*|.*?)' failed for '<HOST>(?::\d+)?'\s\(callid: [^\)]*\) - (?:No matching endpoint found|Not match Endpoint(?: Contact)? ACL|(?:Failed|Error) to authenticate)\s*$
# FreePBX (todo: make optional in v.0.10): # ^(%(__prefix_line)s|\[\]\s*WARNING%(__pid_re)s:?(?:\[C-[\da-f]*\])? )[^:]+: Friendly Scanner from <HOST>$
ignoreregex =
datepattern = {^LN-BEG}
/etc/fail2ban/jail.local:
In the jail.local file, you have to uncomment or add the following asterisk jail:
There you may go to config/filter.d/asterisk.conf and click on history to find all the different versions of that filter file and start testing them with your Asterisk version.
Usually, if you are using a recent and stable Asterisk and Fail2ban version, the latest file present there should work best with your setup.