This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| computing:fail2ban [2026/03/21 22:00] – oemb1905 | computing:fail2ban [2026/03/22 21:31] (current) – oemb1905 | ||
|---|---|---|---|
| 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 40: | Line 44: | ||
| logpath = %(sshd_log)s | logpath = %(sshd_log)s | ||
| backend = %(sshd_backend)s | backend = %(sshd_backend)s | ||
| - | maxretry = 1 | + | maxretry = 3 |
| The repeat offender, or recidivist jail, is listed under '' | The repeat offender, or recidivist jail, is listed under '' | ||
| [recidive] | [recidive] | ||
| - | enabled | + | enabled |
| - | logpath | + | logpath |
| - | banaction | + | banaction = iptables-allports[blocktype=DROP] |
| - | banaction_allports | + | bantime |
| - | bantime | + | maxretry |
| - | 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: | 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: | ||
| Line 113: | Line 116: | ||
| Script, '' | Script, '' | ||
| - | < | + | < |
| + | sudo cat > / | ||
| #!/bin/bash | #!/bin/bash | ||
| # / | # / | ||
| Line 131: | Line 135: | ||
| fi | fi | ||
| - | # Safer extraction without variable-length lookbehind | ||
| banned=$(echo " | banned=$(echo " | ||
| failed=$(echo " | failed=$(echo " | ||
| tbanned=$(echo " | tbanned=$(echo " | ||
| - | actions=$(echo " | + | actions=$(echo " |
| printf "%-30s | %10s | %12s | %12s | %12s\n" | printf "%-30s | %10s | %12s | %12s | %12s\n" | ||
| done | 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 > / | ||
| + | #!/bin/bash | ||
| + | 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 > / | ||
| + | #!/bin/bash | ||
| + | DATE=$(date +" | ||
| + | LOG="/ | ||
| + | |||
| + | # create log (touch is idempotent) | ||
| + | touch " | ||
| + | |||
| + | # generate report | ||
| + | echo " | ||
| + | /bin/bash / | ||
| + | |||
| + | #mail log | ||
| + | mail -s " | ||
| + | rm " | ||
| + | EOF | ||
| + | |||
| + | mkdir -p /home/logs | ||
| + | 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 | ||
| </ | </ | ||
| - | --- // | + | --- // |