{"id":1970,"date":"2017-08-29T17:48:15","date_gmt":"2017-08-29T22:48:15","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=1970"},"modified":"2017-08-30T10:10:32","modified_gmt":"2017-08-30T15:10:32","slug":"fail2ban-fails-to-ban-ssh-login-failures","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2017\/08\/fail2ban-fails-to-ban-ssh-login-failures\/","title":{"rendered":"fail2ban fails to ban SSH login failures"},"content":{"rendered":"<p>fail2ban is one of those magical programs that, in my experience, just works. I&#8217;ve inherited many systems with a working fail2ban configuration, and therefore I didn&#8217;t know much about configuring it or troubleshooting it.<\/p>\n<p><strong>Summary: by default, fail2ban on CentOS 7 does absolutely nothing!<\/strong><\/p>\n<p>One of the things that it is reported (falsely!) to do out-of-the-box is to block repeated SSH login failures. According to <a href=\"https:\/\/www.booleanworld.com\/protecting-ssh-fail2ban\/\">Protecting SSH with Fail2ban<\/a>:<\/p>\n<blockquote><p>Fail2ban should now protect SSH out of the box. If Fail2ban notices six failed login attempts in the last ten minutes, then it blocks that IP for ten minutes.<\/p><\/blockquote>\n<p>I wanted to test this, so I set up 2 virtual machines, a <em>victim<\/em> and an <em>attacker<\/em>.<\/p>\n<p>On the <em>victim<\/em> VM:<br \/>\n<code>[ariel]# sudo yum install epel-release<br \/>\n[ariel]# sudo yum install fail2ban<br \/>\n[ariel]# sudo systemctl start fail2ban<br \/>\n[ariel]# sudo tail -f \/var\/log\/fail2ban<\/code><\/p>\n<p>On the <em>attacker<\/em> VM:<br \/>\n<code>[caliban]# sudo yum install epel-release<br \/>\n[caliban]# sudo yum install sshpass<br \/>\n[caliban]# for i in `seq 1 100`; do sshpass -p 'TopSecret!' admin@ariel; done<\/code><\/p>\n<p>And then I waited. And waited. And waited.<\/p>\n<p>I confirmed that the defaults described matched what was in my <code>\/etc\/fail2ban\/jails.conf<\/code> (excerpted):<br \/>\n<code>bantime  = 600<br \/>\nfindtime  = 600<br \/>\nmaxretry = 5<\/code><\/p>\n<p>In my test, I definitely exceeded that: about 30 failed attempts in 5 minutes. The failures appear in <code>\/var\/log\/secure<\/code>, but nothing appears in <code>\/var\/log\/fail2ban.log<\/code>!<\/p>\n<p>From <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-protect-ssh-with-fail2ban-on-centos-7\">How To Protect SSH With Fail2Ban on CentOS 7<\/a> I found the <code>fail2ban-client status<\/code> command:<\/p>\n<p><code>[ariel]# fail2ban-client status<br \/>\nStatus<br \/>\n|- Number of jail:       0<br \/>\n`- Jail list:<\/code><\/p>\n<p>Zero jails! That&#8217;s definitely a problem.<\/p>\n<p>As mentioned in the above, I created a file, <code>\/etc\/fail2ban\/jail.local<\/code> containing the following:<br \/>\n<code>[sshd]<br \/>\nenabled = true<\/code><\/p>\n<p>New results:<br \/>\n<code>[ariel]# systemctl restart fail2ban<br \/>\n[ariel]# fail2ban-client status<br \/>\nStatus<br \/>\n|- Number of jail:      1<br \/>\n`- Jail list:   sshd<\/code><\/p>\n<p>That looks better! <code>\/var\/log\/fail2ban.log<\/code> now has new entries, and the <em>attacker<\/em> IP address has been banned! Just to confirm I tried to SSH to the machine from the attacker:<\/p>\n<p><code>[caliban]# ssh admin@ariel<br \/>\nssh_exchange_identification: Connection closed by remote host<\/code><\/p>\n<p>Great! Exactly what I expected to happen.<\/p>\n<p>When I look at the <code>\/etc\/fail2ban\/jails.conf<\/code>, I do not see <code>enabled = true<\/code> under the <code>[sshd]<\/code> section. In fact, part of that file explains that all jails are disabled by default:<\/p>\n<p><code># \"enabled\" enables the jails.<br \/>\n#  By default all jails are disabled, and it should stay this way.<br \/>\n#  Enable only relevant to your setup jails in your .local or jail.d\/*.conf<br \/>\n#<br \/>\n# true:  jail will be enabled and log files will get monitored for changes<br \/>\n# false: jail is not enabled<br \/>\nenabled = false<\/code><\/p>\n<p>On CentOS 7, fail2ban is configured to work with firewalld. My <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2017\/08\/using-fail2ban-with-iptables-instead-of-firewalld\/\">next post<\/a> describes using fail2ban with iptables on CentOS 7.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, fail2ban on CentOS 7 does nothing. This post describes enabling the pre-configured jail for sshd to block IP addresses with repeated failed login attempts.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[452,196,356,333],"class_list":["post-1970","post","type-post","status-publish","format-standard","hentry","category-security","tag-fail2ban","tag-linux","tag-security","tag-ssh"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":13,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"predecessor-version":[{"id":1992,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1970\/revisions\/1992"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}