Skip to content

Lucene ES DSL count aggregations - #216

Open
CL0Pinette wants to merge 5 commits into
SigmaHQ:mainfrom
CL0Pinette:feature/lucene_count_aggregation
Open

Lucene ES DSL count aggregations#216
CL0Pinette wants to merge 5 commits into
SigmaHQ:mainfrom
CL0Pinette:feature/lucene_count_aggregation

Conversation

@CL0Pinette

Copy link
Copy Markdown

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 :

  • Lucene only supports sigma detection rules
  • ElastAlert requires to have another component to make queries periodically and has limited support for correlations
  • EQL supports count correlations but has a major drawbacks which is that it doesn't support threshold for number of events but rather an exact number of events.
  • ESQL supports only clocked time boundaries correaltion rules which can limit detection

Therefore, I searched how I could implement what I wanted and I stumbled across the aggs parameters 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 a terms aggregation on the resulting bucket, allowing to implement value_count correlation 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_median and value_percentile

Don't hesitate if you have any questions

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).
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