{"id":4976,"date":"2015-04-20T17:26:20","date_gmt":"2015-04-20T22:26:20","guid":{"rendered":"http:\/\/www.nurango.com\/new\/?p=4976"},"modified":"2023-02-01T21:28:21","modified_gmt":"2023-02-01T21:28:21","slug":"securing-asterisk-using-fail2ban","status":"publish","type":"post","link":"https:\/\/www.nurango.ca\/blog\/securing-asterisk-using-fail2ban","title":{"rendered":"Securing asterisk using Fail2Ban"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><span style=\"font-family: arial,helvetica,sans-serif;\">The following implementation of IPtables and Fail2Ban will HELP protect your asterisk box from malicious and Brute Force attacks.<\/span><\/h4>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">This solution is NOT and should NOT be your own line of defense in PBX security,<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> but it is without question an essential.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code><span style=\"color: red;\">SECURITY NOTE: fail2ban is rather limited in its ability to detect attacks against asterisk.&nbsp;<\/span><\/code><\/span><span style=\"font-family: arial,helvetica,sans-serif;\"><code><\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code>In a nutshell, fail2ban is a log checker therefor it is reactive, not proactive. For additional protection, check out our <a title=\"asterisk security tips\" href=\"https:\/\/www.nurango.ca\/blog\/asterisk-ip-pbx-security-tips\">asterisk security tips<\/a>.<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">&#8212;<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"font-family: arial,helvetica,sans-serif;\">Let&#8217;s Get Started<\/span><\/h3>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Install packages from rpmforge should already be included in your OS. Optionally you can fetch the fail2ban rpm directly from <a title=\"Fail2ban\" href=\"https:\/\/pkgs.org\/download\/fail2ban\" target=\"_blank\" rel=\"nofollow noopener\">rpmforge<\/a>&nbsp;for your specific linux distro<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>CentOS:<\/strong><\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> yum install fail2ban<\/span><\/p>\n\n\n\n<p><strong><span style=\"font-family: arial,helvetica,sans-serif;\">Debian\/Ubuntu:<\/span><\/strong><br><span style=\"font-family: arial,helvetica,sans-serif;\"> apt-get install fail2ban<\/span><\/p>\n\n\n\n<p><strong><span style=\"font-family: arial,helvetica,sans-serif;\">SUSE:<\/span><\/strong><br><span style=\"font-family: arial,helvetica,sans-serif;\"> yast2 -i fail2ban<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> Vicidial:<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> (pre-installed)<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Enable asterisk fail2ban:&nbsp;<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Navigate to the main config file using your favourite text editor:<\/span><\/p>\n\n\n\n<p><em><span style=\"font-family: arial,helvetica,sans-serif;\">nano \/etc\/fail2ban\/jail.conf<\/span><\/em><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Find the line that says [asterisk] or CTRL W &#8211; <em>asterisk<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Add the line &#8211; &#8220;enabled = true&#8221; (without quotes)<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Save and ensure fail2ban is set on boot &#8211; &#8220;chkconfig fail2ban on&#8221;<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Start the service &#8211; &#8220;service fail2ban start&#8221; or &#8220;\/etc\/init.d\/fail2ban start<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><span style=\"text-decoration: underline;\"><em>Notes<\/em><\/span><em>:<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">&#8211; 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&nbsp;emails from trying to go out if your postfix is not setup properly.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">&#8211; If you want to enable the whois feature for IP translation in the Fail2ban email notices, install jwhois (yum install jwhois).<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><span style=\"text-decoration: underline;\"><em>Recommendations<\/em><\/span><em>:<\/em> Increase the ban time at the top of the jail.conf file from 600 seconds to something longer.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Additional:<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Location of Regex filter<strong> &#8211;&nbsp;<\/strong>\/etc\/fail2ban\/filter.d\/asterisk.conf<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">To view the bees caught in the honey pot you can use &#8220;iptables -L&#8221; or &#8220;service fail2ban status&#8221;.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"font-family: arial,helvetica,sans-serif;\">Manual Install with IPTables from source installation:<\/span><\/h3>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Change directories to \/usr\/src:<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> cd \/usr\/src<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Download and extract Fail2Ban (current stable version as of Nov-2021 is 1.0.2):<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">wget https:\/\/github.com\/fail2ban\/fail2ban\/archive\/refs\/tags\/1.0.2.tar.gz<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> tar xvfj fail2ban-1.0.2.tar.gz<\/span><\/p>\n\n\n\n<p><em><span style=\"font-family: arial,helvetica,sans-serif;\">Note: <a href=\"https:\/\/github.com\/fail2ban\/fail2ban\/releases\/tag\/0.11.1\" target=\"_blank\" rel=\"noopener noreferrer\">v11.1<\/a> is available but requires changes to custom failregexs&#8217;. See the <a title=\"Fail2ban changelog\" href=\"https:\/\/github.com\/fail2ban\/fail2ban\/blob\/master\/ChangeLog\" target=\"_blank\" rel=\"nofollow noopener\">Changelog<\/a> before implementing.<\/span><\/em><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Enter the Fail2Ban directory you just extracted:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">cd fail2ban-1.0.2<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Make sure python and iptables are installed:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">CentOS\/Red Hat:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">yum install python iptables<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Debian\/Ubuntu:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">apt-get install python iptables<\/span><\/p>\n\n\n\n<p><strong><span style=\"font-family: arial,helvetica,sans-serif;\">Install Fail2Ban:<\/span><\/strong><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">python setup.py install<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Install the Fail2Ban init script (for source installations):<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Centos\/Red Hat (if you installed via yum\/rpm, the init script has already been installed):<\/span><\/p>\n\n\n\n<p><em><span style=\"font-family: arial,helvetica,sans-serif;\">cp \/usr\/src\/fail2ban-1.0.2\/files\/redhat-initd \/etc\/init.d\/fail2ban<\/span><\/em><br><em><span style=\"font-family: arial,helvetica,sans-serif;\"> chmod 755 \/etc\/init.d\/fail2ban<\/span><\/em><\/p>\n\n\n\n<p><em><span style=\"font-family: arial,helvetica,sans-serif;\">cp \/usr\/src\/fail2ban-1.0.2\/files\/debian-initd \/etc\/init.d\/fail2ban<\/span><\/em><br><em><span style=\"font-family: arial,helvetica,sans-serif;\"> chmod 755 \/etc\/init.d\/fail2ban<\/span><\/em><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">For other distributions&#8217; init scripts such as Gentoo, SuSE, Monit, Nagios, etc, refer to the &#8216;\/files&#8217; folder.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"font-family: arial,helvetica,sans-serif;\">Configure Fail2Ban<\/span><\/h3>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Fail2ban already includes the asterisk failregex in the filters folder along with other defaults located at <em>\/etc\/fail2ban\/filter.d\/.<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">The contents of <strong>\/etc\/fail2ban\/filter.d\/asterisk.conf<\/strong> reflect the following:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em># Fail2Ban configuration file<br>#<br>#<br># $Revision: 250 $<br>#<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>[INCLUDES]<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em># Read common prefixes. If any customizations available &#8212; read them from<br># common.local<br>#before = common.conf<br><\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>[Definition]<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>#_daemon = asterisk<br># Option:&nbsp; failregex<br># Notes.:&nbsp; regex to match the password failures messages in the logfile. The<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host must be matched by a group named &#8220;host&#8221;. The tag &#8220;&lt;HOST&gt;&#8221; can<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; be used for standard IP\/hostname matching and is only an alias for<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (?:::f{4,6}:)?(?P&lt;host&gt;\\S+)<br># Values:&nbsp; TEXT<br>#<br><\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em># Asterisk 1.4 use the following failregex<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>failregex = NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Wrong password<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;:.*&#8217; &#8211; No matching peer found<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; No matching peer found<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Username\/auth name mismatch<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Device does not match ACL<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Peer is not supposed to register<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; ACL error (permit\/deny)<br>NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Device does not match ACL<br>NOTICE.* &lt;HOST&gt; failed to authenticate as &#8216;.*&#8217;$<br>NOTICE.* .*: No registration for peer &#8216;.*&#8217; \\(from &lt;HOST&gt;\\)<br>NOTICE.* .*: Host &lt;HOST&gt; failed MD5 authentication for &#8216;.*&#8217; (.*)<br>NOTICE.* .*: Failed to authenticate user .*@&lt;HOST&gt;;.*<br>NOTICE.* .*: Sending fake auth rejection for device .*\\&lt;sip:.*\\@&lt;HOST&gt;\\&gt;;tag=.*<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em># In Asterisk 1.8 use the same as above, but after &lt;HOST&gt; add :.* before the single quote. This is because in Asterisk 1.8, the log file includes a port number which 1.4 did not.<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em># Option:&nbsp; ignoreregex<br># Notes.:&nbsp; regex to ignore. If this regex matches, the line is ignored.<br># Values:&nbsp; TEXT<br>#<br>ignoreregex =<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">If you&#8217;re having issues with your system not banning properly when the &#8220;Registration from&#8221; section in your log file contains a quotation mark (&#8220;) as in this example:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">[2011-04-07 17:53:11] NOTICE[7557] chan_sip.c: Registration from &#8216;&#8221;69106698&#8243;&lt;sip:69106698@123.123.123.123&gt;;&#8217; failed for &#8216;123.123.123.123&#8217; &#8211; No matching peer found<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Add the following line, with the others above, in asterisk.conf:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code>NOTICE.* .*: Registration from '\\\".*\\\".*' failed for '&lt;HOST&gt;' - No matching peer found<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Recently noticed attacks:<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">[2011-06-21 17:53:11] NOTICE[7557] chan_sip.c: Registration from &#8216;&#8221;XXXXXXXXXX&#8221;&lt;sip:XXXXXXXXXX@123.123.123.123&gt;;&#8217; failed for &#8216;123.123.123.123&#8217; &#8211; Wrong Password<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Adding the following line will block these attempts:<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><samp><code>NOTICE.* .*: Registration from '\\\".*\\\".*' failed for '&lt;HOST&gt;' - Wrong password<\/code><\/samp><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Next edit <strong>\/etc\/fail2ban\/jail.conf<\/strong> 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 <strong>bantime<\/strong> 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 <strong>sendmail-whois<\/strong> action to send notifications to an appropriate address:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code><em>[asterisk-iptables]<\/em><\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code><em>enabled&nbsp; = true<br>\nfilter&nbsp;&nbsp; = asterisk<br>\naction&nbsp;&nbsp; = iptables-allports[name=ASTERISK, protocol=all]<br>\nsendmail-whois[name=ASTERISK, dest=root, sender=fail2ban@example.org]<br>\nlogpath&nbsp; = \/var\/log\/asterisk\/messages<br>\nmaxretry = 5<br>\nbantime = 259200<\/em><\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>note:<\/strong> 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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">note: if fail2ban still failed to identify login attempts, try the syslog logging way.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Don&#8217;t Ban Yourself<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">We don&#8217;t want to ban ourselves by accident. Edit <strong>\/etc\/fail2ban\/jail.conf<\/strong> and edit the <strong>ignoreip<\/strong> 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!<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Asterisk Logging<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">To change this format, open <strong>\/etc\/asterisk\/logger.conf<\/strong> 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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><samp>[general]<\/samp><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><samp>dateformat=%F %T<\/samp><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Then reload the logger module for Asterisk. At the command line, run the following command:<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> <samp>asterisk -rx &#8220;logger reload&#8221;<\/samp><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">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:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">In <strong>\/etc\/asterisk\/logger.conf<\/strong>, 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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code>syslog.local0 =&gt; notice<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Be sure to reload the logger module for Asterisk \u2014 check above for the command to do so. If you chose this option, you will also have to change the <strong>\/etc\/fail2ban\/jail.conf<\/strong> setting under the [asterisk-iptables] section for the <strong>logpath<\/strong> option to the following:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">logpath = \/var\/log\/messages<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Turning it On<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Now it is time to put fail2ban to work. There are a couple steps we need to do first.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong><br>Turn IPTABLES on<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">To start iptables, run the following as root:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>\/etc\/init.d\/iptables start<\/em><br>or<br>CentOS 7: <em>systemctl start firewalld<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">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.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong><br>Turn on Fail2Ban<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">To start Fail2Ban, run the following as root:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><em>\/etc\/init.d\/fail2ban start<\/em> | <em>systemctl start fail2ban<\/em><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Check It<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">If both started properly, issue the following command to view your iptables rules:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">iptables -L -v<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">You should see something like the following for the INPUT chain (you will see more if you have other Fail2Ban filters enabled):<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">Chain INPUT (policy ACCEPT 0 packets, 0 bytes)<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> pkts bytes target prot opt in out source destination<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> 2104K 414M fail2ban-ASTERISK all \u2014 any any anywhere anywhere<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">If you do not see something similar to that, then you have some troubleshooting to do; check out <strong>\/var\/log\/fail2ban.log<\/strong>.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">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 <a href=\"http:\/\/www.fail2ban.org\/wiki\/index.php\/Fail2ban_talk:Community_Portal\" target=\"_blank\" rel=\"nofollow noopener\">this page on the Fail2ban talk:Community Portal<\/a> and may wish to use the suggested fix:<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>You can also test the filter regex expressions using:<\/strong><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code>$fail2ban-regex \/var\/log\/asterisk\/full \/etc\/fail2ban\/filter.d\/asterisk.conf<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">or <code>$fail2ban-regex \/var\/log\/asterisk\/messages \/etc\/fail2ban\/filter.d\/asterisk.conf<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">and<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\"><code>$fail2ban-regex \/var\/log\/secure \/etc\/fail2ban\/filter.d\/sshd.conf<\/code><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">NOTE: The above rules test Asterisk and SSH rules against your log history.<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">To un-ban\/remove an IP evoke: <\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-family: arial,helvetica,sans-serif;\"><em>iptables -D INPUT -s 1.2.3.4 -j DROP<\/em><\/span><\/li>\n\n\n\n<li><span style=\"font-family: arial,helvetica,sans-serif;\"><em>iptables-save<\/em><\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">&#8212;&#8211;<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">A large portion of information was taken from bulak and dominic16y from voip-info:<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> http:\/\/www.voip-info.org\/wiki\/view\/Fail2Ban+%28with+iptables%29+And+Asterisk<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">For information on creating IPtable rules:<\/span><br><span style=\"font-family: arial,helvetica,sans-serif;\"> http:\/\/www.thegeekstuff.com\/2011\/02\/iptables-add-rule\/<\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">For more asterisk security tips, take a look at your <a href=\"https:\/\/www.nurango.ca\/blog\/asterisk-ip-pbx-security-tips\">asterisk Security Guide.<\/a><\/span><\/p>\n\n\n\n<p><span style=\"font-family: arial,helvetica,sans-serif;\">&#8212;&#8212;&#8212;&#8212;&#8212;<\/span><\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>About nurango<\/strong><\/span><\/h5>\n\n\n\n<p>nurango is a leader in <a title=\"secure business phone service\" href=\"https:\/\/www.nurango.ca\/phone-system\">secure business phone<\/a> systems with infrastructure privately run in Canada.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&nbsp; In a nutshell, fail2ban is a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4977,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[4],"tags":[6,7,5],"class_list":["post-4976","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guides","tag-asterisk","tag-fail2ban","tag-security"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/posts\/4976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/comments?post=4976"}],"version-history":[{"count":8,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/posts\/4976\/revisions"}],"predecessor-version":[{"id":10236,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/posts\/4976\/revisions\/10236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/media\/4977"}],"wp:attachment":[{"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/media?parent=4976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/categories?post=4976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nurango.ca\/blog\/wp-json\/wp\/v2\/tags?post=4976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}