From 763a1c7f0cf8cdff0e4c36bf11de94942ad5f3ca Mon Sep 17 00:00:00 2001 From: Jari Huttunen <4654102+jarihu@users.noreply.github.com> Date: Fri, 14 Aug 2026 00:17:03 +0300 Subject: [PATCH] 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 instead of 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. --- config/filter.d/cowrie_json.conf | 22 ++++++++++++------ config/filter.d/cowrie_json.py | 32 --------------------------- fail2ban/tests/files/logs/cowrie_json | 15 +++++++++++++ 3 files changed, 30 insertions(+), 39 deletions(-) delete mode 100644 config/filter.d/cowrie_json.py create mode 100644 fail2ban/tests/files/logs/cowrie_json diff --git a/config/filter.d/cowrie_json.conf b/config/filter.d/cowrie_json.conf index b4ba2115..5d29482d 100644 --- a/config/filter.d/cowrie_json.conf +++ b/config/filter.d/cowrie_json.conf @@ -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 = ^.*$ +failregex = ^\{"eventid": "cowrie\.session\.connect", "src_ip": "",\s*(?:"[^"]+":\s*(?:"[^"]*"|-?\d+(?:\.\d+)?),?\s*){0,12}\}$ -# 2. Syntax-valid regex containing so Fail2ban's compiler doesn't throw an error -failregex = ^.*"src_ip"\s*:\s*"".*$ - -# 3. Disable datepattern stripping so the JSON string arrives intact in Python -datepattern = +ignoreregex = diff --git a/config/filter.d/cowrie_json.py b/config/filter.d/cowrie_json.py deleted file mode 100644 index d98fcfad..00000000 --- a/config/filter.d/cowrie_json.py +++ /dev/null @@ -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) diff --git a/fail2ban/tests/files/logs/cowrie_json b/fail2ban/tests/files/logs/cowrie_json new file mode 100644 index 00000000..69dc27f1 --- /dev/null +++ b/fail2ban/tests/files/logs/cowrie_json @@ -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