rsigma-eval 0.7.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
mod helpers;

use helpers::{corr_engine, eval, process};
use rsigma_eval::{Engine, JsonEvent, parse_pipeline};
use rsigma_parser::parse_sigma_yaml;
use serde_json::json;

#[test]
fn correlation_engine_event_count_e2e() {
    let yaml = r#"
title: Failed Login
id: failed-login
logsource:
    category: auth
detection:
    selection:
        EventType: failed_login
    condition: selection
---
title: Brute Force
correlation:
    type: event_count
    rules:
        - failed-login
    group-by:
        - User
    timespan: 300s
    condition:
        gte: 5
level: critical
"#;
    let mut engine = corr_engine(yaml);

    for i in 0..4 {
        let r = process(
            &mut engine,
            json!({"EventType": "failed_login", "User": "admin"}),
            1000 + i,
        );
        assert!(
            r.correlations.is_empty(),
            "should not fire before threshold"
        );
    }

    let r = process(
        &mut engine,
        json!({"EventType": "failed_login", "User": "admin"}),
        1004,
    );
    assert_eq!(r.correlations.len(), 1);
    assert_eq!(r.correlations[0].rule_title, "Brute Force");
    assert_eq!(
        r.correlations[0].group_key,
        vec![("User".to_string(), "admin".to_string())]
    );
    assert!((r.correlations[0].aggregated_value - 5.0).abs() < f64::EPSILON);

    // Different user should not fire (independent group)
    let r2 = process(
        &mut engine,
        json!({"EventType": "failed_login", "User": "guest"}),
        1010,
    );
    assert!(r2.correlations.is_empty());
}

#[test]
fn correlation_engine_value_count_e2e() {
    let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Login From Many Sources
correlation:
    type: value_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 600s
    condition:
        field: SourceIP
        gte: 3
level: high
"#;
    let mut engine = corr_engine(yaml);

    let base_ts = 1000;
    // Two distinct IPs -- should not fire
    process(
        &mut engine,
        json!({"EventType": "login", "User": "admin", "SourceIP": "10.0.0.1"}),
        base_ts,
    );
    let r = process(
        &mut engine,
        json!({"EventType": "login", "User": "admin", "SourceIP": "10.0.0.2"}),
        base_ts + 1,
    );
    assert!(r.correlations.is_empty());

    // Third distinct IP -- fires
    let r = process(
        &mut engine,
        json!({"EventType": "login", "User": "admin", "SourceIP": "10.0.0.3"}),
        base_ts + 2,
    );
    assert_eq!(r.correlations.len(), 1);
    assert!((r.correlations[0].aggregated_value - 3.0).abs() < f64::EPSILON);
}

#[test]
fn pipeline_transforms_then_evaluates() {
    let pipeline_yaml = r#"
name: ECS mapping
transformations:
  - type: field_name_mapping
    mapping:
      CommandLine: process.command_line
      User: user.name
    rule_conditions:
      - type: logsource
        product: windows
"#;
    let pipeline = parse_pipeline(pipeline_yaml).unwrap();

    let rule_yaml = r#"
title: Detect Whoami
logsource:
    product: windows
    category: process_creation
detection:
    sel:
        CommandLine|contains: 'whoami'
    filter:
        User: 'SYSTEM'
    condition: sel and not filter
level: medium
"#;
    let collection = parse_sigma_yaml(rule_yaml).unwrap();
    let mut engine = Engine::new_with_pipeline(pipeline);
    engine.add_collection(&collection).unwrap();

    // After pipeline, rule expects ECS fields
    let ev = json!({"process.command_line": "cmd /c whoami", "user.name": "attacker"});
    let matches = engine.evaluate(&JsonEvent::borrow(&ev));
    assert_eq!(matches.len(), 1);

    // Filter still works through the mapped field name
    let ev2 = json!({"process.command_line": "cmd /c whoami", "user.name": "SYSTEM"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev2)).is_empty());

    // Original field names should not match
    let ev3 = json!({"CommandLine": "cmd /c whoami", "User": "attacker"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev3)).is_empty());
}

#[test]
fn matched_fields_contain_correct_values() {
    let matches = eval(
        r#"
title: Port Scan
logsource:
    product: firewall
detection:
    selection:
        DestinationPort: 22
        Protocol: TCP
    condition: selection
level: medium
"#,
        json!({"DestinationPort": 22, "Protocol": "TCP", "SourceIP": "10.0.0.1"}),
    );
    assert_eq!(matches.len(), 1);
    let m = &matches[0];
    assert_eq!(m.rule_title, "Port Scan");
    assert_eq!(m.matched_selections, vec!["selection"]);

    let field_names: Vec<&str> = m.matched_fields.iter().map(|f| f.field.as_str()).collect();
    assert!(field_names.contains(&"DestinationPort"));
    assert!(field_names.contains(&"Protocol"));
    // SourceIP was not part of detection, should not appear
    assert!(!field_names.contains(&"SourceIP"));
}

#[test]
fn nested_dot_notation_through_full_chain() {
    let matches = eval(
        r#"
title: Admin Actor
logsource:
    product: cloud
detection:
    selection:
        actor.id: admin
        actor.type: User
    condition: selection
level: high
"#,
        json!({"actor": {"id": "admin", "type": "User"}}),
    );
    assert_eq!(matches.len(), 1);
}

#[test]
fn flat_key_overrides_nested_in_evaluation() {
    // Event has both a flat "actor.id" key and nested {"actor":{"id":...}}
    // Flat key should win per Event semantics
    let matches = eval(
        r#"
title: Flat Key Match
logsource:
    product: test
detection:
    selection:
        actor.id: flat-value
    condition: selection
level: low
"#,
        json!({"actor.id": "flat-value", "actor": {"id": "nested-value"}}),
    );
    assert_eq!(matches.len(), 1, "flat key should override nested");

    // If rule expects the nested value, it should NOT match
    let matches2 = eval(
        r#"
title: Nested Key Match
logsource:
    product: test
detection:
    selection:
        actor.id: nested-value
    condition: selection
level: low
"#,
        json!({"actor.id": "flat-value", "actor": {"id": "nested-value"}}),
    );
    assert!(
        matches2.is_empty(),
        "nested value should be shadowed by flat key"
    );
}

#[test]
fn multi_rule_with_shared_and_targeted_filters() {
    let yaml = r#"
title: Rule A
id: rule-a
logsource:
    product: windows
detection:
    sel:
        EventID: 1
    condition: sel
---
title: Rule B
id: rule-b
logsource:
    product: windows
detection:
    sel:
        EventID: 4688
    condition: sel
---
title: Rule C
id: rule-c
logsource:
    product: windows
detection:
    sel:
        EventID: 7
    condition: sel
---
title: Global Filter
filter:
    rules: []
    env_match:
        Environment: test
    condition: env_match
---
title: Targeted Filter
filter:
    rules:
        - rule-a
    svc_match:
        User: svc_account
    condition: svc_match
"#;
    let collection = parse_sigma_yaml(yaml).unwrap();
    let mut engine = Engine::new();
    engine.add_collection(&collection).unwrap();

    // In test environment: global filter blocks everything
    let ev = json!({"EventID": 1, "Environment": "test", "User": "admin"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev)).is_empty());

    // Rule A as svc_account in prod: targeted filter blocks
    let ev2 = json!({"EventID": 1, "Environment": "prod", "User": "svc_account"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev2)).is_empty());

    // Rule B as svc_account in prod: targeted filter does NOT apply to rule-b
    let ev3 = json!({"EventID": 4688, "Environment": "prod", "User": "svc_account"});
    assert_eq!(engine.evaluate(&JsonEvent::borrow(&ev3)).len(), 1);

    // Rule A as admin in prod: no filter applies
    let ev4 = json!({"EventID": 1, "Environment": "prod", "User": "admin"});
    assert_eq!(engine.evaluate(&JsonEvent::borrow(&ev4)).len(), 1);
}

#[test]
fn filters_with_same_detection_name_do_not_collide() {
    // Regression: two filters both using "selection" as detection name used to
    // overwrite each other's `__filter_selection` key. With the filter_counter
    // fix, they get distinct keys (`__filter_0_selection`, `__filter_1_selection`).
    let yaml = r#"
title: Rule A
id: rule-a
logsource:
    product: test
detection:
    sel:
        EventType: login
    condition: sel
---
title: Filter Env
filter:
    rules:
        - rule-a
    selection:
        Environment: test
    condition: selection
---
title: Filter User
filter:
    rules:
        - rule-a
    selection:
        User: bot
    condition: selection
"#;
    let collection = parse_sigma_yaml(yaml).unwrap();
    let mut engine = Engine::new();
    engine.add_collection(&collection).unwrap();

    // Both filters should apply: env=test is excluded
    let ev1 = json!({"EventType": "login", "Environment": "test", "User": "admin"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev1)).is_empty());

    // User=bot is excluded
    let ev2 = json!({"EventType": "login", "Environment": "prod", "User": "bot"});
    assert!(engine.evaluate(&JsonEvent::borrow(&ev2)).is_empty());

    // Neither filter matches: rule fires
    let ev3 = json!({"EventType": "login", "Environment": "prod", "User": "admin"});
    assert_eq!(engine.evaluate(&JsonEvent::borrow(&ev3)).len(), 1);
}

#[test]
fn global_repeat_through_correlation_engine() {
    let yaml = r#"
action: global
logsource:
    product: windows
    category: process_creation
level: medium
---
title: Detect Cmd
id: detect-cmd
detection:
    selection:
        CommandLine|contains: 'cmd'
    condition: selection
---
action: repeat
title: Detect Powershell
id: detect-ps
detection:
    selection:
        CommandLine|contains: 'powershell'
    condition: selection
---
title: Recon Burst
correlation:
    type: event_count
    rules:
        - detect-cmd
        - detect-ps
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 3
level: high
"#;
    let mut engine = corr_engine(yaml);

    let base_ts = 1000;
    // Mix of cmd and powershell from same user
    process(
        &mut engine,
        json!({"CommandLine": "cmd.exe", "User": "attacker"}),
        base_ts,
    );
    process(
        &mut engine,
        json!({"CommandLine": "powershell -enc", "User": "attacker"}),
        base_ts + 1,
    );

    let r = process(
        &mut engine,
        json!({"CommandLine": "cmd /c whoami", "User": "attacker"}),
        base_ts + 2,
    );
    assert_eq!(
        r.correlations.len(),
        1,
        "3 events from two rules should trigger correlation"
    );
    assert_eq!(r.correlations[0].rule_title, "Recon Burst");
}