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//! - **Semantic validation** ([`validate`]) rejects invalid rules before lowering: a
24//! missing or unusable logsource, conflicting modifiers, values of the wrong type
25//! for their modifiers, invalid regular expressions and CIDR networks, empty
26//! detections, and conditions that reference undefined detections
27//!
28//! ## Quick Start
29//!
30//! ```rust
31//! use rsigma_parser::parse_sigma_yaml;
32//!
33//! let yaml = r#"
34//! title: Detect Whoami
35//! logsource:
36//! product: windows
37//! category: process_creation
38//! detection:
39//! selection:
40//! CommandLine|contains: 'whoami'
41//! condition: selection
42//! level: medium
43//! "#;
44//!
45//! let collection = parse_sigma_yaml(yaml).unwrap();
46//! assert_eq!(collection.rules.len(), 1);
47//! assert_eq!(collection.rules[0].title, "Detect Whoami");
48//! ```
49//!
50//! ## Parsing condition expressions
51//!
52//! ```rust
53//! use rsigma_parser::parse_condition;
54//!
55//! let expr = parse_condition("selection_main and 1 of selection_dword_* and not 1 of filter_*").unwrap();
56//! println!("{expr}");
57//! ```
58
59pub mod ads;
60pub mod ast;
61pub mod condition;
62pub mod diagnostic;
63pub mod emit;
64pub mod error;
65pub mod exemplar;
66pub mod fieldpath;
67#[cfg(feature = "fmt")]
68pub mod fmt;
69pub mod lint;
70pub mod parser;
71pub mod reference;
72pub mod ruleset;
73pub mod selector;
74mod style;
75pub mod validate;
76pub mod value;
77pub mod version;
78mod walk;
79
80// Re-export the most commonly used types and functions at crate root
81pub use ads::{
82 AdsCarrier, AdsContent, AdsDocument, AdsScaffoldEntry, AdsSection, AdsSectionInfo,
83 AdsSectionStatus, ads_catalogue,
84};
85pub use ast::{
86 ArrayQuantifier, ConditionExpr, ConditionOperator, CorrelationCondition, CorrelationRule,
87 CorrelationType, Detection, DetectionItem, Detections, FieldAlias, FieldSpec, FilterRule,
88 FilterRuleTarget, Level, LogSource, Modifier, Quantifier, Related, RelationType,
89 SelectorPattern, SigmaCollection, SigmaDocument, SigmaRule, Status, WindowMode,
90};
91pub use condition::parse_condition;
92pub use diagnostic::{CollectionDiagnostic, DiagnosticKind, DiagnosticSeverity};
93pub use emit::{emit_collection_yaml, emit_correlation_yaml, emit_filter_yaml, emit_rule_yaml};
94pub use error::{Result, SigmaParserError, SigmaParserErrorKind, SourceLocation};
95pub use exemplar::{
96 EXEMPLARS_KEY, Exemplar, ExemplarErrorKind, ExemplarPayload, ExemplarRuleKind,
97 ExemplarShapeError, Expect, TimedEvent, correlation_exemplars, exemplars, exemplars_from_attrs,
98 filter_exemplars, match_exemplar_count, match_exemplar_count_json, parse_exemplars,
99 raw_exemplar_values, raw_match_exemplar_count, raw_winning_exemplars,
100};
101#[cfg(feature = "fmt")]
102pub use fmt::{FormatError, FormatOutcome, RangeEdit, format_yaml, format_yaml_range};
103pub use lint::catalogue::{LintRuleInfo, catalogue};
104#[cfg(feature = "fix")]
105pub use lint::fix::{SourceFixOutcome, apply_fixes_to_source};
106pub use lint::{
107 AdsConfig, FileLintResult, Fix, FixDisposition, FixPatch, InlineSuppressions, LintConfig,
108 LintRule, LintWarning, Severity, Span, apply_suppressions, lint_yaml_directory,
109 lint_yaml_directory_with_config, lint_yaml_file, lint_yaml_file_with_config, lint_yaml_str,
110 lint_yaml_str_with_config, lint_yaml_value, parse_inline_suppressions,
111};
112pub use parser::{parse_field_spec, parse_sigma_directory, parse_sigma_file, parse_sigma_yaml};
113pub use selector::detection_name_matches;
114pub use value::{SigmaString, SigmaValue, SpecialChar, StringPart, Timespan};
115pub use version::{
116 SPEC_VERSION_ARRAY_MATCHING, SPEC_VERSION_FLOOR, SPEC_VERSION_SUPPORTED,
117 array_matching_enabled, is_unsupported, resolve_major,
118};