Skip to main content

ExtractLogSource

Trait ExtractLogSource 

Source
pub trait ExtractLogSource<E: Event> {
    // Required method
    fn extract(&self, event: &E) -> LogSource;
}
Expand description

Derives an event’s LogSource for conflict-based logsource pruning.

Engine, CorrelationEngine, and SchemaRouter are generic over their extractor type, defaulting to the field-reading LogSourceExtractor, and call it once per event with static dispatch. The trait is generic over the event type, so an extractor can read state that only a concrete event type carries.

A dimension left unset is a wildcard for pruning. An extractor that cannot determine a dimension must leave it None rather than guess, so a missing value never prunes a rule.

§Example

An event type that carries its logsource as metadata beside the payload:

use std::borrow::Cow;

use rsigma_eval::event::{Event, EventValue, JsonEvent};
use rsigma_eval::{Engine, ExtractLogSource};
use rsigma_parser::{LogSource, parse_sigma_yaml};
use serde_json::{Value, json};

struct TaggedEvent {
    product: &'static str,
    payload: JsonEvent<'static>,
}

impl Event for TaggedEvent {
    fn get_field(&self, path: &str) -> Option<EventValue<'_>> {
        self.payload.get_field(path)
    }
    fn any_string_value(&self, pred: &dyn Fn(&str) -> bool) -> bool {
        self.payload.any_string_value(pred)
    }
    fn all_string_values(&self) -> Vec<Cow<'_, str>> {
        self.payload.all_string_values()
    }
    fn to_json(&self) -> Value {
        self.payload.to_json()
    }
}

struct MetadataExtractor;

impl ExtractLogSource<TaggedEvent> for MetadataExtractor {
    fn extract(&self, event: &TaggedEvent) -> LogSource {
        LogSource {
            product: Some(event.product.to_string()),
            ..LogSource::default()
        }
    }
}

let rules = parse_sigma_yaml(r#"
title: Linux whoami
logsource:
    product: linux
detection:
    selection:
        CommandLine|contains: whoami
    condition: selection
"#).unwrap();

let mut engine = Engine::with_logsource_extractor(MetadataExtractor);
engine.add_collection(&rules).unwrap();

let payload = || JsonEvent::owned(json!({"CommandLine": "whoami"}));
let linux = TaggedEvent { product: "linux", payload: payload() };
let windows = TaggedEvent { product: "windows", payload: payload() };

assert_eq!(engine.evaluate(&linux).len(), 1);
assert!(engine.evaluate(&windows).is_empty()); // pruned by metadata

Required Methods§

Source

fn extract(&self, event: &E) -> LogSource

Extract the event’s logsource.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§