Score:0

Unable to compile regular expression in Fail2Ban

ar flag

I'm trying to get this regex working in Fail2Ban:

SRC=(?<ADDR>.*) DST.*(?=DPT=5003)

In a regex tester it's working very fine. When testing in Fail2Ban, I get this error:

ERROR: Unable to compile regular expression 'SRC=(?\[?(?:(?:::f{4,6}:)?(?P<ip4>(?:\d{1,3}\.){3}\d{1,3})|(?P<ip6>(?:[0-9a-fA-F]{1,4}::?|::){1,7}(?:[0-9a-fA-F]{1,4}|(?<=:):)))\]?.*) DST.*(?=DPT=5003)'

This is a line from the log I want to crawl:

Jul 14 13:30:44 servername kernel: [  803.539059] [UFW BLOCK] IN=eth0 OUT= MAC=somemacadress SRC=somesourceip DST=somedestinationip LEN=60 TOS=0x00 PREC=0x00 TTL=53 ID=18692 DF PROTO=TCP SPT=50852 DPT=5003 WINDOW=14600 RES=0x00 SYN URGP=0

Basically I want to block an IP if there is a block on DPT=5003

Can somebody help?

Thanks!

jp flag
use `fail2ban-regex` to test your regex
error401 avatar
ar flag
I did, this is where the mentioned error is thrown.
Score:0
il flag

Normally it'd be enough to fix the typo - either use (?:...) note :, or remove group parenthesis:

- SRC=(?<ADDR>.*) DST.*(?=DPT=5003)
+ SRC=<ADDR>.* DST.*(?=DPT=5003)

But this RE is vulnerable (no anchors, contains catch-all's etc)... For better failregex example see https://github.com/fail2ban/fail2ban/pull/2225#issuecomment-766087078

Score:0
jp flag

The regex SRC=(?<ADDR>.*) DST.*(?=DPT=5003) uses PCRE named capture group syntax ?<ADDR> which fail2ban, being written in Python, doesn't understand and doesn't use. You need to use fail2ban own substitution <HOST> to capture IP or hostname. See the Developing Filters from fail2ban docs.

Update: <ADDR> substitution is also recognized, but isn't documented in the page I linked.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.