File size: 2,648 Bytes
da27866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Behaviour Vector Engine.
//!
//! Raw events are folded into a compact per-process vector that decays over
//! time. Raw events themselves are short-lived; the vector is what persists.

use crate::event::EventKind;

/// Half-life of behavioural memory (simulated ms).
const HALF_LIFE_MS: f32 = 60_000.0;

#[derive(Clone, Copy, Debug, Default)]
pub struct BehaviorVector {
    pub network: f32,
    pub filesystem: f32,
    pub memory: f32,
    pub privilege: f32,
    pub injection: f32,
    pub persistence: f32,
}

impl BehaviorVector {
    /// Exponential decay toward zero.
    pub fn decay(&mut self, dt_ms: u64) {
        let f = 0.5f32.powf(dt_ms as f32 / HALF_LIFE_MS);
        self.network *= f;
        self.filesystem *= f;
        self.memory *= f;
        self.privilege *= f;
        self.injection *= f;
        self.persistence *= f;
    }

    /// Fold one event into the vector.
    pub fn apply(&mut self, kind: EventKind) {
        use EventKind::*;
        match kind {
            NetworkConnect => self.network += 1.0,
            FileWrite => self.filesystem += 1.0,
            RwxAlloc => self.memory += 3.0,
            UnsignedModuleLoad => self.memory += 2.0,
            PrivilegeEscalation => self.privilege += 3.0,
            CredentialAccess => self.privilege += 4.0,
            RemoteThreadInjection => self.injection += 4.0,
            PersistenceWrite => self.persistence += 3.0,
            TempExecution => self.filesystem += 2.0,
            DeletedExecutable => self.memory += 2.0,
            _ => {}
        }
    }

    /// Rough single-number summary (used only for display).
    pub fn magnitude(&self) -> f32 {
        (self.network.powi(2)
            + self.filesystem.powi(2)
            + self.memory.powi(2)
            + self.privilege.powi(2)
            + self.injection.powi(2)
            + self.persistence.powi(2))
        .sqrt()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn decay_halves_after_half_life() {
        let mut v = BehaviorVector::default();
        v.apply(EventKind::NetworkConnect);
        v.apply(EventKind::NetworkConnect);
        assert!((v.network - 2.0).abs() < 1e-6);
        v.decay(60_000);
        assert!((v.network - 1.0).abs() < 1e-3);
    }

    #[test]
    fn injection_weighs_more_than_file_io() {
        let mut a = BehaviorVector::default();
        let mut b = BehaviorVector::default();
        a.apply(EventKind::RemoteThreadInjection);
        b.apply(EventKind::FileWrite);
        assert!(a.magnitude() > b.magnitude());
    }
}