Text of securitylab.github.com › research › Fail2exploit as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Fail2exploit: a security audit of Fail2ban | GitHub Security Lab

skip to content Resources Open Source Community Enterprise / Security Lab Research Advisories CodeQL Wall of Fame Resources Open Source Community Enterprise Events Get Involved Security audits don’t always produce interesting results. As a member of GitHub Security Lab , my job is to help improve the security of open source software by finding and reporting vulnerabilities. On this occasion, I audited the open source project Fail2ban and I struck out: I didn’t find any issues worth reporting.

From my perspective as a security researcher, that’s a fail, but for open source software, it’s a win. Fail2ban is often recommended for anyone who’s running a Linux server that’s exposed to the public internet. For example, suppose you have enabled remote OpenSSH access on your home Linux server, but you don’t want your server to be subjected to a constant barrage of break-in attempts from internet pond-life who are trying to brute-force your password.

You can protect your server by installing Fail2ban. Fail2ban is an open source software application that monitors system log files for repeated login failures and temporarily bans the responsible IP addresses to thwart brute-force attacks. I’m always nervous about installing non-standard software. I generally prefer to stick with the default setup, because it’s likely to have received much more scrutiny from a security team.

I also have a particular dislike of antivirus software (AV), due to its invasive nature and its ignominious track record of making security worse rather than better . Fail2ban appears to tick both those boxes, because it’s not installed by default (at least, not on the Linux distribution that I use), and it seems a bit AV-ish, considering that it runs as the root user and has the power to mess with your iptables. So, is it safe?

I think so. In this blog post, I’ll describe what I did to test and audit Fail2ban, and why I think it’s safe. The information in this blog post is based on Fail2ban version 0.11.2-1, installed on Ubuntu 21.04. But first… If you’re thinking of installing Fail2ban, there’s something else that I want you to check first. If you’re running an OpenSSH server, then the first thing that you should do to thwart brute force attacks is to disable password authentication .

You can do that by editing this config file: /etc/ssh/sshd_config By default it has a line that looks like this: #PasswordAuthentication yes Replace that with: PasswordAuthentication no After that, remote clients can only log in using an SSH key. SSH keys are intended to be cryptographically secure, which means that it’s not feasible for an attacker to brute force the private key (assuming that the cryptographic scheme itself is both sound and implemented correctly ).

An attacker who discovers that your SSH server has password authentication disabled is likely to give up very quickly and move on to a different server. If you haven’t used SSH keys before, then I recommend reading this overview of SSH keys from Ubuntu’s documentation . Fail2ban installation and usage Although Fail2ban isn’t usually installed by default, it is usually included in the standard package repositories.

For example, on Ubuntu , installation is this simple: sudo apt-get install fail2ban That’s it! Your server is now protected from SSH password-guessing attacks. Of course, there are many configuration options that you can tweak if you wish (the manual is here ), but the default configuration is fine. If you’re interested in learning more about the internals of Fail2ban, then a useful configuration option to change is the logging level.

You can do that by editing /etc/fail2ban/fail2ban.conf . Find the line that looks like this: loglevel = INFO And for the maximum amount of logging information, replace it with this: loglevel = 1 Enabling email notifications The other configuration change that I made on my own system was to add email notifications. The first step is to install an SMTP client: sudo apt-get install msmtp-mta Then create an msmtp config file : touch ~/.msmtprc chmod 600 ~/.msmtprc emacs ~/.msmtprc # edit contents My ~/.msmtprc looks like this: # Set default values for all following accounts. defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile ~/.msmtp.log # Gmail account gmail host smtp.gmail.com port 587 from kev@gmail.com user kev password abcdabcdabcdabcd # Set a default account account default : gmail (Sadly, my email address isn’t really kev@gmail.com.) For the password, I recommend generating an app password .

Now check that you’re able to send a simple email: printf "Subject: Test email \n Date: ` date --rfc-email ` \n From: Kev <kev@gmail.com> \n To: Kev <kev1337@mail.com> \n Hi \n " | /usr/sbin/sendmail "kev1337@mail.com" (Now you know my real email address! 😉) If that works, then copy your msmtp config file to the root user’s home directory to enable Fail2ban (which runs as the root user) to send emails. sudo cp .msmtprc /root/ Next, create a file named /etc/fail2ban/jail.d/local.conf with the following contents: [DEFAULT] destemail = kev1337@mail.com sender = kev@gmail.com action = %(action_mwl)s (Don’t forget to update the email addresses!) The last step is to restart Fail2ban: sudo systemctl restart fail2ban If everything is working, you should receive an email from Fail2ban, with a title like “[Fail2Ban] sshd: started on …”.

Fail2ban attack surface When I’m doing a security audit, my first question is always: “What’s the attack surface of this application?” The highest risk applications are those that are directly exposed to the public internet. OpenSSH is a good example: it listens on TCP port 22, so anybody on the internet can attempt to attack it by sending it malicious data. An application’s attack surface includes any interface that an attacker can use to interact with it.

Fail2ban is closely related to OpenSSH, so you might expect it to have an equally high-risk attack surface. In fact, the opposite is true. Fail2ban’s core design is based on the clever idea of scanning the system log files. If an attacker tries to log in with SSH but fails to guess the correct password, then sshd writes an error message to the system log. Fail2ban scans the log files for those authentication error messages and, after multiple failed attempts from the same IP address, uses iptables to ban it.

This design is clever because it means that there is no direct connection between Fail2ban and the public internet. Fail2ban’s primary attack surface is the contents of the system log files. In other words, without even looking at Fail2ban’s source code, I can already say that Fail2ban’s attack surface is relatively low risk. Does Fail2ban have any other attack surface? Using lsof , I can see what files and sockets it has open: f2b/serve 8859 root 0r CHR 1,3 0t0 6 /dev/null f2b/serve 8859 root 1u unix 0xffff96308621a800 0t0 73846 type=STREAM f2b/serve 8859 root 2u unix 0xffff96308621a800 0t0 73846 type=STREAM f2b/serve 8859 root 3w REG 8,5 5852 811215 /var/log/fail2ban.log f2b/serve 8859 root 4u unix 0xffff96308621bc00 0t0 73885 /var/run/fail2ban/fail2ban.sock type=STREAM f2b/serve 8859 root 6u REG 8,5 73728 811221 /var/lib/fail2ban/fail2ban.sqlite3 f2b/serve 8859 root 8r a_inode 0,14 0 10376 inotify f2b/serve 8859 root 9r a_inode 0,14 0 10376 inotify The above shows that Fail2ban is using inotify to monitor file changes (it’s easy to confirm by checking the verbose output in fail2ban.log that it is only monitoring files in /var/log ).

It also shows that Fail2ban is accessing an SQLite database and a UNIX domain socket . But a quick check of the file permissions shows that they’re only accessible by the root user: $ ls -l /var/lib/fail2ban/fail2ban.sqlite3 -rw------- 1 root root 90112 Jun 7 10:02 /var/lib/fail2ban/fail2ban.sqlite3 $ ls -l /var/run/fail2ban/fail2ban.sock srwx------ 1 root root 0 Jun 2 15:22 /var/run/fail2ba

The rest of this page is on the live site.