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 packages from rpmforge should already be included in your OS. Optionally you can fetch the fail2ban rpm directly from rpmforge for your specific linux distro

CentOS:
yum install fail2ban

Debian/Ubuntu:
apt-get install fail2ban

SUSE:
yast2 -i fail2ban
Vicidial:
(pre-installed)

Enable asterisk fail2ban: 

Navigate to the main config file using your favourite text editor:

nano /etc/fail2ban/jail.conf

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

Notes:

– 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 root@hostname 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.

Additional:

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:
cd /usr/src

Download and extract Fail2Ban (current stable version as of Nov-2021 is 1.0.2):

wget https://github.com/fail2ban/fail2ban/archive/refs/tags/1.0.2.tar.gz
tar xvfj fail2ban-1.0.2.tar.gz

Note: v11.1 is available but requires changes to custom failregexs’. See the Changelog before implementing.

Enter the Fail2Ban directory you just extracted:

cd fail2ban-1.0.2

Make sure python and iptables are installed:

CentOS/Red Hat:

yum install python iptables

Debian/Ubuntu:

apt-get install python iptables

Install Fail2Ban:

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-1.0.2/files/redhat-initd /etc/init.d/fail2ban
chmod 755 /etc/init.d/fail2ban

cp /usr/src/fail2ban-1.0.2/files/debian-initd /etc/init.d/fail2ban
chmod 755 /etc/init.d/fail2ban

For other distributions’ init scripts such as Gentoo, SuSE, Monit, Nagios, etc, refer to the ‘/files’ folder.

Configure Fail2Ban

Fail2ban already includes the asterisk failregex in the filters folder along with other defaults located at /etc/fail2ban/filter.d/.

The contents of /etc/fail2ban/filter.d/asterisk.conf reflect the following:

# 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
#

# 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
#
ignoreregex =

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[7557] chan_sip.c: Registration from ‘”69106698″<sip:[email protected]>;’ failed for ‘123.123.123.123’ – 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[7557] chan_sip.c: Registration from ‘”XXXXXXXXXX”<sip:[email protected]>;’ failed for ‘123.123.123.123’ – 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:

[asterisk-iptables]

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!

Asterisk Logging

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.

[general]

dateformat=%F %T

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:

/etc/init.d/iptables start
or
CentOS 7: systemctl start firewalld

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:

/etc/init.d/fail2ban start | systemctl start fail2ban

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 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

or $fail2ban-regex /var/log/asterisk/messages /etc/fail2ban/filter.d/asterisk.conf

and

$fail2ban-regex /var/log/secure /etc/fail2ban/filter.d/sshd.conf

NOTE: The above rules test Asterisk and SSH rules against your log history.

To un-ban/remove an IP evoke:

  • iptables -D INPUT -s 1.2.3.4 -j DROP
  • iptables-save

—–

A large portion of information was taken from bulak and dominic16y from voip-info:
http://www.voip-info.org/wiki/view/Fail2Ban+%28with+iptables%29+And+Asterisk

For information on creating IPtable rules:
http://www.thegeekstuff.com/2011/02/iptables-add-rule/

For more asterisk security tips, take a look at your asterisk Security Guide.

—————

About nurango

nurango is a leader in secure business phone systems with infrastructure privately run in Canada.