Why Use Fail2Ban
UFW tells your server what doors to board up so nobody can see them, and what doors to allow authorized users through. PSAD monitors network activity to detect and prevent potential intrusions — repeated attempts to get in. But what about the applications/services your server is running, like SSH and Apache, where your firewall is configured to allow access in? Even though access may be allowed that doesn’t mean all access attempts are valid and harmless. What if someone tries to brute-force their way in to a web-app you’re running on your server? This is where Fail2ban comes in.How It Works
Fail2ban monitors the logs of your applications (like SSH and Apache) to detect and prevent potential intrusions. It will monitor network traffic/logs and prevent intrusions by blocking suspicious activity (e.g. multiple successive failed connections in a short time-span).- As of right now, the only thing running on this server is SSH so we’ll want Fail2ban to monitor SSH and ban as necessary.
- As you install other programs, you’ll need to create/configure the appropriate jails and enable them.
Installation and Configuration
Create Local Configuration
Create the file
/etc/fail2ban/jail.local and add this to it after replacing [LAN SEGMENT] and [your email] with the appropriate values:Your server will need to be able to send e-mails so Fail2ban can let you know of suspicious activity and when it banned an IP.
Create SSH Jail
We need to create a jail for SSH that tells fail2ban to look at SSH logs and use UFW to ban/unban IPs as needed.Create the file Or use this quick command:
/etc/fail2ban/jail.d/ssh.local and add this:In the above we tell fail2ban to use UFW as the
banaction. Fail2ban ships with an action configuration file for UFW. You can see it in /etc/fail2ban/action.d/ufw.conf.Unban an IP Address
To unban an IP address, use this command:[jail]is the name of the jail that has the banned IP[IP]is the IP address you want to unban
192.168.1.100 from SSH:
Custom Jails
Custom jails can be created for other services like Apache, Nginx, or custom applications. The process involves:
- Creating a filter file in
/etc/fail2ban/filter.d/that defines the log patterns to match - Creating a jail configuration in
/etc/fail2ban/jail.d/that references the filter - Enabling the jail with
fail2ban-client