Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nextcloud-auth and nextcloud-domain filters #3581

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Deric-W
Copy link

@Deric-W Deric-W commented Sep 24, 2023

Since nextcloud has a hardening guide which contains a fail2ban filter I decided that it would be useful for including it here (split into two filters to allow different jails).

Since the original filter fails when using the systemd journal I modified it to accept the prefix line (optional) from common.conf.

Before submitting your PR, please review the following checklist:

  • CHOOSE CORRECT BRANCH: if filing a bugfix/enhancement
    against certain release version, choose 0.9, 0.10 or 0.11 branch,
    for dev-edition use master branch
  • CONSIDER adding a unit test if your PR resolves an issue
  • LIST ISSUES this PR resolves
  • MAKE SURE this PR doesn't break existing tests
  • KEEP PR small so it could be easily reviewed.
  • AVOID making unnecessary stylistic changes in unrelated code
  • ACCOMPANY each new failregex for filter X with sample log lines
    within fail2ban/tests/files/logs/X file

@Deric-W Deric-W changed the title Nextcloud add nextcloud-auth and nextcloud-domain filters Sep 24, 2023
@Deric-W
Copy link
Author

Deric-W commented Sep 24, 2023

Do substitutions have to be enclosed in () if you want to apply ? to them?

config/filter.d/nextcloud-auth.conf Outdated Show resolved Hide resolved
config/filter.d/nextcloud-auth.conf Outdated Show resolved Hide resolved
config/filter.d/nextcloud-auth.conf Outdated Show resolved Hide resolved
fail2ban/tests/files/logs/nextcloud-auth Outdated Show resolved Hide resolved
@Neustradamus
Copy link

@Deric-W: Have you seen comments on your PR?

@Deric-W
Copy link
Author

Deric-W commented Jan 18, 2024

Sorry for the very long delay, I totally forgot about this PR.
I will address them when I am back home.

The filters are based on the one in the hardening guide
but split to allow different jails.
@Deric-W
Copy link
Author

Deric-W commented Jan 18, 2024

I noticed that the group regex (?:(?:,?\s*"\w+":(?:"[^"]+"|\w+))*) is vulnerable against input containing quoted quotation marks (\") which causes it to think that a group has ended and fail the match when a group like ...,"url":"/login\"",... would appear in the part we want to match.
I am not sure if this is possible in practice since all groups we want to match in the example log lines are either not directly attacker controlled or url encoded.

I fixed it by modifying the group regex with (?:(?:,?\s*"\w+":(?:"(?:[^"\\]|\\"|\\\\)*"|\w+))*), can you spot any problems with this regex?

@sebres
Copy link
Contributor

sebres commented Jan 19, 2024

can you spot any problems with this regex?

yes, because now [^"\\] would stop on backslash too, and it can be used to escape any char, so theoretically it must be:

- (?:(?:,?\s*"\w+":(?:"(?:[^"\\]|\\"|\\\\)*"|\w+))*)
+ (?:(?:,?\s*"\w+":(?:"(?:[^"\\]+|\\.)*"|\w+))*)

(also note the + quantifier after [^"\\] - that makes regular case much faster)

It was moved to `nextcloud-common.conf`
@Deric-W
Copy link
Author

Deric-W commented Jan 20, 2024

Thanks for catching the mistake 👍
The performance optimization however seems to cause exponential backtracking which makes the test suite hang.
I removed the + quantifier and moved the date pattern and some parts to a nextcloud-common.conf (based on apache-common.conf).

@Deric-W Deric-W requested a review from sebres January 28, 2024 03:09
@yitzhaq
Copy link

yitzhaq commented Feb 2, 2024

One problem with using this filter - though not a problem with the regex or anything here per se - is that it is prone to false positives because the logs don't actually say anything about why the login failed. It could be because of a genuine invalid login and/or brute force attempt, sure, but the logs will look identical if there is some sort of problem with the backend authentication provider, triggering a slew of false alarms. The logged statement is true in both cases, in that the login did indeed fail, but some more detail (like what eg. Dovecot does) would allow to differentiate between these two very different problems, and make it possible to adjust the filter regex accordingly to achieve higher accuracy.

Not something that can be fixed here of course, as this is clearly a Nextcloud bug (or at least I would consider it to be one), but perhaps something to note as a comment, a possible gotcha worth being aware of? I at least had to disable my Nextcloud jail because I could not find any way to prevent this from happening.

@anantakrishna
Copy link

I guess this PR makes #1913 outdated. Does it?

@Deric-W
Copy link
Author

Deric-W commented Apr 29, 2024

I think so, but I noticed that the mentioned PR seems to expect a different nextcloud log format (probably because it is 7 years old).

@joshtrichards
Copy link

@yitzhaq Feel free to bring up your suggestions at the Nextcloud Server (if logging behavior changes are required) and/or Documentation (if rule changes are required) repositories. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants