Skip to content

Block attacks with fail2ban

fail2ban reads the log of a service and, when an address makes too many failed attempts, blocks it at the firewall for a while. The server does not have to do anything: the address cannot even connect. uFTP ships a ready filter and a jail in contrib/fail2ban/.

uFTP has its own limits too: it blocks an address for 5 minutes after MAX_CONNECTION_TRY_PER_IP failed logins. fail2ban keeps the address out for longer and stops it before it reaches the server, at no cost. They work one behind the other.

What the filter counts

Line in the log of uFTP Counts as an attack
login failed for user ... (wrong name or password; a blocked user also gets it) yes
Ip ... blocked due too many password errors (an address that keeps trying while uFTP has locked it out) yes
Connection refused: ... opened more than N connections in the last minute (a flood, see MAX_CONNECTION_RATE_PER_IP) yes
login refused: user ... has too many connections, too many connection from ip address no, on purpose

The last two can hit an honest person (an office behind one address, FileZilla with many transfers): banning them would be the damage.

uFTP groups identical log lines for 10 seconds. An attacker that repeats the same user counts once every 10 seconds, one that changes the user counts at every attempt. Both reach the limit in seconds or in less than a minute.

Install

sudo apt-get install fail2ban
sudo cp contrib/fail2ban/uftp.conf      /etc/fail2ban/filter.d/uftp.conf
sudo cp contrib/fail2ban/uftp.jail.conf /etc/fail2ban/jail.d/uftp.conf
sudo systemctl restart fail2ban

Restart fail2ban, do not only reload it

After you install the jail or change its logpath, restart fail2ban (systemctl restart fail2ban). On a production server a plain fail2ban-client reload after a change of the log file left fail2ban listing the file but not following the new lines: no failed login was counted until the restart.

The jail bans an address that appears 5 times in 10 minutes for 1 hour, on the FTP ports. Edit /etc/fail2ban/jail.d/uftp.conf to match your setup:

[uftp]
enabled  = true
filter   = uftp
logpath  = /var/log/uftpLog.current     # in the LOG_FOLDER of uftpd.cfg
port     = ftp,ftp-data,ftps,ftps-data,21   # add your FTP_PORT if it is not 21
maxretry = 5
findtime = 10m
bantime  = 1h

The log must be on: MAXIMUM_LOG_FILES above 0 in uftpd.cfg.

Why uftpLog.current and not uftpLog_*

uFTP writes a new log file every day (uftpLog_YYYY-MM-DD) and keeps a link to the one of today, uftpLog.current, in the log folder (it needs uFTP 4.3.0 or later). fail2ban follows the link when it moves to the new day, as it does with a file rotated by logrotate. With a wildcard such as uftpLog_* fail2ban would read only the files that exist when it starts and not the one of a new day: the bans would stop after midnight until the next reload.

Test it

The ban blocks only the FTP ports: it does not touch SSH, so you cannot lock yourself out of the server. For the test use a short ban:

  1. In /etc/fail2ban/jail.d/uftp.conf set bantime = 2m and maxretry = 3, then sudo systemctl restart fail2ban.
  2. Check that the filter reads your log (it bans nobody):

    sudo fail2ban-regex /var/log/uftpLog_$(date +%F) /etc/fail2ban/filter.d/uftp.conf
    

    After a few wrong logins the line Lines: ... matched shows a number above zero. 3. From another address (for example a phone on mobile data), log in with a wrong password four times with your FTP client. 4. On the server:

    sudo fail2ban-client status uftp     # the address is in "Banned IP list"
    sudo tail -5 /var/log/fail2ban.log   # "Found ..." and "Ban ..."
    

    A new connection from that address is refused, while the others connect normally. The ban ends by itself after the time you set. 5. To remove a ban at once: sudo fail2ban-client set uftp unbanip ADDRESS. 6. When you are done, put bantime and maxretry back to the real values and restart fail2ban. 7. The day after, check that fail2ban still reads the log: send a few wrong logins as in step 3 and look for new Found lines in /var/log/fail2ban.log.

Good to know

  • Your own mistakes count: 5 wrong passwords in 10 minutes block your FTP for an hour. Add the addresses of people who often mistype to ignoreip in the jail.
  • The ban is done by the firewall of the server. If the server is behind the firewall of a hosting provider, that firewall is not changed.
  • If you change the format of the log (a different version of uFTP), run fail2ban-regex again: the filter must match the lines.
  • It was tested with fail2ban 1.0.2 and, on a production server with the log link of uFTP, with inotify (live: the failed logins of a test address were counted at once and the address was banned): the filter on the log of a real server, a real fail2ban-server (also when the log moves to the file of a new day), and a real ban at the firewall of a container, with the two configuration files unchanged. Also tried on a production server: an address with wrong logins was banned and its connections were refused.