{"id":266,"date":"2012-07-05T23:01:48","date_gmt":"2012-07-06T06:01:48","guid":{"rendered":"https:\/\/analogman.org\/?p=266"},"modified":"2012-07-05T23:01:48","modified_gmt":"2012-07-06T06:01:48","slug":"fail2ban-intrusion-attempts-against-asterisk","status":"publish","type":"post","link":"https:\/\/www.analogman.org\/?p=266","title":{"rendered":"Fail2ban &#8211; Intrusion attempts against Asterisk"},"content":{"rendered":"<p>You can find directions for doing this is several places and I debated whether or not I wanted to post this anywhere but in the process of walking through this\u00a0install for some interns and poking through a number of different log files I noted that the regular expressions being used were a little out of date for some of the current &#8220;attack&#8221; formatting that we were seeing.<\/p>\n<p>I made some changes to the regex portion that I think will be beneficial in finding and blocking more instances of intrusion attempts. ( as of May 2012)<\/p>\n<p>This is on a CentOS box running 5.8 with the tools needed to compile and run asterisk already installed.<\/p>\n<p>see:\u00a0<strong><em><a title=\"Fail2ban Org\" href=\"http:\/\/www.fail2ban.org\/wiki\/index.php\/Main_Page\">http:\/\/www.fail2ban.org\/wiki\/index.php\/Main_Page<\/a> <\/em><\/strong><\/p>\n<p>First install the extra packages for enterprise Linux 5 &#8211; i386 &#8220;epel&#8221;<\/p>\n<p>(of course you can also download a source file from fail2ban.org and go from there &#8211; but that is a different post)<\/p>\n<p><strong>[root]# rpm -Uvh http:\/\/dl.fedoraproject.org\/pub\/epel\/5\/i386\/epel-release-5-4.noarch.rpm<\/strong><\/p>\n<blockquote><p>Retrieving http:\/\/dl.fedoraproject.org\/pub\/epel\/5\/i386\/epel-release-5-4.noarch.rpm<br \/>\nwarning: \/var\/tmp\/rpm-xfer.U0EYpN: Header V3 DSA signature: NOKEY, key ID 217521f6<br \/>\nPreparing&#8230; ########################################### [100%]<br \/>\n1:epel-release ########################################### [100%]<br \/>\nnow check to make sure it has added itself to your repositories<br \/>\n[root]# yum repolist<br \/>\nLoaded plugins: fastestmirror, kmod<br \/>\nLoading mirror speeds from cached hostfile<br \/>\n* base: mirrors.cat.pdx.edu<br \/>\n* epel: linux.mirrors.es.net<br \/>\n* extras: centos.mirror.freedomvoice.com<br \/>\n* updates: mirrors.ecvps.com<br \/>\nepel | 3.4 kB 00:00<br \/>\nepel\/primary_db | 3.1 MB 00:00<br \/>\nrepo id repo name status<br \/>\naddons CentOS-5 &#8211; Addons enabled: 0<br \/>\nasterisk-current CentOS-5 &#8211; Asterisk &#8211; Current enabled: 562<br \/>\nbase CentOS-5 &#8211; Base enabled: 2,725<br \/>\ndigium-current CentOS-5 &#8211; Digium &#8211; Current enabled: 461<br \/>\nepel Extra Packages for Enterprise Linux 5 &#8211; i386 enabled: 5,728<br \/>\nextras CentOS-5 &#8211; Extras enabled: 282<br \/>\nupdates CentOS-5 &#8211; Updates enabled: 510<br \/>\nrepolist: 10,268<\/p><\/blockquote>\n<p>Now that we have modified the repositories we should be able to yum install fail2ban&#8230;lets see<br \/>\n(ok we needed python &#8211; which happens to already be installed on this machine &#8211; note that it is be updated with this installation)<\/p>\n<p><strong>[root]# yum install fail2ban<\/strong><\/p>\n<blockquote><p>Loaded plugins: fastestmirror, kmod<br \/>\nLoading mirror speeds from cached hostfile<br \/>\n* base: centos.mirrors.hoobly.com<br \/>\n* epel: mirror.pnl.gov<br \/>\n* extras: centos.mirror.freedomvoice.com<br \/>\n* updates: mirrors.ecvps.com<br \/>\nSetting up Install Process<br \/>\nResolving Dependencies<br \/>\n&#8211;&gt; Running transaction check<br \/>\n&#8212;&gt; Package fail2ban.noarch 0:0.8.4-29.el5 set to be updated<br \/>\n&#8211;&gt; Processing Dependency: shorewall for package: fail2ban<br \/>\n&#8211;&gt; Processing Dependency: python-inotify for package: fail2ban<br \/>\n&#8211;&gt; Running transaction checkcd<br \/>\n&#8212;&gt; Package python-inotify.noarch 0:0.9.1-1.el5 set to be updated<br \/>\n&#8211;&gt; Processing Dependency: python-ctypes for package: python-inotify<br \/>\n&#8212;&gt; Package shorewall.noarch 0:4.0.15-1.el5 set to be updated<br \/>\n&#8211;&gt; Processing Dependency: shorewall-perl = 4.0.15-1.el5 for package: shorewall<br \/>\n&#8211;&gt; Processing Dependency: shorewall-shell = 4.0.15-1.el5 for package: shorewall<br \/>\n&#8211;&gt; Processing Dependency: shorewall-common = 4.0.15-1.el5 for package: shorewall<br \/>\n&#8211;&gt; Running transaction check<br \/>\n&#8212;&gt; Package python-ctypes.i386 0:1.0.2-3.el5 set to be updated<br \/>\n&#8212;&gt; Package shorewall-common.noarch 0:4.0.15-1.el5 set to be updated<br \/>\n&#8212;&gt; Package shorewall-perl.noarch 0:4.0.15-1.el5 set to be updated<br \/>\n&#8212;&gt; Package shorewall-shell.noarch 0:4.0.15-1.el5 set to be updated<br \/>\n&#8211;&gt; Finished Dependency Resolution<\/p>\n<p>Dependencies Resolved<\/p>\n<p>============================================================================================<br \/>\nPackage Arch Version Repository Size<br \/>\n============================================================================================<br \/>\nInstalling:<br \/>\nfail2ban noarch 0.8.4-29.el5 epel 136 k<br \/>\nInstalling for dependencies:<br \/>\npython-ctypes i386 1.0.2-3.el5 base 207 k<br \/>\npython-inotify noarch 0.9.1-1.el5 epel 86 k<br \/>\nshorewall noarch 4.0.15-1.el5 epel 9.2 k<br \/>\nshorewall-common noarch 4.0.15-1.el5 epel 232 k<br \/>\nshorewall-perl noarch 4.0.15-1.el5 epel 137 k<br \/>\nshorewall-shell noarch 4.0.15-1.el5 epel 76 k<\/p>\n<p>Transaction Summary<br \/>\n============================================================================================<br \/>\nInstall 7 Package(s)<br \/>\nUpgrade 0 Package(s)<\/p>\n<p>Total download size: 883 k<\/p><\/blockquote>\n<p><strong>Is this ok [y\/N]: y<\/strong><\/p>\n<blockquote><p>Downloading Packages:<br \/>\n(1\/7): shorewall-4.0.15-1.el5.noarch.rpm | 9.2 kB 00:00<br \/>\n(2\/7): shorewall-shell-4.0.15-1.el5.noarch.rpm | 76 kB 00:00<br \/>\n(3\/7): python-inotify-0.9.1-1.el5.noarch.rpm | 86 kB 00:00<br \/>\n(4\/7): fail2ban-0.8.4-29.el5.noarch.rpm | 136 kB 00:00<br \/>\n(5\/7): shorewall-perl-4.0.15-1.el5.noarch.rpm | 137 kB 00:00<br \/>\n(6\/7): python-ctypes-1.0.2-3.el5.i386.rpm | 207 kB 00:00<br \/>\n(7\/7): shorewall-common-4.0.15-1.el5.noarch.rpm | 232 kB 00:00<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nTotal 656 kB\/s | 883 kB 00:01<br \/>\nwarning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID 217521f6<br \/>\nepel\/gpgkey | 1.7 kB 00:00<br \/>\nImporting GPG key 0x217521F6 &#8220;Fedora EPEL &lt;epel@fedoraproject.org&gt;&#8221; from \/etc\/pki\/rpm-gpg\/RPM-GPG-KEY-EPEL<\/p><\/blockquote>\n<p><strong>Is this ok [y\/N]: y<\/strong><\/p>\n<blockquote><p>Running rpm_check_debug<br \/>\nRunning Transaction Test<br \/>\nFinished Transaction Test<br \/>\nTransaction Test Succeeded<br \/>\nRunning Transaction<br \/>\nInstalling : shorewall-common 1\/7<br \/>\nInstalling : python-ctypes 2\/7<br \/>\nInstalling : python-inotify 3\/7<br \/>\nInstalling : shorewall-shell 4\/7<br \/>\nInstalling : shorewall-perl 5\/7<br \/>\nInstalling : shorewall 6\/7<br \/>\nInstalling : fail2ban 7\/7<\/p>\n<p>Installed:<br \/>\nfail2ban.noarch 0:0.8.4-29.el5<\/p>\n<p>Dependency Installed:<br \/>\npython-ctypes.i386 0:1.0.2-3.el5 python-inotify.noarch 0:0.9.1-1.el5 shorewall.noarch 0:4.0.15-1.el5 shorewall-common.noarch 0:4.0.15-1.el5<br \/>\nshorewall-perl.noarch 0:4.0.15-1.el5 shorewall-shell.noarch 0:4.0.15-1.el5<\/p>\n<p>Complete!<\/p><\/blockquote>\n<p>[root]#<\/p>\n<p>Take a look at these urls for slightly different approaches:<\/p>\n<p><a href=\"http:\/\/alnazmin.blogspot.com\/2011\/05\/install-fail2ban-on-centos-55.html\">http:\/\/alnazmin.blogspot.com\/2011\/05\/install-fail2ban-on-centos-55.html<\/a><\/p>\n<p><a href=\"http:\/\/www.markinthedark.nl\/news\/ubuntu-linux-unix\/70-configure-fail2ban-for-asterisk-centos-5.html\">http:\/\/www.markinthedark.nl\/news\/ubuntu-linux-unix\/70-configure-fail2ban-for-asterisk-centos-5.html<\/a><\/p>\n<p><a href=\"http:\/\/asbadr.wordpress.com\/2012\/04\/23\/fail2ban-for-asterisk-on-centos-and-gentoo\/\">http:\/\/asbadr.wordpress.com\/2012\/04\/23\/fail2ban-for-asterisk-on-centos-and-gentoo\/<\/a><\/p>\n<p>I edited the \/etc\/fail2ban\/filter.d\/asterisk.conf file in order to reflect some localizations as well as some additions to the\u00a0regular expressions used when fail2ban is looking at the log files to match indications of an &#8216;attack&#8217;<\/p>\n<p><strong>[root]# vi \/etc\/fail2ban\/filter.d\/asterisk.conf<br \/>\n<\/strong><br \/>\n# Fail2Ban configuration file<br \/>\n#<br \/>\n#<br \/>\n# $Revision: 250 $<br \/>\n#<\/p>\n<p>[INCLUDES]<\/p>\n<p># Read common prefixes. If any customizations available &#8212; read them from<br \/>\n# common.local<br \/>\n#before = common.conf<\/p>\n<p>[Definition]<\/p>\n<p>#_daemon = asterisk<\/p>\n<p># Option: failregex<br \/>\n# Notes.: regex to match the password failures messages in the logfile. The<br \/>\n# host must be matched by a group named &#8220;host&#8221;. The tag &#8220;&lt;HOST&gt;&#8221; can<br \/>\n# be used for standard IP\/hostname matching and is only an alias for<br \/>\n# (?:::f{4,6}:)?(?P&lt;host&gt;\\S+)<br \/>\n# Values: TEXT<br \/>\n#<\/p>\n<p>failregex = NOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Wrong password<br \/>\nNOTICE.* .*: Registration from &#8216;\\&#8221;.*\\&#8221;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Wrong password<br \/>\nNOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; No matching peer found<br \/>\nNOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;\\:.*&#8217; &#8211; No matching peer found<br \/>\nNOTICE.* .*: Registration from &#8216;\\&#8221;.*\\&#8221;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; No matching peer found<br \/>\nNOTICE.* .*: Registration from &#8216;\\&#8221;.*\\&#8221;.*&#8217; failed for &#8216;&lt;HOST&gt;\\:.*&#8217; &#8211; No matching peer found<br \/>\nNOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Username\/auth name mismatch<br \/>\nNOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Device does not match ACL<br \/>\nNOTICE.* .*: Registration from &#8216;.*&#8217; failed for &#8216;&lt;HOST&gt;&#8217; &#8211; Peer is not supposed to register<br \/>\nNOTICE.* &lt;HOST&gt; failed to authenticate as &#8216;.*&#8217;$<br \/>\nNOTICE.* .*: No registration for peer &#8216;.*&#8217; \\(from &lt;HOST&gt;\\)<br \/>\nNOTICE.* .*: Host &lt;HOST&gt; failed MD5 authentication for &#8216;.*&#8217; (.*)<br \/>\nNOTICE.* .*: Failed to authenticate user .*@&lt;HOST&gt;.*<br \/>\n# Option: ignoreregex<br \/>\n# Notes.: regex to ignore. If this regex matches, the line is ignored.<br \/>\n# Values: TEXT<br \/>\n#<br \/>\nignoreregex =<\/p>\n<p>Then editing \/etc\/fail2ban\/jail.conf to enter appropriate email addresses, bantimes, etc.<br \/>\n<strong>[root]# vi \/etc\/fail2ban\/jail.conf<\/strong><\/p>\n<blockquote><p>[asterisk-iptables]<\/p>\n<p>enabled = true<br \/>\nfilter = asterisk<br \/>\naction = iptables-allports[name=ASTERISK, protocol=all]<br \/>\nsendmail-whois[name=ASTERISK, dest=someemail@2bridgestech.com, sender=fail2ban@ourcustomer.org]<br \/>\nlogpath = \/var\/log\/asterisk\/full<br \/>\nmaxretry = 5<br \/>\nbantime = 259200<\/p><\/blockquote>\n<p>Don&#8217;t forget to check the email address for reporting ssh notices as well&#8230;<\/p>\n<p>Now when I start fail2ban I get :<\/p>\n<p><strong>[root]# service fail2ban start<\/strong><\/p>\n<p>Starting fail2ban: [ OK ]<\/p>\n<p>Then I want to take a quick look at iptables to see if fail2ban is showing up there.<\/p>\n<p><strong>[root@localhost filter.d]# iptables -L -v<br \/>\n<\/strong><\/p>\n<blockquote><p>Chain INPUT (policy ACCEPT 438 packets, 33411 bytes)<br \/>\npkts bytes target prot opt in out source destination<br \/>\n438 33411 fail2ban-ASTERISK all &#8212; any any anywhere anywhere<\/p>\n<p>Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)<br \/>\npkts bytes target prot opt in out source destination<\/p>\n<p>Chain OUTPUT (policy ACCEPT 217 packets, 24088 bytes)<br \/>\npkts bytes target prot opt in out source destination<\/p>\n<p>Chain fail2ban-ASTERISK (1 references)<br \/>\npkts bytes target prot opt in out source destination<br \/>\n438 33411 RETURN all &#8212; any any anywhere anywhere<\/p>\n<p>Chain fail2ban-SSH (0 references)<br \/>\npkts bytes target prot opt in out source destination<br \/>\n0 0 RETURN all &#8212; any any anywhere anywhere<\/p><\/blockquote>\n<p>[root]#<\/p>\n<p>Now you can test the setup by pushing the log file against the filter we defined<\/p>\n<p><strong>[root]# fail2ban-regex \/var\/log\/asterisk\/full \/etc\/fail2ban\/filter.d\/asterisk.conf<\/strong><\/p>\n<p>If you have a huge log file this could take quite a while as well as max out the cpu so be careful. You might want to fine some &#8216;fail to authenticate&#8217; entries in a log file and copy them into a new file to test against that much smaller file&#8230;<\/p>\n<p>Also to take a better look at the configuration files without comments try this command (replace the jail.conf with the file you want to look at) &#8211;<\/p>\n<p>The &#8220;#&#8221; can be changed to whatever comment char you see in the file; this will also remove \u00a0blank lines from the file<\/p>\n<p><strong>[root]# grep -v &#8216;^$&#8217; jail.conf | grep -v &#8220;#&#8221; | more<\/strong><\/p>\n<p>Hope this helps someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can find directions for doing this is several places and I debated whether or not I wanted to post this anywhere but in the process of walking through this\u00a0install for some interns and poking through a number of different log files I noted that the regular expressions being used were a little out of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[27,8,10],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-linux","category-mods","category-training"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.analogman.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=266"}],"version-history":[{"count":30,"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.analogman.org\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.analogman.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.analogman.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.analogman.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}