{"id":351,"date":"2019-05-14T14:29:45","date_gmt":"2019-05-14T14:29:45","guid":{"rendered":"https:\/\/blog.iabsolute.com\/?p=351"},"modified":"2019-05-14T14:29:45","modified_gmt":"2019-05-14T21:29:45","slug":"secure-ssh-with-fail2ban","status":"publish","type":"post","link":"https:\/\/blog.iabsolute.com\/?p=351","title":{"rendered":"Secure SSH with Fail2Ban"},"content":{"rendered":"\n<p>Remote managing a server is important but I believe securing it is just as important.<br>\nWould you like to type \u201clast\u201d and just relize someone has just login into your server from a far country?<br>\nWell the solution is here!<br>\nWe will be installing fail2ban, witch is capable of monitoring not just SSH but many other daemons.<br>\nIt is quite cool, it send you an email after X attempts and include that bad IP into iptables for X amount of time.<br>\n<strong><br>\nInstalling in Debian:<\/strong><br>\n<code># apt-get install fail2ban<\/code><\/p>\n\n\n\n<p><strong><br>\nInstalling in RedHat,CentOS,Fedora:<br>\n# wget <a href=\"http:\/\/downloads.sourceforge.net\/project\/fail2ban\/fail2ban-stable\/fail2ban-0.8.4\/fail2ban-0.8.4.tar.bz2?use_mirror=ufpr\">http:\/\/downloads.sourceforge.net\/project\/fail2ban\/fail2ban-stable\/fail2ban-0.8.4\/fail2ban-0.8.4.tar.bz2?use_mirror=ufpr<\/a><\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">or you can download <a href=\"http:\/\/blog.vnsj.com\/storage\/download\/linux\/fail2ban\/ENT_v5\/fail2ban-0.8.4.tar.zip\">fail2ban<\/a> from my home server<\/h1>\n\n\n\n<p># tar -xjvf fail2ban-0.8.4.tar.bz2<br>\n# cd fail2ban-0.8.4<br>\n# python setup.py install&nbsp;&nbsp;&nbsp; {if not working then \u201cyum install python-devel\u201d}<br>\nAutostart in RedHat,CentOS,Fedora<br>\n# cp files\/redhat-initd \/etc\/init.d\/fail2ban<br>\n# chkconfig \u2013add fail2ban<br>\n# chkconfig fail2ban on<br>\n# service fail2ban start<\/p>\n\n\n\n<p><strong>Configuring Fail2ban:<\/strong><br>\nFail2ban is automatically configured for the most part. However, little items need to be tweaked. \/etc\/fail2ban\/fail2ban.conf is responsible for general settings for fail2ban, such as what log to append to. More specific settings can be changed in \/etc\/fail2ban\/jail.conf. However, it\u2019s recommended that this file not be directly changed. Instead, make a copy to jail.local. The local file with override the .conf one.<br>\n<code># cp \/etc\/fail2ban\/jail.conf \/etc\/fail2ban\/jail.local<\/code><br>\nFirst, under [DEFAULT] find ignoreip. It\u2019s always important for you to have a way in! These are IPs are fail2ban will ignore \u2013 IPs listed here can always have invalid login. These need to be space separated.<br>\nCheck also the bantime, maxrety and other settings. I believe the bantime of only 10min ( 600 sec) is not enough to handle an attack,<br>\nso I raised it to 86400 (24 hours).Also adjust the logfiles path and names to your system.<br>\n<code><br>\n#vim \/etc\/fail2ban\/jail.local<\/code><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>[DEFAULT]<\/p><p># \u201cignoreip\u201d can be an IP address, a CIDR mask or a DNS host<br>\nignoreip = 127.0.0.1 172.31.0.0\/24 10.10.0.0\/24 192.168.0.0\/24<br>\nbantime = 86400<br>\nmaxretry = 5<\/p><\/blockquote>\n\n\n<p>[ssh-iptables]<\/p>\n\n\n\n<p>\nenabled = true<br>\nfilter = sshd<br>\naction = iptables[name=SSH, port=ssh, protocol=tcp]<br>\nsendmail-whois[name=SSH, dest=you@mail.com, sender=fail2ban@mail.com]<br>\nlogpath = \/var\/log\/auth.log<br>\nmaxretry = 5\n<\/p>\n\n\n\n<p>logpath=\/var\/log\/secure (for RedHat,CentOS,Fedora)<\/p>\n\n\n\n<p>Then restart the service:<br>\n<code># \/etc\/init.d\/fail2ban restart<\/code><br>\nor RedHat<br>\n# service fail2ban restart<\/p>\n\n\n\n<p>And check your iptables:<br>\n<code># iptables -L<\/code><\/p>\n\n\n\n<p>If you want to unblock someone just do:<br>\n<code># iptables -D fail2ban-ssh 1<br>\n<\/code><br>\nShow failed SSH logins by date:<br>\n# cat \/var\/log\/secure | grep \u2018Failed password\u2019 |&nbsp; sort | uniq -c<\/p>\n\n\n\n<p>There is also a cool nagios plugin<\/p>\n\n\n\n<p>More on Fail2Ban<\/p>\n\n\n\n<p><strong>Appendix, Install email server: smail, sendmail:<\/strong><br>\n#apt-get install smail<br>\nTo configure:<br>\n#\/usr\/sbin\/smailconfig<br>\nTest it:<br>\n#\/usr\/sbin\/smailtest<\/p>\n\n\n\n<p>Other Tips<br>\nHELP:<br>\n1.) stop the Service<br>\n\/etc\/init.d\/fail2ban stop<br>\n2.) delete the socket if avalible<br>\nrm \/tmp\/fail2ban.sock<br>\n3.) start the Service<br>\n\/etc\/init.d\/fail2ban start<br>\n4.) check if fail2ban is working<br>\nfail2ban-client ping<br>\nAnswer should be \u201cpong\u201d<br>\n5.) if the answer is not \u201cpong\u201d run away or&nbsp; CRY FOR HELP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Remote managing a server is important but I believe securing it is just as important. Would you like to type \u201clast\u201d and just relize someone has just login into your server from a far country? Well the solution is here! &hellip; <a href=\"https:\/\/blog.iabsolute.com\/?p=351\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-fail2ban"],"_links":{"self":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=351"}],"version-history":[{"count":1,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/351\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}