Struct Rule

Source
pub struct Rule {
    pub detection: Detection,
    pub true_positives: Vec<Value>,
    pub true_negatives: Vec<Value>,
    /* private fields */
}
Expand description

A rule used by the solver to evaluate a Document.

A rule contains the detection logic, along with the true positive and negative tests. The inclusion of these basic test allows for a basic level of verification to be ensured.

Rules are written in YAML and have a simple but powerful syntax.

§Syntax

There are two parts to a rule’s logic: the condition & the identifiers.

§Condition

The condition is the main expression and describes the top level logic for the rule. It can be comprised of the following:

Expression Description
_ and _ The logical conjunction of two operands, where the operands are any of the following:
  • expression: a nested expression.
  • identifier: a key that matches an identifier in the detection block.
_ or _ The logical disjunction of two operands, where the operands are any of the following:
  • expression: a nested expression.
  • identifier: a key that matches an identifier in the detection block.
_ == _ The equality comparison of two operands, where the operands are any of the following:
  • integer: an integer.
  • string: a string.
  • int(field): a field that should be cast as an integer.
  • str(field): a field that should be cast as a string.
_ > _ The greater than comparison of two operands, where the operands are any of the following:
  • integer: an integer.
  • int(field): a field that should be cast as an integer.
_ >= _ The greater than or equal comparison of two operands, where the operands are any of the following:
  • integer: an integer.
  • int(field): a field that should be cast as an integer.
_ < _ The less than comparison of two operands, where the operands are any of the following:
  • integer: an integer.
  • int(field): a field that should be cast as an integer.
_ <= _ The less than or equal comparison of two operands, where the operands are any of the following:
  • integer: an integer.
  • int(field): a field that should be cast as an integer.
all(i) An identifier mutator that evaluates to true only if all conditions for identifier i match.
not _ Negate the result of an expression. NOTE: This will only negate a result that is true or false, it will noop if the result is missing.
of(i, x) An identifier mutator that evaluates to true only if a minimum of x conditions for identifier i match.

§Identifiers

Identifiers are used to describe the matching logic for the values contained within documents. These are then collected by the condition in order to create a rule that can be used to tag a document.

Due to the nature of an identifier, they are essentially just variations on key/value pairs. The following variations are supported, where mappings are treated as conjunctions and sequences are treated as disjunctions:

# K/V Pairs
IDENTIFIER:
    KEY: MATCH

# K/V Pairs with multiple matches
IDENTIFIER:
    KEY:
    - MATCH_0
    - MATCH_1

# K/V Pairs (Grouped)
IDENTIFIER:
    - KEY: MATCH

# K/V Pairs (Nested)
IDENTIFIER:
    KEY:
        KEY: MATCH

Identifiers are unique keys that can be referenced in the condition.

Keys are used to get the values from documents. Keys can be wrapped in the following modifiers:

Expression Description
all(k) A key mutator that evaluates to true only if all matches for keys k match.
of(k, x) A key mutator that evaluates to true only if a minimum of x matches for key k match.

Matches are the expressions which are evaluated against values returned by keys. They support the following syntax:

Expression Description
foo An exact match
foo* Starts with
*foo Ends with
*foo* Contains
?foo Regex
i_ A prefix to convert the match into a case insensitive match.

To escape any of the above in order to achieve literal string matching, combinations of ' and " can be used.

§Examples

Here is a very simple rule example:

detection:
  A:
    foo: "foo*"
    bar: "*bar"
  B:
    foobar:
    - foobar
    - foobaz

  condition: A and B

true_positives:
- foo: foobar
  bar: foobar
  foobar: foobar

true_negatives:
- foo: bar
  bar: foo
  foobar: barfoo

Here is a slightly more complex rule example:

detection:
  A:
    all(phrase):
    - "*quick*"
    - "*brown*"
  B:
    phrase: ibear

  condition: A and not B

true_positives:
- phrase: the quick brown fox

true_negatives:
- foo: the quick brown BEAR

Fields§

§detection: Detection§true_positives: Vec<Value>§true_negatives: Vec<Value>

Implementations§

Source§

impl Rule

Source

pub fn load(path: &Path) -> Result<Self, Error>

Load a rule from a YAML file.

Source

pub fn from_str(s: &str) -> Result<Self, Error>

Load a rule from a YAML string.

Source

pub fn from_value(value: Value) -> Result<Self, Error>

Load a rule from a YAML Value.

Source

pub fn optimise(self, options: Optimisations) -> Self

Optimise the rule with the optimisations provided.

Source

pub fn matches(&self, document: &dyn Document) -> bool

Evaluates the rule against the provided Document, returning true if it has matched.

Source

pub fn validate(&self) -> Result<bool, Error>

Validates the rule’s detection logic against the provided true positives and negatives.

Trait Implementations§

Source§

impl Clone for Rule

Source§

fn clone(&self) -> Rule

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Rule

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Rule

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Rule

§

impl RefUnwindSafe for Rule

§

impl Send for Rule

§

impl Sync for Rule

§

impl Unpin for Rule

§

impl UnwindSafe for Rule

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,