fix: replace _ws_ space encoding with escaped spaces in SIGMA query generation - #1804
Conversation
PR Reviewer Guide 🔍(Review updated until commit c764d9b)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to c764d9b Explore these optional code suggestions:
Previous suggestionsSuggestions up to commit b26e56c
Suggestions up to commit 557b485
Suggestions up to commit 6fc2d9a
Suggestions up to commit 8af723d
Suggestions up to commit 4a1d335
|
|
Persistent review updated to latest commit c7ffeb9 |
|
Thanks for the thorough review. Addressing both flagged areas: Quoted string escaping ( The concern is valid to raise, but Null regexp on error path The assignment ordering is preserved ( cudos: claude |
c7ffeb9 to
6651c1a
Compare
|
Persistent review updated to latest commit 6651c1a |
6651c1a to
b280664
Compare
PR Code Analyzer ❗AI-powered 'Code-Diff-Analyzer' found issues on commit 4a1d335. ⛔ Hard block: Issues at High severity or above will block this PR from merging.
The table above displays the top 10 most important findings. Pull Requests Author(s): Please update your Pull Request according to the report above. Repository Maintainer(s): You can Thanks. |
|
Persistent review updated to latest commit b280664 |
2a525cc to
dfe6919
Compare
|
Persistent review updated to latest commit dfe6919 |
1 similar comment
|
Persistent review updated to latest commit dfe6919 |
dfe6919 to
4a1d335
Compare
|
Persistent review updated to latest commit 4a1d335 |
4a1d335 to
8af723d
Compare
|
Persistent review updated to latest commit 8af723d |
8af723d to
6fc2d9a
Compare
|
Persistent review updated to latest commit 6fc2d9a |
6fc2d9a to
557b485
Compare
|
Persistent review updated to latest commit 557b485 |
557b485 to
b26e56c
Compare
|
Persistent review updated to latest commit b26e56c |
…alues
Removing the legacy _ws_ whitespace encoding exposed several ways a space in a
SIGMA value produced query_string that was either rejected at percolator index
time or silently stopped matching, breaking integration tests that assert a fixed
number of indexed/matched rules (MapperRestApiIT.testWAFMappings, AlertsIT and
CorrelationEngineRestApiIT match-count assertions).
The target fields use rule_analyzer (keyword tokenizer), so a field value indexes
as a single token. All fixes target OSQueryBackend:
1) Field-bound spaced values (buildSpacedValueQuery): emit the value as a quoted
phrase wrapped in wildcards (*"text"* for contains, "text"* for startswith,
*"text" for endswith). A bare escaped-space wildcard (e.g. *C:\Program\ Files*)
fails to parse on path values, while a bare quoted phrase cannot substring-match
the single keyword-analyzed token. Wrapping the quoted phrase in wildcards both
parses reliably and restores contains/startswith/endswith matching. Separator
detection is decoupled from token lowercasing by iterating over the original text.
2) Unbound wildcard values adjacent to whitespace (convertConditionValStr): values
such as the SQL-injection rule's 'select * ' emitted a bare '*' via the unquoted
unboundWildcardExpression, which query_string rejects ("no field mapping for [*]").
A '*' next to whitespace cannot act as a query_string wildcard against the
keyword-analyzed single token anyway, so quote such values (literal '*' matches the
token); genuine whitespace-free wildcards like *Wfuzz* stay unquoted.
Add/adjust QueryBackendTests coverage for the wrapped contains/startswith/endswith
forms (including mixed space/dash and path-style values) and for the unbound
wildcard-with-whitespace case.
Signed-off-by: thecodingshrimp <leonard.stutzer@sap.com>
b26e56c to
c764d9b
Compare
|
Persistent review updated to latest commit c764d9b |
Description
SIGMA detection rules whose field values contain spaces never match because spaces are encoded as literal
_ws_tokens that Lucene cannot decode in all query contexts. This fix replaces the_ws_scheme with backslash-escaped spaces (\) emitted via the existing backend escape loop.Two failure modes fixed:
contains/startswith/endswith):query_stringwildcard terms bypass the field analyzer, so_ws_is matched literally against ingest data that contains real spaces — detections never fire.SigmaRegularExpressionencoded spaces as_ws_, but Lucene regex syntax treats space as a literal character and does not apply therule_ws_filterchar_filter — detections never fire.Additional correctness fix:
SigmaWindowsDashModifierhard-coded a.replace("_ws_", " ")decode followed by.replace(" ", "_ws_")re-encode. This conflicted with the new escaping on the wildcard path and has been removed.Changes:
OSQueryBackend.java: add" "(space) toaddEscapedso the escape loop inSigmaString.convert()emits\via backend config rather than hardcoded string replace.SigmaString.java: remove hardcodedreplace(" ", "_ws_")fromconvert()andtoString()— escaping now flows through the backend-config escape loop.SigmaRegularExpression.java: removereplace(" ", "_ws_")— Lucene regex treats space as a literal, no encoding needed.SigmaWindowsDashModifier.java: remove_ws_decode/re-encode round-trip — values are stored with literal spaces throughout.detector-settings.json/DetectorMonitorConfig.java:rule_ws_filterandrule_analyzerare kept during this transition window; the_ws_token no longer appears in generated queries so the filter is dormant but harmless. Retirement requires a re-index migration and is deferred to a follow-up.Related Issues
Resolves #1024
Check List
QueryBackendTests— whitespace wildcard path (*This\ is\ an\ example*), whitespace quoted path, regex path, windash path; all passing (BUILD SUCCESSFUL).DetectorRestApiIT.testDetectorWithWhitespaceRuleContainsPathandtestDetectorWithWhitespaceRuleQuotedPathadded (compile-verified; require a live OpenSearch cluster to run).--signoff.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.