{"id":10,"date":"2015-09-03T23:01:58","date_gmt":"2015-09-03T23:01:58","guid":{"rendered":"http:\/\/www.ilovesecure.com\/?p=10"},"modified":"2018-10-05T18:52:16","modified_gmt":"2018-10-05T18:52:16","slug":"protect-ssh-login-of-your-linux-server-with-fail2ban","status":"publish","type":"post","link":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/","title":{"rendered":"Protect SSH login of your linux server with fail2ban (Centos 6)"},"content":{"rendered":"<p>If you have a public Linux server on internet, you can see in your log (\/var\/log\/secure) there are tons of people from everywhere trying to login in to your server to get control of your machine. Massive brutal attacks is a common practice. Even if they are not a big issue they are anyway quite annoying. If the attack rate is really high, it can start to be a performance issue.<br \/>\nLet&#8217;s try to improve the security of you server:<\/p>\n<ol>\n<li>Keep your server update, in particular keep updated network software, accessible services (sshd, smtpd, snmp, etc) and related security libraries \u00a0(OpenSSL etc)<\/li>\n<li>remove password based ssh login: There is a dedicated post about <a href=\"http:\/\/www.ilovesecure.com\/index.php\/2015\/11\/03\/secure-ssh-login\/\">Secure SSH Linux Login<\/a><\/li>\n<li>remove root login: look at the same post about <a href=\"http:\/\/www.ilovesecure.com\/index.php\/2015\/11\/03\/secure-ssh-login\/\">Secure SSH Linux Login<\/a><\/li>\n<li>install fail2ban to block brutal force attacks<\/li>\n<\/ol>\n<p><strong>Install Fail2ban in Centos\/RHEL<\/strong><\/p>\n<ol>\n<li><strong>What is Fail2ban and how it works<\/strong><br \/>\nfail2ban is an application monitoring your logs looking per specific anomalies. For example too many failed login from the same IP, meaning somebody not authorized is trying to login into your server. Once a pattern is discovered (example: too many failed login), and action is taken. The typical action is block the IP attacking your server using iptables for a defined period (ip are often dynamic, so no sense to block it forever) but any type of action can be taken by Fail2ban (send email or sms, launch a script, restarting an application, etc). A common use is the ssh login protection, blocking IP with many failed login, but Fail2Ban can manage many different application log (smtp, http, etc)<\/li>\n<li><strong>Enable iptables<\/strong><br \/>\nnormally in my servers iptables (the Linux firewall) is disabled, because I have external devices to filter and control traffic. If the security control is inside the same server, in case of any break into a server they can change the security rules and access to something else, if the security is managed outside changing the rules is much more complex. Anyway to make Fail2ban effective, we need the support of iptables. Iptables is a critical component, so pay attention to modify it, you can lock yourself out. Let&#8217;go:<br \/>\nCheck if iptable is running:<\/p>\n<div class=\"linux-command\">[~]# service iptables status<br \/>\nTable: filter<br \/>\nChain INPUT (policy ACCEPT)<br \/>\nnum\u00a0 target\u00a0\u00a0\u00a0\u00a0 prot opt source\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 destination<br \/>\n&#8230;&#8230;.<\/div>\n<p>In case is not running, start Iptables<\/p>\n<div class=\"linux-command\">[~]# service iptables start<\/div>\n<p>and check if everything is still running: in case you are lock out, just reboot the server (you need ILO\/remote manager or a physical access to server).<br \/>\nIn case it is runing without any problem, make sure it will start on next boot<\/p>\n<div class=\"linux-command\">[~]# chkconfig iptables on<\/div>\n<\/li>\n<li><strong>Install Fail2Ban<\/strong><br \/>\nfirst you need to be sure in your linux box Epel repository is registered:<\/p>\n<div class=\"linux-command\">[~]# rpm -Uvh http:\/\/dl.fedoraproject.org\/pub\/epel\/6\/x86_64\/epel-release-6-8.noarch.rpm<\/div>\n<p>Epel is a repository for application supported directly by Fedora project and indirectly by RedHat. We can consider it reliable (when you install an application from this repo you are quite secure it is the original one, not hacked or compromised).\u00a0install fail2ban with yum<\/p>\n<div class=\"linux-command\">[~]# yum install fail2ban<\/div>\n<p>in case of any error during installation or running the application, maybe it can be due some missing dependencies, so please install ipset with<\/p>\n<div class=\"linux-command\">[~]# yum install ipset<\/div>\n<\/li>\n<li><strong>Configure Fail2ban<\/strong><br \/>\nYour custom configuration will be located in a local configuation file. So copy the original to create your own copy<\/p>\n<div class=\"linux-command\">[~]# cp \/etc\/fail2ban\/jail.conf \/etc\/fail2ban\/jail.local<\/div>\n<p>Now setup some details<\/p>\n<div class=\"linux-command\">[~]# vi \/etc\/fail2ban\/jail.local<\/div>\n<p>in particular how long you want an ip to be banned, the list of your known ip so fail2ban will never ban them, default frequency of errors (in case you can redefine case by case)<\/p>\n<div class=\"linux-command\">[DEFAULT]<br \/>\n# &#8220;ignoreip&#8221; can be an IP address, a CIDR mask or a DNS host. Fail2ban will not<br \/>\n# ban a host which matches an address in this list. Several addresses can be<br \/>\n# defined using space separator.<br \/>\nignoreip = 127.0.0.1<br \/>\n# &#8220;bantime&#8221; is the number of seconds that a host is banned.<br \/>\nbantime = 21600<br \/>\n# A host is banned if it has generated &#8220;maxretry&#8221; during the last &#8220;findtime&#8221;<br \/>\n# seconds.<br \/>\nfindtime = 1800<br \/>\n# &#8220;maxretry&#8221; is the number of failures before a host get banned.<br \/>\nmaxretry = 3<\/div>\n<p>Some detail:<br \/>\n<strong>ignoreip<\/strong>: contain the list of your known ip, separated with a space, you can use single ip or range like 192.168.1.0\/24.<br \/>\n<strong>bantime<\/strong>: the time an IP is banned. We suggest to take 21600 (6 hours), maybe you can chenge it to 43200 (12 hours)<br \/>\n<strong>findtime\/maxretry<\/strong>: if the same IP makes maxretry times in findtime seconds a failed login, it is banned. We suggest 3 times in 1800 seconds<br \/>\nRemember you can repeat this settings inside each control to customize these values.<br \/>\nNow enable sshd check setting &#8220;enable&#8221; in your config file<\/p>\n<div class=\"linux-command\">\n<p>[ssh-ddos]<\/p>\n<p>enabled\u00a0 = true<br \/>\nfilter\u00a0\u00a0 = sshd<br \/>\naction\u00a0\u00a0 = iptables[name=SSH, port=ssh, protocol=tcp]<br \/>\nsendmail-whois[name=SSH, dest=xxxxx@xxxx.xxx, sender=yyyyy@yyyyy.yyy]<br \/>\nlogpath\u00a0 = \/var\/log\/secure<br \/>\nmaxretry = 3<\/p>\n<\/div>\n<p>The name of the jail (ssh-ddos) can be any. If you want to receive an email when an IP is locked, remember to set your email address as &#8220;dest&#8221; and put a sender address (always use an existing address as sender). If you do not want receive any email, just comment out this line.<br \/>\nThen simply start the service<\/p>\n<div class=\"linux-command\">[~]# service fail2ban restart<\/div>\n<p>You will find in your messages log files all log related to fail2ban, IPs banned and IPs unbanned<\/p>\n<div class=\"linux-command\">[~]# grep fail2ban \/var\/log\/messages<\/div>\n<\/li>\n<li><strong>TEST IT!!!<\/strong><br \/>\nMany times I realized fail2ban was not blocking what It was supposed to block due to wrong configuration, wrong log format, wrong log position and so on. So using a server with a different ip from yours, try to login via ssh with wrong user\/pwd and check if your ip is blocked. It take about a minute to block your IP but you can see what is happeneing inside the message log.<br \/>\nIf you do not see any particular problem, make your fail2ban persistent in case of reboot<\/p>\n<div class=\"linux-command\">[~]# chkconfig fail2ban on<\/div>\n<\/li>\n<li><strong>Maintenance<\/strong><br \/>\nRemember in case you restart iptables, you need to restart fail2ban immediately after. Syncerely I suggest to change the script to start\/restart Iptables (\/etc\/init.d\/iptables) to run fail2ban immediatel after iptables start.<\/li>\n<\/ol>\n<p><strong>Side\u00a0effects<\/strong><\/p>\n<p>I used this software so many time in the past, in so many different environments, so I can tell you some strange things can happen due to particular environments.<\/p>\n<ol>\n<li><strong>Massive locking:<\/strong> an example: we used this tool to protect email serves, but if someone changes his password without updating all his clients, the clients looks like an attacker continuously trying to login with the previous wrong login settings. If it is done from the office, the ip of the office is banned so all the people in the\u00a0 office can not access anymore. So remember to set the whitelist in &#8220;ignoreIp&#8221;, only one user can close all the office if the office ip is not whitelisted. You can have the same effect with other shared ip you do not control. If someone, inside a large company, with all people surfing internet with the same IP, tries to access in a wrong way to a service in your server protected by fail2ban and the IP is banned, this means nobody inside this company will be able to access to that specific service on your server.<\/li>\n<li><strong>Private vs public ip<\/strong>: before setup fail2ban pay attention of your network configuration. If your server has a private ip inside a network receiving public traffic through a device (firewall, proxy, whatever) take a look to logs before starting fail2ban. If the device in front of your server is a layer 7 device, probably you are logging the private ip of the device, not the public ip of the clients, so in case of any block, you are blocking the private ip of your network device and so all the traffic coming to your server, not only the bad ip. It can happens typically using fail2ban to block bad http traffic. Take a look to the logs to be sure you are logging real public ip, not the one of you proxy or firewall<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If you have a public Linux server on internet, you can see in your log (\/var\/log\/secure) there are tons of people from everywhere trying to login in to your server to get control of your machine. Massive brutal attacks is a common practice. Even if they are not a big issue they are anyway quite [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,2],"tags":[6,4,5],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-linux","category-server","tag-fail2ban","tag-linux-login","tag-ssh","no-thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure\" \/>\n<meta property=\"og:description\" content=\"If you have a public Linux server on internet, you can see in your log (\/var\/log\/secure) there are tons of people from everywhere trying to login in to your server to get control of your machine. Massive brutal attacks is a common practice. Even if they are not a big issue they are anyway quite [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/\" \/>\n<meta property=\"og:site_name\" content=\"I Love Secure\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-03T23:01:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-10-05T18:52:16+00:00\" \/>\n<meta name=\"author\" content=\"secureadm\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"secureadm\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/\",\"url\":\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/\",\"name\":\"Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure\",\"isPartOf\":{\"@id\":\"https:\/\/www.ilovesecure.com\/#website\"},\"datePublished\":\"2015-09-03T23:01:58+00:00\",\"dateModified\":\"2018-10-05T18:52:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.ilovesecure.com\/#\/schema\/person\/4f0f645b7843e70f478415155f2b0b07\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.ilovesecure.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Protect SSH login of your linux server with fail2ban (Centos 6)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.ilovesecure.com\/#website\",\"url\":\"https:\/\/www.ilovesecure.com\/\",\"name\":\"I Love Secure\",\"description\":\"Security step by step\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.ilovesecure.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.ilovesecure.com\/#\/schema\/person\/4f0f645b7843e70f478415155f2b0b07\",\"name\":\"secureadm\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.ilovesecure.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7ae680ceca3544c8a37149e6254db758ee7c1ecefd5c6ad34aa972b9c5bfd1d0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7ae680ceca3544c8a37149e6254db758ee7c1ecefd5c6ad34aa972b9c5bfd1d0?s=96&d=mm&r=g\",\"caption\":\"secureadm\"},\"url\":\"https:\/\/www.ilovesecure.com\/index.php\/author\/secureadm\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/","og_locale":"en_US","og_type":"article","og_title":"Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure","og_description":"If you have a public Linux server on internet, you can see in your log (\/var\/log\/secure) there are tons of people from everywhere trying to login in to your server to get control of your machine. Massive brutal attacks is a common practice. Even if they are not a big issue they are anyway quite [&hellip;]","og_url":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/","og_site_name":"I Love Secure","article_published_time":"2015-09-03T23:01:58+00:00","article_modified_time":"2018-10-05T18:52:16+00:00","author":"secureadm","twitter_card":"summary_large_image","twitter_misc":{"Written by":"secureadm","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/","url":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/","name":"Protect SSH login of your linux server with fail2ban (Centos 6) - I Love Secure","isPartOf":{"@id":"https:\/\/www.ilovesecure.com\/#website"},"datePublished":"2015-09-03T23:01:58+00:00","dateModified":"2018-10-05T18:52:16+00:00","author":{"@id":"https:\/\/www.ilovesecure.com\/#\/schema\/person\/4f0f645b7843e70f478415155f2b0b07"},"breadcrumb":{"@id":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ilovesecure.com\/index.php\/2015\/09\/03\/protect-ssh-login-of-your-linux-server-with-fail2ban\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ilovesecure.com\/"},{"@type":"ListItem","position":2,"name":"Protect SSH login of your linux server with fail2ban (Centos 6)"}]},{"@type":"WebSite","@id":"https:\/\/www.ilovesecure.com\/#website","url":"https:\/\/www.ilovesecure.com\/","name":"I Love Secure","description":"Security step by step","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ilovesecure.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.ilovesecure.com\/#\/schema\/person\/4f0f645b7843e70f478415155f2b0b07","name":"secureadm","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ilovesecure.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7ae680ceca3544c8a37149e6254db758ee7c1ecefd5c6ad34aa972b9c5bfd1d0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7ae680ceca3544c8a37149e6254db758ee7c1ecefd5c6ad34aa972b9c5bfd1d0?s=96&d=mm&r=g","caption":"secureadm"},"url":"https:\/\/www.ilovesecure.com\/index.php\/author\/secureadm\/"}]}},"_links":{"self":[{"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":44,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ilovesecure.com\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}