rsigma-parser 0.28.0

Parser for Sigma detection rules, correlations, and filters
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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//! Loading ruleset sources with per-document provenance.

use std::collections::HashSet;
use std::path::{Component, Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::ast::{CorrelationRule, FilterRule, FilterRuleTarget, LogSource, SigmaRule};
use crate::diagnostic::CollectionDiagnostic;

use super::error::RulesetError;
use super::spec::RulesetSpec;

/// Options that constrain a resolution.
#[derive(Debug, Clone, Default)]
pub struct ResolveOptions {
    /// Reject any source or overlay path outside this root. Pipelines are not
    /// resolved here, so callers confine pipeline paths themselves.
    pub confine_to: Option<PathBuf>,
}

/// A rule document together with where it came from.
#[derive(Debug, Clone, PartialEq)]
pub struct Candidate {
    /// The parsed document.
    pub doc: CandidateDoc,
    /// Where the document was read from.
    pub origin: RuleOrigin,
}

/// A parsed document from a ruleset source.
#[derive(Debug, Clone, PartialEq)]
pub enum CandidateDoc {
    /// A detection rule.
    Detection(SigmaRule),
    /// A correlation rule.
    Correlation(CorrelationRule),
    /// A filter rule.
    Filter(FilterRule),
}

/// Where a candidate came from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RuleOrigin {
    /// Index of the source in the spec.
    pub source: usize,
    /// The source root: a directory, or the parent of a file source.
    pub root: PathBuf,
    /// Path relative to the root, with `/` separators.
    pub file: PathBuf,
    /// Position among documents of its kind in that file.
    pub ordinal: usize,
}

/// A file read while resolving a ruleset, with the bytes used to hash it.
#[derive(Debug, Clone, PartialEq)]
pub struct SourceFile {
    /// Index of the source in the spec; always 0 for overlay files.
    pub source: usize,
    /// The path as read.
    pub path: PathBuf,
    /// Path relative to the source root (the working directory for overlay
    /// files), with `/` separators.
    pub relative: PathBuf,
    /// The file's bytes as read, hashed into the lockfile digest.
    pub sha256_input: Vec<u8>,
}

/// Everything read from a spec's sources.
#[derive(Debug, Clone, Default)]
pub struct LoadedSources {
    /// Every parsed document, in source and file order.
    pub candidates: Vec<Candidate>,
    /// Every file read.
    pub files: Vec<SourceFile>,
    /// Parse diagnostics, with file paths.
    pub diagnostics: Vec<CollectionDiagnostic>,
}

/// The kind of a Sigma document.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DocumentKind {
    /// A detection rule.
    Detection,
    /// A correlation rule.
    Correlation,
    /// A filter rule.
    Filter,
}

impl DocumentKind {
    /// The lowercase name used in selectors and reports.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Detection => "detection",
            Self::Correlation => "correlation",
            Self::Filter => "filter",
        }
    }
}

impl Candidate {
    /// The document kind.
    pub fn kind(&self) -> DocumentKind {
        match self.doc {
            CandidateDoc::Detection(_) => DocumentKind::Detection,
            CandidateDoc::Correlation(_) => DocumentKind::Correlation,
            CandidateDoc::Filter(_) => DocumentKind::Filter,
        }
    }

    /// The document `id`.
    pub fn id(&self) -> Option<&str> {
        match &self.doc {
            CandidateDoc::Detection(rule) => rule.id.as_deref(),
            CandidateDoc::Correlation(rule) => rule.id.as_deref(),
            CandidateDoc::Filter(rule) => rule.id.as_deref(),
        }
    }

    /// The document `name`.
    pub fn name(&self) -> Option<&str> {
        match &self.doc {
            CandidateDoc::Detection(rule) => rule.name.as_deref(),
            CandidateDoc::Correlation(rule) => rule.name.as_deref(),
            CandidateDoc::Filter(rule) => rule.name.as_deref(),
        }
    }

    /// The document title.
    pub fn title(&self) -> &str {
        match &self.doc {
            CandidateDoc::Detection(rule) => &rule.title,
            CandidateDoc::Correlation(rule) => &rule.title,
            CandidateDoc::Filter(rule) => &rule.title,
        }
    }

    /// The document logsource; correlations have none of their own.
    pub fn logsource(&self) -> Option<&LogSource> {
        match &self.doc {
            CandidateDoc::Detection(rule) => Some(&rule.logsource),
            CandidateDoc::Correlation(_) => None,
            CandidateDoc::Filter(rule) => rule.logsource.as_ref(),
        }
    }

    /// The document tags.
    pub fn tags(&self) -> &[String] {
        match &self.doc {
            CandidateDoc::Detection(rule) => &rule.tags,
            CandidateDoc::Correlation(rule) => &rule.tags,
            CandidateDoc::Filter(rule) => &rule.tags,
        }
    }

    /// The document level.
    pub fn level(&self) -> Option<crate::ast::Level> {
        match &self.doc {
            CandidateDoc::Detection(rule) => rule.level,
            CandidateDoc::Correlation(rule) => rule.level,
            CandidateDoc::Filter(rule) => rule.level,
        }
    }

    /// The document status.
    pub fn status(&self) -> Option<crate::ast::Status> {
        match &self.doc {
            CandidateDoc::Detection(rule) => rule.status,
            CandidateDoc::Correlation(rule) => rule.status,
            CandidateDoc::Filter(rule) => rule.status,
        }
    }

    /// Rules a correlation references, by id or name.
    pub fn referenced_rules(&self) -> &[String] {
        match &self.doc {
            CandidateDoc::Correlation(rule) => &rule.rules,
            _ => &[],
        }
    }

    /// Rules a filter targets, if it is not `Any`.
    pub fn filter_targets(&self) -> Option<&[String]> {
        match &self.doc {
            CandidateDoc::Filter(rule) => match &rule.rules {
                FilterRuleTarget::Any => None,
                FilterRuleTarget::Specific(targets) => Some(targets),
            },
            _ => None,
        }
    }
}

/// Load every source declared by `spec` in declaration order, parsing each
/// file separately so every document keeps its origin.
pub fn load_sources(
    spec: &RulesetSpec,
    opts: &ResolveOptions,
) -> Result<LoadedSources, RulesetError> {
    let mut loaded = LoadedSources::default();

    for (source_index, source) in spec.sources.iter().enumerate() {
        if !source.exists() {
            return Err(RulesetError::MissingSource {
                ruleset: None,
                path: source.clone(),
            });
        }
        let root = if source.is_dir() {
            source.clone()
        } else {
            source
                .parent()
                .map(Path::to_path_buf)
                .unwrap_or_else(|| PathBuf::from("."))
        };

        let mut paths = Vec::new();
        if source.is_dir() {
            // Per source, so overlapping sources each see their full tree and
            // the uniqueness check reports the overlap.
            let mut visited = HashSet::new();
            crate::walk::collect_yaml_files(&root, &root, None, &mut paths, &mut visited).map_err(
                |e| RulesetError::Io {
                    ruleset: None,
                    path: root.clone(),
                    source: std::io::Error::other(e.to_string()),
                },
            )?;
        } else {
            paths.push(source.clone());
        }

        for path in paths {
            let canonical = path.canonicalize().map_err(|e| RulesetError::Io {
                ruleset: None,
                path: path.clone(),
                source: e,
            })?;
            confine(&canonical, opts)?;

            let bytes = std::fs::read(&path).map_err(|e| RulesetError::Io {
                ruleset: None,
                path: path.clone(),
                source: e,
            })?;
            let text = String::from_utf8_lossy(&bytes);
            let relative = relative_to(&path, &root);
            let mut collection = match crate::parse_sigma_yaml(&text) {
                Ok(collection) => collection,
                Err(e) => {
                    loaded
                        .diagnostics
                        .push(CollectionDiagnostic::file(&path, &e));
                    continue;
                }
            };
            for diagnostic in &mut collection.diagnostics {
                diagnostic.path = Some(path.clone());
            }
            loaded.diagnostics.extend(collection.diagnostics);

            loaded.files.push(SourceFile {
                source: source_index,
                path: path.clone(),
                relative: relative.clone(),
                sha256_input: bytes,
            });

            for (ordinal, rule) in collection.rules.into_iter().enumerate() {
                loaded.candidates.push(Candidate {
                    doc: CandidateDoc::Detection(rule),
                    origin: RuleOrigin {
                        source: source_index,
                        root: root.clone(),
                        file: relative.clone(),
                        ordinal,
                    },
                });
            }
            for (ordinal, rule) in collection.correlations.into_iter().enumerate() {
                loaded.candidates.push(Candidate {
                    doc: CandidateDoc::Correlation(rule),
                    origin: RuleOrigin {
                        source: source_index,
                        root: root.clone(),
                        file: relative.clone(),
                        ordinal,
                    },
                });
            }
            for (ordinal, rule) in collection.filters.into_iter().enumerate() {
                loaded.candidates.push(Candidate {
                    doc: CandidateDoc::Filter(rule),
                    origin: RuleOrigin {
                        source: source_index,
                        root: root.clone(),
                        file: relative.clone(),
                        ordinal,
                    },
                });
            }
        }
    }

    Ok(loaded)
}

/// Reject a canonical path outside the confinement root.
pub(crate) fn confine(path: &Path, opts: &ResolveOptions) -> Result<(), RulesetError> {
    let Some(root) = &opts.confine_to else {
        return Ok(());
    };
    let root = root.canonicalize().map_err(|e| RulesetError::Io {
        ruleset: None,
        path: root.clone(),
        source: e,
    })?;
    if path.starts_with(&root) {
        Ok(())
    } else {
        Err(RulesetError::Confinement {
            ruleset: None,
            path: path.to_path_buf(),
        })
    }
}

/// Render `path` relative to `root` with `/` separators.
pub(crate) fn relative_to(path: &Path, root: &Path) -> PathBuf {
    let relative = path.strip_prefix(root).unwrap_or(path);
    let mut out = PathBuf::new();
    for component in relative.components() {
        match component {
            Component::Normal(part) => out.push(part),
            Component::CurDir => {}
            Component::ParentDir => out.push(".."),
            Component::RootDir | Component::Prefix(_) => {}
        }
    }
    let text = out
        .components()
        .filter_map(|component| match component {
            Component::Normal(part) => Some(part.to_string_lossy().into_owned()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("/");
    PathBuf::from(text)
}

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

    fn write(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(path, contents).unwrap();
    }

    fn rule(id: &str, title: &str) -> String {
        format!(
            "title: {title}\nid: {id}\nlogsource:\n    product: linux\ndetection:\n    selection:\n        Field: value\n    condition: selection\n"
        )
    }

    #[test]
    fn walks_nested_directories_in_sorted_order() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write(&root.join("rules/b.yml"), &rule("b", "B"));
        write(&root.join("rules/a/z.yml"), &rule("z", "Z"));
        write(&root.join("rules/a/a.yml"), &rule("a", "A"));
        write(&root.join("rules/.hidden/c.yml"), &rule("c", "C"));

        let spec = RulesetSpec {
            sources: vec![root.join("rules")],
            ..Default::default()
        };
        let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
        let files: Vec<String> = loaded
            .files
            .iter()
            .map(|f| f.relative.to_string_lossy().into_owned())
            .collect();
        assert_eq!(files, ["a/a.yml", "a/z.yml", "b.yml"], "{files:?}");
        assert_eq!(loaded.candidates.len(), 3);
        assert_eq!(loaded.candidates[0].origin.ordinal, 0);
        assert_eq!(loaded.candidates[1].origin.ordinal, 0);
        assert_eq!(loaded.candidates[2].origin.ordinal, 0);
    }

    #[test]
    fn overlapping_sources_load_the_same_files_in_either_order() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write(&root.join("rules/windows/w.yml"), &rule("w", "W"));
        write(&root.join("rules/l.yml"), &rule("l", "L"));
        for sources in [
            vec![root.join("rules"), root.join("rules/windows")],
            vec![root.join("rules/windows"), root.join("rules")],
        ] {
            let spec = RulesetSpec {
                sources,
                ..Default::default()
            };
            let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
            let per_source: Vec<usize> = (0..2)
                .map(|index| {
                    loaded
                        .candidates
                        .iter()
                        .filter(|c| c.origin.source == index)
                        .count()
                })
                .collect();
            let windows_first = spec.sources[0].ends_with("windows");
            let expected = if windows_first { [1, 2] } else { [2, 1] };
            assert_eq!(per_source, expected, "{:?}", spec.sources);
        }
    }

    #[test]
    fn reads_a_file_source_with_any_extension() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("rule.txt");
        write(&path, &rule("x", "X"));
        let spec = RulesetSpec {
            sources: vec![path.clone()],
            ..Default::default()
        };
        let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
        assert_eq!(loaded.candidates.len(), 1);
        assert_eq!(loaded.candidates[0].origin.file, PathBuf::from("rule.txt"));
        assert_eq!(loaded.candidates[0].origin.root, dir.path());
    }

    #[test]
    fn symlink_loop_terminates() {
        #[cfg(unix)]
        {
            let dir = tempfile::tempdir().unwrap();
            let root = dir.path().join("rules");
            write(&root.join("a.yml"), &rule("a", "A"));
            std::os::unix::fs::symlink(&root, root.join("loop")).unwrap();
            let spec = RulesetSpec {
                sources: vec![root],
                ..Default::default()
            };
            let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
            assert_eq!(loaded.candidates.len(), 1);
        }
    }

    #[test]
    fn missing_source_errors() {
        let spec = RulesetSpec {
            sources: vec![PathBuf::from("/definitely/not/here")],
            ..Default::default()
        };
        let error = load_sources(&spec, &ResolveOptions::default()).unwrap_err();
        assert!(
            matches!(error, RulesetError::MissingSource { .. }),
            "{error}"
        );
    }

    #[test]
    fn multi_document_files_get_per_kind_ordinals() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("rules.yml");
        write(
            &path,
            &format!(
                "{}\n---\n{}\n---\ntitle: Filter\nlogsource:\n    product: linux\nfilter:\n    rules: any\n    selection:\n        Field: value\n    condition: selection\n",
                rule("a", "A"),
                rule("b", "B")
            ),
        );
        let spec = RulesetSpec {
            sources: vec![path],
            ..Default::default()
        };
        let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
        assert_eq!(loaded.candidates.len(), 3);
        assert_eq!(loaded.candidates[0].origin.ordinal, 0);
        assert_eq!(loaded.candidates[1].origin.ordinal, 1);
        assert_eq!(loaded.candidates[2].origin.ordinal, 0);
        assert_eq!(loaded.candidates[2].kind(), DocumentKind::Filter);
    }

    #[test]
    fn parse_diagnostics_keep_paths() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("broken.yml");
        write(&path, "title: Broken\nlogsource: {}\ndetection: {}\n");
        let spec = RulesetSpec {
            sources: vec![path.clone()],
            ..Default::default()
        };
        let loaded = load_sources(&spec, &ResolveOptions::default()).unwrap();
        assert!(!loaded.diagnostics.is_empty());
        assert_eq!(loaded.diagnostics[0].path.as_deref(), Some(path.as_path()));
    }

    #[test]
    fn confinement_rejects_outside_and_symlink_escape() {
        let dir = tempfile::tempdir().unwrap();
        let inside = dir.path().join("inside");
        let outside = dir.path().join("outside");
        std::fs::create_dir_all(&inside).unwrap();
        std::fs::create_dir_all(&outside).unwrap();
        write(&outside.join("r.yml"), &rule("x", "X"));

        let opts = ResolveOptions {
            confine_to: Some(inside.clone()),
        };
        let spec = RulesetSpec {
            sources: vec![outside.clone()],
            ..Default::default()
        };
        let error = load_sources(&spec, &opts).unwrap_err();
        assert!(matches!(error, RulesetError::Confinement { .. }), "{error}");

        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(&outside, inside.join("link")).unwrap();
            let spec = RulesetSpec {
                sources: vec![inside.join("link")],
                ..Default::default()
            };
            let error = load_sources(&spec, &opts).unwrap_err();
            assert!(matches!(error, RulesetError::Confinement { .. }), "{error}");
        }
    }
}