{"id":128,"date":"2021-12-28T20:33:00","date_gmt":"2021-12-28T20:33:00","guid":{"rendered":"https:\/\/mgcspace.com\/content\/?p=128"},"modified":"2022-11-23T16:49:00","modified_gmt":"2022-11-23T16:49:00","slug":"how-to-install-fail2ban-in-ubuntu-debian-for-brute-force-protection-in-linux","status":"publish","type":"post","link":"https:\/\/mgcspace.com\/content\/security\/how-to-install-fail2ban-in-ubuntu-debian-for-brute-force-protection-in-linux\/","title":{"rendered":"How to Install Fail2ban in Ubuntu \/ Debian for Brute Force Protection in Linux"},"content":{"rendered":"\n<p>Security is an important aspect of running any kind of server on internet. Almost all servers run one or other kind of service which requires authentication to access them but this also makes them target of malicious actors who want to breach your system and exploit them. <\/p>\n\n\n\n<p>One of the method used is called Brute Force Attack where repeated dictionary based attacks are performed in hope to guess the correct password. There are good chances that an insecure may have to face thousands of such automated attempts any moment, thus wasting significant amount of resources. <\/p>\n\n\n\n<p>To protect Linux servers from such attacks, we can utilize an opensource tool Fail2ban thus increasing our server security. <\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Overview <\/h2>\n\n\n\n<p>Here we have quick overview of steps for your ease.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#whatIsFail2ban\"><strong>What is fail2ban and how it works.<\/strong><\/a><\/li>\n\n\n\n<li><a href=\"#installFail2ban\"><strong>How to Install Fail2ban on Ubuntu \/ Debian <\/strong><\/a><\/li>\n\n\n\n<li><strong><a href=\"#configureFail2ban\">Fail2ban  configuration<\/a><\/strong> <\/li>\n\n\n\n<li><a href=\"#fail2banStatus\"><strong>Testing Fail2ban config and status<\/strong><\/a><\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id=\"whatIsFail2ban\">What is Fail2ban<\/h2>\n\n\n\n<p>You can implement many <a href=\"https:\/\/mgcspace.com\/content\/security\/quick-initial-linux-server-security-and-hardening\/\" data-type=\"post\" data-id=\"76\" target=\"_blank\" rel=\"noreferrer noopener\">security measures<\/a> to protect your server effectively. One such tool is Fail2ban, its an intrusion prevention software framework and written in Python. It is designed to prevent brute-force attacks. To achieve this, it examine log files and then blocks IPs after certain number of failed attempts. It can be customized to offer protection for multiple kind of application and services running on Linux servers as long as they maintain logs for actions for authentication related activities. <\/p>\n\n\n\n<p>Fail2ban can be very flexible and can be customized a lot, not only it can detect failed logins but also can monitor signs of malicious activities looking for exploits.  And then it can use system firewall to block those malicious IPs \/ sources as well as can take other arbitrary actions like sending mail notifications and more. <\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id=\"installFail2ban\">How to Install Fail2ban on Debian \/ Ubuntu and on similar Linux distributions<\/h2>\n\n\n\n<p>Installation of Fail2ban is pretty quick and straightforward for Linux distribution based on Debian and Ubuntu, following steps should work on any recent version.<\/p>\n\n\n\n<p>First of all make sure your distribution is up to date. We are assuming you have configured sudo user with admin privileges. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt upgrade<\/code><\/pre>\n\n\n\n<p>Once you are done with updating your system, you may go ahead and install Fail2ban<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install fail2ban<\/code><\/pre>\n\n\n\n<p>Here, after installation the fail2ban service will be automatically started and enabled. You can verify it buy running following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status fail2ban<\/code><\/pre>\n\n\n\n<p><em>Sample Output<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fail2ban.service - Fail2Ban Service\nLoaded: loaded (\/lib\/systemd\/system\/fail2ban.service; enabled; vendor preset: enabled)\nActive: active (running) since Mon 2021-11-21 20:48:24 CET; 18s ago\nDocs: man:fail2ban(1)\nMain PID: 55134 (f2b\/server)\nTasks: 5 (limit: 2282)\nMemory: 13.5M\nCGroup: \/system.slice\/fail2ban.service\n\u2514\u250055134 \/usr\/bin\/python3 \/usr\/bin\/fail2ban-server -xf start<\/code><\/pre>\n\n\n\n<p>Once fail2ban is up and running, we can now move on to configure it. <\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id=\"configureFail2ban\">Configure Fail2ban to protect the server<\/h2>\n\n\n\n<p>Configuration of Fail2ban is done through its config files found at <code>\/etc\/fail2ban\/<\/code>. At most basic level, fail2ban uses <code>\/etc\/fail2ban\/fail2ban.conf<\/code> as the primary configuration profile for overall setup of fail2ban.  While, the <code>\/etc\/fail2ban\/jail.conf<\/code> contains default configuration for various jails ie. set of instructions which contains filters and actions  for services where matching filters from the logs results in execution of desired actions for that service.<\/p>\n\n\n\n<p>To put in simple words, jail.conf consist of set of instructions where we can configure brute-force protection against pure-ftpd service, ssh service etc. Although in Debian \/ Ubuntu, ssh protection is enabled by default. <\/p>\n\n\n\n<p>Fail2ban reads first reads <code>*.conf<\/code> files and then overrides them with <code>*.local<\/code> files. Hence in order to customize we should create copy of <code>fail2ban.conf<\/code> as <code>fail2ban.local<\/code> and<code> jail.conf<\/code> as <code>jail.local<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo cp \/etc\/fail2ban\/fail2ban.{conf,local}\nsudo cp \/etc\/fail2ban\/jail.{conf,local}<\/code><\/pre>\n\n\n\n<p>Now we will have two new files (<code>\/etc\/fail2ban\/fail2ban.local<\/code> and <code>\/etc\/fail2ban\/jail.local<\/code>) copied from their .conf counterpart. Although, it is not necessary to create copy of <code>*.conf<\/code> files as <code>*.local<\/code> files, you can start with empty .local file also and add only those directives which you want to override.  But we will use copy of files and then discuss directives. <\/p>\n\n\n\n<p>The default <code>\/etc\/fail2ban\/fail2ban.local<\/code> is suitable most of the use case and all configuration choices are well-documented. If you wish you can modify directives like <code>loglevel<\/code> which defines how much data is logged for every event. You can configure under <code>[DEFAULT]<\/code> section.  Anything with &#8220;#&#8221; in front is considered comment and is ignored. For example, below <code>loglevel<\/code> is set to <code>INFO<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;DEFAULT]\n\n# Option: loglevel\n# Notes.: Set the log level output.\n#         CRITICAL\n#         ERROR\n#         WARNING\n#         NOTICE\n#         INFO\n#         DEBUG\n# Values: &#91; LEVEL ]  Default: ERROR\n#\nloglevel = INFO<\/code><\/pre>\n\n\n\n<p>The main action goes into the <code>\/etc\/fail2ban\/jail.local<\/code> file. This  will contain default directives which will be applied to all Jails. However, you can override per jail directive also. There are lots of things you can change from this file which is well  documented through comments in file. For example,  you can change some of the aspects given below : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &#91;DEFAULT]\n\n    ignoreip = x.x.x.x\n    bantime = 24h\n    findtime = 30m\n    maxretry = 3\n    usedns = no\n    destemail = you@yourdomain.com\n    sendername = Fail2Ban\n    sender = you@yourservername.com\n    mta = sendmail\n    action = $(action)s<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ignoreip:<\/strong> It can be a list of IP addresses, CIDR masks or DNS hosts which you want to be ignored by fail2ban.<\/li>\n\n\n\n<li><strong>bantime<\/strong>: It is the period for which a host is banned, we have configured it to 24h.<\/li>\n\n\n\n<li><strong>findtime<\/strong>: A host is banned if it has generated &#8220;maxretry&#8221; during the last &#8220;findtime&#8221;. We have set is 30m, which means that within last 30 minutes if there are failed attempts equals to &#8220;maxretry&#8221; option.<\/li>\n\n\n\n<li><strong>maxretry:<\/strong> It is number of failures before a host get banned.<\/li>\n\n\n\n<li><strong>usedns<\/strong>: It can be <code>yes<\/code>, <code>warn<\/code>, <code>no<\/code> and <code>raw<\/code>. If set to yes, then will perform DNS lookup for hostname and if set to warn, it will do DNS lookup but log it as warning. Setting it to no means hostname will not used for banning. Using it with raw is used to set no-host filters and actions. We have set it to no as we just want IP addresses used for banning.<\/li>\n\n\n\n<li><strong>destemail:<\/strong> Specify the email address where you want Fail2ban to send reports about event, you can further customize per  jail also.<\/li>\n\n\n\n<li><strong>sendername<\/strong>: Specify anything describing name of your server running Fail2ban<\/li>\n\n\n\n<li><strong>sender: <\/strong>Specify full email address as &#8220;from&#8221; address for notifications<\/li>\n\n\n\n<li><strong>mta:<\/strong> Which mail transport agent to use for mails, sendmail is preferred. <\/li>\n\n\n\n<li><strong>action:<\/strong> On match, what should be the action, here you define it on Global level, you can change it at each Jail level also. So, you can customize to enable mail notification as well other customization along with banning. \n<ul class=\"wp-block-list\">\n<li> To ban &amp; send an e-mail with whois report to the destemail. <pre class=\"wp-block-code\"><code>action =&nbsp;%(action_mw)s<\/code><\/pre><\/li>\n\n\n\n<li>Same as above ie. action_mw but also send relevant log lines <pre class=\"wp-block-code\"><code>action =&nbsp;%(action_mwl)s<\/code><\/pre><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Changing which firewall to be used with fail2ban<\/strong><\/p>\n\n\n\n<p>By default, iptables is used by Fail2ban for banning. We can switch to other firewalls like nftables ,ufw, shorewall, firewalld etc. <\/p>\n\n\n\n<p>Default iptables :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>banaction = iptables-multiport\nbanaction_allports = iptables-allports\n<\/code><\/pre>\n\n\n\n<p>Use nftables instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>banaction = nftables\nbanaction_allports = nftables&#91;type=allports]<\/code><\/pre>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>Jails<\/strong><\/p>\n\n\n\n<p>By default, <code>jail.local<\/code> will have various preconfigured jails for you. For example, ssh (for port 22) is already enabled by default in Debian \/ Ubuntu for fail2ban. We have set <code>enabled = true<\/code> explicitly also. You also specify other settings as explained earlier like <code>bantime<\/code>, <code>ignoreip<\/code> inside each jail also.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;sshd]\nenabled = true\nport    = ssh\nlogpath = %(sshd_log)s\nbackend = %(sshd_backend)s<\/code><\/pre>\n\n\n\n<p>Below we have another example jail for  <code>pure-ftpd<\/code> service taken from <code>\/etc\/fail2ban\/jail.local<\/code> . Note we have again set  <code>enabled=true<\/code>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;pure-ftpd]\nenabled   = true\nport     = ftp,ftp-data,ftps,ftps-data\nlogpath  = %(pureftpd_log)s\nbackend  = %(pureftpd_backend)s<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>One we have enabled our desired jails in jail.local files, we we need to restart fail2ban services in order for changes to get in effect. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart fail2ban<\/code><\/pre>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"fail2banStatus\"><strong>Checking Fail2ban status<\/strong> : fail2ban-client<\/h4>\n\n\n\n<p>Fail2ban comes with fail2ban-client utility which we can use for various tasks on fail2ban, like <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>start<\/code>: Starts the Fail2ban server and jails.<\/li>\n\n\n\n<li><code>reload<\/code>: Reloads Fail2ban\u2019s configuration files.<\/li>\n\n\n\n<li><code>stop<\/code>: Terminates the server.<\/li>\n\n\n\n<li><code>status<\/code>: Will show the status of the server, and enable jails.<\/li>\n\n\n\n<li><code>status JAIL<\/code>: Will show the status of the jail, including any currently-banned IPs.<\/li>\n<\/ul>\n\n\n\n<p>For example, to view all enabled jails :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> sudo fail2ban-client status <\/code><\/pre>\n\n\n\n<p><em>Sample Output<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Status \n|- Number of jail:      \n2 `- Jail list:   pure-ftpd, sshd <\/code><\/pre>\n\n\n\n<p><strong>Testing Fail2ban jail: <\/strong><\/p>\n\n\n\n<p>As you can see have 2 Jails enabled ie. pure-ftpd and sshd. We can test using some failed login attempts on sshd server. We had configured <code>maxretry=3<\/code> in last 30 min (using <code>findtime=30m<\/code>). You  can make 3 login attempts to this server&#8217;s ssh port, on fourth attempt, you should get &#8220;connection refused&#8221; error. <br><br>To verify if IP is blocked by fail2ban, you can check in <code>\/var\/log\/fail2ban.log<\/code> file. You may also check using iptables if that IP is blocked by it. Checkout the output below where our sample IP <code>192.168.4.122<\/code> was blocked.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -nL<\/code><\/pre>\n\n\n\n<p><em>Sample output<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iptables -nL\nChain INPUT (policy ACCEPT)\ntarget     prot opt source               destination\nf2b-sshd   tcp  --  0.0.0.0\/0            0.0.0.0\/0            multiport dports 22\n\nChain FORWARD (policy ACCEPT)\ntarget     prot opt source               destination\n\nChain OUTPUT (policy ACCEPT)\ntarget     prot opt source               destination\n\nChain f2b-sshd (1 references)\ntarget     prot opt source               destination\nREJECT     all  --  192.168.4.122       0.0.0.0\/0            reject-with icmp-port-unreachable\nRETURN     all  --  0.0.0.0\/0            0.0.0.0\/0\n<\/code><\/pre>\n\n\n\n<div style=\"height:45px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This tutorial will sum up as basic overview of Fail2ban intrusion detection system configuration on Ubuntu \/ Debian and similar Linux distributions. Fail2ban can do lot more interesting things and covers a lot of applications and services. Nevertheless, its should help you to get started and  you can fine tune it to suite your special needs. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Security is an important aspect of running any kind of server on internet. Almost all servers run one or other kind of service which requires authentication to access them but&hellip;<\/p>\n","protected":false},"author":1,"featured_media":129,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[15,12,16,13,14],"class_list":["post-128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security","tag-debian","tag-firewall","tag-linux","tag-security","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":9,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/posts\/128\/revisions\/146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/media\/129"}],"wp:attachment":[{"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mgcspace.com\/content\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}