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
// ============================================================================
// File: endpoint/file_quarantine.rs
// Description: Encrypted quarantine vault for detected threats with chain of custody
// Author: Andrew Jewell Sr. - AutomataNexus
// Updated: March 24, 2026
// ============================================================================
//! Quarantine Vault — moves detected threats to a secure vault with full
//! chain-of-custody tracking (hash, timestamp, original path, permissions).

use super::Severity;
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::io::Read;
use std::path::{Path, PathBuf};

/// Configuration for the quarantine vault.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuarantineVaultConfig {
    pub vault_dir: PathBuf,
    pub retention_days: u32,
    pub max_vault_size_bytes: u64,
}

impl QuarantineVaultConfig {
    pub fn new(vault_dir: PathBuf) -> Self {
        Self {
            vault_dir,
            retention_days: 30,
            max_vault_size_bytes: 1_073_741_824, // 1 GB
        }
    }
}

/// A record of a quarantined file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuarantineEntry {
    pub id: String,
    pub original_path: PathBuf,
    pub quarantine_path: PathBuf,
    pub sha256: String,
    pub detection_reason: String,
    pub scanner: String,
    pub severity: Severity,
    pub quarantined_at: DateTime<Utc>,
    pub original_permissions: u32,
    pub file_size: u64,
}

/// Secure quarantine vault for detected threats.
pub struct QuarantineVault {
    config: QuarantineVaultConfig,
    index: RwLock<Vec<QuarantineEntry>>,
}

impl QuarantineVault {
    /// Create a new quarantine vault, loading existing index if present.
    pub fn new(config: QuarantineVaultConfig) -> Self {
        let _ = std::fs::create_dir_all(&config.vault_dir);

        let vault = Self {
            config: config.clone(),
            index: RwLock::new(Vec::new()),
        };
        vault.load_index();
        vault
    }

    /// Move a detected file to the quarantine vault.
    pub fn quarantine_file(
        &self,
        path: &Path,
        reason: &str,
        scanner: &str,
        severity: Severity,
    ) -> Result<QuarantineEntry, String> {
        // Read file and compute hash
        let mut file = std::fs::File::open(path).map_err(|e| format!("Cannot open file: {}", e))?;
        let metadata = file.metadata().map_err(|e| format!("Cannot read metadata: {}", e))?;
        let file_size = metadata.len();

        // Check vault size limit
        if self.vault_size() + file_size > self.config.max_vault_size_bytes {
            return Err("Quarantine vault is full".to_string());
        }

        // Compute SHA-256
        let mut hasher = Sha256::new();
        let mut buf = [0u8; 8192];
        loop {
            let n = file.read(&mut buf).map_err(|e| format!("Read error: {}", e))?;
            if n == 0 {
                break;
            }
            hasher.update(&buf[..n]);
        }
        let sha256 = hex::encode(hasher.finalize());

        // Get original permissions
        #[cfg(unix)]
        let original_permissions = {
            use std::os::unix::fs::PermissionsExt;
            metadata.permissions().mode()
        };
        #[cfg(not(unix))]
        let original_permissions = 0o644u32;

        // Generate quarantine filename
        let id = uuid::Uuid::new_v4().to_string();
        let quarantine_path = self.config.vault_dir.join(format!("{}.quarantine", id));

        // Copy file to quarantine (then delete original)
        std::fs::copy(path, &quarantine_path)
            .map_err(|e| format!("Copy to quarantine failed: {}", e))?;
        std::fs::remove_file(path)
            .map_err(|e| format!("Remove original failed: {}", e))?;

        // Strip permissions on quarantine file
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(0o000);
            let _ = std::fs::set_permissions(&quarantine_path, perms);
        }

        let entry = QuarantineEntry {
            id,
            original_path: path.to_path_buf(),
            quarantine_path,
            sha256,
            detection_reason: reason.to_string(),
            scanner: scanner.to_string(),
            severity,
            quarantined_at: Utc::now(),
            original_permissions,
            file_size,
        };

        {
            let mut idx = self.index.write();
            idx.push(entry.clone());
        }
        self.save_index();

        tracing::info!(
            file = %path.display(),
            reason = %reason,
            scanner = %scanner,
            "File quarantined"
        );

        Ok(entry)
    }

    /// Restore a quarantined file to its original location.
    pub fn restore_file(&self, id: &str) -> Result<PathBuf, String> {
        let entry = {
            let idx = self.index.read();
            idx.iter().find(|e| e.id == id).cloned()
        };

        let entry = entry.ok_or_else(|| format!("Quarantine entry '{}' not found", id))?;

        // Restore permissions on quarantine file so we can read it
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(0o644);
            let _ = std::fs::set_permissions(&entry.quarantine_path, perms);
        }

        // Create parent directory if needed
        if let Some(parent) = entry.original_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        // Copy back to original path
        std::fs::copy(&entry.quarantine_path, &entry.original_path)
            .map_err(|e| format!("Restore failed: {}", e))?;

        // Restore original permissions
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = std::fs::Permissions::from_mode(entry.original_permissions);
            let _ = std::fs::set_permissions(&entry.original_path, perms);
        }

        // Remove quarantine file and index entry
        let _ = std::fs::remove_file(&entry.quarantine_path);
        {
            let mut idx = self.index.write();
            idx.retain(|e| e.id != id);
        }
        self.save_index();

        tracing::info!(file = %entry.original_path.display(), "File restored from quarantine");
        Ok(entry.original_path)
    }

    /// Permanently delete a quarantined file.
    pub fn delete_entry(&self, id: &str) -> Result<(), String> {
        let entry = {
            let idx = self.index.read();
            idx.iter().find(|e| e.id == id).cloned()
        };

        let entry = entry.ok_or_else(|| format!("Entry '{}' not found", id))?;

        // Make file deletable
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let _ = std::fs::set_permissions(
                &entry.quarantine_path,
                std::fs::Permissions::from_mode(0o644),
            );
        }

        let _ = std::fs::remove_file(&entry.quarantine_path);
        {
            let mut idx = self.index.write();
            idx.retain(|e| e.id != id);
        }
        self.save_index();
        Ok(())
    }

    /// List all quarantine entries.
    pub fn list_entries(&self) -> Vec<QuarantineEntry> {
        self.index.read().clone()
    }

    /// Get a specific quarantine entry by ID.
    pub fn get_entry(&self, id: &str) -> Option<QuarantineEntry> {
        self.index.read().iter().find(|e| e.id == id).cloned()
    }

    /// Remove entries older than the retention period.
    pub fn cleanup_expired(&self) -> usize {
        let cutoff = Utc::now()
            - chrono::Duration::days(self.config.retention_days as i64);

        let expired: Vec<String> = {
            let idx = self.index.read();
            idx.iter()
                .filter(|e| e.quarantined_at < cutoff)
                .map(|e| e.id.clone())
                .collect()
        };

        let count = expired.len();
        for id in &expired {
            let _ = self.delete_entry(id);
        }
        count
    }

    /// Total size of all quarantined files in bytes.
    pub fn vault_size(&self) -> u64 {
        self.index.read().iter().map(|e| e.file_size).sum()
    }

    /// Save the index to disk atomically (write tmp, then rename).
    fn save_index(&self) {
        let index_path = self.config.vault_dir.join("index.json");
        let tmp_path = self.config.vault_dir.join("index.json.tmp");

        let idx = self.index.read();
        if let Ok(json) = serde_json::to_string_pretty(&*idx) {
            if std::fs::write(&tmp_path, &json).is_ok() {
                let _ = std::fs::rename(&tmp_path, &index_path);
            }
        }
    }

    /// Load the index from disk.
    fn load_index(&self) {
        let index_path = self.config.vault_dir.join("index.json");
        if let Ok(content) = std::fs::read_to_string(&index_path) {
            if let Ok(entries) = serde_json::from_str::<Vec<QuarantineEntry>>(&content) {
                *self.index.write() = entries;
            }
        }
    }
}

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

    fn test_vault() -> (QuarantineVault, PathBuf) {
        let dir = std::env::temp_dir().join(format!("nexus-quarantine-test-{}", uuid::Uuid::new_v4()));
        let config = QuarantineVaultConfig::new(dir.clone());
        (QuarantineVault::new(config), dir)
    }

    #[test]
    fn quarantine_and_verify() {
        let (vault, dir) = test_vault();
        let test_file = dir.join("malware.txt");
        std::fs::write(&test_file, b"definitely malware content").unwrap();

        let entry = vault
            .quarantine_file(&test_file, "Test detection", "test_scanner", Severity::High)
            .unwrap();

        // Original file should be gone
        assert!(!test_file.exists());
        // Quarantine file should exist
        assert!(entry.quarantine_path.exists());
        // Entry should be in index
        assert_eq!(vault.list_entries().len(), 1);
        assert_eq!(entry.scanner, "test_scanner");

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

    #[test]
    fn restore_file_works() {
        let (vault, dir) = test_vault();
        let original_content = b"restore me please";
        let test_file = dir.join("restore_me.txt");
        std::fs::write(&test_file, original_content).unwrap();

        let entry = vault
            .quarantine_file(&test_file, "test", "scanner", Severity::Medium)
            .unwrap();
        let id = entry.id.clone();

        // Restore
        let restored_path = vault.restore_file(&id).unwrap();
        assert!(restored_path.exists());
        assert_eq!(std::fs::read(&restored_path).unwrap(), original_content);
        assert!(vault.list_entries().is_empty());

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

    #[test]
    fn delete_entry_works() {
        let (vault, dir) = test_vault();
        let test_file = dir.join("delete_me.txt");
        std::fs::write(&test_file, b"bye").unwrap();

        let entry = vault
            .quarantine_file(&test_file, "test", "scanner", Severity::Low)
            .unwrap();
        let qpath = entry.quarantine_path.clone();

        vault.delete_entry(&entry.id).unwrap();
        assert!(vault.list_entries().is_empty());
        // quarantine file should be gone too
        assert!(!qpath.exists());

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

    #[test]
    fn vault_size_calculation() {
        let (vault, dir) = test_vault();
        let f1 = dir.join("f1.txt");
        let f2 = dir.join("f2.txt");
        std::fs::write(&f1, &[0u8; 100]).unwrap();
        std::fs::write(&f2, &[0u8; 200]).unwrap();

        vault.quarantine_file(&f1, "t", "s", Severity::Low).unwrap();
        vault.quarantine_file(&f2, "t", "s", Severity::Low).unwrap();
        assert_eq!(vault.vault_size(), 300);

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

    #[test]
    fn index_persistence() {
        let dir = std::env::temp_dir().join(format!("nexus-q-persist-{}", uuid::Uuid::new_v4()));
        let config = QuarantineVaultConfig::new(dir.clone());

        {
            let vault = QuarantineVault::new(config.clone());
            let f = dir.join("persist.txt");
            std::fs::write(&f, b"data").unwrap();
            vault.quarantine_file(&f, "test", "s", Severity::High).unwrap();
            assert_eq!(vault.list_entries().len(), 1);
        }

        // New vault instance should load existing index
        let vault2 = QuarantineVault::new(config);
        assert_eq!(vault2.list_entries().len(), 1);

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

    #[test]
    fn get_entry_by_id() {
        let (vault, dir) = test_vault();
        let f = dir.join("lookup.txt");
        std::fs::write(&f, b"find me").unwrap();

        let entry = vault.quarantine_file(&f, "test", "s", Severity::Medium).unwrap();
        let found = vault.get_entry(&entry.id);
        assert!(found.is_some());
        assert_eq!(found.unwrap().sha256, entry.sha256);

        assert!(vault.get_entry("nonexistent").is_none());
        let _ = std::fs::remove_dir_all(&dir);
    }
}