1001Ferramentas
🚧 Dev

Fail2ban Filter Generator

Build a Fail2ban filter with failregex to detect intrusion attempts in custom logs (failed login, 401, 403).

/etc/fail2ban/filter.d/<name>.conf

Writing a fail2ban filter that actually matches

fail2ban reads logs, looks for lines indicating a failed attempt and bans the source address once they repeat. The filter is what does the recognising: a file holding one or more regular expressions. Writing that file is where most configurations stall, because a regex matching nothing raises no error — it simply never bans anybody.

Enter the filter name and the expressions and the page assembles the file, already carrying the path where it should be saved and the line referencing it from the jail. The detail that decides everything is the host marker: fail2ban replaces that tag with the pattern recognising IPv4 and IPv6 addresses, and that is where it takes the address to ban from. A regex without that marker matches the line but identifies nobody.

Test before shipping. fail2ban's test command takes a log file and a filter and reports how many lines matched and which addresses were extracted — it is the difference between finding the mistake now or three weeks from now, when someone asks why nobody was ever banned. It also pays to take ignoreregex seriously so you do not ban yourself: monitoring and health checks produce exactly the kind of line the filter looks for.

Frequently asked questions

How do I test a filter without waiting for an attack?
With fail2ban-regex followed by the log path and the filter path. It prints how many lines matched per expression and lists the extracted addresses. Zero matches against a log full of attempts means the regex is wrong — nearly always because of the date format or spacing.
Why is the host marker mandatory?
Because it is how fail2ban knows what to ban. It expands internally into a pattern accepting IPv4, IPv6 and, depending on configuration, hostnames. Writing your own IP pattern instead tends to work in testing and fail with IPv6 in production.
Are a filter and a jail the same thing?
No. The filter only says what counts as a failed attempt. The jail ties the ends together: which log to read, which filter to use, how many failures are tolerated, how long the ban lasts and which action to run. A filter no jail references does nothing.

Related Tools