{"componentChunkName":"component---src-templates-post-template-js","path":"/fail2ban-ec2-ssh","webpackCompilationHash":"9f9b08718447f3d4e396","result":{"data":{"markdownRemark":{"id":"fdba9a02-3f75-5d1e-8900-f060c2cd2717","html":"<p>If you are experiencing a brute-force ssh attack (people or robots trying different combinations of username and password to log into remote servers), you probably want to implement a fail2ban strategy to prevent them to keep trying.</p>\n<p>Before writing about fail2ban, if you are using AWS, I will highly suggest checking your Network ACLs. If possible, whitelist the IPs that can use the ssh port in your server. Sadly we have dynamic IP’s here in Argentina and it’s going to be really tedious to go and whitelist my IP every time it changes, especially when you could need to access multiple servers a day.</p>\n<h2 id=\"fail2ban\"><a href=\"#fail2ban\" aria-label=\"fail2ban permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Fail2ban</h2>\n<p>I won’t go too deep on all that you can do with this software, but if you are interested in read more about it in their <a href=\"https://github.com/fail2ban/fail2ban/wiki\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">github wiki</a></p>\n<p>We will use it for blocking IPs to keep trying to access via ssh after N number of attempts.</p>\n<p>First you need to install the package, my server uses ubuntu so we run:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">sudo apt-get install fail2ban</code></pre></div>\n<p>Now to configure you need to create a <code class=\"language-text\">jail.local</code> file in the folder at <code class=\"language-text\">/etc/fail2ban/</code>. You can read the default configuration from  <code class=\"language-text\">/etc/fail2ban/jail.conf</code>. Let’s create that file:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">sudo touch /etc/fail2ban/jail.local</code></pre></div>\n<p>Since we are only going to use the ssh module, we just need these lines in the file:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">[sshd]\nmaxretry = 5\nbantime=10800</code></pre></div>\n<p>Feel free to change those values as it better fits your usage.</p>\n<p>Now we just need to restart the fail2ban service and we are ready to go:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">sudo systemctl restart fail2ban.service</code></pre></div>\n<p>Now your server will reject connections via ssh if they fail to access more than 5 times in a row. Great!</p>\n<p>If you want to check the status of fail2ban, check how many fail attempts happened and how many IPs are banned at some point in time you can run <code class=\"language-text\">fail2ban-client status sshd</code> and it will display something like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Status for the jail: sshd\\\n|- Filter\\\n|  |- Currently failed:\t1\\\n|  |- Total failed:\t75\\\n|  `- File list:\t/var/log/auth.log\\\n`- Actions\\\n|- Currently banned:\t2\\\n|- Total banned:\t6\\\n`- Banned IP list:\t161.35.80.11 186.159.10.210</code></pre></div>\n<h2 id=\"troubleshoot\"><a href=\"#troubleshoot\" aria-label=\"troubleshoot permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Troubleshoot</h2>\n<p>fail2ban adds rules in iptables program from your linux distro. I had issues while trying to execute iptables —list, to check how those rules where applied after configuring fail2ban in one of my staging servers:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">modprobe: ERROR: could not insert &#39;ip_tables&#39;: Cannot allocate memoryiptables v1.4.21: can&#39;t initialize iptables table `filter&#39;:\nTable does not exist (do you need to insmod?)\nPerhaps iptables or your kernel needs to be upgraded.</code></pre></div>\n<p>In a previous post I commented about linux images taking too much space in your inodes disk space (<code class=\"language-text\">df -i</code> returning 99%+ in <code class=\"language-text\">/dev/xvda1</code>). The solution, if applied incorrectly like I did, may corrupt the execution of iptables commands. In short, try to don’t delete the image that you are currently using. To check which version to keep check the output of <code class=\"language-text\">uname -r</code> in your server terminal.<br>\n<br>\nThankfully, we can easily re-install that linux image by using that output too:<br>\n<code class=\"language-text\">sudo apt-get install linux-image-$(uname -r)</code></p>\n<p>That should reinstall the iptables program. I read that some people needed to run <code class=\"language-text\">sudo apt-get full-upgrade</code> and then reinstall the linux image.</p>","fields":{"slug":"fail2ban-ec2-ssh","tagSlugs":["/tag/fail-2-ban/","/tag/ec-2/","/tag/aws/","/tag/ubuntu/","/tag/brute-force-ssh/","/tag/ssh/","/tag/attack/","/tag/devops/","/tag/security/"]},"frontmatter":{"date":"2021-07-14T22:36:51.249Z","description":"If you are experiencing a brute-force ssh attack (people or robots trying different combinations of username and password to log into remote servers), you probably want to implement a fail2ban strategy to prevent them to keep trying.\nHere is a really quick and easy guide on how to get it ready in your ubuntu AWS ec2 instance.","tags":["fail2ban","ec2","aws","ubuntu","brute-force ssh","ssh","attack","devops","security"],"title":"How to prevent brute-force ssh attacks with fail2ban","socialImage":null}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"fail2ban-ec2-ssh"}}}