3. 实验环境搭建

Wazuh 的完整实验环境由四个角色组成:Wazuh Indexer 负责存储与检索安全事件,Wazuh Manager 负责接收 Agent 上报、执行规则匹配与告警生成,Wazuh Dashboard 提供可视化界面,Wazuh Agent 部署在被监控主机上采集日志与文件完整性等数据。本节以一台 Linux 主机(示例为 Ubuntu 22.04,4 vCPU / 8 GB 内存 / 100 GB 磁盘)作为服务端,另用一台 Linux 主机作为被监控端,完成从安装到连通性验证的完整闭环。

一、部署拓扑与端口规划

在动手之前,先明确各组件之间的通信关系与端口占用,这是后续排障的基础。Indexer 默认监听 9200(REST API)与 9300(集群内部通信);Manager 监听 1514(Agent 事件上报)、1515(Agent 注册)、55000(Manager API);Dashboard 监听 443。Agent 主动向 Manager 的 1514/1515 发起连接,Manager 再通过 9200 将告警写入 Indexer,Dashboard 从 Indexer 读取数据并调用 Manager API 获取运行状态。

Wazuh 实验环境部署拓扑与端口关系 被监控主机 Wazuh Agent 采集 /var/log、FIM ossec.conf 配置 Wazuh 服务端主机(Ubuntu 22.04) Wazuh Manager 1514/tcp 事件接收 1515/tcp Agent 注册 55000/tcp Manager API Wazuh Indexer 9200/tcp REST API 9300/tcp 集群通信 存储 wazuh-alerts-* Wazuh Dashboard 443/tcp Web UI 读取 Indexer 数据 调用 Manager API Filebeat 读取 alerts.json 转发至 Indexer 模块:wazuh 1514/1515 9200 55000 9200

二、安装 Wazuh Indexer

Wazuh 官方提供一体化安装脚本,但生产与实验环境都建议分组件安装以便理解依赖关系。首先导入 GPG 密钥并添加软件源:

curl -s https://packages.wazuh.com/key/GPG-KEY-WAZUH | \
  gpg --no-default-keyring --keyring gnupg-ring:/usr/share/keyrings/wazuh.gpg --import
chmod 644 /usr/share/keyrings/wazuh.gpg
echo "deb [signed-by=/usr/share/keyrings/wazuh.gpg] https://packages.wazuh.com/4.x/apt/ stable main" \
  | tee /etc/apt/sources.list.d/wazuh.list
apt update

安装 Indexer 并启用服务:

apt install wazuh-indexer -y
systemctl daemon-reload
systemctl enable --now wazuh-indexer
systemctl status wazuh-indexer --no-pager

Indexer 首次启动需要初始化安全配置(生成证书、设置管理员密码)。执行官方提供的 wazuh-indexer 安全初始化脚本后,用以下命令验证 9200 端口是否正常响应:

curl -k -u admin:<初始密码> https://192.168.56.10:9200
# 预期返回 cluster_name、version.number、tagline: "You Know, for Search"

若返回 Connection refused,优先检查 journalctl -u wazuh-indexer -n 50 中的 JVM 内存报错;实验环境建议在 /etc/wazuh-indexer/jvm.options 中将堆内存设为 -Xms2g -Xmx2g,避免因内存不足反复重启。

三、安装 Wazuh Manager 与 Filebeat

Manager 依赖 Filebeat 将告警转发到 Indexer,因此两者一起安装:

apt install wazuh-manager filebeat -y
systemctl enable --now wazuh-manager
filebeat modules enable wazuh
filebeat setup --index-management -E output.logstash.enabled=false \
  -E 'output.elasticsearch.hosts=["https://192.168.56.10:9200"]' \
  -E 'output.elasticsearch.username=admin' \
  -E 'output.elasticsearch.password=<初始密码>' \
  -E 'output.elasticsearch.ssl.verification_mode=none'
systemctl enable --now filebeat

验证 Manager 是否正常:systemctl status wazuh-manager 应显示 active (running),同时 /var/ossec/logs/ossec.log 中应出现 wazuh-modulesd:syscollector: INFO: Starting evaluation 之类的启动日志。Manager API 可用以下命令测试:

curl -k -u wazuh:wazuh https://192.168.56.10:55000/?pretty=true
# 预期返回 {"data": {"title": "Wazuh API REST", "api_version": "4.x.x", ...}}

四、安装 Wazuh Dashboard

Dashboard 是 Kibana 的定制分支,安装后需要生成证书并配置与 Indexer、Manager 的连接:

apt install wazuh-dashboard -y
# 编辑 /etc/wazuh-dashboard/opensearch_dashboards.yml
# server.host: "0.0.0.0"
# opensearch.hosts: ["https://192.168.56.10:9200"]
# opensearch.username: "admin"
# opensearch.password: "<初始密码>"
# wazuh.api.url: "https://192.168.56.10:55000"
systemctl enable --now wazuh-dashboard

浏览器访问 https://192.168.56.10,使用 admin 账号登录。若页面提示 “Wazuh API 不可达”,通常是 Dashboard 配置中的 API 地址或凭据与 Manager 不一致,可在 /usr/share/wazuh-dashboard/data/wazuh/config/wazuh.yml 中核对。

五、部署 Agent 并完成注册

在被监控主机上安装 Agent,安装过程中需要指定 Manager 地址与注册密钥。先在 Manager 端生成注册密码:

/var/ossec/bin/manage_agents -a 192.168.56.20 -n web-server-01
# 记录输出的 Agent ID 与密钥

然后在被监控主机上执行:

apt install wazuh-agent -y
# 编辑 /var/ossec/etc/ossec.conf,设置:
# <address>192.168.56.10</address>
# <port>1514</port>
/var/ossec/bin/agent-auth -m 192.168.56.10 -A web-server-01
systemctl enable --now wazuh-agent

注册成功后,Manager 端 /var/ossec/bin/agent_control -l 应列出该 Agent,状态为 Active。若显示 Never connected,检查被监控主机到 1514 端口的连通性(nc -zv 192.168.56.10 1514)以及 Agent 端 /var/ossec/logs/ossec.log 中的连接错误。

六、初始连通性验证清单

部署完成后,按以下顺序逐项验证,任何一项失败都会导致后续数据链路中断:

验证项命令 / 操作预期结果
Indexer 存活 curl -k -u admin:密码 https://IP:9200 返回 cluster_name 与版本号
Manager 服务 systemctl status wazuh-manager active (running)
Manager API curl -k -u wazuh:wazuh https://IP:55000/?pretty=true 返回 API 版本信息
Agent 注册 /var/ossec/bin/agent_control -l Agent 状态为 Active
告警入库 Dashboard 中查看 wazuh-alerts-* 索引 存在近期文档,时间戳为当前
端到端事件 在被监控主机执行 sudo su - 触发认证日志 Dashboard 出现 authentication 告警
若 Dashboard 中看不到任何告警,先确认 Filebeat 是否成功写入 Indexer:curl -k -u admin:密码 https://IP:9200/_cat/indices?v,应能看到 wazuh-alerts-4.x-* 索引。若索引不存在,检查 Filebeat 日志 journalctl -u filebeat -n 50 中的 TLS 或认证错误。
实验环境常因内存不足导致 Indexer 被 OOM Killer 终止。建议为服务端主机分配至少 8 GB 内存,并在 /etc/wazuh-indexer/jvm.options 中显式设置堆内存上限,同时用 free -h 和 dmesg | grep -i oom 确认是否发生过内存回收。