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