Hi Stéphane,
I can not get it to work
I tried the following:
- I created the file: /etc/fail2ban/filter.d/matrix-synapse.conf with the following content.
# matrix-synapse configuration file
#
[Init]
maxlines = 3
[Definition]
# Option: failregex
# Notes.: regex to match the password failures messages in the logfile. The
# host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
#
failregex = .*synapse.*8008.*\n.*synapse.*8008.*None - <HOST> - 8008.*403.*
#
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
- I checked this with fail2ban-regex and the correct lines are matched. This is an example of the lines to be matched.
2021-03-11 14:23:13,733 - synapse.http.server - 86 - INFO - None - <XForwardedForRequest at 0x7ff17faa3cc0 method='POST' uri='/_matrix/client/r0/login' clientproto='HTTP/1.1' site='8008'> SynapseError: 403 - Invalid password
2021-03-11 14:23:13,734 - synapse.access.http.8008 - 316 - INFO - None - 10.46.1.173 - 8008 - {None} Processed request: 0.038sec/-0.000sec (0.001sec, 0.000sec) (0.000sec/0.000sec/0) 52B 403 "POST /_matrix/client/r0/login HTTP/1.1" "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1" [0 dbevts]
- Although in the file itself it says not to alter anything because it will be overwritten (I think that is what @jookk is referring to… ) I added the following to /etc/fail2ban/jail.local
[matrix-synapse]
enabled = true
filter = matrix-synapse
logpath = /opt/synapse/homeserver.log
maxretry = 3
- To make sure everything that needs to be restarted is restarted, I reboot my NS VM. The fail2ban.log file shows (amongst others):
2021-03-11 16:20:54,252 fail2ban.filter [8431]: INFO Added logfile: '/opt/synapse/homeserver.log' (pos = 31016, hash = ba87bf00f873627c41466c6f317b9371)
2021-03-11 16:20:54,350 fail2ban.jail [8431]: INFO Jail 'matrix-synapse' started
- I test the setup by logging in from another machine. My failed attempts are registered by fail2ban and a ban is created:
2021-03-11 16:21:26,422 fail2ban.filter [8431]: INFO [matrix-synapse] Found 10.46.1.173 - 2021-03-11 16:21:26
2021-03-11 16:21:28,629 fail2ban.filter [8431]: INFO [matrix-synapse] Found 10.46.1.173 - 2021-03-11 16:21:28
2021-03-11 16:21:33,846 fail2ban.filter [8431]: INFO [matrix-synapse] Found 10.46.1.173 - 2021-03-11 16:21:33
2021-03-11 16:21:34,564 fail2ban.actions [8431]: NOTICE [matrix-synapse] Ban 10.46.1.173
- The ipset shows the ban
ipset list f2b-matrix-synapse
Name: f2b-matrix-synapse
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536 timeout 600
Size in memory: 216
References: 0
Number of entries: 1
Members:
10.46.1.173 timeout 1029
But the ipset is not in iptables:
# iptables -L | grep -i matrix
#
I once had an error in the fail2ban.log, but this has not shown up again:
2021-03-11 14:46:35,439 fail2ban.utils [10080]: ERROR 7f8579c13100 -- exec: ipset add f2b-matrix-synapse 10.46.1.173 timeout 1800 -exist
2021-03-11 14:46:35,439 fail2ban.utils [10080]: ERROR 7f8579c13100 -- stderr: 'ipset v7.1: The set with the given name does not exist'
2021-03-11 14:46:35,439 fail2ban.utils [10080]: ERROR 7f8579c13100 -- returned 1
2021-03-11 14:46:35,439 fail2ban.actions [10080]: ERROR Failed to execute ban jail 'matrix-synapse' action 'shorewall-ipset-proto6' info 'ActionInfo({'ip': '10.46.1.173', 'bantime': 1800, 'fid': <function <lambda> at 0x7f857b402cf8>, 'family': 'inet4', 'raw-ticket': <function <lambda> at 0x7f857b403320>})': Error banning 10.46.1.173
Have you got any idea what I can do to solve this?