31.5 Fail2Ban (Based on IPFW, PF, and IPF)
In network security practice, brute-force attacks are one of the common threats against authentication systems.
Fail2Ban is a log-based Intrusion Prevention System (IPS) that implements adaptive access control by dynamically updating firewall rules, providing real-time protection for servers.
According to the official description, Fail2Ban denies new connections from IP addresses with multiple authentication failures by updating system firewall rules (see How Fail2Ban Works). Fail2Ban is implemented almost entirely in Python (approximately 96% Python code). This section adapts the three common firewalls on FreeBSD (IPFW, PF, and IPF). There is no need to configure all firewalls simultaneously; just choose one of them.
Installing Fail2Ban
The installation process for Fail2Ban is as follows. Users can choose to install via pkg or Ports.
Install using pkg:
# pkg install security/py-fail2banOr install using Ports:
# cd /usr/ports/security/py-fail2ban/
# make install cleanAfter installation, the service must be enabled to start automatically at boot.
Enable the fail2ban service:
# service fail2ban enableViewing Post-Installation Notes for Fail2Ban
After installation, the following command can be used to view the configuration notes provided by the package.
# pkg info -D security/py-fail2banFail2Ban Configuration Guide
The following explains the configuration of Fail2Ban.
Note
The term
jailhere does not refer to a BSD system jail (container). In Fail2Ban, a jail is a monitoring and banning configuration unit for a specific service, where each jail corresponds to a service that needs protection (such as SSH, FTP, etc.).
Fail2Ban's workflow consists of two parts: filters parse logs to identify failed login attempts, and actions invoke firewall rules to execute bans.
The related file structure:
The configuration file example is the /usr/local/etc/fail2ban/jail.conf file (do not edit it directly, see the notes above). Below only the content needed for this book is listed:
① List Fail2Ban filter options:
Note that files starting with bsd- (such as bsd-sshd.conf) are variants provided by the FreeBSD Port maintainer for specific environment adaptation, but since sshd.conf has better compatibility with the standard Fail2Ban configuration, it should be used directly in this section.
② View firewalls supported by Fail2Ban:
Fail2Ban Ban Configuration
Create and edit the file /usr/local/etc/fail2ban/jail.d/sshd.conf with the following content:
Tip
The
192.168.0.0/24in the above example is a placeholder and must be replaced with the actual value.
Configuration notes:
Whitelist, indicating IP ranges that will not be banned. 192.168.0.0/24 represents the range from 192.168.0.0 to 192.168.0.255.
bsd-ipfwis the example firewall; you can choose your own, see below.
In FreeBSD, Fail2Ban provides two IPFW-related action configurations: ipfw and bsd-ipfw. The bsd-ipfw version is specifically optimized for FreeBSD and has better integration with the system.
Warning
If using the IPFW firewall, you must choose
bsd-ipfwinstead ofipfw, otherwise it will not take effect.
Configuring the Firewall
After completing the Fail2Ban configuration, the corresponding firewall must also be configured, and then the service started.
Start the Fail2Ban service:
IPFW
IPFW is the built-in firewall for FreeBSD. The Fail2Ban configuration is the same as above.
Configuring the Service to Start at Boot
Enable the firewall for automatic startup at boot:
Warning
Do not execute the
startcommand immediately, as this may prevent SSH connections.
Modifying the Default IPFW Rule
The default policy of IPFW rules is "default deny," meaning rule 65535 blocks all unmatched traffic. To avoid being locked out during configuration, you can first change it to "default allow":
Display the current IPFW firewall rule list:
References
FreeBSD Project. ipfw(8) - IP firewall and traffic shaper control program[EB/OL]. [2026-03-26]. https://man.freebsd.org/cgi/man.cgi?query=ipfw&sektion=8. Official FreeBSD firewall management tool reference manual, providing complete IPFW command documentation.
dapeng.li. fail2ban[EB/OL]. [2026-03-26]. https://dapeng.li/learning/fail2ban/d4.html. This section's architecture is based on this, which is a Fail2Ban and FreeBSD integration solution.
FreeBSD Project. Chapter 33. Firewalls[EB/OL]. [2026-04-14]. https://docs.freebsd.org/en/books/handbook/firewalls/. FreeBSD Handbook firewall chapter, introducing basic configuration for PF, IPFW, and IPFILTER.
FreeBSD Foundation. FreeBSD's Firewall Feast[EB/OL]. [2026-04-14]. https://freebsdfoundation.org/wp-content/uploads/2017/06/FreeBSDs-Firewall-Feast.pdf. Firewall comparison technical article published by the FreeBSD Foundation.
PF
PF (Packet Filter) is a firewall originating from OpenBSD, configured as follows.
In PF, tables are used to store lists of IP addresses that need to be banned, and anchors are used to organize and manage specific rule sets. Fail2Ban adds IP addresses to PF's table, then applies corresponding rules through anchors to enforce bans.
Fail2Ban Configuration File
Change action=bsd-ipfw in the configuration file /usr/local/etc/fail2ban/jail.d/sshd.conf above to action=pf[port={22 23}, name=ssh]. No other configuration items need to be modified.
Modifying the PF Configuration File
The PF configuration file must be prepared first. The related directory structure:
For PF to start properly, copy the sample PF configuration file to the /etc directory for modification and use:
Edit the /etc/pf.conf file and write:
em0: Theem0in the example is the network interface name and must be changed according to the actual network interface. You can use theifconfigcommand to check.
Service
The PF service startup and configuration are as follows.
References
Xavier Humbert. PF - fail2ban does not feed pf table[EB/OL]. [2026-03-26]. https://forums.freebsd.org/threads/fail2ban-does-not-feed-pf-table.67798/. FreeBSD community forum discussion on Fail2Ban and PF integration issues.
mfechner. Fail2ban on FreeBSD drops complete pf firewall rules[EB/OL]. [2026-03-26]. https://github.com/fail2ban/fail2ban/issues/1915. GitHub Issue documenting Fail2Ban and PF rule conflict issues.
IPFILTER (IPF)
IPFILTER (IPF) is an open-source firewall, configured as follows.
Fail2Ban Configuration File
The ipfilter action invokes IPF commands to add and remove ban rules, so change action=bsd-ipfw in the configuration file /usr/local/etc/fail2ban/jail.d/sshd.conf above to action=ipfilter. No other configuration items need to be modified.
Service
The IPF service configuration and startup are as follows.
For IPF to start and work properly, copy the sample file as the default configuration rule set file (the rules included in the sample file do not affect usage):
Configure the daemon:
Testing the Effect
After configuration is complete, you can test whether Fail2Ban can properly ban IPs. Use the Fail2Ban client to manually add an IP to the ban list to verify functionality.
To see the effect, use the Fail2Ban client to add IP 192.168.179.1 to the ban list under sshd monitoring:
TTY output: This output shows that the SSH service detected multiple authentication failures from 192.168.179.1.
Established SSH connections will also be forcibly disconnected.
Viewing Status
You can view the running status of Fail2Ban with the following command.
View the status of Fail2Ban's sshd monitoring, including the list of banned IPs:
Unbanning an IP
If you need to unban a specific IP, you can perform the following operation.
Unban the specified IP from sshd monitoring:
Troubleshooting and Remaining Issues
If you encounter problems, you can check the logs for troubleshooting.
Fail2Ban logs are located in the /var/log/fail2ban.log file.
Last updated