Skip to content

feat(recommendations): match recommendations by regular expression and program - #497

Open
TheWitness wants to merge 2 commits into
feat/linux-systemd-cataloguefrom
feat/recommendation-patterns
Open

TheWitness wants to merge 2 commits into
feat/linux-systemd-cataloguefrom
feat/recommendation-patterns

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Stacked on #496. This PR targets feat/linux-systemd-catalogue. After #496 merges, retarget it to develop.

Summary

Adds pattern recommendations: a recommendation can be matched by a PCRE regular expression, optionally limited to one syslog program, instead of by an exact identifier word. This is phase 2 of the Linux recommendations work. Most Linux service messages (sshd, kernel, postfix and others) have no stable message code, so identifier matching can't cover them.

A follow-up PR will add a Wazuh-derived (GPL-2.0) Linux catalogue built on this engine.

Behaviour

  • A record with a non-empty regex is a pattern record. Its identifier only names it (for example wazuh-5710), and identifier matching skips it (regex = '').
  • Matching runs in PHP:
    • against the first 2,048 characters of the message, case-sensitive unless the pattern starts with (?i);
    • with an optional program that must equal the message's syslog program, ignoring case.
  • Scoping: a pattern record applies to every sender except a Cacti device recognised as a different vendor, using the recognised vendor rather than the scoped one. A record with an empty vendor applies to all senders.
  • Order: identifier matches first (longest first, as before), then pattern matches, oldest first, up to the limit of three.
  • Validation on save and import:
    • the pattern must compile, be at most 1,024 characters, contain no control characters, and not match the empty string (which would match every message);
    • a program without a pattern is rejected.
  • Safety and performance:
    • Patterns are loaded once per request, at most 2,000, which stays within PHP's 4,096-entry compiled-pattern cache.
    • Matching runs with pcre.backtrack_limit lowered to 100,000 and then restored. A catastrophic pattern such as ^(a+)+$ fails as "no match" in about 1.5 ms instead of stalling.
    • The batched row check skips repeated message, program and vendor combinations.
    • A control-character delimiter (\x01) means patterns never need escaping.

Changes

  • includes/recommendations.php:
    • validation (syslog_recommendation_regex_error, syslog_recommendation_can_match), pattern cache and matcher;
    • lookup(), for_messages(), for_rows() and lookup_json() take the program;
    • syslog_recommendation_program_names() resolves program_id for System Logs rows in one query;
    • store, export and normalize handle regex and program. Those keys appear only when set, so existing catalogues and the bundled .json.gz files round-trip unchanged.
  • includes/schema.php, includes/database.php: the new columns and prefix index on install and upgrade, plus a syslog_db_index_exists() wrapper.
  • syslog.php, includes/functions.php, js/functions.js: pass the program to every lookup path.
  • syslog_recommendations.php:
    • Pattern and Program form fields, a Pattern list column and a Match By filter;
    • Can Match counts pattern records as matchable, and search covers pattern and program;
    • updated in-page help.
  • README ("Pattern recommendations" section and the matching rules), CHANGELOG, and locales/po/cacti.pot (xgettext step only; no .po/.mo files).

Testing

  • New Pest cases:
    • validation, one dataset per rejection reason;
    • export/import round-trip;
    • program, vendor and ordering rules;
    • "the identifier word of a pattern record does not match";
    • backtrack-limit exhaustion with the limit restored;
    • program-name resolution;
    • the edit form's save path, both storing and refusing a pattern.
  • Existing tests adjusted for the intended behaviour change: messages without identifier candidates now trigger exactly one pattern-load query per request, and an empty page still makes none. The sqlite fixtures gained the new columns. The message-table rule that add_column calls have no default is now scoped to the message tables.
  • Full suite against Cacti 1.2.x: 915 passed. The one warning (ProcessHelperCompatTest /proc probe) is also present on develop. tests/regression/cli_schema_upgrade_test.php passes.
  • On MariaDB 10.5:
    • upgrading the old table shape gives existing rows empty regex and program;
    • a fresh CREATE TABLE, the upsert, the pattern load, the identifier lookup (regex = '') and the Can Match filter SQL all run.
  • Micro-benchmark, 500 unique unmatched rows per page: about 60 ms with 330 patterns and about 0.4 s at the 2,000 cap. A typical 50-row page costs a tenth of that.

Compatibility

The export format stays at version 1. Plugin versions without this change ignore regex and program, and would import a pattern record as an identifier record. Its identifier, for example wazuh-5710, does not occur in normal messages.

TheWitness and others added 2 commits December 3, 2026 11:17
…d program

Many messages, most Linux service messages among them, have no stable code
to use as an identifier. A recommendation may now carry a PCRE pattern
(regex) and an optional syslog program (program); it is then matched by the
pattern instead of its identifier.

- syslog_recommendations gains regex varchar(1024) and program varchar(64)
  (NOT NULL DEFAULT ''), added on upgrade with a prefix index on regex.
- Patterns are validated on save and import: they must compile, be at most
  1,024 characters, contain no control characters and not match an empty
  message; a program without a pattern is rejected.
- Pattern records are loaded once per request (at most 2,000, within PHP's
  compiled-pattern cache), matched against the first 2,048 characters under
  a reduced pcre.backtrack_limit, after identifier matches, and apply to
  every sender except a Cacti device recognised as a different vendor.
- Lookups now receive the syslog program: the details flyout, the fallback
  recommendation action and the batched lightbulb check (System Logs rows
  resolve program_id in one query).
- The Recommendations page gains Pattern and Program fields, a Pattern
  column and a Match By filter; Can Match counts pattern records as
  matchable. Import and export carry regex/program only for records that
  use them, so existing catalogues keep their form.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…gram

Syslog programs are often prefixed or suffixed variants of a daemon name
(postfix/smtpd, sshd-session, systemd-logind), and rule sources such as
Wazuh match program names by prefix or substring. A record's program may
now start and/or end with *: postfix* matches postfix/smtpd and *systemd*
matches systemd-logind. A * anywhere else is rejected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@TheWitness

Copy link
Copy Markdown
Member Author

Follow-up commit 71130e3: a pattern record's program may now start and/or end with a * wildcard (postfix* matches postfix/smtpd, *systemd* matches systemd-logind; a * elsewhere is rejected). Rule sources such as Wazuh match program names by prefix or substring, and modern daemons log under variants like sshd-session. Covered by new validation and matching tests; full suite 924 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant