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 ads;
56pub mod ast;
57pub mod condition;
58pub mod emit;
59pub mod error;
60pub mod exemplar;
61pub mod fieldpath;
62pub mod lint;
63pub mod parser;
64pub mod reference;
65pub mod selector;
66pub mod value;
67pub mod version;
68
69// Re-export the most commonly used types and functions at crate root
70pub use ads::{
71    AdsCarrier, AdsContent, AdsDocument, AdsScaffoldEntry, AdsSection, AdsSectionInfo,
72    AdsSectionStatus, ads_catalogue,
73};
74pub use ast::{
75    ArrayQuantifier, ConditionExpr, ConditionOperator, CorrelationCondition, CorrelationRule,
76    CorrelationType, Detection, DetectionItem, Detections, FieldAlias, FieldSpec, FilterRule,
77    FilterRuleTarget, Level, LogSource, Modifier, Quantifier, Related, RelationType,
78    SelectorPattern, SigmaCollection, SigmaDocument, SigmaRule, Status, WindowMode,
79};
80pub use condition::parse_condition;
81pub use emit::{emit_collection_yaml, emit_rule_yaml};
82pub use error::{Result, SigmaParserError, SourceLocation};
83pub use exemplar::{
84    EXEMPLARS_KEY, Exemplar, ExemplarErrorKind, ExemplarPayload, ExemplarRuleKind,
85    ExemplarShapeError, Expect, TimedEvent, correlation_exemplars, exemplars, exemplars_from_attrs,
86    filter_exemplars, match_exemplar_count, match_exemplar_count_json, parse_exemplars,
87    raw_exemplar_values, raw_match_exemplar_count, raw_winning_exemplars,
88};
89pub use lint::catalogue::{LintRuleInfo, catalogue};
90#[cfg(feature = "fix")]
91pub use lint::fix::{SourceFixOutcome, apply_fixes_to_source};
92pub use lint::{
93    AdsConfig, FileLintResult, Fix, FixDisposition, FixPatch, InlineSuppressions, LintConfig,
94    LintRule, LintWarning, Severity, Span, apply_suppressions, lint_yaml_directory,
95    lint_yaml_directory_with_config, lint_yaml_file, lint_yaml_file_with_config, lint_yaml_str,
96    lint_yaml_str_with_config, lint_yaml_value, parse_inline_suppressions,
97};
98pub use parser::{parse_field_spec, parse_sigma_directory, parse_sigma_file, parse_sigma_yaml};
99pub use selector::detection_name_matches;
100pub use value::{SigmaString, SigmaValue, SpecialChar, StringPart, Timespan};
101pub use version::{
102    SPEC_VERSION_ARRAY_MATCHING, SPEC_VERSION_FLOOR, SPEC_VERSION_SUPPORTED,
103    array_matching_enabled, is_unsupported, resolve_major,
104};