Fix cowrie honeypot filter per review (PR #4216)

Replace the custom Python Filter/backend hack with a plain regex
filter, per sebres's review: fail2ban filters shouldn't ship a
custom backend-style parser under config/filter.d, and the previous
prefregex/failregex pair was a no-op catch-all that didn't actually
filter anything.

- Drop cowrie_json.py entirely; parse the JSON log line directly
  with a bounded, anchored failregex (no unbounded .* catch-alls)
  using <ADDR> instead of <HOST> since only an IP is expected.
- Add the required sample log test file
  (fail2ban/tests/files/logs/cowrie_json) covering matched,
  non-matching event, and malformed lines.
This commit is contained in:
Jari Huttunen 2026-08-14 00:17:03 +03:00 • committed by sebres
parent ae4be51e16
commit 763a1c7f0c
3 changed files with 30 additions and 39 deletions

View file

@ -1,10 +1,18 @@
# Fail2Ban filter for Cowrie SSH/Telnet honeypot (https://github.com/cowrie/cowrie)
# Detects new connections logged in Cowrie's JSON output format
# (log.format = json / output.jsonlog in cowrie.cfg), one JSON object per line, e.g.:
# {"eventid": "cowrie.session.connect", "src_ip": "1.2.3.4", "src_port": 41000,
# "dst_ip": "192.168.1.1", "dst_port": 2222, "session": "1a2b3c4d",
# "protocol": "ssh", "message": "New connection: ...", "sensor": "myhost",
# "timestamp": "2024-01-01T00:00:00.000000Z"}
# As Cowrie is a honeypot, any connection to it is unsolicited by definition.
[Init]
datepattern = "timestamp": "%%ExY-%%m-%%dT%%H:%%M:%%S(?:\.%%f)?Z?"
[Definition]
# 1. Reset prefregex so Fail2ban passes the entire line to Python
prefregex = ^<F-CONTENT>.*</F-CONTENT>$
failregex = ^\{"eventid": "cowrie\.session\.connect", "src_ip": "<ADDR>",\s*(?:"[^"]+":\s*(?:"[^"]*"|-?\d+(?:\.\d+)?),?\s*){0,12}\}$
# 2. Syntax-valid regex containing <HOST> so Fail2ban's compiler doesn't throw an error
failregex = ^.*"src_ip"\s*:\s*"<HOST>".*$
# 3. Disable datepattern stripping so the JSON string arrives intact in Python
datepattern =
ignoreregex =

View file

@ -1,32 +0,0 @@
import json
import logging
from fail2ban.server.filter import Filter
logSys = logging.getLogger("fail2ban.filter")
class FileFilter(Filter):
"""
Custom Fail2ban Python Filter that parses Cowrie JSON natively.
"""
def processLine(self, line, flags=None):
try:
# Parse the JSON string
data = json.loads(line)
# Check for the target event
if data.get("eventid") == "cowrie.session.connect":
ip = data.get("src_ip")
if ip:
# Manually register the failure in Fail2ban's internal failManager
ticket = self.getFailTicket(ip, line=line)
self.failManager.addFailure(ticket)
logSys.info("[cowrie_json] Custom JSON match found IP: %s", ip)
except Exception as e:
# Silently ignore non-JSON or malformed log lines
pass
# Return super call so Fail2ban completes line processing
return super().processLine(line, flags)

View file

@ -0,0 +1,15 @@
# Cowrie honeypot JSON log (log.format = json)
# failJSON: { "time": "2024-01-01T00:00:00", "match": true, "host": "1.2.3.4" }
{"eventid": "cowrie.session.connect", "src_ip": "1.2.3.4", "src_port": 41000, "dst_ip": "192.168.1.1", "dst_port": 2222, "session": "1a2b3c4d", "protocol": "ssh", "message": "New connection: 1.2.3.4:41000 (192.168.1.1:2222) [session: 1a2b3c4d]", "sensor": "myhost", "timestamp": "2024-01-01T00:00:00.000000Z"}
# failJSON: { "time": "2024-01-01T00:05:12", "match": true, "host": "2001:db8::1" }
{"eventid": "cowrie.session.connect", "src_ip": "2001:db8::1", "src_port": 52344, "dst_ip": "192.168.1.1", "dst_port": 22, "session": "9f8e7d6c", "protocol": "telnet", "message": "New connection: [2001:db8::1]:52344 (192.168.1.1:22) [session: 9f8e7d6c]", "sensor": "myhost", "timestamp": "2024-01-01T00:05:12.500000Z"}
# not a connect event -> should not match
# failJSON: { "time": "2024-01-01T00:10:00", "match": false }
{"eventid": "cowrie.login.failed", "username": "root", "password": "123456", "message": "login attempt [root/123456] failed", "sensor": "myhost", "timestamp": "2024-01-01T00:10:00.000000Z", "src_ip": "1.2.3.4", "session": "1a2b3c4d"}
# malformed / non-JSON line -> should not match
# failJSON: { "match": false }
not a json line at all