Custom SCA policies in Wazuh: YAML structure, rules, schedule and results
How to write a custom Wazuh SCA policy: YAML sections, the all, any, none conditions, rule types, deployment via agent.conf, scan schedule and reading results.
Wazuh SCA assesses system configuration against YAML policies. The agent runs them locally, and the server turns result changes into alerts. This post describes the syntax of a custom policy, how to deploy it and how to configure the schedule in ossec.conf. The values in the examples are synthetic.
The sca section in ossec.conf
The <sca> section configures the module. The enabled option turns it on, default yes. The skip_nfs option defaults to yes and, on Linux and FreeBSD, excludes file checks on CIFS or NFS mounts. A missing result for a file on such a mount can therefore come from this setting. The <policies> element holds <policy> entries with paths to YAML files, and the enabled="no" attribute disables an entry that was enabled earlier.
A relative path is resolved from the Wazuh installation directory (since version 4.2.0). A file outside it needs a full path.
<sca>
<enabled>yes</enabled>
<scan_on_start>yes</scan_on_start>
<time>04:00</time>
<skip_nfs>yes</skip_nfs>
<policies>
<policy>/var/ossec/etc/custom-sca-files/example-linux-baseline.yml</policy>
<policy enabled="no">etc/shared/example-disabled.yml</policy>
</policies>
</sca>
Scan schedule
| Option | Meaning | Default and limits |
|---|---|---|
scan_on_start | Scan right after the module starts | yes |
interval | Time between scans | 12h; suffixes s, m, h, d, w, M (minutes are m, months are M) |
day | Day of the month, 1 to 31 | The interval must be a multiple of months, default one month |
wday | Day of the week: sunday or sun, monday or mon and so on up to saturday or sat | Excludes day; the interval must be a multiple of weeks, default one week |
time | Time of day as hh:mm | With time alone, the interval must be a multiple of days or weeks, default one day |
A daily scan at a fixed time:
<sca>
<interval>1d</interval>
<time>04:00</time>
</sca>
Where the agent gets policies
By default the agent scans every .yaml and .yml file in its ruleset/sca directory, even when the <policies> section does not exist (behavior since version 3.10.0).
| Agent system | Default directory |
|---|---|
| Linux and Unix | /var/ossec/ruleset/sca |
| Windows | C:\Program Files (x86)\ossec-agent\ruleset\sca |
| macOS | /Library/Ossec/ruleset/sca |
Installs and upgrades do not preserve the contents of these directories, so keep your own files elsewhere, for example in /var/ossec/etc/custom-sca-files/. You disable a built-in policy by changing the file extension or with a <policy enabled="no"> entry that has a relative path, for example ruleset/sca/<file>. After the change, restart the agent (systemctl restart wazuh-agent on Linux, Restart-Service -Name wazuh on Windows, /Library/Ossec/bin/wazuh-control restart on macOS).
Distribution from the server
The server can push a policy file to the agents of a group, like any shared file (group rules are described in the post Agent groups and agent.conf). The procedure from the documentation for the default group:
- On the agent, add
sca.remote_commands=1to thelocal_internal_options.conffile (on Linux,/var/ossec/etc/local_internal_options.conf). Remote commands in policies are disabled by default, because a compromised server could then run commands on endpoints. The option is not needed when the file reaches the agent another way, for example throughscp. - On the server, place the file in
/var/ossec/etc/shared/default/and set its owner towazuh:wazuh. - In
/var/ossec/etc/shared/default/agent.conf, point to the file with the pathetc/shared/<file>(on Windows,shared\<file>).
<agent_config os="Linux">
<sca>
<policies>
<policy>etc/shared/example-linux-baseline.yml</policy>
</policies>
</sca>
</agent_config>
The <sca> block from the server is merged with the agent’s <sca> block. The agent stores files pushed from the server in etc/shared/ (Linux, macOS) or shared\ (Windows), regardless of the group.
Policy file structure
The YAML file has four sections.
| Section | Required | Content |
|---|---|---|
policy | Yes | Policy metadata |
requirements | No | Condition for running the whole file |
variables | No | Variables used in rules |
checks | Yes | List of checks |
When the requirements are not met, the scan of that file does not start at all. The id field in policy and in checks must be unique across all policy files.
Fields of the policy section: id, file, name and description are mandatory strings, references is an optional array of links, and regex_type selects the regular expression engine: osregex (default) or pcre2. The requirements section needs the fields title, description, condition and rules.
Fields of a check in checks:
| Field | Mandatory | Type and values |
|---|---|---|
id | Yes | Integer |
title | Yes | String |
description, rationale, remediation | No | String |
compliance | No | Array of arrays of strings |
references | No | Array of strings |
condition | Yes | all, any or none |
rules | Yes | Array of rules |
regex_type | No | pcre2 or osregex, overrides the policy setting |
You define variables in variables with a $ prefix, for example $sshd_file: /etc/ssh/sshd_config. You can use them in the left part of a rule: f:$list_of_files -> r:^Content to be found or c:systemctl is-enabled $program_name -> r:^enabled. The number of variables in a rule is not limited.
The condition field
condition defines how rule results combine into the check result. A rule with status Passed counts as satisfied.
| Condition | Passed | Failed | Not applicable |
|---|---|---|---|
all | All rules satisfied | At least one rule Failed | No rule is Failed and at least one is Not applicable |
any | At least one rule satisfied | None satisfied, at least one Failed, none Not applicable | None satisfied and at least one Not applicable |
none | None satisfied, at least one Failed, none Not applicable | At least one rule satisfied | None satisfied and at least one Not applicable |
Rule syntax
A rule starts with a target type and, after ->, can have a content test. Without a content test it is an existence check.
| Type | Target | Note |
|---|---|---|
f | File | Existence or content |
d | Directory | Existence, files in the directory or file content |
p | Process | Existence only |
c | Command | Output content only |
r | Registry key or value | Windows only |
Content operators: no operator means a literal comparison of the whole line, r: is a regular expression match, and n: is an integer comparison with a capture group and an operator after the word compare (<, <=, ==, !=, >=, >). The prefix not before a rule negates it, and ! before a content operator negates that operator. You join operators with &&.
f:/proc/sys/net/ipv4/ip_forward -> 1
f:/etc/ssh/sshd_config -> !r:^# && r:Protocol && r:2
f:$sshd_file -> n:^\s*MaxAuthTries\s*\t*(\d+) compare <= 4
not p:sshd
d:/home -> ^.mysql_history$
d:/path/to/directory -> file_name -> content
c:sshd -T -> !r:^\s*maxauthtries\s+4\s*$
r:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa -> LimitBlankPasswordUse -> 1
Rules have a few traps:
- A content test works within a single line.
- Spaces around
->,compareand&&are mandatory, and the evaluation order of parts joined by&&is not defined. - When a file or directory with a content test does not exist, the result is Not applicable, because the check could not be run.
- A negated operator gives Passed for every line that does not match. The rule
f:/etc/ssh_config -> !r:PermitRootLoginpasses if the file has any line withoutPermitRootLogin.
A complete example policy
The Linux policy combines four kinds of rules: numeric, process, command and file content. The rule patterns come from documentation examples.
policy:
id: "example_linux_baseline"
file: "example-linux-baseline.yml"
name: "Example Linux baseline"
description: "Illustrative baseline for the SCA syntax: sshd, processes, accounts."
references:
- https://example.com/baseline
requirements:
title: "Run only on hosts with SSH and local account files"
description: "Skip the whole file when none of these paths exist."
condition: any
rules:
- 'f:$sshd_file'
- 'f:/etc/passwd'
variables:
$sshd_file: /etc/ssh/sshd_config
checks:
- id: 100001
title: "MaxAuthTries is at most 4"
description: "sshd should limit authentication attempts per connection."
rationale: "A low limit reduces the number of password guesses per connection."
remediation: "Set MaxAuthTries to 4 or less in sshd_config."
compliance:
- example_standard: ["1.1"]
condition: all
rules:
- 'f:$sshd_file -> n:^\s*MaxAuthTries\s*\t*(\d+) compare <= 4'
- id: 100002
title: "Netcat is not running"
description: "No netcat process should run on the endpoint."
rationale: "Netcat can open listeners or outbound connections."
remediation: "Investigate the process and stop it if it is unauthorized."
condition: none
rules:
- 'p:nc'
- 'p:netcat'
- id: 100003
title: "Root is the only account with UID 0"
description: "No other account should have UID 0."
rationale: "Every UID 0 account has full privileges."
remediation: "Remove or renumber the extra account."
condition: none
rules:
- 'f:/etc/passwd -> !r:^# && !r:^root: && r:^\w+:\w+:0:'
- id: 100004
title: "The cups service is not enabled"
description: "Print services should be disabled when unused."
rationale: "Fewer enabled services means a smaller attack surface."
remediation: "Run systemctl disable cups."
condition: none
rules:
- 'c:systemctl is-enabled cups -> r:^enabled'
Expected result on an agent with MaxAuthTries 6 in sshd_config, no nc or netcat process, one account with UID 0 and the cups service enabled: checks 100001 and 100004 are Failed (100004 because the rule under the none condition is satisfied), and 100002 and 100003 are Passed.
How to test a policy
The documentation shows a walkthrough on a Linux agent with a policy that looks for a phrase in a test file:
- Create a directory for custom policies, for example
mkdir /var/ossec/etc/custom-sca-files/, and save the YAML file in it. - Set the file owner:
chown wazuh:wazuh /var/ossec/etc/custom-sca-files/<file>. - Add an
<sca>block to the agent’sossec.confwith a<policy enabled="yes">entry and the full path. - Restart the agent, which starts a scan (
scan_on_startdefaults toyes). - Open the Configuration Assessment module on the dashboard and select the agent.
The documentation uses a check with the none condition and the rule f:/usr/share/testfile.txt -> r:^password_enabled: yes$: a Failed result appears when the file contains exactly that line. For process checks, the documentation runs netcat -l 4444 before the scan to trigger a Failed result.
My own recommendation: test on a separate agent or group with a single policy file and change the host state one rule at a time, so you can attribute each result change to one check. After every edit of the policy file, expect alerts for all of its checks (described in the results section).
Where the results show up
The agent keeps a local database of check state, the server keeps a state database for all agents, and the agent sends only the differences between scans plus a summary. An alert appears when a check changes status to Passed, Failed or Not applicable. Not applicable means the check could not be evaluated, for example when one of the rules did not run. The reason field gives the cause, and the API endpoint GET /sca/{agent_id}/checks/{policy_id} returns the same result.
A check event has fields in the data.sca hierarchy, including scan_id, type, policy, check.id, check.title, check.result, check.remediation and check.command. The built-in rules from the 0570-sca_rules.xml file (Wazuh 4.14.0) match them through the fields sca.type, sca.check.result and sca.check.previous_result:
| Rule | Level | Event |
|---|---|---|
| 19001 | 3 | Scan summary with the percentage score sca.score |
| 19002, 19003, 19004, 19005 | 4, 5, 7, 9 | Score below 90, 80, 50 and 30 percent |
| 19007, 19008, 19009 | 7, 3, 3 | Check: Failed, Passed, Not applicable |
| 19010 | 3 | Change from Failed to Passed |
| 19011 | 9 | Change from Passed to Failed |
| 19012, 19013 | 5 | Change from Passed or Failed to Not applicable |
| 19014 | 9 | Change from Not applicable to Failed |
| 19015 | 3 | Change from Not applicable to Passed |
A custom check lands in the same rules, so rule 19011 detects a regression. How rule levels map to alert thresholds is described in the post on rule levels.
When the checksum of the policy file changes, the server writes a message to /var/ossec/logs/ossec.log in the form wazuh-analysisd: INFO: Policy 'cis_debian10' information for agent '001' is outdated. Requested latest scan results., clears the stored policy data and accepts the new results from the agent. Alerts are then raised for every check of the policy.
What the documentation does not specify
The documentation gives no maximum file size or number of checks, no message for a YAML syntax error and no way to validate a file before deployment. It does not describe disabling individual checks, testing SCA events with wazuh-logtest, or how an n: rule behaves when the captured value is not numeric. It also does not state character set differences between osregex and pcre2 for SCA rules, or a recommended scan interval for a specific workload.
Sources
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/sca.html
- https://documentation.wazuh.com/current/user-manual/capabilities/sec-config-assessment/index.html
- https://documentation.wazuh.com/current/user-manual/capabilities/sec-config-assessment/how-it-works.html
- https://documentation.wazuh.com/current/user-manual/capabilities/sec-config-assessment/how-to-configure.html
- https://documentation.wazuh.com/current/user-manual/capabilities/sec-config-assessment/creating-custom-policies.html
- https://documentation.wazuh.com/current/user-manual/capabilities/sec-config-assessment/use-cases.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0570-sca_rules.xml