v1.209.1 BotScan Go matcher — restore broken prefilter + fix srv3 collector OOM (helper-first)#968
Merged
Merged
Conversation
… fleet-wide detection Track-A completion hotfix (rescoped). Fixes srv3 collector OOM AND a fleet-wide prefilter-correctness defect the canary uncovered. ROOT CAUSE (prefilter): the BotScan pattern file uses '|' as its field delimiter, but 3 patterns contain '|' alternation. The shell IFS='|' read mis-splits them into invalid regex, and GNU grep 3.6 rejects the WHOLE `grep -E -f "$_pf"` file (rc=2). With `2>/dev/null || true` the prefilter silently returns EMPTY → process_entry never runs → pattern-based detection has been DEAD fleet-wide. Confirmed live on srv2. FIX: new bounded Go helper `nftban-botscan-matcher` (internal/botscanmatch: Aho-Corasick literal prefilter + RE2 confirm) replaces `grep -E -f`. SKIPS the 3 broken patterns VISIBLY, runs the 152 valid ones. Parity proven: helper output == grep -E -f (valid), byte-identical. Bounded memory → fits srv3 256M cap. Shell resolves engine once (--check); ANY helper problem → unfiltered pass-through (detection preserved), NEVER silently empty. nftband daemon BYTE-IDENTICAL (go list -deps: cmd/nftband does not import botscanmatch). Schema 1.84.0; no source_index/firewall/ban-path/threshold/BotGuard change. 3 broken patterns stay skipped + tracked (OPEN_BOTSCAN_PATTERN_DELIMITER_FIX). Build+vet+test green on lab2.
Contributor
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
…ilter writes) gosec code-scanning doesn't honor inline #nosec G304; wrap os.Open in filepath.Clean (the repo's accepted pattern). Handle bufio.Writer Write/WriteByte/Flush errors in Filter (G104). No behavior change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
v1.209.1 — BotScan prefilter correctness + bounded matcher (Track-A completion hotfix)
Completes v1.209 Track-A by unblocking srv3 and fixing a fleet-wide prefilter-correctness defect the srv3 canary uncovered. Helper-first; nftband daemon byte-identical; nft schema 1.84.0 unchanged; no VERSION bump (release-prep is separate).
Root cause
The BotScan pattern file uses
|as its field delimiter, but 3 patterns contain|alternation in field 2 (EXP_CGIBIN/cgi-bin/.*\.(sh|pl|cgi),EXP_SQLBACKUP,SCAN_BACKUP_SQL). The shellIFS='|' readmis-splits them into invalid regex (/cgi-bin/.*\.(sh), and GNU grep 3.6 rejects the entiregrep -E -f "$_pf"file (rc=2). The shell ran it asgrep … 2>/dev/null || true, so the prefilter silently returned empty →process_entryran zero times → pattern-based BotScan detection (webshell/exploit/scanner/badbot) has been dead fleet-wide. (The 404/endpoint-flood path is separate and still produced signals/backlog.) Confirmed live on srv2 (prefilter on by default, patterns installed, grep rc=2).Corrected corpus facts
Real corpus = 155 patterns (earlier 775/137 were worktree-duplicate / wrong-column artifacts): 98 pure-literal + 57 regex-requiring (char-class/quantifier/anchor; 0 backref/lookaround → all RE2-compatible). 3 are the
|-mis-split alternation patterns.Implementation
internal/botscanmatch/— Aho-Corasick literal prefilter (one anchor/pattern) + RE2 confirm so output == grep's matching-line set. Internal impl, no external dependency. Bounded memory (streaming, bounded automaton) → fits srv3'sMemoryMax=256M, no whole-file slurp.cmd/nftban-botscan-matcher— helper (--filter/--check/--selftest/--version). Skips the 3 broken patterns visibly (--check→ "152 usable, 3 skipped"; stderr count); runs the 152 valid.--check; replacesgrep -E -f. build.sh + RPM/DEB install + ci-go build.Restored-detection impact (must read)
v1.209.1 restores intended pattern-based BotScan detection for the 152 valid patterns. This may INCREASE legitimate BotScan bans, because previously-suppressed detections (real webshell/exploit probes) become active. This is correct/intended, not new behavior — the prefilter is supposed to filter, not error-to-empty.
Safety / fail-safe
On any helper problem (missing binary, zero usable patterns, compile failure), the shell falls through to unfiltered pass-through — the authoritative bash
match_url_gmatcher still runs on all lines (detection preserved, slower). It never silently empties like the old grep path. Skip count is surfaced once per cycle.Package-native validation (lab2 DEB + lab4 RPM — both PASS)
--version1.0.0;--selftestrc0;--check→ "152 usable, 3 skipped".grep -E -frc=2 / kept=0 (broken) vs helper kept=4 (restored); webshellc99.phpkept.grep(valid$_pf) byte-identical.go list -deps ./cmd/nftbanddoes not includebotscanmatch; nocmd/nftbandchange. (Installednftbandsha differs only as a non-reproducible-build artifact — not a re-baseline.)Non-scope
No VERSION bump · no schema/validator-JSON · no daemon logic · no source_index/firewall/ban-path/threshold/action change · no BotGuard default flip · no MalwareGuard/decision-cache · no full BotScan-in-daemon migration · the 3 delimiter-broken patterns are NOT activated (pattern files unchanged).
Follow-up
OPEN_BOTSCAN_PATTERN_DELIMITER_FIX— fix the pattern-file serialization/loader so|-containing patterns aren't mis-split, then re-enable the 3 alternation patterns with parity fixtures. Tracked separately; not in this PR.