hippox-drivers 0.3.5

🦛All indivisible atomic driver units in Hippox.
//! Virus scan skill
//!
//! This driver provides functionality to scan files for known virus
//! signatures using pattern-based detection.
use crate::DriverCallback;
use crate::DriverContext;
use crate::DriverResult;
use crate::{
    DriverCategory,
    types::{Driver, DriverParameter},
};
use crate::{file_exists, scan_file_for_viruses, validate_path};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info, warn};
/// Common virus signatures (hex patterns)
/// In production, this would be a much larger database
pub const VIRUS_SIGNATURES: &[&str] = &[
    // EICAR test virus (harmless test pattern)
    "5844454f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f4f",
    // Common malware patterns (simplified for demonstration)
    "4d5a90000300000004000000ffff0000", // PE header with suspicious flags
    "7f454c46010101000000000000000000", // ELF header with suspicious flags
    "5a4d4d4d4d4d4d4d4d4d4d4d4d4d4d4d", // Suspicious pattern
    "42494949494949494949494949494949", // Suspicious pattern
    "43434343434343434343434343434343", // Suspicious pattern
    "44444444444444444444444444444444", // Suspicious pattern
    "45454545454545454545454545454545", // Suspicious pattern
];
/// Driver for scanning files for viruses
#[derive(Debug)]
pub struct VirusScanDriver;
#[async_trait::async_trait]
impl Driver for VirusScanDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        "file_virus_scan"
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        "Scan a file for viruses using signature-based detection"
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        "Use this skill to scan a file for known virus signatures. Note: This is a basic signature-based scanner, not a full antivirus solution."
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "path".to_string(),
                param_type: "string".to_string(),
                description: "Path to the file to scan".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/tmp/suspicious_file.exe".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "recursive".to_string(),
                param_type: "boolean".to_string(),
                description: "Scan directory recursively (default: false)".to_string(),
                required: false,
                default: Some(Value::Bool(false)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "file_virus_scan",
            "parameters": {
                "path": "/tmp/suspicious_file.exe",
                "recursive": false
            }
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return "INFECTED: /tmp/suspicious_file.exe\nVirus: Virus signature: 4d5a9000...\nFile size: 1024 bytes\nScan time: 2024-01-01 00:00:00"
            .to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::File;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        callback: Option<&dyn DriverCallback>,
        context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing file_virus_scan driver");
        let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
            debug!("Missing 'path' parameter");
            return crate::DriverError::missing_parameter("path");
        })?;
        let recursive = parameters.get("recursive").and_then(|v| v.as_bool()).unwrap_or(false);
        debug!("Scanning {} (recursive: {})", path, recursive);
        let validated_path = validate_path(path, None).map_err(|e| {
            debug!("Failed to validate path: {}", e);
            return crate::DriverError::execution(format!("Failed to validate path: {}", e));
        })?;
        if !validated_path.exists() {
            warn!("Path not found: {}", path);
            return Err(crate::DriverError::execution(format!("Path not found: {}", path)));
        }
        let mut results = Vec::new();
        if validated_path.is_file() {
            debug!("Scanning single file: {}", path);
            let result = scan_file_for_viruses(&validated_path.to_string_lossy(), VIRUS_SIGNATURES).map_err(|e| {
                debug!("Failed to scan file: {}", e);
                return crate::DriverError::execution(format!("Failed to scan file: {}", e));
            })?;
            results.push(result);
        } else if validated_path.is_dir() && recursive {
            debug!("Scanning directory recursively: {}", path);
            for entry in walkdir::WalkDir::new(&validated_path).into_iter().filter_map(|e| e.ok()).filter(|e| e.file_type().is_file()) {
                let file_path = entry.path().to_string_lossy().to_string();
                if let Ok(result) = scan_file_for_viruses(&file_path, VIRUS_SIGNATURES) {
                    results.push(result);
                }
            }
        } else {
            debug!("Path is a directory but recursive not set");
            return Err(crate::DriverError::validation("recursive", "Path is a directory. Use recursive=true to scan directory contents."));
        }
        if results.is_empty() {
            info!("No files scanned");
            return Ok("No files scanned".to_string());
        }
        let mut output = String::new();
        let mut infected_count = 0;
        let mut clean_count = 0;
        for result in results {
            let status = if result.infected {
                infected_count += 1;
                "INFECTED"
            } else {
                clean_count += 1;
                "CLEAN"
            };
            output.push_str(&format!("{}: {}\n", status, result.path));
            if let Some(virus) = result.virus_name {
                output.push_str(&format!("  Virus: {}\n", virus));
            }
            output.push_str(&format!("  Size: {} bytes\n", result.file_size));
            output.push_str(&format!("  Scan time: {}\n", result.scan_time));
        }
        output.push_str(&format!("\nSummary: {} infected, {} clean", infected_count, clean_count));
        if infected_count > 0 {
            output.push_str("\nWARNING: Infected files detected!");
            warn!("Virus scan complete: {} infected files found", infected_count);
        } else {
            output.push_str("\nNo viruses detected.");
            info!("Virus scan complete: no infected files found");
        }
        return Ok(output);
    }
}