This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| computing:fail2ban [2019/11/03 01:41] – oemb1905 | computing:fail2ban [2026/03/22 21:31] (current) – oemb1905 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ------------------------------------------- | ||
| + | * **fail2ban** | ||
| + | * **Jonathan Haack** | ||
| + | * **Haack' | ||
| + | * **support@haacksnetworking.org** | ||
| + | ------------------------------------------- | ||
| + | // | ||
| + | ------------------------------------------- | ||
| + | |||
| + | This tutorial is designed to help you install fail2ban and get a basic set of configurations in place. My logic is as follows: | ||
| + | |||
| + | //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 11: | 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 |
| - | The recidive filter below states that the last 3 weeks will be reviewed and if the ip address in question was banned more for 2 or more of those weeks, then the stricter ban of 20 weeks takes effect. | + | Increase db purge age so as to retain enough |
| - | [recidive] | + | |
| + | < | ||
| + | |||
| + | Add '' | ||
| + | |||
| + | | ||
| enabled = true | enabled = true | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | In order for this to work, the database purge parameter needs to be adjusted | + | The repeat offender, or recidivist jail, is listed under '' |
| - | | + | |
| - | | + | enabled |
| + | logpath | ||
| + | | ||
| + | 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: | ||
| - | Once your configuration changes are done, restart | + | [apache-auth] |
| + | enabled | ||
| + | port = http, | ||
| + | logpath | ||
| + | maxretry = 5 #increased to 5 | ||
| + | |||
| + | Once you activate desired jails, restart service | ||
| | | ||
| - | sudo systemctl restart fail2ban.service | + | sudo systemctl restart fail2ban.service |
| - | sudo tail -f / | + | sudo fail2ban-client reload |
| - | sudo iptables -L f2b-sshd | + | |
| + | Hope this helps! | ||
| + | |||
| + | fail2ban-client set ssh unbanip 10.xx.15x.12x | ||
| + | fail2ban-client unban --all | ||
| + | |||
| + | Another method that does more than individual services, and instead zaps all records: | ||
| + | |||
| + | sudo systemctl stop fail2ban | ||
| + | sudo truncate -s 0 / | ||
| + | sudo rm / | ||
| + | sudo systemctl restart fail2ban | ||
| + | |||
| + | Systemd log issues. Change the sshd jail as follows | ||
| + | |||
| + | sudo nano / | ||
| + | backend = systemd | ||
| + | #backend = %(sshd_backend)s | ||
| + | |||
| + | Some recommend adding backend = systemd into jail.conf, but I've found that does nothing. The error over ipv6 not being set and using auto can be removed as follows: | ||
| + | |||
| + | sudo nano / | ||
| + | ' | ||
| + | |||
| + | To check a particular jail's statistics: | ||
| + | |||
| + | sudo fail2ban-client status recidive | ||
| + | |||
| + | 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 << ' | ||
| + | # | ||
| + | | ||
| + | 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 | ||
| - | Hope this helps! | + | # Set permissions and reload the jail |
| + | chmod 750 / | ||
| + | systemctl restart fail2ban.service | ||
| + | </ | ||
| - | --- //[[oemb1905@jonathanhaack.com|oemb1905]] | + | --- //[[alerts@haacksnetworking.org|oemb1905]] |