linux:apps:fail2ban
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux:apps:fail2ban [2023/09/06 14:37] – [/etc/fail2ban/filter.d/www-login-fail.conf] oscar | linux:apps:fail2ban [2024/12/28 08:18] (current) – [/etc/fail2ban/filter.d/nginx-x00.conf] oscar | ||
|---|---|---|---|
| Line 276: | Line 276: | ||
| ===== Actions ===== | ===== Actions ===== | ||
| - | This file is responsible for setting up the firewall with a structure that allows easy modifications for banning malicious hosts, and for adding and removing those hosts as necessary. It is located in the following directory: **/ | + | This file is responsible for setting up the firewall with a structure that allows easy modifications for banning malicious hosts, and for adding and removing those hosts as necessary. It is located in the following directory: **/ |
| - | E.g. the action that our SSH service invokes is called | + | |
| - | sudo nano / | + | |
| - | With the comments removed, this file looks something like this: | + | |
| - | < | + | |
| - | [INCLUDES] | + | |
| - | before = iptables-blocktype.conf | + | |
| - | + | ||
| - | [Definition] | + | |
| - | actionstart = iptables -N fail2ban-< | + | |
| - | iptables -A fail2ban-< | + | |
| - | iptables -I < | + | |
| - | + | ||
| - | actionstop = iptables -D < | + | |
| - | + | ||
| - | actioncheck = iptables -n -L < | + | |
| - | + | ||
| - | actionban = iptables -I fail2ban-< | + | |
| - | + | ||
| - | actionunban = iptables -D fail2ban-< | + | |
| - | + | ||
| - | [Init] | + | |
| - | name = default | + | |
| - | port = ssh | + | |
| - | protocol = tcp | + | |
| - | chain = INPUT | + | |
| - | </ | + | |
| ==== Ban IP Range (subnet) ==== | ==== Ban IP Range (subnet) ==== | ||
| In some cases spammers have several systems infected in a specific subnet. By using different alternating IP nummers in this subnet, they can avoid Fail2Ban detection. In these cases it could be helpful to block a whole subnet range at once. | In some cases spammers have several systems infected in a specific subnet. By using different alternating IP nummers in this subnet, they can avoid Fail2Ban detection. In these cases it could be helpful to block a whole subnet range at once. | ||
| Line 532: | Line 505: | ||
| [Definition] | [Definition] | ||
| - | failregex = ^{" | + | # Blocking repeated 404|444|403|400 |
| + | # This will also match requests that are entirely empty | ||
| + | failregex = ^< | ||
| - | ignoreregex | + | datepattern |
| + | ^[^\[]*\[({DATE}) | ||
| + | {^LN-BEG} | ||
| + | |||
| + | journalmatch = _SYSTEMD_UNIT=nginx.service + _COMM=nginx | ||
| </ | </ | ||
| Line 578: | Line 557: | ||
| ignoreregex = | ignoreregex = | ||
| + | </ | ||
| + | ===== MAIL ===== | ||
| + | ==== / | ||
| + | Fail2ban depends on the log files of postfix, dovecot and rspamd. It should only be started after these services. To achieve this add these services into: | ||
| + | < | ||
| + | # nano / | ||
| + | ------------------------------------------- | ||
| + | [Unit] | ||
| + | ... | ||
| + | After=network.target iptables.service firewalld.service ip6tables.service ipset.service nftables.service dovecot.service postfix.service rspamd.service | ||
| </ | </ | ||
| + | |||
| + | ==== Config ==== | ||
| + | We enables the following 3 default Debian fail2ban installation jails for our mail server: | ||
| + | * **dovecot.conf**: | ||
| + | * **postfix.conf**: | ||
| + | * **postfix-sasl.conf**: | ||
| + | ==== / | ||
| + | < | ||
| + | [sshd] | ||
| + | |||
| + | # To use more aggressive sshd modes set filter parameter " | ||
| + | # normal (default), ddos, extra or aggressive (combines all). | ||
| + | # See " | ||
| + | enabled = true | ||
| + | #mode = normal | ||
| + | port = ssh | ||
| + | logpath = %(sshd_log)s | ||
| + | #backend = %(sshd_backend)s | ||
| + | backend = systemd | ||
| + | |||
| + | # | ||
| + | # Mail servers | ||
| + | # | ||
| + | [postfix] | ||
| + | # To use another modes set filter parameter " | ||
| + | enabled = true | ||
| + | mode = more | ||
| + | port = smtp, | ||
| + | bantime | ||
| + | findtime = 14400 | ||
| + | maxretry = 3 | ||
| + | #logpath = %(postfix_log)s | ||
| + | logpath = / | ||
| + | backend = %(postfix_backend)s | ||
| + | |||
| + | [postfix-rbl] | ||
| + | filter | ||
| + | port = smtp, | ||
| + | logpath | ||
| + | backend | ||
| + | maxretry = 1 | ||
| + | |||
| + | [postfix-sasl] | ||
| + | enabled | ||
| + | filter | ||
| + | port = smtp, | ||
| + | bantime | ||
| + | findtime = 43200 | ||
| + | maxretry = 2 | ||
| + | banaction = nftables-subnet[type=multiport] | ||
| + | # You might consider monitoring / | ||
| + | # running postfix since it would provide the same log lines at the | ||
| + | # " | ||
| + | # | ||
| + | logpath | ||
| + | backend | ||
| + | |||
| + | [sendmail-auth] | ||
| + | port = submission, | ||
| + | logpath = %(syslog_mail)s | ||
| + | backend = %(syslog_backend)s | ||
| + | |||
| + | [sendmail-reject] | ||
| + | # To use more aggressive modes set filter parameter " | ||
| + | # normal (default), extra or aggressive | ||
| + | # See " | ||
| + | #mode = normal | ||
| + | port = smtp, | ||
| + | logpath | ||
| + | backend | ||
| + | |||
| + | |||
| + | # dovecot defaults to logging to the mail syslog facility | ||
| + | # but can be set by syslog_facility in the dovecot configuration. | ||
| + | [dovecot] | ||
| + | enabled | ||
| + | port = pop3, | ||
| + | bantime | ||
| + | findtime = 43200 | ||
| + | maxretry = 2 | ||
| + | #logpath = %(dovecot_log)s | ||
| + | logpath = / | ||
| + | backend = %(dovecot_backend)s | ||
| + | |||
| + | |||
| + | [sieve] | ||
| + | port = smtp, | ||
| + | logpath = %(dovecot_log)s | ||
| + | backend = %(dovecot_backend)s | ||
| + | |||
| + | [mysqld-auth] | ||
| + | port = 3306 | ||
| + | logpath | ||
| + | backend | ||
| + | |||
| + | # Generic filter for PAM. Has to be used with action which bans all | ||
| + | # ports such as iptables-allports, | ||
| + | [pam-generic] | ||
| + | # pam-generic filter can be customized to monitor specific subset of ' | ||
| + | banaction = %(banaction_allports)s | ||
| + | logpath | ||
| + | backend | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| ===== Links ===== | ===== Links ===== | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
linux/apps/fail2ban.1694011024.txt.gz · Last modified: by oscar
