Skip to main content

rsigma_parser/
lib.rs

1//! # rsigma-parser
2//!
3//! A comprehensive parser for Sigma detection rules, correlations, and filters.
4//!
5//! This crate parses Sigma YAML files into a strongly-typed AST, handling:
6//!
7//! - **Detection rules**: field matching, wildcards, boolean conditions, field modifiers
8//! - **Condition expressions**: `and`, `or`, `not`, `1 of`, `all of`, parenthesized groups
9//! - **Correlation rules**: `event_count`, `value_count`, `temporal`, aggregations
10//! - **Filter rules**: additional conditions applied to referenced rules
11//! - **Rule collections**: multi-document YAML, `action: global/reset/repeat`
12//! - **Value types**: strings with wildcards, numbers, booleans, null, regex, CIDR
13//! - **All 30+ field modifiers**: `contains`, `endswith`, `startswith`, `re`, `cidr`,
14//!   `base64`, `base64offset`, `wide`, `windash`, `all`, `cased`, `exists`, `fieldref`,
15//!   comparison operators, regex flags, timestamp parts, and more
16//!
17//! ## Architecture
18//!
19//! - **PEG grammar** ([`pest`]) for condition expression parsing with correct operator
20//!   precedence (`NOT` > `AND` > `OR`) and Pratt parsing
21//! - **yaml_serde** for YAML structure deserialization
22//! - **Custom parsing** for field modifiers, wildcard strings, and timespan values
23//!
24//! ## Quick Start
25//!
26//! ```rust
27//! use rsigma_parser::parse_sigma_yaml;
28//!
29//! let yaml = r#"
30//! title: Detect Whoami
31//! logsource:
32//!     product: windows
33//!     category: process_creation
34//! detection:
35//!     selection:
36//!         CommandLine|contains: 'whoami'
37//!     condition: selection
38//! level: medium
39//! "#;
40//!
41//! let collection = parse_sigma_yaml(yaml).unwrap();
42//! assert_eq!(collection.rules.len(), 1);
43//! assert_eq!(collection.rules[0].title, "Detect Whoami");
44//! ```
45//!
46//! ## Parsing condition expressions
47//!
48//! ```rust
49//! use rsigma_parser::parse_condition;
50//!
51//! let expr = parse_condition("selection_main and 1 of selection_dword_* and not 1 of filter_*").unwrap();
52//! println!("{expr}");
53//! ```
54
55pub mod ast;
56pub mod condition;
57pub mod error;
58pub mod fieldpath;
59pub mod lint;
60pub mod parser;
61pub mod selector;
62pub mod value;
63pub mod version;
64
65// Re-export the most commonly used types and functions at crate root
66pub use ast::{
67    ArrayQuantifier, ConditionExpr, ConditionOperator, CorrelationCondition, CorrelationRule,
68    CorrelationType, Detection, DetectionItem, Detections, FieldAlias, FieldSpec, FilterRule,
69    FilterRuleTarget, Level, LogSource, Modifier, Quantifier, Related, RelationType,
70    SelectorPattern, SigmaCollection, SigmaDocument, SigmaRule, Status, WindowMode,
71};
72pub use condition::parse_condition;
73pub use error::{Result, SigmaParserError, SourceLocation};
74pub use lint::{
75    FileLintResult, Fix, FixDisposition, FixPatch, InlineSuppressions, LintConfig, LintRule,
76    LintWarning, Severity, Span, apply_suppressions, lint_yaml_directory,
77    lint_yaml_directory_with_config, lint_yaml_file, lint_yaml_file_with_config, lint_yaml_str,
78    lint_yaml_str_with_config, lint_yaml_value, parse_inline_suppressions,
79};
80pub use parser::{parse_field_spec, parse_sigma_directory, parse_sigma_file, parse_sigma_yaml};
81pub use selector::detection_name_matches;
82pub use value::{SigmaString, SigmaValue, SpecialChar, StringPart, Timespan};
83pub use version::{
84    SPEC_VERSION_ARRAY_MATCHING, SPEC_VERSION_FLOOR, SPEC_VERSION_SUPPORTED,
85    array_matching_enabled, is_unsupported, resolve_major,
86};