Fail2Ban from www.fail2ban.org is a great tool to block unwanted IP addresses from accessing your server. It works along with iptables, and checks the log files for predefined patterns, and on finding a matching pattern blocks the IP address which is responsible to generate that pattern.
Every asterisk installation must have this tool installed to secure it against unwanted SIP registration attempts, which is the most common type of attack on asterisks servers. This one security measure alone is enough to make your asterisk server secure. Here I’ll describe how to do it. These instructions I have primarily taken from voip-info.org (http://ilovetovoip.com/QQ).
After installing fail2ban, create a file called asterisk.conf in the fail2ban filters folder:
touch /etc/fail2ban/filter.d/asterisk.conf
Now add the following in this file:
# Fail2Ban configuration file
#
#
# $Revision: 250 $
#
[INCLUDES]
# Read common prefixes. If any customizations available -- read them from
# common.local
#before = common.conf
[Definition]
#_daemon = asterisk
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
#
failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>' - Wrong password
NOTICE.* .*: Registration from '.*' failed for '<HOST>' - No matching peer found
NOTICE.* .*: Registration from '.*' failed for '<HOST>' - Username/auth name mismatch
NOTICE.* .*: Registration from '.*' failed for '<HOST>' - Device does not match ACL
NOTICE.* <HOST> failed to authenticate as '.*'$
NOTICE.* .*: No registration for peer '.*' \(from <HOST>\)
NOTICE.* .*: Host <HOST> failed MD5 authentication for '.*' (.*)
NOTICE.* .*: Failed to authenticate user .*@<HOST>.*
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
In the above lines you can see that fail2ban will look for the pharases: “Wrong password”, “No matching peer found”, “Username/auth name mismatch” and “Device does not match ACL”.
Next add the following lines in /etc/fail2ban/jail.conf
[asterisk-iptables]
enabled = true
filter = asterisk
action = iptables-allports[name=ASTERISK, protocol=all]
sendmail-whois[name=ASTERISK, dest=root, sender=fail2ban@example.org]
logpath = /var/log/asterisk/full
maxretry = 5
bantime = 259200
This filter will ban the bad IPs for three days, i.e. 259200 seconds.
DON’T BAN YOURSELF
Make sure you don’t ban your own IP address. For this purpose edit /etc/fail2ban/jail.conf and add your IP address in the ignoreip list under the [DEFAULT] section.
Asterisk Logging Date and Time Format
We must change how Asterisk does its time stamp for logging. The default format does not work with Fail2Ban because the pattern Fail2Ban uses that would match this format has a beginning of line character (^), and Asterisk puts its date/time inside of []. However the other formats that Fail2Ban supports do not have this character and can be used with Asterisk
To change this format open /etc/asterisk/logger.conf and add the following line under [general] section (You may have to create this before the [logfiles] section). This causes the date and time to be formatted as Year-Month-Day Hour:Minute:Second, [2008-10-01 13:40:04] is an example.
[general]
dateformat=%F %T
Then reload the logger module for Asterisk, at the command line enter the following command:
asterisk -rx "logger reload"
If for some reason you do not want to change the date/time format for your normal asterisk logs (maybe you already have scripts that use it or something and do not want to edit them) you can do the following instead
In /etc/asterisk/logger.conf add the following line under the [logfiles] section for Asterisk to log NOTICE level events to the syslog (/var/log/messages) as well as its normal log file. These entries in syslog will have a Date/Time stamp that is usable by Fail2Ban.
syslog.local0 => notice
Be sure to reload the logger module for Asterisk, check above of the command to do so. If you chose this option you will also have to change the /etc/fail2ban/jail.conf setting under the [asterisk-iptables] section for the logpath option to the following:
logpath = /var/log/messages
TURNING IT ON
Now it is time to put fail2ban to work. There are a couple steps we need to do first.
Turn IPTABLES on
By default iptables allows all traffic. So if we turn it on it will not block any traffic until Fail2Ban creates deny rules for attackers. You should create your own firewall rules and setup for iptables, but that is beyond the scope of this guide. Just know that Fail2Ban, by default, inserts rules at the top of the chain, so they will override any rules you have configured in iptables. This is good because you may allow all sip traffic in and then the Fail2Ban will block individual hosts, after they have done an attack, before they are allowed by this rule again.
To start iptables type the following as root:
service iptables start
OR
/etc/init.d/iptables start
Turn on Fail2Ban
To start Fail2Ban type the following as root:
service fail2ban start
OR
/etc/init.d/fail2ban start
Check It
If both started properly issue the following command to view your iptables rules:
iptables -L -v
You should see something like the following for the INPUT chain (you will see more if you have other Fail2Ban filters enabled):
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 2104K 414M fail2ban-ASTERISK all — any any anywhere anywhere
If you do not seem something similar to that then you have some troubleshooting to, check out /var/log/fail2ban.log.
Turn it on for good
If all is well up to this point lets make sure that fail2ban and iptables restart with the server by issuing the following commands.
Centos/Redhat:
chkconfig iptables on chkconfig fail2ban on
Debian/Ubuntu:
update-rc.d iptables defaults update-rc.d fail2ban defaults
You should now be somewhat protected against SIP scans and brute force attacks!




I had an issue with fail2ban not banning all attempts to hack into my asterisk based PBX due to the fact that the ** contained a quotation mark (“).
I was able to fix this by adding the following line with the others in asterisk.conf listed above:
NOTICE.* .*: Registration from '\".*\".*' failed for '' - No matching peer found
Hope this helps.
.J.