This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| computing:fail2ban [2025/12/09 04:30] – oemb1905 | computing:fail2ban [2026/03/22 21:31] (current) – oemb1905 | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| * **Jonathan Haack** | * **Jonathan Haack** | ||
| * **Haack' | * **Haack' | ||
| - | * **oemb1905@jonathanhaack.com** | + | * **support@haacksnetworking.org** |
| ------------------------------------------- | ------------------------------------------- | ||
| Line 11: | Line 11: | ||
| ------------------------------------------- | ------------------------------------------- | ||
| - | This tutorial is designed to help you install fail2ban and get a basic set of configurations in place. | + | This tutorial is designed to help you install fail2ban and get a basic set of configurations in place. |
| + | |||
| + | //My defaults give a very small 1 hour punishment for first time offense of 3 violations of any jail. Repeat offenders, however, get immediate life-bans. So, the default is very tolerant and the extreme is essentially for life punishment. Using this framework, you override individual services with '' | ||
| + | |||
| + | You will need to change this recipe to fit your needs. At the same time, there are certain aspects in this tutorial, that are just " | ||
| sudo cp / | sudo cp / | ||
| Line 21: | Line 25: | ||
| <action = %(action_mwl)s> | <action = %(action_mwl)s> | ||
| - | If you attempt to log in via ssh and fail within any 4 hour period 4 different times, then you are immediately blocked | + | Default policy targets the middle |
| [DEFAULT] | [DEFAULT] | ||
| - | bantime | + | bantime |
| - | findtime | + | findtime |
| - | maxretry = 4 | + | maxretry = 3 |
| - | TheIf over the last 3 weeks the ip address in question was banned at least twice, then the stricter ban of 20 weeks takes effect. | + | Increase db purge age so as to retain enough |
| - | + | ||
| - | [recidive] | + | |
| - | enabled = true | + | |
| - | logpath | + | |
| - | banaction = %(banaction_allports)s | + | |
| - | bantime | + | |
| - | findtime = 3w | + | |
| - | maxretry = 2 | + | |
| - | + | ||
| - | In order for this to work, the database | + | |
| sudo nano / | sudo nano / | ||
| < | < | ||
| - | | + | |
| - | Okay, so far, fail2ban is installed, configured, and its service might even be running post-installation, | + | Add '' |
| [sshd] | [sshd] | ||
| Line 50: | Line 44: | ||
| logpath = %(sshd_log)s | logpath = %(sshd_log)s | ||
| backend = %(sshd_backend)s | backend = %(sshd_backend)s | ||
| + | maxretry = 3 | ||
| - | After enabling the elements you are in need of, and once all of your other basic configuration changes are done, restart the service, and then check the logs to verify functionality and debug as needed. | + | The repeat offender, or recidivist jail, is listed under '' |
| + | |||
| + | [recidive] | ||
| + | enabled | ||
| + | logpath | ||
| + | banaction = iptables-allports[blocktype=DROP] | ||
| + | bantime | ||
| + | maxretry | ||
| + | |||
| + | Here's an example of keeping postfix more tolerant, so that you don't get false positives on more common services while users are setting up stuff or accessing public facing resources: | ||
| + | |||
| + | [apache-auth] | ||
| + | enabled | ||
| + | port = http, | ||
| + | logpath | ||
| + | maxretry = 5 # | ||
| + | |||
| + | Once you activate desired jails, restart service or reload config: | ||
| | | ||
| - | sudo systemctl restart fail2ban.service | + | sudo systemctl restart fail2ban.service |
| - | sudo tail -f / | + | sudo fail2ban-client |
| - | sudo iptables -L f2b-sshd | + | |
| - | sudo fail2ban-client | + | |
| Hope this helps! | Hope this helps! | ||
| Line 83: | Line 93: | ||
| To check a particular jail's statistics: | To check a particular jail's statistics: | ||
| - | sudo fail2ban-client status | + | sudo fail2ban-client status |
| + | |||
| + | Install rpl and use it to change default banaction to DROP: | ||
| + | |||
| + | sudo apt install rpl | ||
| + | sudo rpl -q ' | ||
| + | sudo rpl -q ' | ||
| + | sudo fail2ban-client reload | ||
| + | |||
| + | Small script / one-liner to avoid remembering iptables flags for jails I monitor a lot: | ||
| + | |||
| + | cat << ' | ||
| + | # | ||
| + | iptables -L f2b-recidive -v -n | ||
| + | EOF | ||
| + | chmod 750 / | ||
| + | |||
| + | Change all reject rules to drop for a given iptables fail2ban managed jail/ | ||
| + | |||
| + | sudo iptables -L f2b-recidive -n --line-numbers | grep REJECT | awk ' | ||
| + | |||
| + | Script, '' | ||
| + | |||
| + | < | ||
| + | sudo cat > / | ||
| + | # | ||
| + | # / | ||
| + | |||
| + | #header | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Get list of jails | ||
| + | jails=$(sudo fail2ban-client status | grep "Jail list" | sed ' | ||
| + | |||
| + | for jail in $jails; do | ||
| + | stats=$(sudo fail2ban-client status " | ||
| + | if [ -z " | ||
| + | printf "%-30s | inactive or error\n" | ||
| + | continue | ||
| + | fi | ||
| + | |||
| + | banned=$(echo " | ||
| + | failed=$(echo " | ||
| + | tbanned=$(echo " | ||
| + | actions=$(echo " | ||
| + | |||
| + | printf "%-30s | %10s | %12s | %12s | %12s\n" | ||
| + | done | ||
| + | EOF | ||
| + | |||
| + | sudo chmod 750 / | ||
| + | </ | ||
| + | |||
| + | A small script that I wrote before I knew how to write systemd units that checks and restarts the service: | ||
| + | |||
| + | < | ||
| + | sudo cat > / | ||
| + | # | ||
| + | RESTART="/ | ||
| + | STATUS="/ | ||
| + | SERVICE=" | ||
| + | LOGFILE="/ | ||
| + | if $STATUS | grep -q -E ' | ||
| + | echo " | ||
| + | echo " | ||
| + | $RESTART >> " | ||
| + | echo " | ||
| + | # Send email with the log content | ||
| + | mail -s " | ||
| + | fail2ban@haacksnetworking.org < " | ||
| + | else | ||
| + | # Optional: log successful check (uncomment if desired) | ||
| + | # echo " | ||
| + | exit 0 | ||
| + | fi | ||
| + | exit 0 | ||
| + | EOF | ||
| + | sudo chmod 750 / | ||
| + | </ | ||
| + | |||
| + | Here is another script that sends the fail2ban-stats report to an email of one's choosing: | ||
| + | |||
| + | < | ||
| + | sudo cat > / | ||
| + | # | ||
| + | DATE=$(date +" | ||
| + | LOG="/ | ||
| + | |||
| + | # create log (touch is idempotent) | ||
| + | touch " | ||
| + | |||
| + | # generate report | ||
| + | echo " | ||
| + | /bin/bash / | ||
| + | |||
| + | #mail log | ||
| + | mail -s " | ||
| + | rm " | ||
| + | EOF | ||
| + | |||
| + | mkdir -p / | ||
| + | sudo chmod 750 / | ||
| + | </ | ||
| + | |||
| + | Run it hourly: | ||
| + | |||
| + | 0 * * * * / | ||
| + | |||
| + | Create a custom jail for postfix floods, for example: | ||
| + | |||
| + | < | ||
| + | cat << ' | ||
| + | |||
| + | [postfix-flood-attack] | ||
| + | enabled | ||
| + | maxretry = 1 | ||
| + | filter | ||
| + | action | ||
| + | logpath | ||
| + | |||
| + | EOF | ||
| + | |||
| + | # 2. Create the filter definition | ||
| + | cat << ' | ||
| + | [Definition] | ||
| + | failregex = lost connection after AUTH from (.*)\[< | ||
| + | ignoreregex = | ||
| + | EOF | ||
| + | |||
| + | # Set permissions and reload the jail | ||
| + | chmod 750 / | ||
| + | systemctl restart fail2ban.service | ||
| + | </ | ||
| - | --- // | + | --- // |