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

Feat: Add filter for filtering ufw blocked events #2225

Open
wants to merge 4 commits into
base: 0.11
Choose a base branch
from

Conversation

mbologna
Copy link
Contributor

Fail2ban filter configuration for ufw blocked events (typically port scans)

By default every connection attempt blocked by ufw will be logged, e.g.:

Sep 14 20:56:41 sierra kernel: [UFW BLOCK] [...] SRC=<REDACTED> DST=<REDACTED> LEN=40 TOS=0x04 PREC=0x00 TTL=48 ID=5614 PROTO=TCP SPT=34092 DPT=23 WINDOW=43012 RES=0x00 SYN URGP=0

By carefully setting this filter we ban every IP that tries too many times to connect to non-allowed ports.

@sebres
Copy link
Contributor

sebres commented Sep 14, 2018

Just a notice to me / link #1945 (similar thing for iptables, port scans, etc).

Fail2ban filter configuration for ufw blocked events (typically port scans)

By default every connection attempt blocked by ufw will be logged, e.g.:

    Sep 14 20:56:41 sierra kernel: [UFW BLOCK] [...] SRC=<REDACTED> DST=<REDACTED> LEN=40 TOS=0x04 PREC=0x00 TTL=48 ID=5614 PROTO=TCP SPT=34092 DPT=23 WINDOW=43012 RES=0x00 SYN URGP=0

By carefully setting this filter we ban every IP that tries
too many times to connect to non-allowed ports.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.0003%) to 97.775% when pulling e8d1f07 on mbologna:ufw-port-scan into 8a0c06b on fail2ban:0.11.

@coveralls
Copy link

coveralls commented Sep 14, 2018

Coverage Status

Coverage remained the same at 97.774% when pulling e8d1f07 on mbologna:ufw-port-scan into 8a0c06b on fail2ban:0.11.

@mnbf9rca
Copy link

Is this going to be adopted?

@mbologna
Copy link
Contributor Author

mbologna commented Jan 23, 2021

Is this going to be adopted?

I hope so! Maintainer @sebres, what can I do to help you merge this?

@sebres
Copy link
Contributor

sebres commented Jan 23, 2021

I'm not a maintainer :) a dev either.
As for merge, in my opinion it is too risky to use such a filters (see #1945 (comment)) without to know what you do.
Anyway why do you want the filter will be added to stock fail2ban necessarily?
Everyone who needs it can do it simply adding such a jail (with empty filter and direct failregex).

But OK... basically two things bother me here:

  1. there are dozen firewalls logging with iptables to kernel log with almost the same format... so would be absolutely nice if it can be made configurable (so we will not have later dozen filters doing basically the same thing).
  2. too vulnerable failregex (for example take a look aforementioned comment). Just to compare how it could be:
- .*\[UFW BLOCK\] IN=.* SRC=<HOST>
+ # ^\s*\S+ kernel:(?: +\[[^\]]+\])? \[UFW BLOCK\](?: (?:IN=\w+|OUT=|(?:(?!OUT=|IN=|SRC=)[A-Z]+=\S*)))* SRC=<ADDR> DST=\S+
+ ^\s*\S+ kernel:(?: +\[[^\]]+\]){0,2}(?: (?:IN=\w+|OUT=|(?:(?!OUT=|IN=|SRC=)[A-Z]+=\S*)))* SRC=<ADDR> DST=\S+

it is anchored, does not contain catch-all's, monitoring incoming traffic only, etc

@OpenNya
Copy link

OpenNya commented Jun 3, 2021

^\s*\S+ kernel:(?: +\[[^\]]+\])?[^\n\[=]*(?:(?<= )(?:IN=\w+|OUT=|(?:(?!OUT=|IN=)[A-Z]+=[^ \[]*)+) )*SRC=<ADDR> DST=\S+

Regex proposed by you on Jan 23 is not working either (starting year 2020 I think).
I have checked it upon ufw.log syslog and kern.log.
Would you please to remove or correct it to not misguide people landed here by google search.

$ fail2ban-regex  /var/log/ufw.log '^\s*\S+ kernel:(?: +\[[^\]]+\])?[^\n\[=]*(?:(?<= )(?:IN=\w+|OUT=|(?:(?!OUT=|IN=)[A-Z]+=[^ \[]*)+) )*SRC=<ADDR> DST=\S+'

Running tests
=============
Use   failregex line : ^\s*\S+ kernel:(?: +\[[^\]]+\])?[^\n\[=]*(?:(?<= )...
Use         log file : /var/log/ufw.log
Use         encoding : UTF-8

Results
=======
Failregex: 0 total
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
|  [20478] {^LN-BEG}(?:DAY )?MON Day %k:Minute:Second(?:\.Microseconds)?(?: ExYear)?
`-
Lines: 20478 lines, 0 ignored, **0 matched, 20478 missed**
[processed in 0.65 sec]

@sebres
Copy link
Contributor

sebres commented Jun 4, 2021

Regex proposed by you on Jan 23 is not working either (starting year 2020 I think).

May be ufw blocked events simply not logged there? :) But if you are sure...

Would you please to remove or correct it to not misguide people landed here by google search.

I could, if you'd provide a "new" log-format you have now.

@OpenNya
Copy link

OpenNya commented Jun 4, 2021

I could, if you'd provide a "new" log-format you have now.

Original log but IPs are faked.
https://pastebin.com/Du6yuxJA

@sebres
Copy link
Contributor

sebres commented Jun 5, 2021

Done (I corrected the comment above also):

Here are the REs:

  1. for [UFW BLOCK]:
^\s*\S+ kernel:(?: +\[[^\]]+\])? \[UFW BLOCK\](?: (?:IN=\w+|OUT=|(?:(?!OUT=|IN=|SRC=)[A-Z]+=\S*)))* SRC=<ADDR> DST=\S+
  1. common, for any other firewall logging with iptables to kernel log:
^\s*\S+ kernel:(?: +\[[^\]]+\]){0,2}(?: (?:IN=\w+|OUT=|(?:(?!OUT=|IN=|SRC=)[A-Z]+=\S*)))* SRC=<ADDR> DST=\S+

@OpenNya
Copy link

OpenNya commented Jun 5, 2021

Done (I corrected the comment above also)

Works perfectly.
I know this is not the best way to fight port scanners but it helps to block the most inadequate ones.
Thanks for the help!

@mbologna
Copy link
Contributor Author

Is this stale?

Copy link
Contributor

@sebres sebres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change...
However as already mentioned above it's too risky (because settings dependent and too error-prune), so I'm still not convinced fail2ban really needs filters like that...

config/filter.d/ufw-port-scan.conf Outdated Show resolved Hide resolved
@mbologna mbologna requested a review from sebres April 3, 2023 18:57
@cjwijtmans
Copy link

will this be released?

@sebres
Copy link
Contributor

sebres commented Oct 9, 2023

will this be released?

Maybe... (I already provided my thoughts about it).
Although nobody hinders one to use this filter/failregexe as it is.

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