From 47b33414cbd981a249ea2fe868fc261b67fd1dc9 Mon Sep 17 00:00:00 2001 From: Gianluca Brigandi Date: Wed, 18 Jun 2025 16:50:26 -0700 Subject: [PATCH] feat: comprehensive Wazuh integration with Docker CI/CD and expanded security operations Major enhancements: - Added Docker image building and publishing to GitHub Container Registry with multi-platform support (linux/amd64, linux/arm64) - Expanded from basic alert retrieval to comprehensive security operations with 14 MCP tools covering: * Vulnerability management (agent vulnerability summaries, critical vulnerabilities) * Agent monitoring (running agents, processes, network ports) * System statistics (weekly stats, remoted stats, log collector stats) * Log analysis (manager logs, error logs with search capabilities) * Cluster management (health checks, node listing) - Updated environment configuration to support both Wazuh Manager API and Wazuh Indexer with proper SSL handling - Enhanced documentation with detailed use cases, Docker deployment options, and comprehensive tool descriptions - Upgraded wazuh-client dependency to v0.1.1 for expanded API capabilities - Added agent ID formatting and validation for consistent three-digit zero-padded identifiers This transforms the server from a simple alert fetcher into a full-featured security operations platform for AI-assisted Wazuh management. --- .env.example | 46 +- .github/workflows/release.yml | 39 + Cargo.toml | 2 +- README.md | 218 ++++-- src/main.rs | 1283 ++++++++++++++++++++++++++++++++- 5 files changed, 1511 insertions(+), 77 deletions(-) diff --git a/.env.example b/.env.example index 59538a4..7cb1877 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,39 @@ -# Wazuh API Configuration -WAZUH_HOST=localhost -WAZUH_PORT=55000 -WAZUH_USER=admin -WAZUH_PASS=admin -VERIFY_SSL=false +# Wazuh MCP Server Environment Configuration Example +# +# Copy this file to .env and fill in your specific values. +# Lines starting with # are comments. -# MCP Server Configuration -MCP_SERVER_PORT=8000 +# Wazuh Manager API Configuration +# Hostname or IP address of the Wazuh Manager API server. +WAZUH_API_HOST=localhost +# Port number for the Wazuh Manager API. +WAZUH_API_PORT=55000 +# Username for Wazuh Manager API authentication. +WAZUH_API_USERNAME=wazuh +# Password for Wazuh Manager API authentication. +WAZUH_API_PASSWORD=wazuh + +# Wazuh Indexer API Configuration +# Hostname or IP address of the Wazuh Indexer API server. +WAZUH_INDEXER_HOST=localhost +# Port number for the Wazuh Indexer API. +WAZUH_INDEXER_PORT=9200 +# Username for Wazuh Indexer API authentication. +WAZUH_INDEXER_USERNAME=admin +# Password for Wazuh Indexer API authentication. +WAZUH_INDEXER_PASSWORD=admin + +# SSL Configuration for Wazuh Connections +# Set to "true" to verify SSL certificates for Wazuh API and Indexer connections. +# Set to "false" to disable SSL verification (not recommended for production). +WAZUH_VERIFY_SSL=false + +# Protocol for Wazuh Connections (Optional) +# Overrides the default protocol used by the wazuh-client. +# Typically "http" or "https". If not set, the client's default (usually https) will be used. +# WAZUH_TEST_PROTOCOL=https + +# Logging Configuration +# Controls the log level for the application and its dependencies. +# Examples: "info", "debug", "trace", "mcp_server_wazuh=debug,wazuh_client=info" +RUST_LOG=info diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 48eef1f..de03baa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,6 +7,7 @@ on: permissions: contents: write # Needed to create releases + packages: write # Needed to push to GitHub Container Registry jobs: create_release: @@ -76,3 +77,41 @@ jobs: asset_name: mcp-server-wazuh-${{ matrix.asset_name_suffix }} asset_content_type: application/octet-stream + build_docker: + name: Build and Push Docker Image + needs: create_release + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/gbrigandi/mcp-server-wazuh + tags: | + type=ref,event=tag + type=raw,value=latest + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + diff --git a/Cargo.toml b/Cargo.toml index 17d71e3..3fc85da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/gbrigandi/mcp-server-wazuh" readme = "README.md" [dependencies] -wazuh-client = "0.1.0" +wazuh-client = "0.1.1" rmcp = { version = "0.1.5", features = ["server", "transport-io"] } tokio = { version = "1", features = ["full"] } reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false } diff --git a/README.md b/README.md index c5107d0..3cc1b7d 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,82 @@ -# Wazuh MCP Server +# Wazuh MCP Server - Talk to your SIEM A Rust-based server designed to bridge the gap between a Wazuh Security Information and Event Management (SIEM) system and applications requiring contextual security data, specifically tailored for the Claude Desktop Integration using the Model Context Protocol (MCP). ## Overview -Modern AI assistants like Claude can benefit significantly from real-time context about the user's environment. For security operations, this means providing relevant security alerts and events. Wazuh is a popular open-source SIEM, but its API output isn't directly consumable by systems expecting MCP format. +Modern AI assistants like Claude can benefit significantly from real-time context about the user's security environment. The Wazuh MCP Server bridges this gap by providing comprehensive access to Wazuh SIEM data through natural language interactions. + +This server transforms complex Wazuh API responses into MCP-compatible format, enabling AI assistants to access: + +- **Security Alerts & Events** from the Wazuh Indexer for threat detection and incident response +- **Agent Management & Monitoring** including health status, system processes, and network ports +- **Vulnerability Assessment** data for risk management and patch prioritization +- **Security Rules & Configuration** for detection optimization and compliance validation +- **System Statistics & Performance** metrics for operational monitoring and audit trails +- **Log Analysis & Forensics** capabilities for incident investigation and compliance reporting +- **Cluster Health & Management** for infrastructure reliability and availability requirements +- **Compliance Monitoring & Gap Analysis** for regulatory frameworks like PCI-DSS, HIPAA, SOX, and GDPR + +Rather than requiring manual API calls or complex queries, security teams can now ask natural language questions like "Show me critical vulnerabilities on web servers," "What processes are running on agent 001?" or "Are we meeting PCI-DSS logging requirements?" and receive structured, actionable data from their Wazuh deployment. + +This approach is particularly valuable for compliance teams who need to quickly assess security posture, identify gaps in monitoring coverage, validate rule effectiveness, and generate evidence for audit requirements across distributed infrastructure. ![](media/wazuh-alerts-1.png) ## Example Use Cases -The Wazuh MCP Server, by bridging Wazuh's security data with MCP-compatible applications, unlocks several powerful use cases: +The Wazuh MCP Server provides direct access to Wazuh security data through natural language interactions, enabling several practical use cases: -* **Delegated Alert Triage:** Automate alert categorization and prioritization via AI, focusing analyst attention on critical events. -* **Enhanced Alert Correlation:** Enrich alerts by correlating with CVEs, OSINT, and other threat intelligence for deeper context and risk assessment. -* **Dynamic Security Visualizations:** Generate on-demand reports and visualizations of Wazuh data to answer specific security questions. -* **Multilingual Security Operations:** Query Wazuh data and receive insights in multiple languages for global team accessibility. -* **Natural Language Data Interaction:** Query Wazuh data using natural language for intuitive access to security information. -* **Contextual Augmentation for Other Tools:** Use Wazuh data as context to enrich other MCP-enabled tools and AI assistants. +### Security Alert Analysis +* **Alert Triage and Investigation:** Query recent security alerts with `get_wazuh_alert_summary` to quickly identify and prioritize threats requiring immediate attention. +* **Alert Pattern Recognition:** Analyze alert trends and patterns to identify recurring security issues or potential attack campaigns. + +### Vulnerability Management +* **Agent Vulnerability Assessment:** Use `get_wazuh_vulnerability_summary` and `get_wazuh_critical_vulnerabilities` to assess security posture of specific agents and prioritize patching efforts. +* **Risk-Based Vulnerability Prioritization:** Correlate vulnerability data with agent criticality and exposure to focus remediation efforts. + +### System Monitoring and Forensics +* **Process Analysis:** Investigate running processes on agents using `get_wazuh_agent_processes` for threat hunting and system analysis. +* **Network Security Assessment:** Monitor open ports and network services with `get_wazuh_agent_ports` to identify potential attack vectors. +* **Agent Health Monitoring:** Track agent status and connectivity using `get_wazuh_running_agents` to ensure comprehensive security coverage. + +### Security Operations Intelligence +* **Rule Effectiveness Analysis:** Review and analyze security detection rules with `get_wazuh_rules_summary` to optimize detection capabilities. +* **Manager Performance Monitoring:** Track system performance and statistics using tools like `get_wazuh_weekly_stats`, `get_wazuh_remoted_stats`, and `get_wazuh_log_collector_stats`. +* **Cluster Health Management:** Monitor Wazuh cluster status with `get_wazuh_cluster_health` and `get_wazuh_cluster_nodes` for operational reliability. + +### Incident Response and Forensics +* **Log Analysis:** Search and analyze manager logs using `search_wazuh_manager_logs` and `get_wazuh_manager_error_logs` for incident investigation. +* **Agent-Specific Investigation:** Combine multiple tools to build comprehensive profiles of specific agents during security incidents. +* **Natural Language Security Queries:** Ask complex security questions in natural language and receive structured data from multiple Wazuh components. + +### Operational Efficiency +* **Automated Reporting:** Generate security reports and summaries through conversational interfaces without manual API calls. +* **Cross-Component Analysis:** Correlate data from both Wazuh Indexer (alerts) and Wazuh Manager (agents, rules, vulnerabilities) for comprehensive security insights. +* **Multilingual Security Operations:** Access Wazuh data and receive insights in multiple languages for global security teams. + +### Threat Intelligence Gathering and Response + +For enhanced threat intelligence capabilities, the Wazuh MCP Server can be combined with the **[Cortex MCP Server](https://github.com/gbrigandi/mcp-server-cortex/)** to create a powerful security analysis ecosystem. + +**Enhanced Capabilities with Cortex Integration:** +* **Artifact Analysis:** Automatically analyze suspicious files, URLs, domains, and IP addresses found in Wazuh alerts using Cortex's 140+ analyzers +* **IOC Enrichment:** Enrich indicators of compromise (IOCs) from Wazuh alerts with threat intelligence from multiple sources including VirusTotal, Shodan, MISP, and more +* **Automated Threat Hunting:** Combine Wazuh's detection capabilities with Cortex's analysis engines to automatically investigate and classify threats +* **Multi-Source Intelligence:** Leverage analyzers for reputation checks, malware analysis, domain analysis, and behavioral analysis +* **Response Orchestration:** Use analysis results to inform automated response actions and alert prioritization + +**Example Workflow:** +1. Wazuh detects a suspicious file hash or network connection in an alert +2. The AI assistant automatically queries the Cortex MCP Server to analyze the artifact using multiple analyzers +3. Results from VirusTotal, hybrid analysis, domain reputation, and other sources are correlated +4. The combined intelligence provides context for incident response decisions +5. Findings can be used to update Wazuh rules or trigger additional monitoring ## Requirements - An MCP (Model Context Protocol) compatible LLM client (e.g., Claude Desktop) -- A running Wazuh server (v4.x recommended) with the API enabled and accessible. +- A running Wazuh server (v4.12 recommended) with the API enabled and accessible. - Network connectivity between this server and the Wazuh API (if API interaction is used). ## Installation @@ -35,7 +89,14 @@ The Wazuh MCP Server, by bridging Wazuh's security data with MCP-compatible appl * Make the downloaded binary executable (e.g., `chmod +x mcp-server-wazuh-linux-amd64`). * (Optional) Rename it to something simpler like `mcp-server-wazuh` and move it to a directory in your system's `PATH` for easier access. -### Option 2: Build from Source +### Option 2: Docker + +1. **Pull the Docker Image:** + ```bash + docker pull ghcr.io/gbrigandi/mcp-server-wazuh:latest + ``` + +### Option 3: Build from Source 1. **Prerequisites:** * Install Rust: [https://www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install) @@ -63,11 +124,16 @@ Configure your `claude_desktop_config.json` file: "command": "/path/to/mcp-server-wazuh", "args": [], "env": { - "WAZUH_HOST": "your_wazuh_host", - "WAZUH_USER": "admin", - "WAZUH_PASS": "your_wazuh_password", - "WAZUH_PORT": "9200", - "VERIFY_SSL": "false", + "WAZUH_API_HOST": "your_wazuh_manager_api_host", + "WAZUH_API_PORT": "55000", + "WAZUH_API_USERNAME": "your_wazuh_api_user", + "WAZUH_API_PASSWORD": "your_wazuh_api_password", + "WAZUH_INDEXER_HOST": "your_wazuh_indexer_host", + "WAZUH_INDEXER_PORT": "9200", + "WAZUH_INDEXER_USERNAME": "your_wazuh_indexer_user", + "WAZUH_INDEXER_PASSWORD": "your_wazuh_indexer_password", + "WAZUH_VERIFY_SSL": "false", + "WAZUH_TEST_PROTOCOL": "https", "RUST_LOG": "info" } } @@ -79,25 +145,91 @@ Replace `/path/to/mcp-server-wazuh` with the actual path to your binary and conf Once configured, your LLM client should be able to launch and communicate with the `mcp-server-wazuh` to access Wazuh security data. +If using Docker, create a `.env` file with your Wazuh configuration: + +```bash +WAZUH_API_HOST=your_wazuh_manager_api_host +WAZUH_API_PORT=55000 +WAZUH_API_USERNAME=your_wazuh_api_user +WAZUH_API_PASSWORD=your_wazuh_api_password +WAZUH_INDEXER_HOST=your_wazuh_indexer_host +WAZUH_INDEXER_PORT=9200 +WAZUH_INDEXER_USERNAME=your_wazuh_indexer_user +WAZUH_INDEXER_PASSWORD=your_wazuh_indexer_password +WAZUH_VERIFY_SSL=false +WAZUH_TEST_PROTOCOL=https +RUST_LOG=info +``` + +Configure your `claude_desktop_config.json` file: + +``` +{ + "mcpServers": { + "wazuh": { + "command": "docker", + "args": [ + "run", "--rm", "-i", + "--env-file", "/path/to/your/.env", + "ghcr.io/gbrigandi/mcp-server-wazuh:latest" + ] + } + } +} +``` + ## Configuration Configuration is managed through environment variables. A `.env` file can be placed in the project root for local development. -| Variable | Description | Default | Required (for API) | -| ----------------- | ------------------------------------------------- | ----------- | ------------------ | -| `WAZUH_HOST` | Hostname or IP address of the Wazuh API server. | `localhost` | Yes | -| `WAZUH_PORT` | Port number for the Wazuh API. | `9200` | Yes | -| `WAZUH_USER` | Username for Wazuh API authentication. | `admin` | Yes | -| `WAZUH_PASS` | Password for Wazuh API authentication. | `admin` | Yes | -| `VERIFY_SSL` | Set to `true` to verify the Wazuh API's SSL cert. | `false` | No | -| `MCP_SERVER_PORT` | Port for this MCP server to listen on (if HTTP enabled). | `8000` | No | -| `RUST_LOG` | Log level (e.g., `info`, `debug`, `trace`). | `info` | No | +| Variable | Description | Default | Required | +| ------------------------ | ------------------------------------------------------------------------------ | ----------- | -------- | +| `WAZUH_API_HOST` | Hostname or IP address of the Wazuh Manager API server. | `localhost` | Yes | +| `WAZUH_API_PORT` | Port number for the Wazuh Manager API. | `55000` | Yes | +| `WAZUH_API_USERNAME` | Username for Wazuh Manager API authentication. | `wazuh` | Yes | +| `WAZUH_API_PASSWORD` | Password for Wazuh Manager API authentication. | `wazuh` | Yes | +| `WAZUH_INDEXER_HOST` | Hostname or IP address of the Wazuh Indexer API server. | `localhost` | Yes | +| `WAZUH_INDEXER_PORT` | Port number for the Wazuh Indexer API. | `9200` | Yes | +| `WAZUH_INDEXER_USERNAME` | Username for Wazuh Indexer API authentication. | `admin` | Yes | +| `WAZUH_INDEXER_PASSWORD` | Password for Wazuh Indexer API authentication. | `admin` | Yes | +| `WAZUH_VERIFY_SSL` | Set to `true` to verify SSL certificates for Wazuh API and Indexer connections. | `false` | No | +| `WAZUH_TEST_PROTOCOL` | Protocol for Wazuh connections (e.g., "http", "https"). Overrides client default. | `https` | No | +| `RUST_LOG` | Log level (e.g., `info`, `debug`, `trace`). | `info` | No | -**Note on `VERIFY_SSL`:** For production environments using the Wazuh API, it is strongly recommended to set `VERIFY_SSL=true` and ensure proper certificate validation. Setting it to `false` disables certificate checks, which is insecure. +**Note on `WAZUH_VERIFY_SSL`:** For production environments, it is strongly recommended to set `WAZUH_VERIFY_SSL=true` and ensure proper certificate validation for both Wazuh Manager API and Wazuh Indexer connections. Setting it to `false` disables certificate checks, which is insecure. +The "Required: Yes" indicates that these variables are essential for the server to connect to the respective Wazuh components. While defaults are provided, they are unlikely to match a production or non-local setup. + +## Building + +### Prerequisites + +- Install Rust: [https://www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install) +- Install Docker and Docker Compose (optional, for containerized deployment): [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/) + +### Local Development + +1. **Clone the repository:** + ```bash + git clone https://github.com/gbrigandi/mcp-server-wazuh.git + cd mcp-server-wazuh + ``` +2. **Configure (if using Wazuh API):** + - Copy the example environment file: `cp .env.example .env` + - Edit the `.env` file with your specific Wazuh API details (e.g. `WAZUH_API_HOST`, `WAZUH_API_PORT`). +3. **Build:** + ```bash + cargo build + ``` +4. **Run:** + ```bash + cargo run + # Or use the run script (which might set up stdio mode): + # ./run.sh + ``` ## Architecture -The server is built using the [rmcp](https://crates.io/crates/rmcp) framework and facilitates communication between MCP clients (e.g., Claude Desktop, IDE extensions) and the Wazuh MCP Server via stdio transport. The server interacts with the Wazuh Indexer API to fetch security alerts and other data. +The server is built using the [rmcp](https://crates.io/crates/rmcp) framework and facilitates communication between MCP clients (e.g., Claude Desktop, IDE extensions) and the Wazuh MCP Server via stdio transport. The server interacts with the Wazuh Indexer and Wazuh Manager APIs to fetch security alerts and other data. ```mermaid sequenceDiagram @@ -139,38 +271,6 @@ sequenceDiagram This stdio interaction allows for tight integration with local development tools or other applications that can manage child processes. An optional HTTP endpoint (`/mcp`) may also be available for clients that prefer polling. - -## Building - -### Prerequisites - -- Install Rust: [https://www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install) -- Install Docker and Docker Compose (optional, for containerized deployment): [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/) - -### Local Development - -1. **Clone the repository:** - ```bash - git clone https://github.com/gbrigandi/mcp-server-wazuh.git - cd mcp-server-wazuh - ``` -2. **Configure (if using Wazuh API):** - - Copy the example environment file: `cp .env.example .env` - - Edit the `.env` file with your specific Wazuh API details (`WAZUH_HOST`, `WAZUH_PORT`, `WAZUH_USER`, `WAZUH_PASS`). -3. **Build:** - ```bash - cargo build - ``` -4. **Run:** - ```bash - cargo run - # Or use the run script (which might set up stdio mode): - # ./run.sh - ``` - If the HTTP server is enabled, it will start listening on the port specified by `MCP_SERVER_PORT` (default 8000). Otherwise, it will operate in stdio mode. - -## Stdio Mode Operation - The server communicates via `stdin` and `stdout` using JSON-RPC 2.0 messages, adhering to the Model Context Protocol (MCP). Example interaction flow: diff --git a/src/main.rs b/src/main.rs index e190bef..194b0b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,22 +20,56 @@ // - They use `serde::Deserialize` for parsing input and `schemars::JsonSchema` // for generating a schema that MCP clients can use to understand how to call the tools. // -// - `wazuh` module: -// - `WazuhIndexerClient`: Handles communication with the Wazuh Indexer API. -// - Provides methods to fetch alerts and other security data from Wazuh. +// - `wazuh_client` crate: +// - This external crate is used to interact with both the Wazuh Manager API and the Wazuh Indexer API. +// - `WazuhClientFactory` is used to create specific clients (e.g., `WazuhIndexerClient`, `RulesClient`, `AgentsClient`, `LogsClient`, `ClusterClient`, `VulnerabilityClient`). // // Workflow: -// 1. Server starts and listens for MCP requests on stdio or HTTP-SSE. +// 1. Server starts and listens for MCP requests on stdio // 2. MCP client sends a `call_tool` request. // 3. `WazuhToolsServer` dispatches to the appropriate tool method based on the tool name. // 4. The tool method parses parameters, interacts with the Wazuh client to fetch data. // 5. The result (success with data or error) is packaged into a `CallToolResult` // and sent back to the MCP client. // +// Exposed Tools: +// The server exposes a set of tools categorized by the Wazuh component they interact with: +// +// Wazuh Indexer Tools: +// - `get_wazuh_alert_summary`: Retrieves a summary of security alerts from the Wazuh Indexer. +// +// Wazuh Manager Tools: +// - `get_wazuh_rules_summary`: Fetches security rules defined in the Wazuh Manager. +// - `get_wazuh_vulnerability_summary`: Gets vulnerability scan results for a specific agent from the Wazuh Manager. +// - `get_wazuh_critical_vulnerabilities`: Retrieves critical vulnerabilities for an agent. +// - `get_wazuh_running_agents`: Lists active and inactive agents connected to the Wazuh Manager. +// - `get_wazuh_agent_processes`: Retrieves running processes on a specific agent (via Syscollector, data reported to Manager). +// - `get_wazuh_agent_ports`: Lists open network ports on a specific agent (via Syscollector, data reported to Manager). +// - `search_wazuh_manager_logs`: Searches logs generated by the Wazuh Manager. +// - `get_wazuh_manager_error_logs`: Retrieves error-specific logs from the Wazuh Manager. +// - `get_wazuh_log_collector_stats`: Gets log collection statistics for an agent. +// - `get_wazuh_remoted_stats`: Fetches statistics from the Wazuh Manager's remoted daemon. +// - `get_wazuh_weekly_stats`: Retrieves aggregated weekly statistics from the Wazuh Manager. +// - `get_wazuh_cluster_health`: Checks the health status of the Wazuh Manager cluster. +// - `get_wazuh_cluster_nodes`: Lists nodes participating in the Wazuh Manager cluster. +// +// (Detailed parameters and descriptions for each tool are available via the MCP `get_tools` command or in the server's `get_info` response.) +// // Configuration: -// The server requires `WAZUH_HOST`, `WAZUH_PORT`, `WAZUH_USER`, `WAZUH_PASS`, and `VERIFY_SSL` -// environment variables to connect to the Wazuh instance. Logging is controlled by `RUST_LOG`. +// The server requires the following environment variables to connect to the Wazuh instance: +// - `WAZUH_API_HOST`: Hostname or IP address of the Wazuh API. +// - `WAZUH_API_PORT`: Port number for the Wazuh API (default: 55000). +// - `WAZUH_API_USERNAME`: Username for Wazuh API authentication. +// - `WAZUH_API_PASSWORD`: Password for Wazuh API authentication. +// - `WAZUH_INDEXER_HOST`: Hostname or IP address of the Wazuh Indexer. +// - `WAZUH_INDEXER_PORT`: Port number for the Wazuh Indexer API (default: 9200). +// - `WAZUH_INDEXER_USERNAME`: Username for Wazuh Indexer authentication. +// - `WAZUH_INDEXER_PASSWORD`: Password for Wazuh Indexer authentication. +// - `WAZUH_VERIFY_SSL`: Set to "true" to enable SSL certificate verification, "false" otherwise (default: false). +// - `WAZUH_TEST_PROTOCOL`: (Optional) Protocol to use for Wazuh API/Indexer connections, e.g., "http" or "https" (default: "https"). +// Logging behavior is controlled by the `RUST_LOG` environment variable (e.g., `RUST_LOG=info,mcp_server_wazuh=debug`). +use reqwest::StatusCode; use rmcp::{ Error as McpError, ServerHandler, ServiceExt, model::{ @@ -49,7 +83,7 @@ use std::env; use clap::Parser; use dotenv::dotenv; -use wazuh_client::{WazuhClientFactory, WazuhIndexerClient, RulesClient}; +use wazuh_client::{WazuhClientFactory, WazuhIndexerClient, RulesClient, VulnerabilityClient, AgentsClient, LogsClient, ClusterClient, Port as WazuhPort}; #[derive(Parser, Debug)] #[command(name = "mcp-server-wazuh")] @@ -77,16 +111,151 @@ struct GetRulesSummaryParams { filename: Option, } +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetVulnerabilitySummaryParams { + #[schemars(description = "Maximum number of vulnerabilities to retrieve (default: 100)")] + limit: Option, + #[schemars(description = "Agent ID to filter vulnerabilities by (required, e.g., \"0\", \"1\", \"001\")")] + agent_id: String, + #[schemars(description = "Severity level to filter by (Low, Medium, High, Critical) (optional)")] + severity: Option, + #[schemars(description = "CVE ID to search for (optional)")] + cve: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetRunningAgentsParams { + #[schemars(description = "Maximum number of agents to retrieve (default: 100)")] + limit: Option, + #[schemars(description = "Agent status filter (active, disconnected, pending, never_connected) (default: active)")] + status: Option, + #[schemars(description = "Agent name to search for (optional)")] + name: Option, + #[schemars(description = "Agent IP address to filter by (optional)")] + ip: Option, + #[schemars(description = "Agent group to filter by (optional)")] + group: Option, + #[schemars(description = "Operating system platform to filter by (optional)")] + os_platform: Option, + #[schemars(description = "Agent version to filter by (optional)")] + version: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetCriticalVulnerabilitiesParams { + #[schemars(description = "Agent ID to get critical vulnerabilities for (required, e.g., \"0\", \"1\", \"001\")")] + agent_id: String, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetAgentProcessesParams { + #[schemars(description = "Agent ID to get processes for (required, e.g., \"0\", \"1\", \"001\")")] + agent_id: String, + #[schemars(description = "Maximum number of processes to retrieve (default: 100)")] + limit: Option, + #[schemars(description = "Search string to filter processes by name or command (optional)")] + search: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetAgentPortsParams { + #[schemars(description = "Agent ID to get network ports for (required, e.g., \"0\", \"1\", \"001\")")] + agent_id: String, + #[schemars(description = "Maximum number of ports to retrieve (default: 100)")] + limit: Option, + #[schemars(description = "Protocol to filter by (e.g., \"tcp\", \"udp\") (optional)")] + protocol: Option, + #[schemars(description = "State to filter by (e.g., \"LISTEN\", \"ESTABLISHED\") (optional)")] + state: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct SearchManagerLogsParams { + #[schemars(description = "Maximum number of log entries to retrieve (default: 100)")] + limit: Option, + #[schemars(description = "Number of log entries to skip (default: 0)")] + offset: Option, + #[schemars(description = "Log level to filter by (e.g., \"error\", \"warning\", \"info\") (optional)")] + level: Option, + #[schemars(description = "Log tag to filter by (e.g., \"wazuh-modulesd\") (optional)")] + tag: Option, + #[schemars(description = "Search term to filter log descriptions (optional)")] + search_term: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetManagerErrorLogsParams { + #[schemars(description = "Maximum number of error log entries to retrieve (default: 100)")] + limit: Option, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetLogCollectorStatsParams { + #[schemars(description = "Agent ID to get log collector stats for (required, e.g., \"0\", \"1\", \"001\")")] + agent_id: String, +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetRemotedStatsParams { + // No parameters needed for remoted stats +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetWeeklyStatsParams { + // No parameters needed for weekly stats +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetClusterHealthParams { + // No parameters needed for cluster health +} + +#[derive(Debug, serde::Deserialize, schemars::JsonSchema)] +struct GetClusterNodesParams { + #[schemars(description = "Maximum number of nodes to retrieve (optional, Wazuh API default is 500)")] + limit: Option, + #[schemars(description = "Number of nodes to skip (offset) (optional, default: 0)")] + offset: Option, + #[schemars(description = "Filter by node type (e.g., 'master', 'worker') (optional)")] + node_type: Option, +} + #[derive(Clone)] struct WazuhToolsServer { #[allow(dead_code)] // Kept for future expansion to other Wazuh clients wazuh_factory: Arc, wazuh_indexer_client: Arc, wazuh_rules_client: Arc>, + wazuh_vulnerability_client: Arc>, + wazuh_agents_client: Arc>, + wazuh_logs_client: Arc>, + wazuh_cluster_client: Arc>, } #[tool(tool_box)] impl WazuhToolsServer { + fn format_agent_id(agent_id_str: &str) -> Result { + // Attempt to parse as a number first + if let Ok(num) = agent_id_str.parse::() { + if num > 999 { + Err(format!( + "Agent ID '{}' is too large. Must be a number between 0 and 999.", + agent_id_str + )) + } else { + Ok(format!("{:03}", num)) + } + } else if agent_id_str.len() == 3 && agent_id_str.chars().all(|c| c.is_ascii_digit()) { + // Already correctly formatted (e.g., "001") + Ok(agent_id_str.to_string()) + } else { + Err(format!( + "Invalid agent_id format: '{}'. Must be a number (e.g., 1, 12) or a 3-digit string (e.g., 001, 012).", + agent_id_str + )) + } + } + fn new() -> Result { dotenv().ok(); @@ -130,13 +299,21 @@ impl WazuhToolsServer { let wazuh_indexer_client = wazuh_factory.create_indexer_client(); let wazuh_rules_client = wazuh_factory.create_rules_client(); + let wazuh_vulnerability_client = wazuh_factory.create_vulnerability_client(); + let wazuh_agents_client = wazuh_factory.create_agents_client(); + let wazuh_logs_client = wazuh_factory.create_logs_client(); + let wazuh_cluster_client = wazuh_factory.create_cluster_client(); Ok(Self { wazuh_factory: Arc::new(wazuh_factory), wazuh_indexer_client: Arc::new(wazuh_indexer_client), wazuh_rules_client: Arc::new(tokio::sync::Mutex::new(wazuh_rules_client)), + wazuh_vulnerability_client: Arc::new(tokio::sync::Mutex::new(wazuh_vulnerability_client)), + wazuh_agents_client: Arc::new(tokio::sync::Mutex::new(wazuh_agents_client)), + wazuh_logs_client: Arc::new(tokio::sync::Mutex::new(wazuh_logs_client)), + wazuh_cluster_client: Arc::new(tokio::sync::Mutex::new(wazuh_cluster_client)), }) - } + } #[tool( name = "get_wazuh_alert_summary", @@ -317,6 +494,1073 @@ impl WazuhToolsServer { } } } + + #[tool( + name = "get_wazuh_vulnerability_summary", + description = "Retrieves a summary of Wazuh vulnerability detections for a specific agent. Returns formatted vulnerability information including CVE ID, severity, detection time, and agent details. Supports filtering by severity level." + )] + async fn get_wazuh_vulnerability_summary( + &self, + #[tool(aggr)] params: GetVulnerabilitySummaryParams, + ) -> Result { + let limit = params.limit.unwrap_or(100); + let offset = 0; // Default offset, can be extended in future if needed + + // agent_id is now a required String. If missing, serde would have failed deserialization. + // We just need to format it. + let agent_id = match Self::format_agent_id(¶ms.agent_id) { + Ok(formatted_id) => formatted_id, + Err(err_msg) => { + tracing::error!("Error formatting agent_id for vulnerability summary: {}", err_msg); + return Ok(CallToolResult::error(vec![Content::text(err_msg)])); + } + }; + + tracing::info!( + limit = %limit, + agent_id = %agent_id, + severity = ?params.severity, + cve = ?params.cve, + "Retrieving Wazuh vulnerability summary" + ); + + let mut vulnerability_client = self.wazuh_vulnerability_client.lock().await; + + // Filter by CVE if specified by searching through results + let vulnerabilities = if let Some(cve_filter) = ¶ms.cve { + match vulnerability_client.get_agent_vulnerabilities( + &agent_id, + Some(1000), // Get more results to filter + Some(offset), + params.severity.as_deref(), + ).await { + Ok(all_vulns) => { + let filtered: Vec<_> = all_vulns + .into_iter() + .filter(|v| v.cve.to_lowercase().contains(&cve_filter.to_lowercase())) + .take(limit as usize) + .collect(); + Ok(filtered) + } + Err(e) => Err(e), + } + } else { + vulnerability_client.get_agent_vulnerabilities( + &agent_id, + Some(limit), + Some(offset), + params.severity.as_deref(), + ).await + }; + + match vulnerabilities { + Ok(vulnerabilities) => { + if vulnerabilities.is_empty() { + tracing::info!("No Wazuh vulnerabilities found matching criteria. Returning standard message."); + return Ok(CallToolResult::success(vec![Content::text( + "No Wazuh vulnerabilities found matching the specified criteria.", + )])); + } + + let num_vulnerabilities = vulnerabilities.len(); + let mcp_content_items: Vec = vulnerabilities + .into_iter() + .map(|vuln| { + let severity_indicator = match vuln.severity.to_lowercase().as_str() { + "critical" => "🔴 CRITICAL", + "high" => "🟠 HIGH", + "medium" => "🟡 MEDIUM", + "low" => "🟢 LOW", + _ => &vuln.severity, + }; + + let published_info = if let Some(published) = &vuln.published { + format!("\nPublished: {}", published) + } else { + String::new() + }; + + let updated_info = if let Some(updated) = &vuln.updated { + format!("\nUpdated: {}", updated) + } else { + String::new() + }; + + let detection_time_info = if let Some(detection_time) = &vuln.detection_time { + format!("\nDetection Time: {}", detection_time) + } else { + String::new() + }; + + let agent_info = { + let id_str = vuln.agent_id.as_deref(); + let name_str = vuln.agent_name.as_deref(); + + match (id_str, name_str) { + (Some("000"), Some(name)) => format!("\nAgent: {} (Wazuh Manager, ID: 000)", name), + (Some("000"), None) => "\nAgent: Wazuh Manager (ID: 000)".to_string(), + (Some(id), Some(name)) => format!("\nAgent: {} (ID: {})", name, id), + (Some(id), None) => format!("\nAgent ID: {}", id), + (None, Some(name)) => format!("\nAgent: {} (ID: Unknown)", name), // Should ideally not happen if ID is a primary key for agent context + (None, None) => String::new(), // No agent information available + } + }; + + let cvss_info = if let Some(cvss) = &vuln.cvss { + let mut cvss_parts = Vec::new(); + if let Some(cvss2) = &cvss.cvss2 { + if let Some(score) = cvss2.base_score { + cvss_parts.push(format!("CVSS2: {}", score)); + } + } + if let Some(cvss3) = &cvss.cvss3 { + if let Some(score) = cvss3.base_score { + cvss_parts.push(format!("CVSS3: {}", score)); + } + } + if !cvss_parts.is_empty() { + format!("\nCVSS Scores: {}", cvss_parts.join(", ")) + } else { + String::new() + } + } else { + String::new() + }; + + let reference_info = if let Some(reference) = &vuln.reference { + format!("\nReference: {}", reference) + } else { + String::new() + }; + + let description = vuln.description.as_deref().unwrap_or("No description available"); + + let formatted_text = format!( + "CVE: {}\nSeverity: {}\nTitle: {}\nDescription: {}{}{}{}{}{}{}", + vuln.cve, + severity_indicator, + vuln.title, + description, + published_info, + updated_info, + detection_time_info, + agent_info, + cvss_info, + reference_info + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} vulnerabilities into {} MCP content items", num_vulnerabilities, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + match e { + wazuh_client::WazuhApiError::HttpError { status, message: _, url: _ } if status == StatusCode::NOT_FOUND => { + tracing::info!("No vulnerability summary found for agent {}. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No vulnerability summary found for agent {}.", agent_id), + )])); + } + _ => { + } + } + let err_msg = format!("Error retrieving vulnerabilities from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_critical_vulnerabilities", + description = "Retrieves critical vulnerabilities for a specific Wazuh agent. Returns formatted vulnerability information including CVE ID, title, description, CVSS scores, and detection details. Only shows vulnerabilities with 'Critical' severity level." + )] + async fn get_wazuh_critical_vulnerabilities( + &self, + #[tool(aggr)] params: GetCriticalVulnerabilitiesParams, + ) -> Result { + let agent_id = match Self::format_agent_id(¶ms.agent_id) { + Ok(formatted_id) => formatted_id, + Err(err_msg) => { + tracing::error!("Error formatting agent_id for critical vulnerabilities: {}", err_msg); + return Ok(CallToolResult::error(vec![Content::text(err_msg)])); + } + }; + + tracing::info!( + agent_id = %agent_id, + "Retrieving critical vulnerabilities for Wazuh agent" + ); + + let mut vulnerability_client = self.wazuh_vulnerability_client.lock().await; + + match vulnerability_client.get_critical_vulnerabilities(&agent_id).await { + Ok(vulnerabilities) => { + if vulnerabilities.is_empty() { + tracing::info!("No critical vulnerabilities found for agent {}. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No critical vulnerabilities found for agent {}.", agent_id), + )])); + } + + let num_vulnerabilities = vulnerabilities.len(); + let mcp_content_items: Vec = vulnerabilities + .into_iter() + .map(|vuln| { + let published_info = if let Some(published) = &vuln.published { + format!("\nPublished: {}", published) + } else { + String::new() + }; + + let updated_info = if let Some(updated) = &vuln.updated { + format!("\nUpdated: {}", updated) + } else { + String::new() + }; + + let detection_time_info = if let Some(detection_time) = &vuln.detection_time { + format!("\nDetection Time: {}", detection_time) + } else { + String::new() + }; + + let agent_info = if let Some(agent_name) = &vuln.agent_name { + format!("\nAgent: {} (ID: {})", agent_name, vuln.agent_id.as_deref().unwrap_or("Unknown")) + } else if let Some(agent_id) = &vuln.agent_id { + format!("\nAgent ID: {}", agent_id) + } else { + String::new() + }; + + let cvss_info = if let Some(cvss) = &vuln.cvss { + let mut cvss_parts = Vec::new(); + if let Some(cvss2) = &cvss.cvss2 { + if let Some(score) = cvss2.base_score { + cvss_parts.push(format!("CVSS2: {}", score)); + } + } + if let Some(cvss3) = &cvss.cvss3 { + if let Some(score) = cvss3.base_score { + cvss_parts.push(format!("CVSS3: {}", score)); + } + } + if !cvss_parts.is_empty() { + format!("\nCVSS Scores: {}", cvss_parts.join(", ")) + } else { + String::new() + } + } else { + String::new() + }; + + let reference_info = if let Some(reference) = &vuln.reference { + format!("\nReference: {}", reference) + } else { + String::new() + }; + + let description = vuln.description.as_deref().unwrap_or("No description available"); + + let formatted_text = format!( + "🔴 CRITICAL VULNERABILITY\nCVE: {}\nTitle: {}\nDescription: {}{}{}{}{}{}{}", + vuln.cve, + vuln.title, + description, + published_info, + updated_info, + detection_time_info, + agent_info, + cvss_info, + reference_info + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} critical vulnerabilities into {} MCP content items", num_vulnerabilities, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + match e { + wazuh_client::WazuhApiError::HttpError { status, message: _, url: _ } if status == StatusCode::NOT_FOUND => { + tracing::info!("No critical vulnerabilities found for agent {}. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No critical vulnerabilities found for agent {}.", agent_id), + )])); + } + _ => { + } + } + let err_msg = format!("Error retrieving critical vulnerabilities from Wazuh for agent {}: {}", agent_id, e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_running_agents", + description = "Retrieves a list of Wazuh agents with their current status and details. Returns formatted agent information including ID, name, IP, status, OS details, and last activity. Supports filtering by status, name, IP, group, OS platform, and version." + )] + + async fn get_wazuh_running_agents( + &self, + #[tool(aggr)] params: GetRunningAgentsParams, + ) -> Result { + let limit = params.limit.unwrap_or(100); + let status = params.status.as_deref().unwrap_or("active"); // Default to active agents + + tracing::info!( + limit = %limit, + status = %status, + name = ?params.name, + ip = ?params.ip, + group = ?params.group, + os_platform = ?params.os_platform, + version = ?params.version, + "Retrieving Wazuh agents" + ); + + let mut agents_client = self.wazuh_agents_client.lock().await; + + match agents_client.get_agents( + Some(limit), + None, // offset + None, // select + None, // sort + None, // search + Some(status), + None, // query + None, // older_than + params.os_platform.as_deref(), + None, // os_version + None, // os_name + None, // manager_host + params.version.as_deref(), + params.group.as_deref(), + None, // node_name + params.name.as_deref(), + params.ip.as_deref(), + None, // register_ip + None, // group_config_status + None, // distinct + ).await { + Ok(agents) => { + if agents.is_empty() { + tracing::info!("No Wazuh agents found matching criteria. Returning standard message."); + return Ok(CallToolResult::success(vec![Content::text( + format!("No Wazuh agents found matching the specified criteria (status: {}).", status), + )])); + } + + let num_agents = agents.len(); + let mcp_content_items: Vec = agents + .into_iter() + .map(|agent| { + let status_indicator = match agent.status.to_lowercase().as_str() { + "active" => "🟢 ACTIVE", + "disconnected" => "🔴 DISCONNECTED", + "pending" => "🟡 PENDING", + "never_connected" => "⚪ NEVER CONNECTED", + _ => &agent.status, + }; + + let ip_info = if let Some(ip) = &agent.ip { + format!("\nIP: {}", ip) + } else { + String::new() + }; + + let register_ip_info = if let Some(register_ip) = &agent.register_ip { + if agent.ip.as_ref() != Some(register_ip) { + format!("\nRegistered IP: {}", register_ip) + } else { + String::new() + } + } else { + String::new() + }; + + let os_info = if let Some(os) = &agent.os { + let mut os_parts = Vec::new(); + if let Some(name) = &os.name { + os_parts.push(name.clone()); + } + if let Some(version) = &os.version { + os_parts.push(version.clone()); + } + if let Some(arch) = &os.arch { + os_parts.push(format!("({})", arch)); + } + if !os_parts.is_empty() { + format!("\nOS: {}", os_parts.join(" ")) + } else { + String::new() + } + } else { + String::new() + }; + + let version_info = if let Some(version) = &agent.version { + format!("\nAgent Version: {}", version) + } else { + String::new() + }; + + let group_info = if let Some(groups) = &agent.group { + if !groups.is_empty() { + format!("\nGroups: {}", groups.join(", ")) + } else { + String::new() + } + } else { + String::new() + }; + + let last_keep_alive_info = if let Some(last_keep_alive) = &agent.last_keep_alive { + format!("\nLast Keep Alive: {}", last_keep_alive) + } else { + String::new() + }; + + let date_add_info = if let Some(date_add) = &agent.date_add { + format!("\nRegistered: {}", date_add) + } else { + String::new() + }; + + let node_info = if let Some(node_name) = &agent.node_name { + format!("\nNode: {}", node_name) + } else { + String::new() + }; + + let config_status_info = if let Some(config_status) = &agent.group_config_status { + let config_indicator = match config_status.to_lowercase().as_str() { + "synced" => "✅ SYNCED", + "not synced" => "❌ NOT SYNCED", + _ => config_status, + }; + format!("\nConfig Status: {}", config_indicator) + } else { + String::new() + }; + + let agent_id_display = if agent.id == "000" { + format!("{} (Wazuh Manager)", agent.id) + } else { + agent.id.clone() + }; + + let formatted_text = format!( + "Agent ID: {}\nName: {}\nStatus: {}{}{}{}{}{}{}{}{}{}", + agent_id_display, + agent.name, + status_indicator, + ip_info, + register_ip_info, + os_info, + version_info, + group_info, + last_keep_alive_info, + date_add_info, + node_info, + config_status_info + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} agents into {} MCP content items", num_agents, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + let err_msg = format!("Error retrieving agents from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_agent_processes", + description = "Retrieves a list of running processes for a specific Wazuh agent. Returns formatted process information including PID, name, state, user, and command. Supports filtering by process name/command." + )] + async fn get_wazuh_agent_processes( + &self, + #[tool(aggr)] params: GetAgentProcessesParams, + ) -> Result { + let agent_id = match Self::format_agent_id(¶ms.agent_id) { + Ok(formatted_id) => formatted_id, + Err(err_msg) => { + tracing::error!("Error formatting agent_id for agent processes: {}", err_msg); + return Ok(CallToolResult::error(vec![Content::text(err_msg)])); + } + }; + let limit = params.limit.unwrap_or(100); + let offset = 0; + + tracing::info!( + agent_id = %agent_id, + limit = %limit, + search = ?params.search, + "Retrieving Wazuh agent processes" + ); + + let mut vulnerability_client = self.wazuh_vulnerability_client.lock().await; + + match vulnerability_client.get_agent_processes( + &agent_id, + Some(limit), + Some(offset), + params.search.as_deref(), + ).await { + Ok(processes) => { + if processes.is_empty() { + tracing::info!("No processes found for agent {} with current filters. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No processes found for agent {} matching the specified criteria.", agent_id), + )])); + } + + let num_processes = processes.len(); + let mcp_content_items: Vec = processes + .into_iter() + .map(|process| { + let mut details = vec![ + format!("PID: {}", process.pid), + format!("Name: {}", process.name), + ]; + + if let Some(state) = &process.state { + details.push(format!("State: {}", state)); + } + if let Some(ppid) = &process.ppid { + details.push(format!("PPID: {}", ppid)); + } + if let Some(euser) = &process.euser { + details.push(format!("User: {}", euser)); + } + if let Some(cmd) = &process.cmd { + details.push(format!("Command: {}", cmd)); + } + if let Some(start_time_str) = &process.start_time { + if let Ok(start_time_unix) = start_time_str.parse::() { + // Assuming start_time is a Unix timestamp in seconds + use chrono::DateTime; + if let Some(dt) = DateTime::from_timestamp(start_time_unix, 0) { + details.push(format!("Start Time: {}", dt.format("%Y-%m-%d %H:%M:%S UTC"))); + } else { + details.push(format!("Start Time: {} (raw)", start_time_str)); + } + } else { + // If it's not a simple number, print as is + details.push(format!("Start Time: {}", start_time_str)); + } + } + if let Some(resident_mem) = process.resident { + details.push(format!("Memory (Resident): {} KB", resident_mem / 1024)); // Assuming resident is in bytes + } + if let Some(vm_size) = process.vm_size { + details.push(format!("Memory (VM Size): {} KB", vm_size / 1024)); // Assuming vm_size is in bytes + } + + Content::text(details.join("\n")) + }) + .collect(); + + tracing::info!("Successfully processed {} processes for agent {} into {} MCP content items", num_processes, agent_id, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + match e { + wazuh_client::WazuhApiError::HttpError { status, message: _, url: _ } if status == StatusCode::NOT_FOUND => { + tracing::info!("No process data found for agent {}. Syscollector might not have run or data is unavailable.", agent_id); + Ok(CallToolResult::success(vec![Content::text( + format!("No process data found for agent {}. The agent might not exist, syscollector data might be unavailable, or the agent is not active.", agent_id), + )])) + } + _ => { + let err_msg = format!("Error retrieving processes for agent {} from Wazuh: {}", agent_id, e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + } + } + + #[tool( + name = "get_wazuh_cluster_health", + description = "Checks the health of the Wazuh cluster. Returns whether the cluster is enabled, running, and if nodes are connected." + )] + async fn get_wazuh_cluster_health( + &self, + #[tool(aggr)] _params: GetClusterHealthParams, // No params used + ) -> Result { + tracing::info!("Retrieving Wazuh cluster health"); + + let mut cluster_client = self.wazuh_cluster_client.lock().await; + + match cluster_client.is_cluster_healthy().await { + Ok(is_healthy) => { + let health_status_text = if is_healthy { + "Cluster is healthy: Yes".to_string() + } else { + // To provide more context, we can fetch the basic status + match cluster_client.get_cluster_status().await { + Ok(status) => { + let mut reasons = Vec::new(); + if !status.enabled.eq_ignore_ascii_case("yes") { + reasons.push("cluster is not enabled"); + } + if !status.running.eq_ignore_ascii_case("yes") { + reasons.push("cluster is not running"); + } + // is_cluster_healthy already checks n_connected_nodes > 0 if enabled and running + // If it's still false, and enabled/running are "yes", it implies no connected nodes or healthcheck failed. + if status.enabled.eq_ignore_ascii_case("yes") && status.running.eq_ignore_ascii_case("yes") { + match cluster_client.get_cluster_healthcheck().await { + Ok(hc) if hc.n_connected_nodes == 0 => reasons.push("no nodes are connected"), + Err(_) => reasons.push("healthcheck endpoint failed or reported issues"), + _ => {} // Healthy implies connected nodes + } + } + if reasons.is_empty() && !is_healthy { // Should not happen if logic is correct + reasons.push("unknown reason, check detailed logs or healthcheck endpoint"); + } + format!("Cluster is healthy: No. Reasons: {}", reasons.join("; ")) + } + Err(_) => { + "Cluster is healthy: No. Additionally, failed to retrieve basic cluster status for more details.".to_string() + } + } + }; + tracing::info!("Successfully retrieved cluster health: {}", health_status_text); + Ok(CallToolResult::success(vec![Content::text(health_status_text)])) + } + Err(e) => { + let err_msg = format!("Error retrieving cluster health from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_cluster_nodes", + description = "Retrieves a list of nodes in the Wazuh cluster. Returns formatted node information including name, type, version, IP, and status. Supports filtering by limit, offset, and node type." + )] + async fn get_wazuh_cluster_nodes( + &self, + #[tool(aggr)] params: GetClusterNodesParams, + ) -> Result { + tracing::info!( + limit = ?params.limit, + offset = ?params.offset, + node_type = ?params.node_type, + "Retrieving Wazuh cluster nodes" + ); + + let mut cluster_client = self.wazuh_cluster_client.lock().await; + + match cluster_client.get_cluster_nodes( + params.limit, + params.offset, + params.node_type.as_deref(), + ).await { + Ok(nodes) => { + if nodes.is_empty() { + tracing::info!("No Wazuh cluster nodes found matching criteria. Returning standard message."); + return Ok(CallToolResult::success(vec![Content::text( + "No Wazuh cluster nodes found matching the specified criteria.", + )])); + } + + let num_nodes = nodes.len(); + let mcp_content_items: Vec = nodes + .into_iter() + .map(|node| { + let status_indicator = match node.status.to_lowercase().as_str() { + "connected" | "active" => "🟢 CONNECTED", // Assuming 'active' is also a good state + "disconnected" => "🔴 DISCONNECTED", + _ => &node.status, + }; + let formatted_text = format!( + "Node Name: {}\nType: {}\nVersion: {}\nIP: {}\nStatus: {}", + node.name, node.node_type, node.version, node.ip, status_indicator + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} cluster nodes into {} MCP content items", num_nodes, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + let err_msg = format!("Error retrieving cluster nodes from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "search_wazuh_manager_logs", + description = "Searches Wazuh manager logs. Returns formatted log entries including timestamp, tag, level, and description. Supports filtering by limit, offset, level, tag, and a search term." + )] + async fn search_wazuh_manager_logs( + &self, + #[tool(aggr)] params: SearchManagerLogsParams, + ) -> Result { + let limit = params.limit.unwrap_or(100); + let offset = params.offset.unwrap_or(0); + + tracing::info!( + limit = %limit, + offset = %offset, + level = ?params.level, + tag = ?params.tag, + search_term = ?params.search_term, + "Searching Wazuh manager logs" + ); + + let mut logs_client = self.wazuh_logs_client.lock().await; + + match logs_client.get_manager_logs( + Some(limit), + Some(offset), + params.level.as_deref(), + params.tag.as_deref(), + params.search_term.as_deref(), + ).await { + Ok(log_entries) => { + if log_entries.is_empty() { + tracing::info!("No Wazuh manager logs found matching criteria. Returning standard message."); + return Ok(CallToolResult::success(vec![Content::text( + "No Wazuh manager logs found matching the specified criteria.", + )])); + } + + let num_logs = log_entries.len(); + let mcp_content_items: Vec = log_entries + .into_iter() + .map(|log_entry| { + let formatted_text = format!( + "Timestamp: {}\nTag: {}\nLevel: {}\nDescription: {}", + log_entry.timestamp, log_entry.tag, log_entry.level, log_entry.description + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} manager log entries into {} MCP content items", num_logs, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + let err_msg = format!("Error searching manager logs from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_manager_error_logs", + description = "Retrieves Wazuh manager error logs. Returns formatted log entries including timestamp, tag, level (error), and description." + )] + async fn get_wazuh_manager_error_logs( + &self, + #[tool(aggr)] params: GetManagerErrorLogsParams, + ) -> Result { + let limit = params.limit.unwrap_or(100); + + tracing::info!(limit = %limit, "Retrieving Wazuh manager error logs"); + + let mut logs_client = self.wazuh_logs_client.lock().await; + + match logs_client.get_error_logs(Some(limit)).await { + Ok(log_entries) => { + if log_entries.is_empty() { + tracing::info!("No Wazuh manager error logs found. Returning standard message."); + return Ok(CallToolResult::success(vec![Content::text( + "No Wazuh manager error logs found.", + )])); + } + + let num_logs = log_entries.len(); + let mcp_content_items: Vec = log_entries + .into_iter() + .map(|log_entry| { + let formatted_text = format!( + "Timestamp: {}\nTag: {}\nLevel: {}\nDescription: {}", + log_entry.timestamp, log_entry.tag, log_entry.level, log_entry.description + ); + Content::text(formatted_text) + }) + .collect(); + + tracing::info!("Successfully processed {} manager error log entries into {} MCP content items", num_logs, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + let err_msg = format!("Error retrieving manager error logs from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_log_collector_stats", + description = "Retrieves log collector statistics for a specific Wazuh agent. Returns information about events processed, dropped, bytes, and target log files." + )] + async fn get_wazuh_log_collector_stats( + &self, + #[tool(aggr)] params: GetLogCollectorStatsParams, + ) -> Result { + let agent_id = match Self::format_agent_id(¶ms.agent_id) { + Ok(formatted_id) => formatted_id, + Err(err_msg) => { + tracing::error!("Error formatting agent_id for log collector stats: {}", err_msg); + return Ok(CallToolResult::error(vec![Content::text(err_msg)])); + } + }; + + tracing::info!(agent_id = %agent_id, "Retrieving Wazuh log collector stats"); + + let mut logs_client = self.wazuh_logs_client.lock().await; + + match logs_client.get_logcollector_stats(&agent_id).await { + Ok(stats) => { + let targets_info: String = stats.targets.iter() + .map(|target| format!(" - Name: {}, Drops: {}", target.name, target.drops)) + .collect::>() + .join("\n"); + + let formatted_text = format!( + "Log Collector Stats for Agent: {}\nTotal Events: {}\nEvents Dropped: {}\nBytes Processed: {}\nTargets:\n{}", + stats.agent_id, stats.events, stats.events_dropped, stats.bytes, targets_info + ); + + tracing::info!("Successfully retrieved log collector stats for agent {}", agent_id); + Ok(CallToolResult::success(vec![Content::text(formatted_text)])) + } + Err(e) => { + // Check if the error is due to agent not found or stats not available + if let wazuh_client::WazuhApiError::ApiError(msg) = &e { + if msg.contains(&format!("Log collector stats for agent {} not found", agent_id)) || + msg.contains("Agent Not Found") { // General agent not found + tracing::info!("No log collector stats found for agent {}. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No log collector stats found for agent {}. The agent might not exist or stats are unavailable.", agent_id), + )])); + } + } + match e { + wazuh_client::WazuhApiError::HttpError { status, .. } if status == StatusCode::NOT_FOUND => { + tracing::info!("No log collector stats found for agent {} (HTTP 404). Returning standard message.", agent_id); + Ok(CallToolResult::success(vec![Content::text( + format!("No log collector stats found for agent {}. The agent might not exist or stats are unavailable.", agent_id), + )])) + } + _ => { + let err_msg = format!("Error retrieving log collector stats for agent {} from Wazuh: {}", agent_id, e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + } + } + + #[tool( + name = "get_wazuh_remoted_stats", + description = "Retrieves statistics from the Wazuh remoted daemon. Returns information about queue size, TCP sessions, event counts, and message traffic." + )] + async fn get_wazuh_remoted_stats( + &self, + #[tool(aggr)] _params: GetRemotedStatsParams, // No params used + ) -> Result { + tracing::info!("Retrieving Wazuh remoted stats"); + + let mut logs_client = self.wazuh_logs_client.lock().await; + + match logs_client.get_remoted_stats().await { + Ok(stats) => { + let formatted_text = format!( + "Wazuh Remoted Statistics:\nTotal Queue Size: {}\nTCP Sessions: {}\nEvent Count: {}\nControl Message Count: {}\nDiscarded Message Count: {}\nMessages Sent: {}\nBytes Received: {}\nDequeued After Close: {}", + stats.total_queue_size, + stats.tcp_sessions, + stats.evt_count, + stats.ctrl_count, + stats.discarded_count, + stats.msg_sent, + stats.recv_bytes, + stats.dequeued_after_close + ); + + tracing::info!("Successfully retrieved remoted stats"); + Ok(CallToolResult::success(vec![Content::text(formatted_text)])) + } + Err(e) => { + let err_msg = format!("Error retrieving remoted stats from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + + #[tool( + name = "get_wazuh_agent_ports", + description = "Retrieves a list of open network ports for a specific Wazuh agent. Returns formatted port information including local/remote IP and port, protocol, state, and associated process/PID. Supports filtering by protocol and state." + )] + async fn get_wazuh_agent_ports( + &self, + #[tool(aggr)] params: GetAgentPortsParams, + ) -> Result { + let agent_id = match Self::format_agent_id(¶ms.agent_id) { + Ok(formatted_id) => formatted_id, + Err(err_msg) => { + tracing::error!("Error formatting agent_id for agent ports: {}", err_msg); + return Ok(CallToolResult::error(vec![Content::text(err_msg)])); + } + }; + let limit = params.limit.unwrap_or(100); + let offset = 0; // Default offset + + tracing::info!( + agent_id = %agent_id, + limit = %limit, + protocol = ?params.protocol, + state = ?params.state, + "Retrieving Wazuh agent network ports" + ); + + let mut vulnerability_client = self.wazuh_vulnerability_client.lock().await; + + // Note: The wazuh_client::VulnerabilityClient::get_agent_ports provided in the prompt + // only supports filtering by protocol. If state filtering is needed, the client would need an update. + // For now, we pass params.protocol and ignore params.state for the API call, + // but we can filter by state client-side if necessary, or acknowledge this limitation. + // The current wazuh-client `get_agent_ports` does not support state filtering directly in its parameters. + // We will filter client-side for now if `params.state` is provided. + match vulnerability_client.get_agent_ports( + &agent_id, + Some(limit * 2), // Fetch more to allow for client-side state filtering + Some(offset), + params.protocol.as_deref(), + ).await { + Ok(mut ports) => { + // Client-side filtering for state if provided + if let Some(state_filter) = ¶ms.state { + ports.retain(|port| { + port.state.as_ref().is_some_and(|s| s.eq_ignore_ascii_case(state_filter)) + }); + } + + // Apply limit after client-side filtering + ports.truncate(limit as usize); + + if ports.is_empty() { + tracing::info!("No network ports found for agent {} with current filters. Returning standard message.", agent_id); + return Ok(CallToolResult::success(vec![Content::text( + format!("No network ports found for agent {} matching the specified criteria.", agent_id), + )])); + } + + let num_ports = ports.len(); + let mcp_content_items: Vec = ports + .into_iter() + .map(|port: WazuhPort| { // Explicitly type port + let mut details = vec![ + format!("Protocol: {}", port.protocol), + format!("Local: {}:{}", port.local.ip, port.local.port), + ]; + + if let Some(remote) = &port.remote { + details.push(format!("Remote: {}:{}", remote.ip, remote.port)); + } + if let Some(state) = &port.state { + details.push(format!("State: {}", state)); + } + if let Some(process_name) = &port.process { // process field in WazuhPort is Option + details.push(format!("Process Name: {}", process_name)); + } + if let Some(pid) = port.pid { // pid field in WazuhPort is Option + details.push(format!("PID: {}", pid)); + } + if let Some(inode) = port.inode { + details.push(format!("Inode: {}", inode)); + } + if let Some(tx_queue) = port.tx_queue { + details.push(format!("TX Queue: {}", tx_queue)); + } + if let Some(rx_queue) = port.rx_queue { + details.push(format!("RX Queue: {}", rx_queue)); + } + + Content::text(details.join("\n")) + }) + .collect(); + + tracing::info!("Successfully processed {} network ports for agent {} into {} MCP content items", num_ports, agent_id, mcp_content_items.len()); + Ok(CallToolResult::success(mcp_content_items)) + } + Err(e) => { + match e { + wazuh_client::WazuhApiError::HttpError { status, message: _, url: _ } if status == StatusCode::NOT_FOUND => { + tracing::info!("No network port data found for agent {}. Syscollector might not have run or data is unavailable.", agent_id); + Ok(CallToolResult::success(vec![Content::text( + format!("No network port data found for agent {}. The agent might not exist, syscollector data might be unavailable, or the agent is not active.", agent_id), + )])) + } + _ => { + let err_msg = format!("Error retrieving network ports for agent {} from Wazuh: {}", agent_id, e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + } + } + + #[tool( + name = "get_wazuh_weekly_stats", + description = "Retrieves weekly statistics from the Wazuh manager. Returns a JSON object detailing various metrics aggregated over the past week." + )] + async fn get_wazuh_weekly_stats( + &self, + #[tool(aggr)] _params: GetWeeklyStatsParams, // No params used + ) -> Result { + tracing::info!("Retrieving Wazuh weekly stats"); + + let mut logs_client = self.wazuh_logs_client.lock().await; + + match logs_client.get_weekly_stats().await { + Ok(stats_value) => { + match serde_json::to_string_pretty(&stats_value) { + Ok(formatted_json) => { + tracing::info!("Successfully retrieved and formatted weekly stats."); + Ok(CallToolResult::success(vec![Content::text(formatted_json)])) + } + Err(e) => { + let err_msg = format!("Error formatting weekly stats JSON: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } + Err(e) => { + let err_msg = format!("Error retrieving weekly stats from Wazuh: {}", e); + tracing::error!("{}", err_msg); + Ok(CallToolResult::error(vec![Content::text(err_msg)])) + } + } + } } #[tool(tool_box)] @@ -336,7 +1580,28 @@ impl ServerHandler for WazuhToolsServer { - 'get_wazuh_alert_summary': Retrieves a summary of Wazuh security alerts. \ Optionally takes 'limit' parameter to control the number of alerts returned (defaults to 100).\n\ - 'get_wazuh_rules_summary': Retrieves a summary of Wazuh security rules. \ - Supports filtering by 'level', 'group', and 'filename' parameters, with 'limit' to control the number of rules returned (defaults to 100)." + Supports filtering by 'level', 'group', and 'filename' parameters, with 'limit' to control the number of rules returned (defaults to 100).\n\ + - 'get_wazuh_vulnerability_summary': Retrieves a summary of Wazuh vulnerability detections for a specific agent. \ + Requires an 'agent_id' parameter. This must be provided as a string, representing the numeric ID of the agent (e.g., \"0\", \"1\", \"12\", \"001\", \"012\"). The server will automatically format this string into a three-digit, zero-padded identifier. For instance, an input of \"0\" will be treated as \"000\", \"1\" as \"001\", and \"12\" as \"012\". Supports filtering by 'severity' and 'cve' parameters, with 'limit' to control the number of vulnerabilities returned (defaults to 100).\n\ + - 'get_wazuh_critical_vulnerabilities': Retrieves only critical vulnerabilities for a specific agent. \ + Requires an 'agent_id' parameter. This must be provided as a string, representing the numeric ID of the agent (e.g., \"0\", \"1\", \"12\", \"001\", \"012\"). The server will automatically format this string into a three-digit, zero-padded identifier. For instance, an input of \"0\" will be treated as \"000\", \"1\" as \"001\", and \"12\" as \"012\". Returns detailed information about vulnerabilities with 'Critical' severity level.\n\ + - 'get_wazuh_running_agents': Retrieves a list of Wazuh agents with their current status and details. \ + Supports filtering by 'status' (active, disconnected, pending, never_connected), 'name', 'ip', 'group', 'os_platform', and 'version' parameters, with 'limit' to control the number of agents returned (defaults to 100, status defaults to 'active').\n\ + - 'get_wazuh_agent_processes': Retrieves a list of running processes for a specific Wazuh agent. \ + Requires an 'agent_id' parameter (formatted as described for other agent-specific tools). Supports 'limit' (default 100) and 'search' (to filter by process name or command line) parameters.\n\ + - 'get_wazuh_agent_ports': Retrieves a list of open network ports for a specific Wazuh agent. \ + Requires an 'agent_id' parameter (formatted as described for other agent-specific tools). Supports 'limit' (default 100), 'protocol' (e.g., \"tcp\", \"udp\"), and 'state' (e.g., \"LISTEN\", \"ESTABLISHED\") parameters to filter the results. Note: State filtering is performed client-side by this server.\n\ + - 'search_wazuh_manager_logs': Searches Wazuh manager logs. \ + Optional parameters: 'limit' (default 100), 'offset' (default 0), 'level' (e.g., \"error\", \"info\"), 'tag' (e.g., \"wazuh-modulesd\"), 'search_term' (for free-text search in log descriptions).\n\ + - 'get_wazuh_manager_error_logs': Retrieves Wazuh manager error logs. \ + Optional parameter: 'limit' (default 100).\n\ + - 'get_wazuh_log_collector_stats': Retrieves log collector statistics for a specific Wazuh agent. \ + Requires an 'agent_id' parameter (formatted as described for other agent-specific tools).\n\ + - 'get_wazuh_remoted_stats': Retrieves statistics from the Wazuh remoted daemon (manager-wide).\n\ + - 'get_wazuh_weekly_stats': Retrieves weekly statistics from the Wazuh manager. Returns a JSON object detailing various metrics aggregated over the past week. No parameters required.\n\ + - 'get_wazuh_cluster_health': Checks the health of the Wazuh cluster. Returns a textual summary of the cluster's health status (e.g., enabled, running, connected nodes). No parameters required.\n\ + - 'get_wazuh_cluster_nodes': Retrieves a list of nodes in the Wazuh cluster. \ + Optional parameters: 'limit' (max nodes, API default 500), 'offset' (default 0), 'node_type' (e.g., \"master\", \"worker\")." .to_string(), ), }