pub struct Engine { /* private fields */ }Expand description
The main rule evaluation engine.
Holds a set of compiled rules and provides methods to evaluate events against them. Supports optional logsource routing for performance.
§Example
use rsigma_parser::parse_sigma_yaml;
use rsigma_eval::{Engine, Event};
use rsigma_eval::event::JsonEvent;
use serde_json::json;
let yaml = r#"
title: Detect Whoami
logsource:
product: windows
category: process_creation
detection:
selection:
CommandLine|contains: 'whoami'
condition: selection
level: medium
"#;
let collection = parse_sigma_yaml(yaml).unwrap();
let mut engine = Engine::new();
engine.add_collection(&collection).unwrap();
let event_val = json!({"CommandLine": "cmd /c whoami"});
let event = JsonEvent::borrow(&event_val);
let matches = engine.evaluate(&event);
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].rule_title, "Detect Whoami");Implementations§
Source§impl Engine
impl Engine
Sourcepub fn new_with_pipeline(pipeline: Pipeline) -> Self
pub fn new_with_pipeline(pipeline: Pipeline) -> Self
Create a new engine with a pipeline.
Sourcepub fn set_bloom_prefilter(&mut self, enabled: bool)
pub fn set_bloom_prefilter(&mut self, enabled: bool)
Enable or disable bloom-filter pre-filtering of positive substring detection items.
When enabled, evaluate* short-circuits any positive substring
matcher (Contains / StartsWith / EndsWith / AhoCorasickSet,
alone or wrapped in CaseInsensitiveGroup) whose field cannot
possibly contain a needle trigram.
Disabled by default. The per-event probe (trigram extraction +
double hashing) costs ~1 µs on a typical CommandLine field, which
outweighs the savings on rule sets where most events overlap with
at least one needle. Enable for workloads that pair many substring
rules with mostly-non-matching events; benchmark with
eval_bloom_rejection before flipping it on in production.
Sourcepub fn bloom_prefilter_enabled(&self) -> bool
pub fn bloom_prefilter_enabled(&self) -> bool
Returns whether bloom pre-filtering is currently enabled.
Sourcepub fn set_bloom_max_bytes(&mut self, max_bytes: usize)
pub fn set_bloom_max_bytes(&mut self, max_bytes: usize)
Set the memory budget for the per-field bloom index.
Must be called before add_collection / add_rule for the new
budget to take effect on the existing rule set; otherwise it is
applied at the next index rebuild. The default budget is 1 MB,
shared across all per-field filters. Lower the cap on memory-
constrained deployments; raise it for large rule sets where the
default starts evicting useful filters.
Sourcepub fn bloom_max_bytes(&self) -> Option<usize>
pub fn bloom_max_bytes(&self) -> Option<usize>
Returns the configured bloom memory budget, if one has been set
explicitly. None means the crate default (1 MB) is in use.
Sourcepub fn set_include_event(&mut self, include: bool)
pub fn set_include_event(&mut self, include: bool)
Set global include_event — when true, all match results include
the full event JSON regardless of per-rule custom attributes.
Sourcepub fn add_pipeline(&mut self, pipeline: Pipeline)
pub fn add_pipeline(&mut self, pipeline: Pipeline)
Add a pipeline to the engine.
Pipelines are applied to rules during add_rule / add_collection.
Only affects rules added after this call.
Sourcepub fn add_rule(&mut self, rule: &SigmaRule) -> Result<()>
pub fn add_rule(&mut self, rule: &SigmaRule) -> Result<()>
Add a single parsed Sigma rule.
If pipelines are set, the rule is cloned and transformed before compilation. The inverted index is rebuilt after adding the rule.
Sourcepub fn add_collection(&mut self, collection: &SigmaCollection) -> Result<()>
pub fn add_collection(&mut self, collection: &SigmaCollection) -> Result<()>
Add all detection rules from a parsed collection, then apply filters.
Filter rules modify referenced detection rules by appending exclusion
conditions. Correlation rules are handled by CorrelationEngine.
The inverted index is rebuilt once after all rules and filters are loaded.
Sourcepub fn add_collection_with_pipelines(
&mut self,
collection: &SigmaCollection,
pipelines: &[Pipeline],
) -> Result<()>
pub fn add_collection_with_pipelines( &mut self, collection: &SigmaCollection, pipelines: &[Pipeline], ) -> Result<()>
Add all detection rules from a collection, applying the given pipelines.
This is a convenience method that temporarily sets pipelines, adds the collection, then clears them. The inverted index is rebuilt once after all rules and filters are loaded.
Sourcepub fn apply_filter(&mut self, filter: &FilterRule) -> Result<()>
pub fn apply_filter(&mut self, filter: &FilterRule) -> Result<()>
Apply a filter rule to all referenced detection rules and rebuild the index.
Sourcepub fn add_compiled_rule(&mut self, rule: CompiledRule)
pub fn add_compiled_rule(&mut self, rule: CompiledRule)
Add a pre-compiled rule directly and rebuild the index.
Sourcepub fn evaluate<E: Event>(&self, event: &E) -> Vec<MatchResult>
pub fn evaluate<E: Event>(&self, event: &E) -> Vec<MatchResult>
Evaluate an event against candidate rules using the inverted index.
Sourcepub fn evaluate_with_logsource<E: Event>(
&self,
event: &E,
event_logsource: &LogSource,
) -> Vec<MatchResult>
pub fn evaluate_with_logsource<E: Event>( &self, event: &E, event_logsource: &LogSource, ) -> Vec<MatchResult>
Evaluate an event against candidate rules matching the given logsource.
Uses the inverted index for candidate pre-filtering, then applies the
logsource constraint. Only rules whose logsource is compatible with
event_logsource are evaluated.
Sourcepub fn evaluate_batch<E: Event + Sync>(
&self,
events: &[&E],
) -> Vec<Vec<MatchResult>>
pub fn evaluate_batch<E: Event + Sync>( &self, events: &[&E], ) -> Vec<Vec<MatchResult>>
Evaluate a batch of events, returning per-event match results.
When the parallel feature is enabled, events are evaluated concurrently
using rayon’s work-stealing thread pool. Otherwise, falls back to
sequential evaluation.
Sourcepub fn rule_count(&self) -> usize
pub fn rule_count(&self) -> usize
Number of rules loaded in the engine.
Sourcepub fn rules(&self) -> &[CompiledRule]
pub fn rules(&self) -> &[CompiledRule]
Access the compiled rules.