rsigma 0.24.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
use std::collections::{BTreeSet, HashSet};
use std::path::PathBuf;
use std::process;

use clap::Args;
use rsigma_eval::Engine;
use rsigma_parser::{SigmaCollection, SigmaParserError, parse_sigma_directory, parse_sigma_file};
use serde::Serialize;

use crate::output::{OutputCtx, OutputFormat, Tabular, render_report};

/// Arguments for `rsigma rule validate` (and the deprecated `rsigma validate`).
#[derive(Args, Debug)]
pub(crate) struct ValidateArgs {
    /// Sigma YAML file, or directory searched recursively for .yml and .yaml files
    pub path: PathBuf,

    /// Show details for each file (not just summary)
    #[arg(short, long)]
    pub verbose: bool,

    /// Processing pipeline(s) to apply. Accepts builtin names (ecs_windows, sysmon) or YAML file paths
    #[arg(short = 'p', long = "pipeline")]
    pub pipelines: Vec<PathBuf>,

    /// Also resolve dynamic pipeline sources during validation.
    /// Sources must be reachable (file/command/HTTP) for validation to pass.
    #[arg(long = "resolve-sources")]
    pub resolve_sources: bool,

    /// External source file(s) or directory of source files
    #[arg(long = "source", value_name = "FILE_OR_DIR")]
    pub source_files: Vec<PathBuf>,
}

#[derive(Debug, Serialize)]
struct ValidateSummary {
    path: String,
    total: usize,
    detection_rules: usize,
    correlation_rules: usize,
    filter_rules: usize,
    parse_errors: usize,
    compile_ok: usize,
    compile_errors: usize,
    pipelines: usize,
}

#[derive(Debug, Serialize)]
struct ValidateRow {
    path: String,
    status: String,
    errors: String,
}

impl Tabular for ValidateRow {
    fn headers() -> &'static [&'static str] {
        &["PATH", "STATUS", "ERRORS"]
    }
    fn row(&self) -> Vec<String> {
        vec![self.path.clone(), self.status.clone(), self.errors.clone()]
    }
}

pub(crate) fn cmd_validate(args: ValidateArgs, ctx: OutputCtx) {
    let ValidateArgs {
        path,
        verbose,
        pipelines: pipeline_paths,
        resolve_sources,
        source_files,
    } = args;
    // Dynamic source resolution (`--resolve-sources` / `--source`) needs the
    // async runtime + source resolver, which ship with the `daemon` feature.
    #[cfg(feature = "daemon")]
    let pipelines = resolve_validate_sources(
        crate::load_pipelines(&pipeline_paths),
        resolve_sources,
        &source_files,
        !ctx.explicit_format,
    );
    #[cfg(not(feature = "daemon"))]
    let pipelines = {
        let loaded = crate::load_pipelines(&pipeline_paths);
        if resolve_sources || !source_files.is_empty() {
            eprintln!(
                "error: --resolve-sources/--source require the `daemon` feature; \
                 rebuild with `--features daemon`"
            );
            process::exit(crate::exit_code::CONFIG_ERROR);
        }
        loaded
    };

    let parsed = if path.is_dir() {
        parse_sigma_directory(&path)
    } else {
        match parse_sigma_file(&path) {
            Err(SigmaParserError::Io(e)) => Err(SigmaParserError::Io(e)),
            Err(e) => {
                let mut collection = SigmaCollection::new();
                collection.errors.push(format!("{}: {e}", path.display()));
                Ok(collection)
            }
            ok => ok,
        }
    };

    match parsed {
        Ok(collection) => {
            let total = collection.len();
            let rules = collection.rules.len();
            let correlations = collection.correlations.len();
            let filters = collection.filters.len();
            let parse_errors = collection.errors.len();

            let mut engine = Engine::new();
            for p in &pipelines {
                engine.add_pipeline(p.clone());
            }

            // Batch all rules through `add_rules` so the inverted index and
            // bloom filter rebuild exactly once for the whole corpus instead
            // of once per rule (the latter is O(N²) and turns 3K-rule loads
            // into a multi-minute stall).
            let batch_errors = engine.add_rules(&collection.rules);
            let compile_ok = collection.rules.len() - batch_errors.len();
            let mut compile_errors: Vec<String> = batch_errors
                .into_iter()
                .map(|(idx, e)| {
                    let rule = &collection.rules[idx];
                    let id = rule.id.as_deref().unwrap_or(&rule.title);
                    format!("{id}: {e}")
                })
                .collect();
            compile_errors.extend(validate_correlation_references(&collection));

            let parse_error_msgs: Vec<String> =
                collection.errors.iter().map(|e| e.to_string()).collect();

            let summary = ValidateSummary {
                path: path.display().to_string(),
                total,
                detection_rules: rules,
                correlation_rules: correlations,
                filter_rules: filters,
                parse_errors,
                compile_ok,
                compile_errors: compile_errors.len(),
                pipelines: pipelines.len(),
            };

            if ctx.explicit_format {
                let mut rows = Vec::new();
                for err in &parse_error_msgs {
                    rows.push(ValidateRow {
                        path: path.display().to_string(),
                        status: "parse_error".into(),
                        errors: err.clone(),
                    });
                }
                for err in &compile_errors {
                    rows.push(ValidateRow {
                        path: path.display().to_string(),
                        status: "compile_error".into(),
                        errors: err.clone(),
                    });
                }
                if rows.is_empty() {
                    rows.push(ValidateRow {
                        path: path.display().to_string(),
                        status: "ok".into(),
                        errors: String::new(),
                    });
                }
                let envelope = serde_json::json!({
                    "summary": summary,
                    "parse_errors": parse_error_msgs,
                    "compile_errors": compile_errors,
                });
                // NDJSON: one row per finding (or a single ok row). JSON: envelope.
                match ctx.format {
                    OutputFormat::Ndjson
                    | OutputFormat::Table
                    | OutputFormat::Csv
                    | OutputFormat::Tsv => render_report(&ctx, &envelope, &rows),
                    OutputFormat::Json => {
                        crate::output::render_json(&envelope, ctx.pretty_json());
                    }
                }
            } else {
                println!("Parsed {total} documents from {}", path.display());
                println!("  Detection rules:   {rules}");
                println!("  Correlation rules: {correlations}");
                println!("  Filter rules:      {filters}");
                println!("  Parse errors:      {parse_errors}");
                tracing::info!(
                    total,
                    detection_rules = rules,
                    correlation_rules = correlations,
                    filter_rules = filters,
                    parse_errors,
                    rules_path = %path.display(),
                    "Validation parsed",
                );

                if !pipelines.is_empty() {
                    println!("  Pipeline applied:  {} pipeline(s)", pipelines.len(),);
                }
                println!("  Compiled OK:       {compile_ok}");
                println!("  Compile errors:    {}", compile_errors.len());

                if verbose {
                    if !collection.errors.is_empty() {
                        println!("\nParse errors:");
                        for err in &collection.errors {
                            println!("  - {err}");
                        }
                    }
                    if !compile_errors.is_empty() {
                        println!("\nCompile errors:");
                        for err in &compile_errors {
                            println!("  - {err}");
                        }
                    }
                }
            }

            if parse_errors > 0 || !compile_errors.is_empty() {
                process::exit(crate::exit_code::RULE_ERROR);
            }
        }
        Err(e) => {
            eprintln!("Error: {e}");
            process::exit(crate::exit_code::RULE_ERROR);
        }
    }
}

fn validate_correlation_references(collection: &rsigma_parser::SigmaCollection) -> Vec<String> {
    let mut errors = validate_reference_identities(collection);
    let mut known = HashSet::new();
    for rule in &collection.rules {
        known.extend(rule.id.iter().map(String::as_str));
        known.extend(rule.name.iter().map(String::as_str));
    }
    for correlation in &collection.correlations {
        known.extend(correlation.id.iter().map(String::as_str));
        known.extend(correlation.name.iter().map(String::as_str));
    }

    errors.extend(collection.correlations.iter().flat_map(|correlation| {
        correlation
            .rules
            .iter()
            .filter(|rule_ref| !known.contains(rule_ref.as_str()))
            .map(|rule_ref| {
                let identity = correlation
                    .id
                    .as_deref()
                    .or(correlation.name.as_deref())
                    .unwrap_or(&correlation.title);
                format!("{identity}: unknown rule reference: {rule_ref}")
            })
    }));
    errors
}

/// Report correlation references that resolve to more than one detection or
/// correlation rule, through a duplicate id, a duplicate name, or a name that
/// equals another rule's id.
fn validate_reference_identities(collection: &rsigma_parser::SigmaCollection) -> Vec<String> {
    let rules: Vec<(Option<&str>, Option<&str>, &str)> = collection
        .rules
        .iter()
        .map(|rule| {
            (
                rule.id.as_deref(),
                rule.name.as_deref(),
                rule.title.as_str(),
            )
        })
        .chain(collection.correlations.iter().map(|correlation| {
            (
                correlation.id.as_deref(),
                correlation.name.as_deref(),
                correlation.title.as_str(),
            )
        }))
        .collect();

    let referenced: BTreeSet<&str> = collection
        .correlations
        .iter()
        .flat_map(|correlation| correlation.rules.iter().map(String::as_str))
        .collect();

    referenced
        .into_iter()
        .filter_map(|rule_ref| {
            let matches: Vec<String> = rules
                .iter()
                .filter(|(id, name, _)| *id == Some(rule_ref) || *name == Some(rule_ref))
                .map(|(_, _, title)| format!("'{title}'"))
                .collect();
            (matches.len() > 1).then(|| {
                format!(
                    "{rule_ref}: ambiguous rule reference matches {}",
                    matches.join(", ")
                )
            })
        })
        .collect()
}

/// Load external sources and, when `--resolve-sources` is set, resolve every
/// dynamic pipeline and external source, returning the expanded pipelines.
/// Exits with `CONFIG_ERROR` on a load or resolution failure.
#[cfg(feature = "daemon")]
fn resolve_validate_sources(
    mut pipelines: Vec<rsigma_eval::Pipeline>,
    resolve_sources: bool,
    source_files: &[PathBuf],
    print_human_ok: bool,
) -> Vec<rsigma_eval::Pipeline> {
    // Load external sources alongside pipeline-embedded ones (validating that
    // the source files parse, regardless of whether we resolve them).
    let external_sources = if !source_files.is_empty() {
        match rsigma_runtime::sources::registry::load_external_sources(source_files) {
            Ok(ext) => ext.into_iter().map(|(s, _)| s).collect::<Vec<_>>(),
            Err(e) => {
                eprintln!("Error loading external sources: {e}");
                process::exit(crate::exit_code::CONFIG_ERROR);
            }
        }
    } else {
        Vec::new()
    };

    if resolve_sources {
        let has_dynamic = pipelines.iter().any(|p| p.is_dynamic()) || !external_sources.is_empty();
        if has_dynamic {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap_or_else(|e| {
                    eprintln!("Failed to create async runtime for source resolution: {e}");
                    process::exit(crate::exit_code::CONFIG_ERROR);
                });

            let resolver = rsigma_runtime::DefaultSourceResolver::new();
            let mut resolved_pipelines = Vec::with_capacity(pipelines.len());
            let mut source_errors: Vec<String> = Vec::new();

            // Resolve the external sources once into a shared data map that
            // every dynamic pipeline's `${source.*}` references expand against.
            let resolved_data = match rt.block_on(rsigma_runtime::sources::resolve_all(
                &resolver,
                &external_sources,
            )) {
                Ok(data) => data,
                Err(e) => {
                    source_errors.push(format!("external sources: {e}"));
                    std::collections::HashMap::new()
                }
            };

            for pipeline in &pipelines {
                if pipeline.is_dynamic() {
                    let mut expanded = rsigma_runtime::sources::template::TemplateExpander::expand(
                        pipeline,
                        &resolved_data,
                    );
                    if let Err(e) = rsigma_runtime::sources::include::expand_includes(
                        &mut expanded,
                        &resolved_data,
                        &external_sources,
                        false,
                    ) {
                        source_errors.push(format!("pipeline '{}': {e}", pipeline.name));
                    }
                    resolved_pipelines.push(expanded);
                } else {
                    resolved_pipelines.push(pipeline.clone());
                }
            }

            if !source_errors.is_empty() {
                eprintln!("Source resolution errors:");
                for err in &source_errors {
                    eprintln!("  - {err}");
                }
                process::exit(crate::exit_code::CONFIG_ERROR);
            }

            pipelines = resolved_pipelines;
            if print_human_ok {
                println!("  Sources resolved:  OK");
            }
        }
    }

    pipelines
}