rust-threat-detector 2.0.0

Advanced memory-safe SIEM threat detection with ML-based scoring, automated incident response, and threat hunting capabilities
Documentation
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! # Anomaly Detection Engine
//!
//! Statistical and time-series based anomaly detection for identifying
//! unusual patterns in security logs and metrics.

use crate::{LogEntry, ThreatAlert, ThreatCategory, ThreatSeverity};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

/// Anomaly detection method
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DetectionMethod {
    /// Z-score based detection (standard deviations from mean)
    ZScore,
    /// Moving average with threshold
    MovingAverage,
    /// Exponential smoothing
    ExponentialSmoothing,
    /// Inter-Quartile Range (IQR)
    IQR,
}

/// Time series metric for tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeries {
    pub name: String,
    pub values: VecDeque<f64>,
    pub timestamps: VecDeque<DateTime<Utc>>,
    pub max_size: usize,
}

impl TimeSeries {
    /// Create new time series with maximum history
    pub fn new(name: String, max_size: usize) -> Self {
        Self {
            name,
            values: VecDeque::with_capacity(max_size),
            timestamps: VecDeque::with_capacity(max_size),
            max_size,
        }
    }

    /// Add value to time series
    pub fn add(&mut self, value: f64, timestamp: DateTime<Utc>) {
        if self.values.len() >= self.max_size {
            self.values.pop_front();
            self.timestamps.pop_front();
        }
        self.values.push_back(value);
        self.timestamps.push_back(timestamp);
    }

    /// Calculate mean
    pub fn mean(&self) -> f64 {
        if self.values.is_empty() {
            return 0.0;
        }
        self.values.iter().sum::<f64>() / self.values.len() as f64
    }

    /// Calculate standard deviation
    pub fn std_dev(&self) -> f64 {
        if self.values.len() < 2 {
            return 0.0;
        }
        let mean = self.mean();
        let variance = self.values.iter().map(|x| (x - mean).powi(2)).sum::<f64>()
            / (self.values.len() - 1) as f64;
        variance.sqrt()
    }

    /// Calculate moving average over window
    pub fn moving_average(&self, window_size: usize) -> f64 {
        if self.values.is_empty() {
            return 0.0;
        }
        let window = window_size.min(self.values.len());
        let start = self.values.len().saturating_sub(window);
        self.values.iter().skip(start).sum::<f64>() / window as f64
    }

    /// Get percentile value
    pub fn percentile(&self, p: f64) -> f64 {
        if self.values.is_empty() {
            return 0.0;
        }
        let mut sorted: Vec<f64> = self.values.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let index = ((p / 100.0) * (sorted.len() - 1) as f64).round() as usize;
        sorted[index]
    }

    /// Calculate IQR (Inter-Quartile Range)
    pub fn iqr(&self) -> (f64, f64, f64) {
        let q1 = self.percentile(25.0);
        let q3 = self.percentile(75.0);
        let iqr = q3 - q1;
        (q1, q3, iqr)
    }
}

/// Anomaly detection engine
pub struct AnomalyDetector {
    metrics: HashMap<String, TimeSeries>,
    z_score_threshold: f64,
    iqr_multiplier: f64,
    moving_avg_window: usize,
    smoothing_alpha: f64,
}

impl AnomalyDetector {
    /// Create new anomaly detector with default parameters
    pub fn new() -> Self {
        Self {
            metrics: HashMap::new(),
            z_score_threshold: 3.0, // 3 sigma
            iqr_multiplier: 1.5,    // Standard IQR multiplier
            moving_avg_window: 10,
            smoothing_alpha: 0.3, // Exponential smoothing factor
        }
    }

    /// Create with custom parameters
    pub fn with_params(
        z_score_threshold: f64,
        iqr_multiplier: f64,
        moving_avg_window: usize,
        smoothing_alpha: f64,
    ) -> Self {
        Self {
            metrics: HashMap::new(),
            z_score_threshold,
            iqr_multiplier,
            moving_avg_window,
            smoothing_alpha,
        }
    }

    /// Track metric value
    pub fn track_metric(&mut self, name: &str, value: f64, timestamp: DateTime<Utc>) {
        let metric = self
            .metrics
            .entry(name.to_string())
            .or_insert_with(|| TimeSeries::new(name.to_string(), 1000));
        metric.add(value, timestamp);
    }

    /// Detect anomaly using specified method
    pub fn detect(
        &self,
        metric_name: &str,
        current_value: f64,
        method: DetectionMethod,
    ) -> Option<AnomalyResult> {
        let metric = self.metrics.get(metric_name)?;

        if metric.values.is_empty() {
            return None;
        }

        match method {
            DetectionMethod::ZScore => self.detect_zscore(metric, current_value),
            DetectionMethod::MovingAverage => self.detect_moving_avg(metric, current_value),
            DetectionMethod::ExponentialSmoothing => self.detect_exponential(metric, current_value),
            DetectionMethod::IQR => self.detect_iqr(metric, current_value),
        }
    }

    /// Detect using Z-score (standard deviations from mean)
    fn detect_zscore(&self, metric: &TimeSeries, value: f64) -> Option<AnomalyResult> {
        if metric.values.len() < 10 {
            return None; // Need sufficient history
        }

        let mean = metric.mean();
        let std_dev = metric.std_dev();

        if std_dev == 0.0 {
            return None; // No variation
        }

        let z_score = (value - mean).abs() / std_dev;

        if z_score > self.z_score_threshold {
            Some(AnomalyResult {
                metric_name: metric.name.clone(),
                current_value: value,
                expected_value: mean,
                deviation: z_score,
                method: DetectionMethod::ZScore,
                severity: self.calculate_severity(z_score, self.z_score_threshold),
                description: format!(
                    "Value {:.2} deviates {:.2} standard deviations from mean {:.2}",
                    value, z_score, mean
                ),
            })
        } else {
            None
        }
    }

    /// Detect using moving average
    fn detect_moving_avg(&self, metric: &TimeSeries, value: f64) -> Option<AnomalyResult> {
        if metric.values.len() < self.moving_avg_window {
            return None;
        }

        let moving_avg = metric.moving_average(self.moving_avg_window);
        let std_dev = metric.std_dev();

        if std_dev == 0.0 {
            return None;
        }

        let deviation = (value - moving_avg).abs() / std_dev;

        if deviation > self.z_score_threshold {
            Some(AnomalyResult {
                metric_name: metric.name.clone(),
                current_value: value,
                expected_value: moving_avg,
                deviation,
                method: DetectionMethod::MovingAverage,
                severity: self.calculate_severity(deviation, self.z_score_threshold),
                description: format!(
                    "Value {:.2} deviates from moving average {:.2} by {:.2} std devs",
                    value, moving_avg, deviation
                ),
            })
        } else {
            None
        }
    }

    /// Detect using exponential smoothing
    fn detect_exponential(&self, metric: &TimeSeries, value: f64) -> Option<AnomalyResult> {
        if metric.values.is_empty() {
            return None;
        }

        // Calculate exponentially weighted moving average
        let mut ewma = metric.values[0];
        for &v in metric.values.iter().skip(1) {
            ewma = self.smoothing_alpha * v + (1.0 - self.smoothing_alpha) * ewma;
        }

        let std_dev = metric.std_dev();
        if std_dev == 0.0 {
            return None;
        }

        let deviation = (value - ewma).abs() / std_dev;

        if deviation > self.z_score_threshold {
            Some(AnomalyResult {
                metric_name: metric.name.clone(),
                current_value: value,
                expected_value: ewma,
                deviation,
                method: DetectionMethod::ExponentialSmoothing,
                severity: self.calculate_severity(deviation, self.z_score_threshold),
                description: format!(
                    "Value {:.2} deviates from exponential moving average {:.2}",
                    value, ewma
                ),
            })
        } else {
            None
        }
    }

    /// Detect using IQR (Inter-Quartile Range)
    fn detect_iqr(&self, metric: &TimeSeries, value: f64) -> Option<AnomalyResult> {
        if metric.values.len() < 10 {
            return None;
        }

        let (q1, q3, iqr) = metric.iqr();
        let lower_bound = q1 - self.iqr_multiplier * iqr;
        let upper_bound = q3 + self.iqr_multiplier * iqr;

        if value < lower_bound || value > upper_bound {
            let deviation = if value < lower_bound {
                (lower_bound - value) / iqr
            } else {
                (value - upper_bound) / iqr
            };

            Some(AnomalyResult {
                metric_name: metric.name.clone(),
                current_value: value,
                expected_value: (q1 + q3) / 2.0,
                deviation,
                method: DetectionMethod::IQR,
                severity: self.calculate_severity(deviation, 1.0),
                description: format!(
                    "Value {:.2} outside IQR bounds [{:.2}, {:.2}]",
                    value, lower_bound, upper_bound
                ),
            })
        } else {
            None
        }
    }

    /// Calculate severity based on deviation
    fn calculate_severity(&self, deviation: f64, threshold: f64) -> ThreatSeverity {
        let ratio = deviation / threshold;
        if ratio > 3.0 {
            ThreatSeverity::Critical
        } else if ratio > 2.0 {
            ThreatSeverity::High
        } else if ratio > 1.5 {
            ThreatSeverity::Medium
        } else {
            ThreatSeverity::Low
        }
    }

    /// Analyze log for metric anomalies
    pub fn analyze_log(&mut self, log: &LogEntry) -> Vec<ThreatAlert> {
        let mut alerts = Vec::new();

        // Extract metrics from log metadata
        for (key, value_str) in &log.metadata {
            if let Ok(value) = value_str.parse::<f64>() {
                let metric_name = format!("log.{}", key);
                self.track_metric(&metric_name, value, log.timestamp);

                // Try all detection methods
                for method in &[
                    DetectionMethod::ZScore,
                    DetectionMethod::MovingAverage,
                    DetectionMethod::IQR,
                ] {
                    if let Some(anomaly) = self.detect(&metric_name, value, *method) {
                        alerts.push(anomaly.to_threat_alert(log));
                        break; // Only generate one alert per metric
                    }
                }
            }
        }

        alerts
    }

    /// Get metric statistics
    pub fn get_metric(&self, name: &str) -> Option<&TimeSeries> {
        self.metrics.get(name)
    }

    /// Get all tracked metrics
    pub fn get_all_metrics(&self) -> Vec<&str> {
        self.metrics.keys().map(|s| s.as_str()).collect()
    }

    /// Clear old data from metrics
    pub fn clear_old_data(&mut self, before: DateTime<Utc>) {
        for metric in self.metrics.values_mut() {
            while let Some(&timestamp) = metric.timestamps.front() {
                if timestamp < before {
                    metric.timestamps.pop_front();
                    metric.values.pop_front();
                } else {
                    break;
                }
            }
        }
    }
}

impl Default for AnomalyDetector {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of anomaly detection
#[derive(Debug, Clone)]
pub struct AnomalyResult {
    pub metric_name: String,
    pub current_value: f64,
    pub expected_value: f64,
    pub deviation: f64,
    pub method: DetectionMethod,
    pub severity: ThreatSeverity,
    pub description: String,
}

impl AnomalyResult {
    /// Convert to ThreatAlert
    pub fn to_threat_alert(&self, source_log: &LogEntry) -> ThreatAlert {
        ThreatAlert {
            alert_id: format!("ANOMALY-{}", chrono::Utc::now().timestamp()),
            timestamp: Utc::now(),
            severity: self.severity,
            category: ThreatCategory::AnomalousActivity,
            description: format!(
                "Statistical anomaly in {}: {}",
                self.metric_name, self.description
            ),
            source_log: format!("{} - {}", source_log.timestamp, source_log.message),
            indicators: vec![
                format!("Current: {:.2}", self.current_value),
                format!("Expected: {:.2}", self.expected_value),
                format!("Deviation: {:.2}", self.deviation),
                format!("Method: {:?}", self.method),
            ],
            recommended_action:
                "Investigate metric anomaly, review related logs, check for system issues"
                    .to_string(),
            threat_score: self.calculate_threat_score(),
            correlated_alerts: vec![],
        }
    }

    fn calculate_threat_score(&self) -> u32 {
        let base_score = match self.severity {
            ThreatSeverity::Info => 10,
            ThreatSeverity::Low => 25,
            ThreatSeverity::Medium => 50,
            ThreatSeverity::High => 75,
            ThreatSeverity::Critical => 95,
        };

        // Adjust based on deviation magnitude
        let deviation_bonus = (self.deviation * 2.0).min(20.0) as u32;
        (base_score + deviation_bonus).min(100)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Duration;
    use std::collections::HashMap;

    #[test]
    fn test_time_series_mean() {
        let mut ts = TimeSeries::new("test".to_string(), 100);
        ts.add(10.0, Utc::now());
        ts.add(20.0, Utc::now());
        ts.add(30.0, Utc::now());

        assert_eq!(ts.mean(), 20.0);
    }

    #[test]
    fn test_time_series_std_dev() {
        let mut ts = TimeSeries::new("test".to_string(), 100);
        for i in 1..=10 {
            ts.add(i as f64, Utc::now());
        }

        let std_dev = ts.std_dev();
        assert!(std_dev > 0.0);
        assert!(std_dev < 4.0); // Approximate std dev for 1-10
    }

    #[test]
    fn test_time_series_moving_average() {
        let mut ts = TimeSeries::new("test".to_string(), 100);
        ts.add(10.0, Utc::now());
        ts.add(20.0, Utc::now());
        ts.add(30.0, Utc::now());
        ts.add(40.0, Utc::now());

        let ma = ts.moving_average(2);
        assert_eq!(ma, 35.0); // (30 + 40) / 2
    }

    #[test]
    fn test_time_series_percentile() {
        let mut ts = TimeSeries::new("test".to_string(), 100);
        for i in 1..=100 {
            ts.add(i as f64, Utc::now());
        }

        assert_eq!(ts.percentile(0.0), 1.0);
        assert_eq!(ts.percentile(100.0), 100.0);
        let median = ts.percentile(50.0);
        assert!((49.0..=52.0).contains(&median));
    }

    #[test]
    fn test_time_series_iqr() {
        let mut ts = TimeSeries::new("test".to_string(), 100);
        for i in 1..=100 {
            ts.add(i as f64, Utc::now());
        }

        let (q1, q3, iqr) = ts.iqr();
        assert!((24.0..=27.0).contains(&q1));
        assert!((73.0..=77.0).contains(&q3));
        assert!((48.0..=52.0).contains(&iqr));
    }

    #[test]
    fn test_zscore_detection() {
        let mut detector = AnomalyDetector::new();

        // Build baseline
        for i in 0..20 {
            detector.track_metric("test_metric", 100.0 + (i as f64), Utc::now());
        }

        // Test normal value - should not detect
        let result = detector.detect("test_metric", 110.0, DetectionMethod::ZScore);
        assert!(result.is_none());

        // Test anomalous value - should detect
        let result = detector.detect("test_metric", 500.0, DetectionMethod::ZScore);
        assert!(result.is_some());
        let anomaly = result.unwrap();
        assert_eq!(anomaly.metric_name, "test_metric");
        assert_eq!(anomaly.current_value, 500.0);
    }

    #[test]
    fn test_moving_average_detection() {
        let mut detector = AnomalyDetector::new();

        for i in 0..15 {
            detector.track_metric("test_metric", 50.0 + i as f64, Utc::now());
        }

        let result = detector.detect("test_metric", 200.0, DetectionMethod::MovingAverage);
        assert!(result.is_some());
    }

    #[test]
    fn test_iqr_detection() {
        let mut detector = AnomalyDetector::new();

        // Normal distribution
        for i in 1..=20 {
            detector.track_metric("test_metric", i as f64 * 10.0, Utc::now());
        }

        // Outlier
        let result = detector.detect("test_metric", 1000.0, DetectionMethod::IQR);
        assert!(result.is_some());

        // Normal value
        let result = detector.detect("test_metric", 105.0, DetectionMethod::IQR);
        assert!(result.is_none());
    }

    #[test]
    fn test_exponential_smoothing() {
        let mut detector = AnomalyDetector::with_params(3.0, 1.5, 10, 0.3);

        for i in 0..20 {
            detector.track_metric("test_metric", 100.0 + (i as f64), Utc::now());
        }

        let result = detector.detect("test_metric", 500.0, DetectionMethod::ExponentialSmoothing);
        assert!(result.is_some());
    }

    #[test]
    fn test_severity_calculation() {
        let detector = AnomalyDetector::new();

        assert_eq!(
            detector.calculate_severity(10.0, 3.0),
            ThreatSeverity::Critical
        );
        assert_eq!(detector.calculate_severity(6.5, 3.0), ThreatSeverity::High);
        assert_eq!(
            detector.calculate_severity(4.8, 3.0),
            ThreatSeverity::Medium
        );
        assert_eq!(detector.calculate_severity(3.2, 3.0), ThreatSeverity::Low);
    }

    #[test]
    fn test_analyze_log() {
        let mut detector = AnomalyDetector::new();

        // Build baseline
        for _ in 0..20 {
            let mut metadata = HashMap::new();
            metadata.insert("request_count".to_string(), "100".to_string());
            let log = LogEntry {
                timestamp: Utc::now(),
                source_ip: Some("192.168.1.1".to_string()),
                user: Some("test".to_string()),
                event_type: "metric".to_string(),
                message: "Normal traffic".to_string(),
                metadata,
            };
            detector.analyze_log(&log);
        }

        // Anomalous log
        let mut metadata = HashMap::new();
        metadata.insert("request_count".to_string(), "10000".to_string());
        let log = LogEntry {
            timestamp: Utc::now(),
            source_ip: Some("192.168.1.1".to_string()),
            user: Some("test".to_string()),
            event_type: "metric".to_string(),
            message: "Spike in traffic".to_string(),
            metadata,
        };

        let alerts = detector.analyze_log(&log);
        assert!(!alerts.is_empty());
        assert_eq!(alerts[0].category, ThreatCategory::AnomalousActivity);
    }

    #[test]
    fn test_clear_old_data() {
        let mut detector = AnomalyDetector::new();

        let old_time = Utc::now() - Duration::hours(2);
        let new_time = Utc::now();

        detector.track_metric("test", 10.0, old_time);
        detector.track_metric("test", 20.0, new_time);

        let metric = detector.get_metric("test").unwrap();
        assert_eq!(metric.values.len(), 2);

        let cutoff = Utc::now() - Duration::hours(1);
        detector.clear_old_data(cutoff);

        let metric = detector.get_metric("test").unwrap();
        assert_eq!(metric.values.len(), 1);
        assert_eq!(metric.values[0], 20.0);
    }

    #[test]
    fn test_get_all_metrics() {
        let mut detector = AnomalyDetector::new();

        detector.track_metric("metric1", 10.0, Utc::now());
        detector.track_metric("metric2", 20.0, Utc::now());
        detector.track_metric("metric3", 30.0, Utc::now());

        let metrics = detector.get_all_metrics();
        assert_eq!(metrics.len(), 3);
        assert!(metrics.contains(&"metric1"));
        assert!(metrics.contains(&"metric2"));
        assert!(metrics.contains(&"metric3"));
    }

    #[test]
    fn test_anomaly_to_threat_alert() {
        let anomaly = AnomalyResult {
            metric_name: "test_metric".to_string(),
            current_value: 500.0,
            expected_value: 100.0,
            deviation: 5.0,
            method: DetectionMethod::ZScore,
            severity: ThreatSeverity::High,
            description: "Test anomaly".to_string(),
        };

        let log = LogEntry {
            timestamp: Utc::now(),
            source_ip: Some("192.168.1.1".to_string()),
            user: Some("test".to_string()),
            event_type: "test".to_string(),
            message: "test message".to_string(),
            metadata: HashMap::new(),
        };

        let alert = anomaly.to_threat_alert(&log);
        assert_eq!(alert.severity, ThreatSeverity::High);
        assert_eq!(alert.category, ThreatCategory::AnomalousActivity);
        assert!(alert.threat_score > 0);
    }
}