Wazuh SCA 如何编写并校验自定义合规策略 YAML?
【免费下载链接】wazuhWazuh - The Open Source Security Platform. Unified XDR and SIEM protection for endpoints and cloud workloads.项目地址: https://gitcode.com/GitHub_Trending/wa/wazuh
Wazuh 的 SCA(Security Configuration Assessment)模块按 YAML 策略文件评估系统安全配置。内置的 CIS 等策略由包管理维护,不能满足你的自定义合规基线时,需要自己编写策略 YAML、放到 agent 上加载,并确认策略能通过解析校验、扫描后产生可判断的检查结果。本文覆盖从编写策略文件、配置<sca>加载,到在 agent 启动校验和扫描结果中确认策略有效的完整路径,适用于 Wazuh 5.x 环境(5.x 中所有 SCA 规则均按 PCRE2 求值,regex_type字段被忽略)。
主要依据文档:Creating custom SCA policies、SCA Configuration Reference、SCA policies from 4.x to 5.x、Output Samples。
策略文件由哪几个部分组成
一份自定义策略文件包含四个 section,其中policy和checks必填,requirements和variables可选:
| Section | 是否必填 |
|---|---|
policy | 是 |
requirements | 否 |
variables | 否 |
checks | 是 |
requirements未满足时,对应策略文件的扫描不会启动,可用于限定策略只在特定系统上运行。
编写策略文件
以下示例取自项目文档,展示了各 section 的典型写法(示例中 check 的id与合规键、MITRE 标识均按文档原样给出):
policy: id: "unix_audit" file: "sca_unix_audit.yml" name: "System audit for Unix based systems" description: "Guidance for establishing a secure configuration for Unix based systems." references: - https://www.ssh.com/ssh/ requirements: name: "Check that the SSH service and password-related files are present on the system" description: "Requirements for running the SCA scan against the Unix based systems policy." condition: any rules: - 'f:$sshd_file' - 'f:/etc/passwd' - 'f:/etc/shadow' variables: $sshd_file: /etc/ssh/sshd_config checks: - id: 3000 name: "SSH Hardening: Port should not be 22" description: "The ssh daemon should not be listening on port 22 (the default value) for incoming connections." rationale: "Changing the default port you may reduce the number of successful attacks from zombie bots." remediation: "Change the Port option value in the sshd_config file." compliance: pci_dss: ["2.2.4"] nist_800_53: ["CM.1"] mitre: tactic: id: - "TA0008" name: - "Lateral Movement" technique: id: - "T1021" name: - "Remote Services" condition: all rules: - 'f:$sshd_file -> !r:^# && r:Port && !r:\s*\t*22$'policy section 字段
| 字段 | 必填 | 说明 |
|---|---|---|
id | 是 | 策略 ID(字符串) |
file | 是 | 策略文件名 |
name | 是 | 策略名称 |
description | 是 | 简要描述 |
references | 否 | 参考链接数组 |
policy和checks下的id字段在所有策略文件中必须唯一。
规则(rules)写法
规则分两种形式:
- 存在性检查:
RULE_TYPE:target - 内容检查:
RULE_TYPE:target -> OPERATOR:value
规则类型前缀:
| 类型 | 前缀 |
|---|---|
| 文件 | f |
| 目录 | d |
| 进程 | p |
| 命令 | c |
| 注册表(Windows) | r |
文档给出的规则示例,可直接参照:
f:/etc/sshd_config d:/etc not p:sshd r:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa f:/etc/ssh_config -> !r:PermitRootLogin f:/etc/ssh_config -> !r:^# && r:Protocol && r:2 c:systemctl is-enabled cups -> r:^enabled f:/etc/ssh_config -> n:REGEX(\d+) compare <= 4 c:command -> n:REGEX(\d+) compare >= number r:path/to/key -> value -> content f:/proc/sys/net/ipv4/ip_forward -> 1 c:sshd -T -> !r:^\s*maxauthtries\s+4\s*$写规则时的硬约束:
- 5.x 中所有规则按 PCRE2 求值,不能沿用 4.x 的 OSRegex 语法,
regex_type字段即使保留也会被忽略; - 内容检查按行求值;
->、&&、compare两侧的空格是必须的;- 复合规则用
&&串联多个子表达式,not可对整条规则取反; - 变量在
variablessection 中定义、以$前缀引用,一条规则中变量数量不限,例如f:$list_of_files -> r:^Content、c:systemctl is-enabled $program_name -> r:^enabled。
condition 如何聚合规则结果
condition必填,取值为all、any、none,分别表示所有规则通过 / 至少一条通过 / 没有规则通过时该 check 判定为 Passed。文档给出的完整判定表:
| Condition | Passed | Failed | Not applicable | 结果 |
|---|---|---|---|---|
| all | yes | no | no | Passed |
| all | * | no | yes | Not applicable |
| all | * | yes | * | Failed |
| any | yes | * | * | Passed |
| any | no | yes | no | Failed |
| any | no | * | yes | Not applicable |
| none | yes | * | * | Failed |
| none | no | * | yes | Not applicable |
| none | no | yes | no | Passed |
*表示该列取值不影响最终结果。
compliance 与 mitre 元数据
compliance是对象,键只能从以下值中选取:cmmc、fedramp、gdpr、hipaa、iso_27001、nis2、nist_800_171、nist_800_53、pci_dss、tsc。未知键会被拒绝、记录警告并从 check 中剔除,所以不要沿用 4.x 里的pci_dss_v4.0这类带版本号的旧键。
mitre是独立对象,支持tactic、technique、subtechnique三个键;每个键下是id与name两个等长的平行数组,同一位置的名字对应同一位置的标识(名称取自 MITRE ATT&CK 目录):
mitre: tactic: id: - "TA0008" name: - "Lateral Movement" technique: id: - "T1021" name: - "Remote Services"在 agent 上加载策略文件
策略文件放在 agent 上后,在/var/ossec/etc/ossec.conf的<sca>段中用<policy>指向它(绝对路径或相对 Wazuh 安装目录的相对路径均可):
<sca> <enabled>yes</enabled> <scan_on_start>yes</scan_on_start> <interval>6h</interval> <policies> <policy>/var/ossec/etc/shared/custom_linux.yml</policy> <policy enabled="no">/var/ossec/etc/shared/old_policy.yml</policy> </policies> </sca>enabled="no"属性可以保留策略引用但临时禁用它;- 不配置
<policies>时,模块按操作系统自动加载默认策略; - 位于
/var/ossec/etc/shared/的策略由 manager 分发。agent 把路径中含etc/shared/的策略视为远程策略,其中的命令规则(c:)只有开启 internal optionsca.remote_commands=1后才会执行(默认0,即禁用;本地路径的策略不受该选项限制,始终可以执行命令); sca.commands_timeout(默认 30 秒,范围 1–300)控制扫描中命令执行的超时,防止挂起的命令阻塞扫描;- 不要把自定义策略存在
$WAZUH_HOME/ruleset/sca下,包升级会用官方策略覆盖该目录,自定义策略应放在管理员自维护的路径并显式引用。
校验策略是否生效
1. 启动校验
SCA 模块在 agent 启动时校验配置,包含五个方面:
- 所有指定的策略文件存在且可读;
- 策略文件是结构正确的合法 YAML;
- 策略包含必需元数据字段;
- 时间间隔在有效范围内;
- agent 对策略文件路径有读权限。
出错时的行为:非关键问题(如缺少可选字段)记录警告;关键配置错误会导致模块无法初始化(模块被禁用);无效的可选参数回退到默认值。校验信息查看/var/ossec/logs/ossec.log。
2. 搜索具体的解析与编译错误
官方迁移文档给出的验证流程是:把策略装到一台非生产 5.x agent(或 manager)上,重启服务并运行一次扫描,然后在 Wazuh 日志中搜索以下消息,确认没有解析和正则错误:
Failed to parse policyInvalid compliance keyUnexpected compliance formatPCRE2 compilation failed
同时注意路径类警告:未被静默跳过的策略路径若不存在,日志会出现File '...' not found或Policy file '...' not found警告。
3. 核对扫描结果
扫描后每个 check 产生result状态,取值为:Not run(尚未执行)、Passed、Failed、Not applicable。结果通过双事件系统落地:stateful 事件持久化并与 manager 同步,stateless 事件即时发送告警。文档给出的 stateful 事件示例(示例结果,字段结构供核对用):
{ "check": { "condition": "all", "id": "CUST001", "name": "Ensure SMBv1 is disabled.", "result": "Not run", "rules": [ "r:HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters -> n:SMB1 compare == 0" ] }, "policy": { "file": "custom_windows_policy.yml", "id": "custom_policy_win", "name": "Custom Windows Hardening Policy" } }对照你的策略文件检查两点:事件里policy.file与check.id指向你写入的策略和 check;result落在文档定义的四种取值之内,并且通过/失败分布与预期行为一致(官方建议先在一组 agent 上比对预期结果,再扩大部署)。
边界与限制
policy.id与checks[*].id在所有策略文件间必须唯一;- 4.x 旧策略没有自动迁移工具,需人工改写(
title改name、compliance 转对象、MITRE 移入mitre、n:表达式改用\d+与标准compare运算符、去掉已废弃的<skip_nfs>),详见 SCA policies from 4.x to 5.x; - 命令规则在共享/远程策略中受
sca.remote_commands限制,默认不执行; <sca>扫描间隔有效范围60s–1d;<skip_nfs>已废弃,仅被解析但无效果。
策略文件的具体字段与规则语法可以继续对照 custom-policies.md,同步协议与本地数据库结构见 architecture.md 与 database-schema.md。
【免费下载链接】wazuhWazuh - The Open Source Security Platform. Unified XDR and SIEM protection for endpoints and cloud workloads.项目地址: https://gitcode.com/GitHub_Trending/wa/wazuh
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考