Skip to main content

rsigma_eval/
lib.rs

1//! # rsigma-eval
2//!
3//! Evaluator for Sigma detection and correlation rules.
4//!
5//! This crate consumes the AST produced by [`rsigma_parser`] and evaluates it
6//! against events in real time using a compile-then-evaluate model.
7//!
8//! ## Architecture
9//!
10//! - **Detection rules** (stateless): compiled once into optimized matchers,
11//!   each event is matched with zero allocation on the hot path.
12//! - **Correlation rules** (stateful): time-windowed aggregation over detection
13//!   matches, supporting `event_count`, `value_count`, `temporal`,
14//!   `temporal_ordered`, `value_sum`, `value_avg`, `value_percentile`,
15//!   and `value_median`.
16//!
17//! ## Quick Start — Detection Only
18//!
19//! ```rust
20//! use rsigma_parser::parse_sigma_yaml;
21//! use rsigma_eval::Engine;
22//! use rsigma_eval::event::JsonEvent;
23//! use serde_json::json;
24//!
25//! let yaml = r#"
26//! title: Detect Whoami
27//! logsource:
28//!     product: windows
29//!     category: process_creation
30//! detection:
31//!     selection:
32//!         CommandLine|contains: 'whoami'
33//!     condition: selection
34//! level: medium
35//! "#;
36//!
37//! let collection = parse_sigma_yaml(yaml).unwrap();
38//! let mut engine = Engine::new();
39//! engine.add_collection(&collection).unwrap();
40//!
41//! let event_val = json!({"CommandLine": "cmd /c whoami"});
42//! let event = JsonEvent::borrow(&event_val);
43//! let matches = engine.evaluate(&event);
44//! assert_eq!(matches.len(), 1);
45//! ```
46//!
47//! ## Quick Start — With Correlations
48//!
49//! ```rust
50//! use rsigma_parser::parse_sigma_yaml;
51//! use rsigma_eval::{CorrelationEngine, CorrelationConfig};
52//! use rsigma_eval::event::JsonEvent;
53//! use serde_json::json;
54//!
55//! let yaml = r#"
56//! title: Login
57//! id: login-rule
58//! logsource:
59//!     category: auth
60//! detection:
61//!     selection:
62//!         EventType: login
63//!     condition: selection
64//! ---
65//! title: Many Logins
66//! correlation:
67//!     type: event_count
68//!     rules:
69//!         - login-rule
70//!     group-by:
71//!         - User
72//!     timespan: 60s
73//!     condition:
74//!         gte: 3
75//! level: high
76//! "#;
77//!
78//! let collection = parse_sigma_yaml(yaml).unwrap();
79//! let mut engine = CorrelationEngine::new(CorrelationConfig::default());
80//! engine.add_collection(&collection).unwrap();
81//!
82//! for i in 0..3 {
83//!     let v = json!({"EventType": "login", "User": "admin"});
84//!     let event = JsonEvent::borrow(&v);
85//!     let result = engine.process_event_at(&event, 1000 + i);
86//!     if i == 2 {
87//!         let correlations = result.iter().filter(|r| r.is_correlation()).count();
88//!         assert_eq!(correlations, 1);
89//!     }
90//! }
91//! ```
92
93mod candidate_index;
94pub mod compiler;
95pub mod correlation;
96pub mod correlation_engine;
97pub mod engine;
98pub mod error;
99pub mod event;
100pub mod explain;
101pub mod field_observer;
102pub mod fields;
103pub mod logsource;
104pub mod matcher;
105pub mod pipeline;
106pub mod result;
107pub mod router;
108pub mod rule_draft;
109pub mod rule_metadata;
110pub mod rule_tune;
111pub mod schema;
112pub mod schema_discovery;
113mod witness;
114
115// Re-export the most commonly used types and functions at crate root
116pub use compiler::{
117    CompiledDetection, CompiledDetectionItem, CompiledRule, compile_rule, compile_to_compiled,
118    evaluate_rule,
119};
120pub use correlation::{
121    CompiledCondition, CompiledCorrelation, EventBuffer, EventRef, EventRefBuffer, GroupByField,
122    GroupKey, WindowState,
123};
124pub use correlation_engine::{
125    CorrelationAction, CorrelationConfig, CorrelationEngine, CorrelationEventMode, CorrelationInfo,
126    CorrelationSnapshot, CorrelationStateSnapshot, GroupKeyPart, GroupStateInfo, ProcessResult,
127    TimestampFallback,
128};
129pub use engine::Engine;
130pub use error::{EvalError, Result};
131pub use event::{Event, EventValue, JsonEvent, KvEvent, MapEvent, MappedEvent, PlainEvent};
132pub use explain::{
133    ConditionTrace, DetectionTrace, ItemTrace, MatchReason, RuleExplanation, SelectionBranch,
134    explain_rule,
135};
136pub use field_observer::{FieldCoverage, FieldObservation, FieldObservationEntry, FieldObserver};
137pub use fields::{FieldOrigin, FieldSource, RuleFieldSet};
138pub use logsource::LogSourceExtractor;
139pub use matcher::{CompiledMatcher, MatchDescriptor};
140pub use pipeline::{
141    Pipeline, PipelineState, TransformationItem, TransformedRule, apply_pipelines,
142    apply_pipelines_with_state,
143    builtin::{
144        builtin_names as builtin_pipeline_names, resolve_builtin as resolve_builtin_pipeline,
145    },
146    merge_pipelines, parse_pipeline, parse_pipeline_file, parse_sources, parse_sources_dir,
147    parse_sources_file, parse_transformation_items, transform_collection, transform_rule,
148    validate_source_refs,
149};
150pub use result::{
151    CorrelationBody, DetectionBody, EvaluationResult, FieldMatch, MatchDetailLevel, MatcherKind,
152    ProcessResultExt, ResultBody, RuleHeader,
153};
154pub use router::{RouteOutcome, RouteResult, SchemaPruning, SchemaRouter};
155pub use rule_draft::{
156    DraftConfig, DraftError, DraftFieldReport, DraftReport, Stability, draft_rule,
157};
158pub use rule_metadata::{RuleBundleMetadata, RuleIdentity, RuleKind, RuleMetadataLookup};
159pub use rule_tune::{
160    TuneConfig, TuneError, TuneExpectationDiff, TuneFieldDisposition, TuneFieldReport, TuneReport,
161    TuneSelectionReport, TuneVerification, tune_rule,
162};
163pub use schema::{
164    FieldValueConfig, OnUnknown, PredicateOutcome, RouteDecision, RoutingConfig, RoutingPlan,
165    SchemaBinding, SchemaClassifier, SchemaCountEntry, SchemaError, SchemaExplanation, SchemaMatch,
166    SchemaObservation, SchemaObserver, SchemaPredicate, SchemaPredicateConfig, SchemaSignature,
167    SchemaSignatureConfig, SchemaSignaturesFile, SignatureExplanation, UnknownShapeEntry,
168    builtin_schema_names, load_schema_config, load_schema_signatures, parse_schema_config,
169    parse_schema_signatures, validate_schema_config,
170};
171pub use schema_discovery::{
172    CandidateSource, DiscoveryCandidate, DiscoveryConfig, DiscoveryReport, DiscoveryStats,
173    FieldProfile, cluster_count, mine_events, mine_shapes,
174};