Lucene ES DSL count aggregations - #216
Open
CL0Pinette wants to merge 5 commits into
Open
Conversation
Declare correlation support for event_count and value_count via ES DLS aggregation queries. Add search.groupby templates and condition operator mapping for bucket_selector scripts.
Generates ES _search body with terms aggregation + bucket_selector for event_count correlation rules. Supports: - Single or multiple group-by fields (nested terms aggs) - All comparison operators (gte, gt, lt, lte, eq, neq) - Timespan as range filter on @timestamp - Multiple referenced rules combined with OR
Generates ES _search body with terms aggregation + cardinality sub-aggregation + bucket_selector for value_count correlation rules. Used when detecting distinct value thresholds (e.g., 10 distinct users from the same source IP within a time window).
Correlation rules return dicts (complete ES _search bodies) rather than Lucene strings. finalize_query_dsl_lucene now detects this and passes the dict through without wrapping it in query_string.
- Add 8 correlation tests: event_count (single/multi groupby, no groupby, lt operator, default output), value_count (single groupby, gt operator), temporal (not supported error) - Fix finish_query to pass correlation dicts through without string formatting via query_expression template - Fix timespan: use .seconds instead of .to_seconds() - Extract Lucene query from DSL dict when referenced rules were finalized in dsl_lucene format - Add explicit temporal/temporal_ordered overrides that raise SigmaFeatureNotSupportedByBackendError All 41 tests pass (33 existing + 8 new).
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.
Hello,
Note
First of all, I have developped this feature with AI because I have no idea how the pysigma backend development works, so there might be multiple things to improve/fix before merging.
I needed to have correlation rules that could match a threshold numbers of events, e.g. if there were more than 50 events matching a rule in 15 minutes.
This project currently supports different methods for sigma rules but they didn't fit my use case :
Therefore, I searched how I could implement what I wanted and I stumbled across the
aggsparameters in elastic dsl queries. By using nested aggregations, we can represents all the group-by clauses in a sigma correlation rule. And then compute the number of documents per bucket by just counting the number of results, or get the number of distinct values by using atermsaggregation on the resulting bucket, allowing to implementvalue_countcorrelation rule.The implementation has been directed by me but most the development has been made by AI.
I would be glad if this is merged because I think it addresses a lot of use case that cannot be covered currently. Also I think we can then extend this backend to support
value_avg,value_sum,value_medianandvalue_percentileDon't hesitate if you have any questions