Wazuh SCA 如何编写并校验自定义合规策略 YAML?
2026/9/14 15:30:45 网站建设 项目流程

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,其中policychecks必填,requirementsvariables可选:

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参考链接数组

policychecks下的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:^Contentc:systemctl is-enabled $program_name -> r:^enabled

condition 如何聚合规则结果

condition必填,取值为allanynone,分别表示所有规则通过 / 至少一条通过 / 没有规则通过时该 check 判定为 Passed。文档给出的完整判定表:

ConditionPassedFailedNot applicable结果
allyesnonoPassed
all*noyesNot applicable
all*yes*Failed
anyyes**Passed
anynoyesnoFailed
anyno*yesNot applicable
noneyes**Failed
noneno*yesNot applicable
nonenoyesnoPassed

*表示该列取值不影响最终结果。

compliance 与 mitre 元数据

compliance是对象,键只能从以下值中选取:cmmcfedrampgdprhipaaiso_27001nis2nist_800_171nist_800_53pci_dsstsc未知键会被拒绝、记录警告并从 check 中剔除,所以不要沿用 4.x 里的pci_dss_v4.0这类带版本号的旧键。

mitre是独立对象,支持tactictechniquesubtechnique三个键;每个键下是idname两个等长的平行数组,同一位置的名字对应同一位置的标识(名称取自 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 启动时校验配置,包含五个方面:

  1. 所有指定的策略文件存在且可读;
  2. 策略文件是结构正确的合法 YAML;
  3. 策略包含必需元数据字段;
  4. 时间间隔在有效范围内;
  5. agent 对策略文件路径有读权限。

出错时的行为:非关键问题(如缺少可选字段)记录警告;关键配置错误会导致模块无法初始化(模块被禁用);无效的可选参数回退到默认值。校验信息查看/var/ossec/logs/ossec.log

2. 搜索具体的解析与编译错误

官方迁移文档给出的验证流程是:把策略装到一台非生产 5.x agent(或 manager)上,重启服务并运行一次扫描,然后在 Wazuh 日志中搜索以下消息,确认没有解析和正则错误:

  • Failed to parse policy
  • Invalid compliance key
  • Unexpected compliance format
  • PCRE2 compilation failed

同时注意路径类警告:未被静默跳过的策略路径若不存在,日志会出现File '...' not foundPolicy file '...' not found警告。

3. 核对扫描结果

扫描后每个 check 产生result状态,取值为:Not run(尚未执行)、PassedFailedNot 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.filecheck.id指向你写入的策略和 check;result落在文档定义的四种取值之内,并且通过/失败分布与预期行为一致(官方建议先在一组 agent 上比对预期结果,再扩大部署)。

边界与限制

  • policy.idchecks[*].id在所有策略文件间必须唯一;
  • 4.x 旧策略没有自动迁移工具,需人工改写(titlename、compliance 转对象、MITRE 移入mitren:表达式改用\d+与标准compare运算符、去掉已废弃的<skip_nfs>),详见 SCA policies from 4.x to 5.x;
  • 命令规则在共享/远程策略中受sca.remote_commands限制,默认不执行;
  • <sca>扫描间隔有效范围60s1d<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),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询