While a firewall provides the first layer of protection and restricts access to defined ports and IP ranges, it is essential to further secure the Asterisk server itself because:
These points demonstrate that, while an upstream firewall is very important, it cannot be a panacea. Security only becomes robust and reliable through several coordinated protection mechanisms. Protection on the Asterisk server is therefore essential to ensure secure and trouble-free operation. Regardless of whether this is a Raspberry Pi, as shown here, a virtual machine internally or in the cloud, or a single bare metal server on the internet.
Therefore, a solid firewall using the UFW tool based on IPTables is demonstrated here.
To simplify the configuration of IPTables rules, we use the program UFW (Uncomplicated Firewall), which is installed as follows.
~# apt-get update
~# apt install -y ufw
First, we save the default settings on the firewall.
The corresponding rules:
~# ufw default deny incoming
~# ufw default allow outgoing
and
~# ufw allow from 87.128.0.0/11 to any port 5060 proto udp comment 'Allow SIP signaling from Telekom SIP servers (UDP 5060)'
~# ufw allow from 217.0.0.0/13 to any port 5060 proto udp comment ‘Allow SIP signaling from Telekom SIP servers (UDP 5060)’
~# ufw allow from 87.128.0.0/11 to any port 10000:20000 proto udp comment 'Allow RTP media streams from Telekom IP-Bereichen (UDP 10000-20000)'
~# ufw allow from 217.0.0.0/13 to any port 10000:20000 proto udp comment ‘Allow RTP media streams from Telekom IP-Bereichen (UDP 10000-20000)’
~# ufw allow from 192.168.0.0/16 to any port 80 proto tcp comment 'Allow HTTP from internal hosts'
~# ufw allow from 192.168.0.0/16 to any port 443 proto tcp comment 'Allow HTTPS from internal host's
~# ufw allow from 192.168.0.0/16 to any port 22 proto tcp comment 'Allow SSH from internal hosts'
Please note:
The network ranges "87.128.0.0/11" and "217.0.0.0/13" belong to Telekom, and here, too, different registrar IP addresses may apply in your area, especially if your provider is not Telekom. And instead of "192.168.0.0/16," your configuration will be used here as well.
Finally, the firewall is started.
~# ufw enable
~# ufw status verbose
Or if it is already active, the rule will be reloaded with
~# ufw reload
~# ufw status verbose
This means your Asterisk is also rock solidly protected.
If you also want to fend off attacks, you can do so very effectively with Fail2ban. This is installed with
~# apt -y install fail2ban
Then a /etc/fail2ban/jail.local
is created and filled.
~# vi /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 192.168.0.0/24
ignorecommand =
bantime = 3600
findtime = 600
maxretry = 5
maxmatches = %(maxretry)s
action = %(action_mwl)s
destemail = fail2ban@example.tld
sender = asterisk@example.tld
mta = sendmail
[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
bantime = 3600
findtime = 3600
[asterisk]
enabled = true
filter = asterisk
port = 5060,5061,10000:20000
protocol = udp,tcp
logpath = /var/log/asterisk/full
maxretry = 5
findtime = 600
bantime = 3600
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
bantime = 7d
findtime = 1d
maxretry = 5
The filters for asterisk
and sshd
, respectively recidive
, are located in /etc/fail2ban/filter.d/.
The recidive
jail monitors which IPs have been banned by Fail2Ban more often (i.e., "repeat offenders"). And the email delivery settings in [DEFAULT]
are only effective if your Asterisk also has a mail client configured. This is to be expected, since otherwise, sending voicemail messages via email wouldn't work.
~# systemctl restart fail2ban.service
~# systemctl status fail2ban.service
starts the Fail2Ban. And you can view the jails this way.
~# fail2ban-client status
~# fail2ban-client status sshd
~# fail2ban-client status asterisk
This means Asterisk has absolutely solid security. Depending on which services and ports need to be open, this can be secured via the firewall or with Fail2ban.