In this guide, I will show you how to secure your Asterisk and FreePBX installation by building an effective VoIP Blacklist using geolocation filtering. Today there are a large number of brute-force attacks and VoIP fraud attempts targeting Asterisk, FreePBX and other PBX systems on the internet.
It is every system administrator's job to make sure the success rate for such attempts is reduced to near zero. One way to protect Asterisk and FreePBX from such attempts is to use Fail2ban and the VoIP Blacklist.
This will save you bandwidth and protect your business. To make our job easier, we will use VoIPBL , a distributed VoIP blacklist that aims to protect against VoIP fraud and to minimize abuse of networks with publicly exposed PBXs .

How does VoIPBL keep you secure?
The VoIPBL Geolocation feature lets you block all network traffic from countries that a network does not need to communicate with or that are known to be sources of malicious activity. You can check whether your IP address is blacklisted on their site.
How to install VoIPBL
The VoIP Blacklist depends on Fail2ban to get banned on your PBX server. Make sure the fail2banpackage is installed and the service is running:
sudo yum install epel-release
sudo yum install fail2ban fail2ban-systemd
For Ubuntu and other Debian-family systems, run:
sudo apt-get -y install fail2ban ufw
If you are running CentOS 6 or another RHEL 6 family system, install iptables-services and fail2ban fail2ban-systemd
sudo yum install iptables-services fail2ban
The default settings for Fail2ban are configured in /etc/fail2ban/jail.conf
A basic fail2ban configuration will include ssh monitoring. Let's add it to the /etc/fail2ban/jail.localfile .
$ sudo vim /etc/fail2ban/jail.local
Add the following content:
[postfix]
enabled = true
port = smtp
filter = postfix
logpath = /var/log/mail.log
maxretry = 3
[ssh]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
[vsftpd]
enabled = false
port = ftp
filter = vsftpd
logpath = /var/log/auth.log
maxretry = 5
[pure-ftpd]
enabled = true
port = ftp
filter = pure-ftpd
logpath = /var/log/syslog
maxretry = 3
Then start and enable the fail2ban service:
sudo systemctl enable fail2ban.service
sudo systemctl start fail2ban.service
Download the voipbl.shscript and place it under/usr/local/bin/
wget http://www.voipbl.org/voipbl.sh -O /usr/local/bin/voipbl.sh
Make the script executable:
chmod +x /usr/local/bin/voipbl.sh
The above uses iptables. If your system supports ipset , you can use the following script instead:
#!/bin/bash
URL="http://www.voipbl.org/update/"
set -e
echo "Downloading rules from VoIP Blacklist"
wget -qO - $URL -O /tmp/voipbl.txt
echo "Loading rules..."
# Check if rule set exists and create one if required
if ! $(/usr/sbin/ipset list voipbl > /dev/null 2>&1); then
ipset -N voipbl iphash
fi
#Check if rule in iptables
if ! $(/sbin/iptables -w --check INPUT -m set --match-set voipbl src -j DROP > /dev/null 2>&1); then
/sbin/iptables -I INPUT 1 -m set --match-set voipbl src -j DROP
fi
# Create temporary chain
ipset destroy voipbl_temp > /dev/null 2>&1 || true
ipset -N voipbl_temp iphash
cat /tmp/voipbl.txt |\
awk '{ print "if [ ! -z \""$1"\" -a \""$1"\" != \"#\" ]; then /usr/sbin/ipset -A voipbl_temp \""$1"\" ;fi;"}' | sh
ipset swap voipbl_temp voipbl
ipset destroy voipbl_temp || true
echo "Done! Rules loaded"
Then add a new Fail2ban Jail to /etc/fail2ban/jail.conf:
[asterisk-iptables]
action = iptables-allports[name=ASTERISK, protocol=all]
voipbl[serial=XXXXXXXXXX]
Now define the VoIP Blacklist actions for Fail2ban in /etc/fail2ban/action.d/voipbl.conf .
sudo vim /etc/fail2ban/action.d/voipbl.conf
Add:
# Description: Configuration for Fail2Ban
[Definition]
actionban = <getcmd> "<url>/ban/?serial=<serial>&ip=<ip>&count=<failures>"
actionunban = <getcmd> "<url>/unban/?serial=<serial>&ip=<ip>&count=<failures>"
[Init]
getcmd = wget --no-verbose --tries=3 --waitretry=10 --connect-timeout=10 \
--read-timeout=60 --retry-connrefused --output-document=- \
--user-agent=Fail2Ban
url = http://www.voipbl.org
Now we can create a cron job file to update the rules every 3 hours :
$ sudo vim /etc/cron.d/voipbl
# update blacklist each 4 hours
0 */4 * * * * root /usr/local/bin/voipbl.sh
When you are done, restart the fail2ban daemon to protect against VoIP fraud:
sudo systemct restart fail2ban