rsigma-eval 0.28.0

Evaluator for Sigma detection and correlation rules: match rules against events
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Read-only introspection of correlation window state.
//!
//! The only built-in visibility into correlation state is the aggregate
//! `correlation_state_entries` count. That answers "is there state?" but not
//! the operator's real question: "why did this correlation not fire?"
//! [`CorrelationEngine::introspect`] projects, per correlation and per group,
//! the current aggregate versus the threshold (the gap made explicit), the
//! window contents, the last alert and remaining suppression, and the seconds
//! until the next eviction. It is a read-only projection over the existing
//! state with no hot-path cost.

use serde::Serialize;

use rsigma_parser::{ConditionOperator, CorrelationType};

use super::CorrelationEngine;
use crate::correlation::{CompiledCorrelation, GroupKey, WindowState};
use crate::tenant::TenantId;

/// A snapshot of every compiled correlation and its live per-group window
/// state at the moment [`CorrelationEngine::introspect`] was called.
#[derive(Debug, Clone, Serialize)]
pub struct CorrelationStateSnapshot {
    /// One entry per compiled correlation (independent of whether it has
    /// active state).
    pub correlations: Vec<CorrelationInfo>,
    /// One entry per live `(correlation, group_key)` window.
    pub groups: Vec<GroupStateInfo>,
}

/// Static description of one compiled correlation.
#[derive(Debug, Clone, Serialize)]
pub struct CorrelationInfo {
    pub index: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub title: String,
    #[serde(rename = "type")]
    pub correlation_type: CorrelationType,
    pub timespan_secs: u64,
    pub group_by: Vec<String>,
    pub rule_refs: Vec<String>,
    /// The threshold predicates rendered for display, e.g. `>= 5`.
    pub threshold: String,
    /// Number of live group windows for this correlation.
    pub active_groups: usize,
    /// Ruleset that owns this correlation, set when rows from several rulesets
    /// share one list. `RulesetEngines::introspect` keys its map by ruleset
    /// and leaves this `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ruleset: Option<String>,
}

/// One field of a resolved group key.
#[derive(Debug, Clone, Serialize)]
pub struct GroupKeyPart {
    pub field: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
}

/// Live state of one `(correlation, group_key)` window.
#[derive(Debug, Clone, Serialize)]
pub struct GroupStateInfo {
    pub correlation_index: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub correlation_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub correlation_name: Option<String>,
    pub correlation_title: String,
    #[serde(rename = "type")]
    pub correlation_type: CorrelationType,
    /// The resolved group key, field by field.
    pub group_key: Vec<GroupKeyPart>,
    /// A flat `field=value` rendering of the group key for display and
    /// substring filtering.
    pub group_key_display: String,
    /// The current aggregate (count / sum / avg / distinct / fired-rule count).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub got: Option<f64>,
    /// The threshold predicates rendered for display.
    pub threshold: String,
    /// Whether the threshold condition is currently satisfied.
    pub met: bool,
    /// Number of retained entries in the window.
    pub entries: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub earliest: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latest: Option<i64>,
    pub timespan_secs: u64,
    /// Seconds until the oldest entry leaves the window, relative to the
    /// latest observed event time (`earliest + timespan - latest`, clamped at
    /// zero).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seconds_to_eviction: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_alert: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suppress_secs: Option<u64>,
    /// Seconds remaining in the suppression window after the last alert,
    /// relative to the latest observed event time.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suppression_remaining: Option<i64>,
    /// The raw window state (timestamps / values), serialized as-is.
    pub window: WindowState,
    /// Ruleset that owns this window, set when rows from several rulesets
    /// share one list. `RulesetEngines::introspect` keys its map by ruleset
    /// and leaves this `None`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ruleset: Option<String>,
    /// Tenant that owns this window, when tenancy is enabled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant: Option<TenantId>,
}

impl<L> CorrelationEngine<L> {
    /// Snapshot every compiled correlation and its live per-group window state.
    pub fn introspect(&self) -> CorrelationStateSnapshot {
        self.introspect_for_tenant(None, None, None)
    }

    /// Like [`introspect`](CorrelationEngine::introspect) but keeps only the
    /// correlations whose id, name, or title equals `id_filter` and the groups
    /// whose rendered key contains `group_filter`.
    pub fn introspect_filtered(
        &self,
        id_filter: Option<&str>,
        group_filter: Option<&str>,
    ) -> CorrelationStateSnapshot {
        self.introspect_for_tenant(id_filter, group_filter, None)
    }

    /// Like [`introspect_filtered`](CorrelationEngine::introspect_filtered)
    /// but, when `tenant_filter` is set, keeps only groups owned by that
    /// tenant and counts only its groups as active.
    pub fn introspect_for_tenant(
        &self,
        id_filter: Option<&str>,
        group_filter: Option<&str>,
        tenant_filter: Option<&TenantId>,
    ) -> CorrelationStateSnapshot {
        let mut active_per_corr = vec![0usize; self.correlations.len()];
        for key in self.state.keys() {
            if tenant_filter.is_none_or(|tenant| key.tenant.as_ref() == Some(tenant)) {
                active_per_corr[key.corr_idx] += 1;
            }
        }

        let correlations = self
            .correlations
            .iter()
            .enumerate()
            .filter(|(_, c)| matches_id(c, id_filter))
            .map(|(i, c)| CorrelationInfo {
                index: i,
                id: c.id.clone(),
                name: c.name.clone(),
                title: c.title.clone(),
                correlation_type: c.correlation_type,
                timespan_secs: c.timespan_secs,
                group_by: c.group_by.iter().map(|g| g.name().to_string()).collect(),
                rule_refs: c.rule_refs.clone(),
                threshold: render_threshold(c),
                active_groups: active_per_corr[i],
                ruleset: None,
            })
            .collect();

        let mut groups = Vec::new();
        for (key, ws) in &self.state {
            let corr = &self.correlations[key.corr_idx];
            if !matches_id(corr, id_filter) {
                continue;
            }
            if !tenant_filter.is_none_or(|tenant| key.tenant.as_ref() == Some(tenant)) {
                continue;
            }
            let parts = group_key_parts(corr, &key.group_key);
            let display = render_group_key(&parts);
            if let Some(filter) = group_filter
                && !display.contains(filter)
            {
                continue;
            }

            let got = ws.current_value(
                corr.correlation_type,
                &corr.rule_refs,
                corr.condition.percentile,
            );
            let met = ws
                .check_condition(
                    &corr.condition,
                    corr.correlation_type,
                    &corr.rule_refs,
                    corr.extended_expr.as_ref(),
                )
                .is_some();
            let earliest = ws.earliest_timestamp();
            let latest = ws.latest_timestamp();
            let seconds_to_eviction = match (earliest, latest) {
                (Some(e), Some(l)) => Some((e + corr.timespan_secs as i64 - l).max(0)),
                _ => None,
            };
            let last_alert = self.last_alert.get(key).copied();
            let suppress_secs = corr.suppress_secs.or(self.config.suppress);
            let suppression_remaining = match (last_alert, suppress_secs, latest) {
                (Some(la), Some(s), Some(l)) => Some((la + s as i64 - l).max(0)),
                _ => None,
            };

            groups.push(GroupStateInfo {
                correlation_index: key.corr_idx,
                correlation_id: corr.id.clone(),
                correlation_name: corr.name.clone(),
                correlation_title: corr.title.clone(),
                correlation_type: corr.correlation_type,
                group_key: parts,
                group_key_display: display,
                got,
                threshold: render_threshold(corr),
                met,
                entries: ws.entry_count(),
                earliest,
                latest,
                timespan_secs: corr.timespan_secs,
                seconds_to_eviction,
                last_alert,
                suppress_secs,
                suppression_remaining,
                window: ws.clone(),
                ruleset: None,
                tenant: key.tenant.clone(),
            });
        }

        // Deterministic order for stable output and golden tests.
        groups.sort_by(|a, b| {
            a.correlation_index
                .cmp(&b.correlation_index)
                .then_with(|| a.group_key_display.cmp(&b.group_key_display))
                .then_with(|| a.tenant.cmp(&b.tenant))
        });

        CorrelationStateSnapshot {
            correlations,
            groups,
        }
    }
}

fn matches_id(corr: &CompiledCorrelation, id_filter: Option<&str>) -> bool {
    match id_filter {
        None => true,
        Some(f) => {
            corr.id.as_deref() == Some(f) || corr.name.as_deref() == Some(f) || corr.title == f
        }
    }
}

fn group_key_parts(corr: &CompiledCorrelation, key: &GroupKey) -> Vec<GroupKeyPart> {
    corr.group_by
        .iter()
        .enumerate()
        .map(|(i, field)| GroupKeyPart {
            field: field.name().to_string(),
            value: key.0.get(i).and_then(|v| v.clone()),
        })
        .collect()
}

fn render_group_key(parts: &[GroupKeyPart]) -> String {
    if parts.is_empty() {
        return "(no group-by)".to_string();
    }
    parts
        .iter()
        .map(|p| format!("{}={}", p.field, p.value.as_deref().unwrap_or("<none>")))
        .collect::<Vec<_>>()
        .join(", ")
}

fn render_threshold(corr: &CompiledCorrelation) -> String {
    let preds: Vec<String> = corr
        .condition
        .predicates
        .iter()
        .map(|(op, v)| format!("{} {}", op_symbol(*op), v))
        .collect();
    if preds.is_empty() {
        "(none)".to_string()
    } else {
        preds.join(", ")
    }
}

fn op_symbol(op: ConditionOperator) -> &'static str {
    match op {
        ConditionOperator::Lt => "<",
        ConditionOperator::Lte => "<=",
        ConditionOperator::Gt => ">",
        ConditionOperator::Gte => ">=",
        ConditionOperator::Eq => "==",
        ConditionOperator::Neq => "!=",
    }
}

#[cfg(test)]
mod tests {
    use crate::event::JsonEvent;
    use crate::{CorrelationConfig, CorrelationEngine};
    use rsigma_parser::parse_sigma_yaml;
    use serde_json::json;

    const RULES: &str = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 3
level: high
"#;

    fn engine() -> CorrelationEngine {
        let coll = parse_sigma_yaml(RULES).unwrap();
        let mut e = CorrelationEngine::new(CorrelationConfig::default());
        e.add_collection(&coll).unwrap();
        e
    }

    #[test]
    fn introspect_reports_gap_below_threshold() {
        let mut e = engine();
        // Two logins for admin: below the gte:3 threshold.
        for i in 0..2 {
            let v = json!({"EventType": "login", "User": "admin"});
            e.process_event_at(&JsonEvent::borrow(&v), 1000 + i);
        }
        let snap = e.introspect();
        assert_eq!(snap.correlations.len(), 1);
        assert_eq!(snap.correlations[0].threshold, ">= 3");
        assert_eq!(snap.correlations[0].active_groups, 1);

        let g = snap
            .groups
            .iter()
            .find(|g| g.group_key_display.contains("admin"))
            .expect("admin group present");
        assert_eq!(g.got, Some(2.0));
        assert!(!g.met);
        assert_eq!(g.entries, 2);
        assert_eq!(g.threshold, ">= 3");
        assert_eq!(g.group_key_display, "User=admin");
    }

    #[test]
    fn introspect_marks_met_when_threshold_reached() {
        let mut e = engine();
        for i in 0..3 {
            let v = json!({"EventType": "login", "User": "admin"});
            e.process_event_at(&JsonEvent::borrow(&v), 1000 + i);
        }
        let snap = e.introspect();
        let g = &snap.groups[0];
        assert_eq!(g.got, Some(3.0));
        assert!(g.met);
        // The window spans ts 1000..=1002 with a 60s timespan, so the oldest
        // entry (1000) is evicted 60s after it arrived, relative to the latest.
        assert_eq!(g.seconds_to_eviction, Some(1000 + 60 - 1002));
    }

    #[test]
    fn introspect_filter_by_group_substring() {
        let mut e = engine();
        for u in ["admin", "alice"] {
            let v = json!({"EventType": "login", "User": u});
            e.process_event_at(&JsonEvent::borrow(&v), 1000);
        }
        let snap = e.introspect_filtered(None, Some("alice"));
        assert_eq!(snap.groups.len(), 1);
        assert!(snap.groups[0].group_key_display.contains("alice"));
    }

    #[test]
    fn introspect_filter_by_unknown_id_is_empty() {
        let mut e = engine();
        let v = json!({"EventType": "login", "User": "admin"});
        e.process_event_at(&JsonEvent::borrow(&v), 1000);
        let snap = e.introspect_filtered(Some("nope"), None);
        assert!(snap.correlations.is_empty());
        assert!(snap.groups.is_empty());
    }
}