{"id":13,"date":"2019-05-29T07:55:57","date_gmt":"2019-05-29T07:55:57","guid":{"rendered":"http:\/\/untanux.lt\/?p=13"},"modified":"2020-01-28T11:58:36","modified_gmt":"2020-01-28T11:58:36","slug":"install-fail2ban-on-centos-7","status":"publish","type":"post","link":"https:\/\/untanux.lt\/?p=13","title":{"rendered":"Install Fail2ban on CentOS 7"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"install-fail2ban-on-centos-7\"><\/h2>\n\n\n\n<p>While Fail2ban is not available in the official CentOS package repository, it is packaged for the <a href=\"https:\/\/fedoraproject.org\/wiki\/EPEL\">EPEL project<\/a>.  EPEL, standing for Extra Packages for Enterprise Linux, can be installed with a release package that <em>is<\/em> available from CentOS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum install epel-release\n<\/code><\/pre>\n\n\n\n<p>You will be prompted to continue&#8212;press <strong>y<\/strong>, followed by <strong>Enter<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum promptTransaction Summary\n============================================================================\nInstall  1 Package\n\nTotal download size: 14 k\nInstalled size: 24 k\nIs this ok [y\/d\/N]: y\n<\/code><\/pre>\n\n\n\n<p>Now we should be able to install the <code>fail2ban<\/code> package:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum install fail2ban\n<\/code><\/pre>\n\n\n\n<p>Again, press <strong>y<\/strong> and <strong>Enter<\/strong> when prompted to continue.<\/p>\n\n\n\n<p>Once the installation has finished, use <code>systemctl<\/code> to enable the <code>fail2ban<\/code> service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable fail2ban\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"configure-local-settings\">Configure Local Settings<\/h2>\n\n\n\n<p>The Fail2ban service keeps its configuration files in the <code>\/etc\/fail2ban<\/code> directory.  There, you can find a file with default values called <code>jail.conf<\/code>.\n  Since this file may be overwritten by package upgrades, we shouldn&#8217;t \nedit it in-place.  Instead, we&#8217;ll write a new file called <code>jail.local<\/code>.  Any values defined in <code>jail.local<\/code> will override those in <code>jail.conf<\/code>.<\/p>\n\n\n\n<p><code>jail.conf<\/code> contains a <code>[DEFAULT]<\/code> section, followed by sections for individual services.  <code>jail.local<\/code> may override any of these values.  Additionally, files in <code>\/etc\/fail2ban\/jail.d\/<\/code> can be used to override settings in both of these files.  Files are applied in the following order:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>\/etc\/fail2ban\/jail.conf<\/code><\/li><li><code>\/etc\/fail2ban\/jail.d\/*.conf<\/code>, alphabetically<\/li><li><code>\/etc\/fail2ban\/jail.local<\/code><\/li><li><code>\/etc\/fail2ban\/jail.d\/*.local<\/code>, alphabetically<\/li><\/ol>\n\n\n\n<p>Any file may contain a <code>[DEFAULT]<\/code> section, executed \nfirst, and may also contain sections for individual jails.  The last \nvavalue set for a given parameter takes precedence.<\/p>\n\n\n\n<p>Let&#8217;s begin by writing a very simple version of <code>jail.local<\/code>.  Open a new file using <code>nano<\/code> (or your editor of choice):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/fail2ban\/jail.local\n<\/code><\/pre>\n\n\n\n<p>Paste the following:\n\/etc\/fail2ban\/jail.local<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[DEFAULT]\n# Ban hosts for one hour:\nbantime = 3600\n\n# Override \/etc\/fail2ban\/jail.d\/00-firewalld.conf:\nbanaction = iptables-multiport\n\n<\/code><\/pre>\n\n\n<p>[sshd]<\/p>\n\n\n\n<p>\nenabled = true\n\n<\/p>\n\n\n\n<p>This overrides three settings:  It sets a new default <code>bantime<\/code> for all services, makes sure we&#8217;re using <code>iptables<\/code> for firewall configuration, and enables the <code>sshd<\/code> jail.<\/p>\n\n\n\n<p>Exit and save the new file (in <code>nano<\/code>, press <strong>Ctrl-X<\/strong> to exit, <strong>y<\/strong> to save, and <strong>Enter<\/strong> to confirm the filename).  Now we can restart the <code>fail2ban<\/code> service using <code>systemctl<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart fail2ban\n<\/code><\/pre>\n\n\n\n<p>The <code>systemctl<\/code> command should finish without any output.  In order to check that the service is running, we can use <code>fail2ban-client<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client status\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>OutputStatus\n|- Number of jail:      1\n`- Jail list:   sshd\n<\/code><\/pre>\n\n\n\n<p>You can also get more detailed information about a specific jail:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client status sshd\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"explore-available-settings\">Explore Available Settings<\/h2>\n\n\n\n<p>The version of <code>jail.local<\/code> we defined above is a good start, but you may want to adjust a number of other settings.  Open <code>jail.conf<\/code>,\n and we&#8217;ll examine some of the defaults.  If you decide to change any of\n these values, remember that they should be copied to the appropriate \nsection of <code>jail.local<\/code> and adjusted there, rather than modified in-place.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/fail2ban\/jail.conf\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"default-settings-for-all-jails\">Default Settings for All Jails<\/h3>\n\n\n\n<p>First, scroll through the <code>[DEFAULT]<\/code> section.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ignoreip = 127.0.0.1\/8\n<\/code><\/pre>\n\n\n\n<p>You can adjust the source addresses that Fail2ban ignores by adding a value to the <code>ignoreip<\/code>\n parameter.  Currently, it is configured not to ban any traffic coming \nfrom the local machine.  You can include additional addresses to ignore \nby appending them to the end of the parameter, separated by a space.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bantime = 600\n<\/code><\/pre>\n\n\n\n<p>The <code>bantime<\/code> parameter sets the length of time that a \nclient will be banned when they have failed to authenticate correctly.  \nThis is measured in seconds.  By default, this is set to 600 seconds, or\n 10 minutes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>findtime = 600\nmaxretry = 3\n<\/code><\/pre>\n\n\n\n<p>The next two parameters that you want to pay attention to are <code>findtime<\/code> and <code>maxretry<\/code>.  These work together to establish the conditions under which a client should be banned.<\/p>\n\n\n\n<p>The <code>maxretry<\/code> variable sets the number of tries a client has to authenticate within a window of time defined by <code>findtime<\/code>,\n before being banned.  With the default settings, Fail2ban will ban a \nclient that unsuccessfully attempts to log in 3 times within a 10 minute\n window.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>destemail = root@localhost\nsendername = Fail2Ban\nmta = sendmail\n<\/code><\/pre>\n\n\n\n<p>If you wish to configure email alerts, you may need to override the <code>destemail<\/code>, <code>sendername<\/code>, and <code>mta<\/code> settings.  The <code>destemail<\/code> parameter sets the email address that should receive ban messages.  The <code>sendername<\/code> sets the value of the &#8220;From&#8221; field in the email.  The <code>mta<\/code> parameter configures what mail service will be used to send mail.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>action = $(action_)s\n<\/code><\/pre>\n\n\n\n<p>This parameter configures the action that Fail2ban takes when it wants to institute a ban.  The value <code>action_<\/code>\n is defined in the file shortly before this parameter.  The default \naction is to simply configure the firewall to reject traffic from the \noffending host until the ban time elapses.<\/p>\n\n\n\n<p>If you would like to configure email alerts, you can override this value from <code>action_<\/code> to <code>action_mw<\/code>.  If you want the email to include the relevant log lines, you can change it to <code>action_mwl<\/code>.  You&#8217;ll want to make sure you have the appropriate mail settings configured if you choose to use mail alerts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"settings-for-individual-jails\">Settings for Individual Jails<\/h3>\n\n\n\n<p>After <code>[DEFAULT]<\/code>, we&#8217;ll encounter sections configuring individual jails for different services.  These will typically include a <code>port<\/code> to be banned and a <code>logpath<\/code> to monitor for malicious access attempts.  For example, the SSH jail we already enabled in <code>jail.local<\/code> has the following settings:\n\/etc\/fail2ban\/jail.local<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[sshd]\n\nport    = ssh\nlogpath = %(sshd_log)s\n<\/code><\/pre>\n\n\n\n<p>In this case, <code>ssh<\/code> is a pre-defined variable for the standard SSH port, and <code>%(sshd_log)s<\/code> uses a value defined elsewhere in Fail2ban&#8217;s standard configuration (this helps keep <code>jail.conf<\/code> portable between different operating systems).<\/p>\n\n\n\n<p>Another setting you may encounter is the <code>filter<\/code> that will be used to decide whether a line in a log indicates a failed authentication.<\/p>\n\n\n\n<p>The <code>filter<\/code> value is actually a reference to a file located in the <code>\/etc\/fail2ban\/filter.d<\/code> directory, with its <code>.conf<\/code>\n extension removed.  This file contains the regular expressions that \ndetermine whether a line in the log is bad.  We won&#8217;t be covering this \nfile in-depth in this guide, because it is fairly complex and the \npredefined settings match appropriate lines well.<\/p>\n\n\n\n<p>However, you can see what kind of filters are available by looking into that directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls \/etc\/fail2ban\/filter.d\n<\/code><\/pre>\n\n\n\n<p>If you see a file that looks to be related to a service you are \nusing, you should open it with a text editor.  Most of the files are \nfairly well commented and you should be able to tell what type of \ncondition the script was designed to guard against.  Most of these \nfilters have appropriate (disabled) sections in <code>jail.conf<\/code> that we can enable in <code>jail.local<\/code> if desired.<\/p>\n\n\n\n<p>For instance, pretend that we are serving a website using Nginx and \nrealize that a password-protected portion of our site is getting slammed\n with login attempts.  We can tell Fail2ban to use the <code>nginx-http-auth.conf<\/code> file to check for this condition within the <code>\/var\/log\/nginx\/error.log<\/code> file.<\/p>\n\n\n\n<p>This is actually already set up in a section called <code>[nginx-http-auth]<\/code> in our <code>\/etc\/fail2ban\/jail.conf<\/code> file.  We would just need to add an <code>enabled<\/code> parameter for the <code>nginx-http-auth<\/code> jail to <code>jail.local<\/code>:\n\/etc\/fail2ban\/jail.local<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[DEFAULT]\n# Ban hosts for one hour:\nbantime = 3600\n\n# Override \/etc\/fail2ban\/jail.d\/00-firewalld.conf:\nbanaction = iptables-multiport\n\n<\/code><\/pre>\n\n\n<p>[sshd]<\/p>\n\n\n\n<p>\nenabled = true\n\n[nginx-http-auth]\nenabled = true\n\n<\/p>\n\n\n\n<p>And restart the <code>fail2ban<\/code> service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart fail2ban\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"monitor-fail2ban-logs-and-firewall-configuration\">Monitor Fail2ban Logs and Firewall Configuration<\/h2>\n\n\n\n<p>It&#8217;s important to know that a service like Fail2ban is working as-intended.  Start by using <code>systemctl<\/code> to check the status of the service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status fail2ban\n<\/code><\/pre>\n\n\n\n<p>If something seems amiss here, you can troubleshoot by checking logs for the <code>fail2ban<\/code> unit since the last boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo journalctl -b -u fail2ban\n<\/code><\/pre>\n\n\n\n<p>Next, use <code>fail2ban-client<\/code> to query the overall status of <code>fail2ban-server<\/code>, or any individual jail:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client status\nsudo fail2ban-client status jail_name\n<\/code><\/pre>\n\n\n\n<p>Follow Fail2ban&#8217;s log for a record of recent actions (press <strong>Ctrl-C<\/strong> to exit):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tail -F \/var\/log\/fail2ban.log\n<\/code><\/pre>\n\n\n\n<p>List the current rules configured for iptables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -L\n<\/code><\/pre>\n\n\n\n<p>Show iptables rules in a format that reflects the commands necessary to enable each rule:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -S\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>You should now be able to configure some basic banning policies for \nyour services.  Fail2ban is very easy to set up, and is a great way to \nprotect any kind of service that uses authentication.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While Fail2ban is not available in the official CentOS package repository, it is packaged for the EPEL project. EPEL, standing for Extra Packages for Enterprise Linux, can be installed with a release package that is available from CentOS: You will be prompted to continue&#8212;press y, followed by Enter: Now we should be able to install [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/untanux.lt\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/untanux.lt\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/untanux.lt\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/untanux.lt\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/untanux.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":0,"href":"https:\/\/untanux.lt\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"https:\/\/untanux.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/untanux.lt\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/untanux.lt\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}