Edit - Updated thread here discussing the newer fail2ban client for APIBAN.
The last few Open Source Lounge sessions have seen conversation around apiban.org, with last night’s session getting a detailed presentation by Fred Posner on how it all works. The apiban.org project is a SIP blacklist compiled from honeypots with a simple API that allows you to dynamically update iptables rules with known current attackers. Generally I’m not a fan of blacklists on principle, but there are obviously situations where whitelisting won’t work. If one is working with this constraint, a good strategy is to be proactive (blacklist) in addition to being reactive (fail2ban/responsive).
While last night’s session was in progress, I applied for an API key and did some basic API queries using a browser to get a sense of how it all worked. This morning I stopped fail2ban and opened the SIP ports to the internet on a test system (FreePBX 15, fail2ban stopped, firewall running, responsive disabled, sip services enabled for INTERNET zone) and started getting a flood of intrusion attempts almost immediately. I let the attackers have fun for most of the day, and periodically checked the offending IPs against the current apiban blacklist. All of the bad guys I was seeing were already present on the apiban blacklist; thing are looking good.
Set up the free apiban client on my FreePBX 15 Distro and created a cron task to run every 5 min. Instantly all of the offending IPs got blocked. I’ve restarted fail2ban and will continue to monitor for the next short while to make sure there are no surprise interactions with the FreePBX firewall or iptables, but initial success looks promising.
Steps to Setup apiban on FreePBX Distro
Step 1 - Browse to https://apiban.org/ and request an api key by email. Response is immediate
Step 2 - Browse to apiban clients github page apiban/clients at master · palner/apiban · GitHub and review the instructions there. At the time of this writing, this is:
Login as root, download and chown the files:
[root@pbx ~]# mkdir /usr/local/bin/apiban
[root@pbx ~]# cd /usr/local/bin/apiban
[root@pbx apiban]# wget https://github.com/palner/apiban/raw/master/clients/go/apiban-iptables-client
**** snip ****
[root@pbx apiban]# chmod +x apiban-iptables-client
[root@pbx apiban]# wget https://raw.githubusercontent.com/palner/apiban/master/clients/go/apiban-iptables/config.json
**** snip ****
[root@pbx apiban]# ls -l
total 7700
-rwxr-xr-x 1 root root 7878161 Aug 21 16:53 apiban-iptables-client
-rw-r--r-- 1 root root 59 Aug 21 16:54 config.json
Edit config.json and add your API key. API calls are limited to a small number per minute, so one key per PBX is recommended. Then run the client for the first time:
[root@pbx apiban]# ./apiban-iptables-client
Nothing appears to happen, but if you check the log:
[root@pbx apiban]# cat /var/log/apiban-client.log
2020/08/21 16:55:33 ** Started APIBAN CLIENT
2020/08/21 16:55:33 Licensed under GPLv2. See LICENSE for details.
2020/08/21 16:55:33 no command line arguments received
2020/08/21 16:55:33 IPTABLES doesn't contain APIBAN. Creating now...
2020/08/21 16:55:33 APIBAN chain was created - Resetting LKID
2020/08/21 16:55:34 failed to get banned list: unauthorized
2020/08/21 16:55:50 ** Started APIBAN CLIENT
2020/08/21 16:55:50 Licensed under GPLv2. See LICENSE for details.
2020/08/21 16:55:50 no command line arguments received
2020/08/21 16:55:51 Blocking 185.53.88.78/32
2020/08/21 16:55:51 Blocking 163.172.207.104/32
2020/08/21 16:55:51 Blocking 62.210.149.30/32
2020/08/21 16:55:51 Blocking 104.37.175.137/32
2020/08/21 16:55:51 Blocking 77.247.110.58/32
2020/08/21 16:55:51 Blocking 146.88.240.4/32
2020/08/21 16:55:51 Blocking 62.210.162.99/32
2020/08/21 16:55:51 Blocking 51.158.25.170/32
2020/08/21 16:55:51 Blocking 45.176.240.44/32
2020/08/21 16:55:51 Blocking 62.210.151.21/32
2020/08/21 16:55:51 Blocking 45.143.220.163/32
2020/08/21 16:55:51 Blocking 212.129.17.32/32
** snip **
And finally check iptables for the new rules:
[root@pbx apiban]# iptables-save | grep API
:APIBAN - [0:0]
-A INPUT -j APIBAN
-A FORWARD -j APIBAN
-A APIBAN -s 185.53.88.78/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 163.172.207.104/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 62.210.149.30/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 104.37.175.137/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 77.247.110.58/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 146.88.240.4/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 62.210.162.99/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 51.158.25.170/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 45.176.240.44/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 62.210.151.21/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 45.143.220.163/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 212.129.17.32/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 185.53.88.113/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 188.165.222.17/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 95.216.117.54/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 163.172.7.215/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 51.79.57.12/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 50.234.173.102/32 -j REJECT --reject-with icmp-port-unreachable
-A APIBAN -s 113.141.70.184/32 -j REJECT --reject-with icmp-port-unreachable
** snip **
Step 3 - create a cron task to automate everything
Edit the file /etc/crontab
and add the following lines at the end
# update apiban iptables
*/5 * * * * root /usr/local/bin/apiban/apiban-iptables-client >/dev/null 2>&1