192 |
- |
1 |
# Fail2Ban filter for openssh
|
|
|
2 |
#
|
|
|
3 |
# If you want to protect OpenSSH from being bruteforced by password
|
|
|
4 |
# authentication then get public key authentication working before disabling
|
|
|
5 |
# PasswordAuthentication in sshd_config.
|
|
|
6 |
#
|
|
|
7 |
#
|
|
|
8 |
# "Connection from <HOST> port \d+" requires LogLevel VERBOSE in sshd_config
|
|
|
9 |
#
|
|
|
10 |
|
|
|
11 |
[INCLUDES]
|
|
|
12 |
|
|
|
13 |
# Read common prefixes. If any customizations available -- read them from
|
|
|
14 |
# common.local
|
|
|
15 |
before = common.conf
|
|
|
16 |
|
|
|
17 |
[DEFAULT]
|
|
|
18 |
|
|
|
19 |
_daemon = sshd
|
|
|
20 |
|
|
|
21 |
# optional prefix (logged from several ssh versions) like "error: ", "error: PAM: " or "fatal: "
|
|
|
22 |
__pref = (?:(?:error|fatal): (?:PAM: )?)?
|
|
|
23 |
# optional suffix (logged from several ssh versions) like " [preauth]"
|
|
|
24 |
#__suff = (?: port \d+)?(?: \[preauth\])?\s*
|
|
|
25 |
__suff = (?: (?:port \d+|on \S+|\[preauth\])){0,3}\s*
|
|
|
26 |
__on_port_opt = (?: (?:port \d+|on \S+)){0,2}
|
|
|
27 |
# close by authenticating user:
|
193 |
- |
28 |
__authng_user = (?: (?:invalid|authenticating) user <F-USER>\S+|.*?</F-USER>)?
|
192 |
- |
29 |
|
|
|
30 |
# for all possible (also future) forms of "no matching (cipher|mac|MAC|compression method|key exchange method|host key type) found",
|
|
|
31 |
# see ssherr.c for all possible SSH_ERR_..._ALG_MATCH errors.
|
|
|
32 |
__alg_match = (?:(?:\w+ (?!found\b)){0,2}\w+)
|
|
|
33 |
|
|
|
34 |
# PAM authentication mechanism, can be overridden, e. g. `filter = sshd[__pam_auth='pam_ldap']`:
|
|
|
35 |
__pam_auth = pam_[a-z]+
|
|
|
36 |
|
|
|
37 |
[Definition]
|
|
|
38 |
|
|
|
39 |
prefregex = ^<F-MLFID>%(__prefix_line)s</F-MLFID>%(__pref)s<F-CONTENT>.+</F-CONTENT>$
|
|
|
40 |
|
|
|
41 |
cmnfailre = ^[aA]uthentication (?:failure|error|failed) for <F-USER>.*</F-USER> from <HOST>( via \S+)?%(__suff)s$
|
|
|
42 |
^User not known to the underlying authentication module for <F-USER>.*</F-USER> from <HOST>%(__suff)s$
|
193 |
- |
43 |
<cmnfailre-failed-pub-<publickey>>
|
|
|
44 |
^Failed <cmnfailed> for (?P<cond_inv>invalid user )?<F-USER>(?P<cond_user>\S+)|(?(cond_inv)(?:(?! from ).)*?|[^:]+)</F-USER> from <HOST>%(__on_port_opt)s(?: ssh\d*)?(?(cond_user): |(?:(?:(?! from ).)*)$)
|
192 |
- |
45 |
^<F-USER>ROOT</F-USER> LOGIN REFUSED FROM <HOST>
|
|
|
46 |
^[iI](?:llegal|nvalid) user <F-USER>.*?</F-USER> from <HOST>%(__suff)s$
|
193 |
- |
47 |
^User <F-USER>\S+|.*?</F-USER> from <HOST> not allowed because not listed in AllowUsers%(__suff)s$
|
|
|
48 |
^User <F-USER>\S+|.*?</F-USER> from <HOST> not allowed because listed in DenyUsers%(__suff)s$
|
|
|
49 |
^User <F-USER>\S+|.*?</F-USER> from <HOST> not allowed because not in any group%(__suff)s$
|
192 |
- |
50 |
^refused connect from \S+ \(<HOST>\)
|
|
|
51 |
^Received <F-MLFFORGET>disconnect</F-MLFFORGET> from <HOST>%(__on_port_opt)s:\s*3: .*: Auth fail%(__suff)s$
|
193 |
- |
52 |
^User <F-USER>\S+|.*?</F-USER> from <HOST> not allowed because a group is listed in DenyGroups%(__suff)s$
|
|
|
53 |
^User <F-USER>\S+|.*?</F-USER> from <HOST> not allowed because none of user's groups are listed in AllowGroups%(__suff)s$
|
192 |
- |
54 |
^<F-NOFAIL>%(__pam_auth)s\(sshd:auth\):\s+authentication failure;</F-NOFAIL>(?:\s+(?:(?:logname|e?uid|tty)=\S*)){0,4}\s+ruser=<F-ALT_USER>\S*</F-ALT_USER>\s+rhost=<HOST>(?:\s+user=<F-USER>\S*</F-USER>)?%(__suff)s$
|
193 |
- |
55 |
^maximum authentication attempts exceeded for <F-USER>.*</F-USER> from <HOST>%(__on_port_opt)s(?: ssh\d*)?%(__suff)s$
|
|
|
56 |
^User <F-USER>\S+|.*?</F-USER> not allowed because account is locked%(__suff)s
|
192 |
- |
57 |
^<F-MLFFORGET>Disconnecting</F-MLFFORGET>(?: from)?(?: (?:invalid|authenticating)) user <F-USER>\S+</F-USER> <HOST>%(__on_port_opt)s:\s*Change of username or service not allowed:\s*.*\[preauth\]\s*$
|
193 |
- |
58 |
^Disconnecting: Too many authentication failures(?: for <F-USER>\S+|.*?</F-USER>)?%(__suff)s$
|
192 |
- |
59 |
^<F-NOFAIL>Received <F-MLFFORGET>disconnect</F-MLFFORGET></F-NOFAIL> from <HOST>%(__on_port_opt)s:\s*11:
|
|
|
60 |
<mdre-<mode>-other>
|
|
|
61 |
^<F-MLFFORGET><F-MLFGAINED>Accepted \w+</F-MLFGAINED></F-MLFFORGET> for <F-USER>\S+</F-USER> from <HOST>(?:\s|$)
|
|
|
62 |
|
193 |
- |
63 |
cmnfailed-any = \S+
|
|
|
64 |
cmnfailed-ignore = \b(?!publickey)\S+
|
|
|
65 |
cmnfailed-invalid = <cmnfailed-ignore>
|
|
|
66 |
cmnfailed-nofail = (?:<F-NOFAIL>publickey</F-NOFAIL>|\S+)
|
|
|
67 |
cmnfailed = <cmnfailed-<publickey>>
|
|
|
68 |
|
192 |
- |
69 |
mdre-normal =
|
|
|
70 |
# used to differentiate "connection closed" with and without `[preauth]` (fail/nofail cases in ddos mode)
|
|
|
71 |
mdre-normal-other = ^<F-NOFAIL><F-MLFFORGET>(Connection closed|Disconnected)</F-MLFFORGET></F-NOFAIL> (?:by|from)%(__authng_user)s <HOST>(?:%(__suff)s|\s*)$
|
|
|
72 |
|
|
|
73 |
mdre-ddos = ^Did not receive identification string from <HOST>
|
193 |
- |
74 |
^kex_exchange_identification: (?:[Cc]lient sent invalid protocol identifier|[Cc]onnection closed by remote host)
|
192 |
- |
75 |
^Bad protocol version identification '.*' from <HOST>
|
|
|
76 |
^<F-NOFAIL>SSH: Server;Ltype:</F-NOFAIL> (?:Authname|Version|Kex);Remote: <HOST>-\d+;[A-Z]\w+:
|
|
|
77 |
^Read from socket failed: Connection <F-MLFFORGET>reset</F-MLFFORGET> by peer
|
|
|
78 |
# same as mdre-normal-other, but as failure (without <F-NOFAIL>) and [preauth] only:
|
193 |
- |
79 |
mdre-ddos-other = ^<F-MLFFORGET>(Connection (?:closed|reset)|Disconnected)</F-MLFFORGET> (?:by|from)%(__authng_user)s <HOST>%(__on_port_opt)s\s+\[preauth\]\s*$
|
192 |
- |
80 |
|
193 |
- |
81 |
mdre-extra = ^Received <F-MLFFORGET>disconnect</F-MLFFORGET> from <HOST>%(__on_port_opt)s:\s*14: No(?: supported)? authentication methods available
|
192 |
- |
82 |
^Unable to negotiate with <HOST>%(__on_port_opt)s: no matching <__alg_match> found.
|
|
|
83 |
^Unable to negotiate a <__alg_match>
|
|
|
84 |
^no matching <__alg_match> found:
|
|
|
85 |
# part of mdre-ddos-other, but user name is supplied (invalid/authenticating) on [preauth] phase only:
|
|
|
86 |
mdre-extra-other = ^<F-MLFFORGET>Disconnected</F-MLFFORGET>(?: from)?(?: (?:invalid|authenticating)) user <F-USER>\S+|.*?</F-USER> <HOST>%(__on_port_opt)s \[preauth\]\s*$
|
|
|
87 |
|
|
|
88 |
mdre-aggressive = %(mdre-ddos)s
|
|
|
89 |
%(mdre-extra)s
|
|
|
90 |
# mdre-extra-other is fully included within mdre-ddos-other:
|
|
|
91 |
mdre-aggressive-other = %(mdre-ddos-other)s
|
|
|
92 |
|
193 |
- |
93 |
# Parameter "publickey": nofail (default), invalid, any, ignore
|
|
|
94 |
publickey = nofail
|
|
|
95 |
# consider failed publickey for invalid users only:
|
|
|
96 |
cmnfailre-failed-pub-invalid = ^Failed publickey for invalid user <F-USER>(?P<cond_user>\S+)|(?:(?! from ).)*?</F-USER> from <HOST>%(__on_port_opt)s(?: ssh\d*)?(?(cond_user): |(?:(?:(?! from ).)*)$)
|
|
|
97 |
# consider failed publickey for valid users too (don't need RE, see cmnfailed):
|
|
|
98 |
cmnfailre-failed-pub-any =
|
|
|
99 |
# same as invalid, but consider failed publickey for valid users too, just as no failure (helper to get IP and user-name only, see cmnfailed):
|
|
|
100 |
cmnfailre-failed-pub-nofail = <cmnfailre-failed-pub-invalid>
|
|
|
101 |
# don't consider failed publickey as failures (don't need RE, see cmnfailed):
|
|
|
102 |
cmnfailre-failed-pub-ignore =
|
|
|
103 |
|
192 |
- |
104 |
cfooterre = ^<F-NOFAIL>Connection from</F-NOFAIL> <HOST>
|
|
|
105 |
|
|
|
106 |
failregex = %(cmnfailre)s
|
|
|
107 |
<mdre-<mode>>
|
|
|
108 |
%(cfooterre)s
|
|
|
109 |
|
|
|
110 |
# Parameter "mode": normal (default), ddos, extra or aggressive (combines all)
|
|
|
111 |
# Usage example (for jail.local):
|
|
|
112 |
# [sshd]
|
|
|
113 |
# mode = extra
|
|
|
114 |
# # or another jail (rewrite filter parameters of jail):
|
|
|
115 |
# [sshd-aggressive]
|
|
|
116 |
# filter = sshd[mode=aggressive]
|
|
|
117 |
#
|
|
|
118 |
mode = normal
|
|
|
119 |
|
|
|
120 |
#filter = sshd[mode=aggressive]
|
|
|
121 |
|
|
|
122 |
ignoreregex =
|
|
|
123 |
|
|
|
124 |
maxlines = 1
|
|
|
125 |
|
|
|
126 |
journalmatch = _SYSTEMD_UNIT=sshd.service + _COMM=sshd
|
|
|
127 |
|
|
|
128 |
# DEV Notes:
|
|
|
129 |
#
|
|
|
130 |
# "Failed \S+ for .*? from <HOST>..." failregex uses non-greedy catch-all because
|
|
|
131 |
# it is coming before use of <HOST> which is not hard-anchored at the end as well,
|
|
|
132 |
# and later catch-all's could contain user-provided input, which need to be greedily
|
|
|
133 |
# matched away first.
|
|
|
134 |
#
|
|
|
135 |
# Author: Cyril Jaquier, Yaroslav Halchenko, Petr Voralek, Daniel Black and Sergey Brester aka sebres
|
|
|
136 |
# Rewritten using prefregex (and introduced "mode" parameter) by Serg G. Brester.
|