nativeInstallerredmine-plus-agile

Configure fail2ban

Before running the commands shown on this page, you should load the Bitnami stack environment by executing the installdir/use_APPNAME script (Linux and MacOS) or by clicking the shortcut in the Start Menu under “Start -> Bitnami APPNAME Stack -> Application console” (Windows). On OS X VMs, the installation directory is /opt/bitnami and OS X VM users can click the “Open Terminal” button to run commands. Learn more about the Bitnami stack environment and about OS X VMs.

If you want to limit the number of login attempts (and avoid brute-force attacks), install fail2ban and configure it to work with the Bitnami Redmine Stack. Follow these steps, depending on your installation type:

NOTE: The Approach A sections referred to below do not apply to Bitnami native installers. Users of Bitnami native installers should refer only to the Approach B sections.

  • Install fail2ban:

    • Debian:

        $ sudo apt-get update
        $ sudo apt-get install fail2ban
      
    • CentOS:

        $ sudo yum install epel-release
        $ sudo yum install fail2ban
        $ sudo systemctl enable fail2ban
        $ sudo systemctl start fail2ban
      
  • Modify the Redmine environment.rb configuration file to allow the Redmine logger to add timestamps to the production.log file. To do this, add the following lines:

    NOTE: Depending on your installation type, the Redmine environment.rb configuration file can be found on the following locations:

    • Approach A (Bitnami installations using system packages): installdir/redmine/config/environment.rb

    • Approach B (Self-contained Bitnami installations): installdir/apps/redmine/htdocs/config/environment.rb

      class Logger
        def format_message(severity, timestamp, progname, msg)
          "#{timestamp} (#{$$}) #{msg}\n"
        end
      end
    
  • Restart the Apache server.

      $ sudo installdir/ctlscript.sh restart apache
    

Next, configure fail2ban following the steps below:

  • Create the /etc/fail2ban/filter.d/redmine.conf file with the following code:

      [Definition]
    
      failregex = Failed [-/\w]+ for .* from <HOST>
      ignoreregex =
    
  • Create the /etc/fail2ban/jail.local file and add the code below, depending on your installation type:

    • Approach A (Bitnami installations using system packages):

        [redmine]
        enabled = true
        filter = redmine
        port = http,https
        logpath = installdir/redmine/log/production.log
        maxretry = 5
        findtime = 600
        bantime = 600
      
    • Approach B (Self-contained Bitnami installations):

        [redmine]
        enabled = true
        filter = redmine
        port = http,https
        logpath = installdir/apps/redmine/htdocs/log/production.log
        maxretry = 5
        findtime = 600
        bantime = 600
      

    This fail2ban configuration will ban the IP of any user that tried to access five (maxretry) different times in 10 minutes (findtime) without success. Note that the ban only will affect the ports 80 and 443, and the user with the banned IP will not be able to contact the web server for 10 minutes (bantime).

  • Before applying the configuration,test if the regex configuration is correct. To test this, browse to your Bitnami Redmine login page and use a non-existent user/password to get a login error. Then, run this command depending on your installation type:

    • Approach A (Bitnami installations using system packages):

        $ fail2ban-regex installdir/redmine/log/production.log /etc/fail2ban/filter.d/redmine.conf
      
    • Approach B (Self-contained Bitnami installations):

        $ fail2ban-regex installdir/apps/redmine/htdocs/log/production.log /etc/fail2ban/filter.d/redmine.conf
      

    If the last two lines of the output show you at least “1 matched”, the regex is properly configured:

      Lines: 412 lines, 0 ignored, 1 matched, 397 missed
      Missed line(s):: too many to print.  Use --print-all-missed to print all 397 lines
    
  • Apply the configuration with this command

    • Debian:

        $ sudo /etc/init.d/fail2ban restart
      
    • CentOS:

        $ sudo systemctl restart fail2ban
      

To check if all is working, try logging in five different times with bad credentials. On the fifth unsuccessful attempt, you will be banned for 10 minutes.

Last modification July 15, 2021