matrix-docker-ansible-deploy icon indicating copy to clipboard operation
matrix-docker-ansible-deploy copied to clipboard

Feature Request: Implement fail2ban optional activation for Synapse

Open AnonyPla opened this issue 3 years ago • 12 comments

/etc/fail2ban/filter.d/matrix-synapse.conf

[Definition]
failregex = .*synapse.access.http.8008 - .* INFO - POST-[0-9]{1,10} - <HOST> - 8008 - {None} Processed request:.* 52B 403 "POST \/_matrix\/client\/r0\/login.*$

ignoreregex =

/etc/fail2ban/jail.d/matrix-synapse.conf

[matrix-synapse]
enabled = true
backend = systemd
journalmatch = _SYSTEMD_UNIT=matrix-synapse.service
bantime  = 1d
findtime  = 10m
maxretry = 1
maxmatches = %(maxretry)s
banaction = iptables-allports
chain = FORWARD

And finally in the matrix-synapse matrix.yourdomain.com.log.config

Change the following from WARNING to INFO:

loggers:
    synapse:
        level: INFO

This enables fail2ban to jail failed login attempts (bad password and/or user not existing) at the cost of switching logging from WARNING to INFO.

To mitigate this on small servers, a limit can be set on the logging within /etc/systemd/journald.conf

By adding the following line: MaxRetentionSec=1day and possibly also adding a cronjob to purge logs older than x, example: 0 6 * * * journalctl --vacuum-time=1d

Edit: As a bonus, this configuration will also protect Synapse Admin from same attempts.

AnonyPla avatar Nov 27 '21 10:11 AnonyPla

Does this only work on systemctl version of it or also the docker version

shreyasajj avatar Nov 29 '21 23:11 shreyasajj

Looks nice! Thank you for the research and getting it organized like that, @AnonyPla!

At the very least, we should document this somewhere in docs/.

It would otherwise be great if we could optionally set it all (most) up automatically via a new role (matrix-fail2ban?). For this, in the spirit of this playbook ("all services running in containers"), we'd need to be able run fail2ban in a container, which is probably tricky.

spantaleev avatar Nov 30 '21 06:11 spantaleev

Brief research suggests it is doable.

  • https://stackoverflow.com/questions/68609694/how-to-implement-fail2ban-inside-a-docker-container-running-nginx
  • https://github.com/crazy-max/docker-fail2ban

altsalt avatar Nov 30 '21 14:11 altsalt

synapse is not on 8448? The regex should be something like that: failregex = .::ffff:<HOST> - 8448 - Received request: POST.\n.Got login request.\n.Attempted to login as. .::ffff:<HOST> - 8448 - Received request: POST.\n.Got login request.\n.Failed password login.

If the implementation will be for both Synapse and Dendrite, will be great :)

TheLillo avatar Feb 10 '22 21:02 TheLillo

I tinkered a bit with that stuff and switched over to crowdsec reading from journald. Pattern writing was way easier using grok than regex.

ToeiRei avatar Aug 10 '22 22:08 ToeiRei

Any Update about this?

juergen852 avatar Nov 21 '22 22:11 juergen852

I was hoping to swing back to this but have not yet found the space. @ToeiRei I haven't messed with CrowdSec but it looks like a reasonable option. I used to use SSHGuard and there are probably other options. Really someone just needs to take the initiative and draft up a PR.

altsalt avatar Nov 22 '22 19:11 altsalt

All you need is the http scenarios and you are well protected for the most part.

ToeiRei avatar Nov 22 '22 19:11 ToeiRei

I appreciate that they have an active docker hub image. To make use of the journalctl stuff, we'd need to use the Debian image rather than Alpine. I'm not sure whether this image is already being pulled in by matrix-docker-ansible-deploy and would definitely like to keep changes to the minimum.

I'm not sure when I'll have time to really dig into this but would love for someone to take it on.

altsalt avatar Nov 22 '22 19:11 altsalt

crowdsec also comes as a go binary (?) that you just unpack. So even alpine would work.

What I could do is writing a crowdsec pattern for specific attacks against matrix servers, if I get some log messages of such incidents

ToeiRei avatar Nov 22 '22 20:11 ToeiRei

I think part of this feature request was adding fail2ban (or CrowdSec) as a service that can be deployed with the script. This would help secure the system as a whole, rather than just Matrix stuff.

altsalt avatar Nov 22 '22 23:11 altsalt

I wouldn't install it as a container then and just use the debian package via ansible then as it's system level (iptables!)

ToeiRei avatar Nov 22 '22 23:11 ToeiRei