rsigma 0.23.0

CLI for parsing, validating, linting and evaluating Sigma detection rules
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! `engine discover-schemas`: mine unrecognized events into candidate schema
//! signatures.
//!
//! Reads a JSON/NDJSON corpus, runs the mining core over the events no built-in
//! or `--schema-config` signature recognizes, and prints ranked candidate
//! declarative signatures (plus a paste-ready `schemas:` block) for a human to
//! review and commit. It does not load rules or evaluate detections, and it
//! never applies a discovered signature on its own.
//!
//! Pairs with `engine classify`: classify shows you have unknowns, discover
//! proposes signatures, and classifying again with the pasted config verifies
//! them. `--dry-run` short-circuits that loop by reclassifying the same corpus
//! with the proposals loaded and reporting the before/after per-schema counts.

use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;
use std::process;

use clap::{Args, ValueEnum};
use rsigma_eval::{
    CandidateSource, DiscoveryConfig, DiscoveryReport, JsonEvent, SchemaClassifier,
    SchemaSignature, load_schema_config, mine_events,
};
use serde::Serialize;

use crate::output::{DelimitedWriter, OutputCtx, OutputFormat, Tabular, render_json};

/// What `engine discover-schemas` prints.
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum, Default)]
pub(crate) enum EmitMode {
    /// Candidates with stats, in the global output format.
    #[default]
    Report,
    /// Only the paste-ready `schemas:` YAML block.
    Yaml,
}

/// Arguments for `rsigma engine discover-schemas`.
#[derive(Args, Debug)]
pub(crate) struct DiscoverArgs {
    /// A single event as a JSON string, or @path to read NDJSON from a file.
    /// If omitted, reads NDJSON from stdin.
    #[arg(short, long)]
    pub event: Option<String>,

    /// Path to a YAML file of user-defined schema signatures. Events these
    /// already recognize are excluded from mining, so a defined schema is never
    /// re-proposed.
    #[arg(long = "schema-config", value_name = "PATH")]
    pub schema_config: Option<PathBuf>,

    /// Minimum events a cluster must contain to yield a candidate.
    #[arg(long, default_value_t = 3)]
    pub min_support: u64,

    /// Jaccard similarity (0.0-1.0) at or above which shapes merge into one
    /// cluster. Higher is stricter (more, tighter clusters).
    #[arg(long, default_value_t = 0.6)]
    pub similarity: f64,

    /// Maximum candidates to emit, highest support first.
    #[arg(long, default_value_t = 20)]
    pub max_candidates: usize,

    /// Maximum predicates per candidate signature.
    #[arg(long, default_value_t = 3)]
    pub max_predicates: usize,

    /// Propose presence predicates only; never emit `equals`/`in` value markers.
    #[arg(long)]
    pub no_value_markers: bool,

    /// Reclassify the corpus with the proposed signatures loaded and report the
    /// before/after per-schema counts.
    #[arg(long)]
    pub dry_run: bool,

    /// What to print: the full report (default) or only the `schemas:` YAML.
    #[arg(long, value_enum, default_value_t = EmitMode::Report)]
    pub emit: EmitMode,
}

pub(crate) fn cmd_discover(args: DiscoverArgs, ctx: OutputCtx) {
    let signatures = match &args.schema_config {
        Some(path) => match load_schema_config(path) {
            // Only the signatures matter here; routing is irrelevant to mining.
            Ok((sigs, _routing)) => sigs,
            Err(e) => {
                eprintln!("Error loading schema config: {e}");
                process::exit(crate::exit_code::CONFIG_ERROR);
            }
        },
        None => Vec::new(),
    };

    let corpus = match read_corpus(args.event) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("{e}");
            process::exit(crate::exit_code::RULE_ERROR);
        }
    };

    let base_classifier = if signatures.is_empty() {
        SchemaClassifier::builtin()
    } else {
        SchemaClassifier::with_user_signatures(signatures.clone())
    };

    let config = DiscoveryConfig {
        min_support: args.min_support,
        similarity: args.similarity,
        max_candidates: args.max_candidates,
        max_predicates: args.max_predicates,
        value_markers: !args.no_value_markers,
        ..DiscoveryConfig::default()
    };

    let report = mine_events(
        corpus.events.iter().map(JsonEvent::borrow),
        &base_classifier,
        &config,
    );

    // `--emit yaml` short-circuits to the paste-ready block on stdout.
    if args.emit == EmitMode::Yaml {
        print!("{}", report.to_signatures_yaml());
        return;
    }

    let dry_run = args
        .dry_run
        .then(|| compute_dry_run(&corpus.events, &signatures, &report));

    let dto = DiscoverReport::build(&report, corpus.parse_errors, dry_run);

    match ctx.format {
        OutputFormat::Json => render_json(&dto, true),
        OutputFormat::Ndjson => {
            for c in &dto.candidates {
                render_json(c, false);
            }
            print_summary_stderr(&dto, &ctx);
        }
        OutputFormat::Csv => render_delimited(&dto, ',', &ctx),
        OutputFormat::Tsv => render_delimited(&dto, '\t', &ctx),
        OutputFormat::Table => print_table(&dto, &report, &ctx),
    }
}

// ---------------------------------------------------------------------------
// Corpus reading (mirrors `engine classify`)
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct Corpus {
    events: Vec<serde_json::Value>,
    parse_errors: usize,
}

fn read_corpus(event_arg: Option<String>) -> Result<Corpus, String> {
    let mut events = Vec::new();
    let mut parse_errors = 0usize;

    let push_line = |line: &str, events: &mut Vec<serde_json::Value>, errs: &mut usize| {
        if line.trim().is_empty() {
            return;
        }
        match serde_json::from_str::<serde_json::Value>(line) {
            Ok(v) => events.push(v),
            Err(_) => *errs += 1,
        }
    };

    match event_arg {
        Some(s) if s.starts_with('@') => {
            let path = PathBuf::from(&s[1..]);
            if path
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("evtx"))
            {
                return Err(
                    "`engine discover-schemas` reads JSON/NDJSON; .evtx files are binary. Decode \
                     with `engine eval -e @file.evtx` or convert to NDJSON first."
                        .to_string(),
                );
            }
            let file = File::open(&path)
                .map_err(|e| format!("Error opening event file '{}': {e}", path.display()))?;
            for line in BufReader::new(file).lines() {
                let line = line.map_err(|e| format!("Error reading event file: {e}"))?;
                push_line(&line, &mut events, &mut parse_errors);
            }
        }
        Some(s) => {
            let value: serde_json::Value =
                serde_json::from_str(&s).map_err(|e| format!("Invalid JSON event: {e}"))?;
            events.push(value);
        }
        None => {
            let stdin = io::stdin();
            for line in stdin.lock().lines() {
                let line = line.map_err(|e| format!("Error reading stdin: {e}"))?;
                push_line(&line, &mut events, &mut parse_errors);
            }
        }
    }

    Ok(Corpus {
        events,
        parse_errors,
    })
}

// ---------------------------------------------------------------------------
// Dry-run: before/after classification impact
// ---------------------------------------------------------------------------

fn compute_dry_run(
    events: &[serde_json::Value],
    base_signatures: &[SchemaSignature],
    report: &DiscoveryReport,
) -> DryRun {
    let before_classifier = if base_signatures.is_empty() {
        SchemaClassifier::builtin()
    } else {
        SchemaClassifier::with_user_signatures(base_signatures.to_vec())
    };
    let mut after_signatures = base_signatures.to_vec();
    after_signatures.extend(report.candidates.iter().map(|c| c.signature()));
    let after_classifier = SchemaClassifier::with_user_signatures(after_signatures);

    let before = tally(events, &before_classifier);
    let after = tally(events, &after_classifier);
    DryRun { before, after }
}

fn tally(events: &[serde_json::Value], classifier: &SchemaClassifier) -> BTreeMap<String, u64> {
    let mut counts: BTreeMap<String, u64> = BTreeMap::new();
    for value in events {
        let event = JsonEvent::borrow(value);
        let key = classifier
            .classify(&event)
            .map(|m| m.name)
            .unwrap_or_else(|| "unknown".to_string());
        *counts.entry(key).or_insert(0) += 1;
    }
    counts
}

// ---------------------------------------------------------------------------
// Report DTO (owns the JSON/text shape; the core stays serialization-free)
// ---------------------------------------------------------------------------

#[derive(Debug, Serialize)]
struct DiscoverReport {
    summary: DiscoverSummary,
    candidates: Vec<CandidateDto>,
    #[serde(skip_serializing_if = "Option::is_none")]
    dry_run: Option<DryRun>,
    /// Paste-ready `schemas:` block, identical to `--emit yaml`.
    signatures_yaml: String,
}

#[derive(Debug, Serialize)]
struct DiscoverSummary {
    events_mined: u64,
    shapes: usize,
    clusters: usize,
    candidates: usize,
    parse_errors: usize,
}

#[derive(Debug, Serialize)]
struct CandidateDto {
    name: String,
    specificity: u32,
    source: CandidateSource,
    support: u64,
    coverage_of_unknown: f64,
    predicates: Vec<String>,
    sample_field_sets: Vec<Vec<String>>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    overlap_warnings: Vec<String>,
}

#[derive(Debug, Serialize)]
struct DryRun {
    before: BTreeMap<String, u64>,
    after: BTreeMap<String, u64>,
}

impl DiscoverReport {
    fn build(report: &DiscoveryReport, parse_errors: usize, dry_run: Option<DryRun>) -> Self {
        let candidates = report
            .candidates
            .iter()
            .map(|c| CandidateDto {
                name: c.name.clone(),
                specificity: c.specificity,
                source: c.source,
                support: c.support,
                coverage_of_unknown: c.coverage_of_unknown,
                predicates: c.predicate_descriptions(),
                sample_field_sets: c.sample_field_sets.clone(),
                overlap_warnings: c.overlap_warnings.clone(),
            })
            .collect();
        DiscoverReport {
            summary: DiscoverSummary {
                events_mined: report.stats.events_mined,
                shapes: report.stats.shapes,
                clusters: report.stats.clusters,
                candidates: report.stats.candidates,
                parse_errors,
            },
            candidates,
            dry_run,
            signatures_yaml: report.to_signatures_yaml(),
        }
    }
}

// ---------------------------------------------------------------------------
// Rendering
// ---------------------------------------------------------------------------

impl Tabular for CandidateDto {
    fn headers() -> &'static [&'static str] {
        &[
            "NAME",
            "SUPPORT",
            "COVERAGE",
            "SPEC",
            "SOURCE",
            "PREDICATES",
        ]
    }
    fn row(&self) -> Vec<String> {
        vec![
            self.name.clone(),
            self.support.to_string(),
            format!("{:.0}%", self.coverage_of_unknown * 100.0),
            self.specificity.to_string(),
            source_str(self.source).to_string(),
            self.predicates.join("; "),
        ]
    }
}

fn source_str(s: CandidateSource) -> &'static str {
    match s {
        CandidateSource::Corpus => "corpus",
        CandidateSource::KeysOnly => "keys-only",
    }
}

fn summary_line(dto: &DiscoverReport) -> String {
    let s = &dto.summary;
    format!(
        "Mined {} event(s) into {} shape(s), {} cluster(s); {} candidate(s), {} parse error(s)",
        s.events_mined, s.shapes, s.clusters, s.candidates, s.parse_errors
    )
}

fn print_summary_stderr(dto: &DiscoverReport, ctx: &OutputCtx) {
    if ctx.show_stats() {
        eprintln!("{}", summary_line(dto));
    }
}

fn render_delimited(dto: &DiscoverReport, sep: char, ctx: &OutputCtx) {
    print_summary_stderr(dto, ctx);
    let mut writer = DelimitedWriter::new(sep, CandidateDto::headers());
    for c in &dto.candidates {
        writer.push(&c.row());
    }
}

fn print_table(dto: &DiscoverReport, report: &DiscoveryReport, ctx: &OutputCtx) {
    if ctx.show_stats() {
        eprintln!("{}", summary_line(dto));
    }
    if dto.candidates.is_empty() {
        if ctx.show_progress() {
            eprintln!("No candidate schemas discovered.");
        }
        return;
    }
    if ctx.show_stats() {
        eprintln!();
    }
    crate::output::render_table(&dto.candidates);

    // Advisory warnings below the table.
    for c in &dto.candidates {
        for w in &c.overlap_warnings {
            eprintln!("warning [{}]: {w}", c.name);
        }
    }

    if let Some(dry) = &dto.dry_run {
        print_dry_run(dry);
    }

    // The paste-ready block last, so the operator can copy it directly.
    println!("\n# Paste into a --schema-config file:");
    print!("{}", report.to_signatures_yaml());
}

fn print_dry_run(dry: &DryRun) {
    println!("\nDry run (classification impact):");
    let mut schemas: Vec<&String> = dry.before.keys().chain(dry.after.keys()).collect();
    schemas.sort();
    schemas.dedup();
    for schema in schemas {
        let before = dry.before.get(schema).copied().unwrap_or(0);
        let after = dry.after.get(schema).copied().unwrap_or(0);
        let delta = after as i64 - before as i64;
        let arrow = if delta > 0 {
            format!(" (+{delta})")
        } else if delta < 0 {
            format!(" ({delta})")
        } else {
            String::new()
        };
        println!("  {schema}: {before} -> {after}{arrow}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn corpus_values(n: usize, vendor: &str) -> Vec<serde_json::Value> {
        (0..n)
            .map(|i| json!({"vendor": vendor, "event_type": "alert", "seq": i}))
            .collect()
    }

    #[test]
    fn builds_report_from_mined_corpus() {
        let values = corpus_values(10, "acme");
        let classifier = SchemaClassifier::builtin();
        let report = mine_events(
            values.iter().map(JsonEvent::borrow),
            &classifier,
            &DiscoveryConfig::default(),
        );
        let dto = DiscoverReport::build(&report, 0, None);
        assert_eq!(dto.summary.events_mined, 10);
        assert!(!dto.candidates.is_empty());
        assert!(dto.signatures_yaml.contains("schemas:"));
    }

    #[test]
    fn dry_run_shows_unknown_shrinking_after_proposals() {
        let values = corpus_values(8, "acme");
        let classifier = SchemaClassifier::builtin();
        let report = mine_events(
            values.iter().map(JsonEvent::borrow),
            &classifier,
            &DiscoveryConfig::default(),
        );
        let dry = compute_dry_run(&values, &[], &report);
        // Before: everything is generic_json/unknown; after: the proposals
        // reclassify the vendor events under a discovered schema.
        let discovered_after: u64 = dry
            .after
            .iter()
            .filter(|(k, _)| k.starts_with("discovered"))
            .map(|(_, v)| *v)
            .sum();
        assert!(discovered_after > 0, "proposals should capture events");
        let generic_before = dry.before.get("generic_json").copied().unwrap_or(0);
        let generic_after = dry.after.get("generic_json").copied().unwrap_or(0);
        assert!(
            generic_after < generic_before,
            "unknown/generic should shrink"
        );
    }

    #[test]
    fn read_corpus_rejects_evtx() {
        let err = read_corpus(Some("@evidence.evtx".to_string())).expect_err("should error");
        assert!(err.contains(".evtx"));
    }

    #[test]
    fn read_corpus_skips_blank_and_counts_bad_lines() {
        // Inline single event path is exercised elsewhere; here validate the DTO
        // parse-error surfacing via a direct construction.
        let values = corpus_values(3, "acme");
        let classifier = SchemaClassifier::builtin();
        let report = mine_events(
            values.iter().map(JsonEvent::borrow),
            &classifier,
            &DiscoveryConfig::default(),
        );
        let dto = DiscoverReport::build(&report, 2, None);
        assert_eq!(dto.summary.parse_errors, 2);
    }
}