The following implementation of IPtables and Fail2Ban will HELP protect your asterisk box from malicious and Brute Force attacks.
This solution is NOT and should NOT be your own line of defense in PBX security,
but it is without question an essential.
SECURITY NOTE: fail2ban is rather limited in its ability to detect attacks against asterisk.
In a nutshell, fail2ban is a log checker therefor it is reactive, not proactive. For additional protection, check out our asterisk security tips.
Let’s Get Started
Install rpmforge or optionally fetch the fail2ban rpm directly from rpmforge.
Install fail2ban using yum:
yum install fail2ban
apt-get install fail2ban
yast2 -i fail2ban
Enable asterisk fail2ban:
Navigate to the main config file using your favourite text editor:
Find the line that says [asterisk] or CTRL W – asterisk
Add the line – “enabled = true” (without quotes)
Save and ensure fail2ban is set on boot – “chkconfig fail2ban on”
Start the service – “service fail2ban start” or “/etc/init.d/fail2ban start
– If you will not use the email notification feature (can get annoying), simply remove the sendmail-whois line. This will save some disk space and stop [email protected] emails from trying to go out if your postfix is not setup properly.
– If you want to enable the whois feature for IP translation in the Fail2ban email notices, install jwhois (yum install jwhois).
Recommendations: Increase the ban time at the top of the jail.conf file from 600 seconds to something longer.
Location of Regex filter – /etc/fail2ban/filter.d/asterisk.conf
To view the bees caught in the honey pot you can use “iptables -L” or “service fail2ban status”.
Manual Install with IPTables from Source installation:
Change directories to /usr/src:
Download and extract Fail2Ban (current as of 2016 but check for newer releases):
tar jxf fail2ban-0.8.11.tar.gz –(using yum? “yum install fail2ban”)
Enter the Fail2Ban directory you just extracted:
Make sure python and iptables are installed:
yum install python iptables
apt-get install python iptables
python setup.py install
Install the Fail2Ban init script (for source installations):
Centos/Red Hat (if you installed via yum/rpm, the init script has already been installed):
cp /usr/src/fail2ban-0.8.11/files/redhat-initd /etc/init.d/fail2ban
chmod 755 /etc/init.d/fail2ban
For other distributions’ init scripts, please refer to documentation specific to them.
We need to create a configuration for Fail2Ban so that it can understand attacks against Asterisk.
Create a new filter configuration for Asterisk:
The contents of /etc/fail2ban/filter.d/asterisk.conf should be the following:
# Fail2Ban configuration file
# $Revision: 250 $
# Read common prefixes. If any customizations available — read them from
#before = common.conf
#_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
# Values: TEXT
# Asterisk 1.4 use the following failregex
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>’ – 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.* .*: Registration from ‘.*’ failed for ‘<HOST>’ – Peer is not supposed to register
NOTICE.* .*: Registration from ‘.*’ failed for ‘<HOST>’ – ACL error (permit/deny)
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>;.*
NOTICE.* .*: Sending fake auth rejection for device .*\<sip:.*\@<HOST>\>;tag=.*
# In Asterisk 1.8 use the same as above, but after <HOST> add :.* before the single quote. This is because in Asterisk 1.8, the log file includes a port number which 1.4 did not.
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
If you’re having issues with your system not banning properly when the “Registration from” section in your log file contains a quotation mark (“) as in this example:
[2011-04-07 17:53:11] NOTICE chan_sip.c: Registration from ‘”69106698″<sip:[email protected]>;’ failed for ‘220.127.116.11’ – No matching peer found
Add the following line, with the others above, in asterisk.conf:
NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>' - No matching peer found
Recently noticed attacks:
[2011-06-21 17:53:11] NOTICE chan_sip.c: Registration from ‘”XXXXXXXXXX”<sip:[email protected]>;’ failed for ‘18.104.22.168’ – Wrong Password
Adding the following line will block these attempts:
NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>' - Wrong password
Next edit /etc/fail2ban/jail.conf to include the following section so that it uses the new filter. This does a 3-day ban on the IP that performed the attack. It is recommend to set the bantime in the [DEFAULT] section so if affects all attacks. It is also recommend to turn on an iptables ban for ssh, httpd/apache, and ftp if they are running on the system. Be sure to edit the sendmail-whois action to send notifications to an appropriate address:
enabled = true
filter = asterisk
action = iptables-allports[name=ASTERISK, protocol=all]
sendmail-whois[name=ASTERISK, dest=root, [email protected]]
logpath = /var/log/asterisk/messages
maxretry = 5
bantime = 259200
note: logpath = /var/log/asterisk/messages is for vanilla asterisk, use logpath = /var/log/asterisk/full for freepbx. You can check the name of the log file in logger.conf.
note: if fail2ban still failed to identify login attempts, try the syslog logging way.
Don’t Ban Yourself
We don’t want to ban ourselves by accident. Edit /etc/fail2ban/jail.conf and edit the ignoreip option under the [DEFAULT] section to include your IP addresses or network, as well as any other hosts or networks you do not wish to ban. Note that the addresses must be separated by a SPACE character!
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 . The other formats that Fail2Ban supports, however, 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.
Then reload the logger module for Asterisk. At the command line, run 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 for 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, run the following as root:
Depending on your install, you may or may not have the iptables init script installed. Please refer to an iptables install/setup guide for your distribution for more information.
Turn on Fail2Ban
To start Fail2Ban, run the following as root:
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 see something similar to that, then you have some troubleshooting to do; check out /var/log/fail2ban.log.
If you do not see all your rules, or if you see a different subset of rules after stopping and restarting fail2ban, you may be experiencing the issue described on this page on the Fail2ban talk:Community Portal and may wish to use the suggested fix:
You can also test the filter regex expressions using:
$fail2ban-regex /var/log/asterisk/full /etc/fail2ban/filter.d/asterisk.conf
$fail2ban-regex /var/log/asterisk/messages /etc/fail2ban/filter.d/asterisk.conf
$fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/sshd.conf
NOTE: The above rules test Asterisk and SSH rules against your log history.
A large portion of information was taken from bulak and dominic16y from voip-info:
For information on creating IPtable rules:
For checking ban list and removing IPs etc check out this tutorial: http://www.cyberciti.biz/faq/linux-howto-check-ip-blocked-against-iptables/