PL EN
WB-996262BDTLP:CLEARWazuh 4

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

OptionMeaningDefault and limits
scan_on_startScan right after the module startsyes
intervalTime between scans12h; suffixes s, m, h, d, w, M (minutes are m, months are M)
dayDay of the month, 1 to 31The interval must be a multiple of months, default one month
wdayDay of the week: sunday or sun, monday or mon and so on up to saturday or satExcludes day; the interval must be a multiple of weeks, default one week
timeTime of day as hh:mmWith 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 systemDefault directory
Linux and Unix/var/ossec/ruleset/sca
WindowsC:\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:

  1. On the agent, add sca.remote_commands=1 to the local_internal_options.conf file (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 through scp.
  2. On the server, place the file in /var/ossec/etc/shared/default/ and set its owner to wazuh:wazuh.
  3. In /var/ossec/etc/shared/default/agent.conf, point to the file with the path etc/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.

SectionRequiredContent
policyYesPolicy metadata
requirementsNoCondition for running the whole file
variablesNoVariables used in rules
checksYesList 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:

FieldMandatoryType and values
idYesInteger
titleYesString
description, rationale, remediationNoString
complianceNoArray of arrays of strings
referencesNoArray of strings
conditionYesall, any or none
rulesYesArray of rules
regex_typeNopcre2 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.

ConditionPassedFailedNot applicable
allAll rules satisfiedAt least one rule FailedNo rule is Failed and at least one is Not applicable
anyAt least one rule satisfiedNone satisfied, at least one Failed, none Not applicableNone satisfied and at least one Not applicable
noneNone satisfied, at least one Failed, none Not applicableAt least one rule satisfiedNone 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.

TypeTargetNote
fFileExistence or content
dDirectoryExistence, files in the directory or file content
pProcessExistence only
cCommandOutput content only
rRegistry key or valueWindows 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 ->, compare and && 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:PermitRootLogin passes if the file has any line without PermitRootLogin.

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:

  1. Create a directory for custom policies, for example mkdir /var/ossec/etc/custom-sca-files/, and save the YAML file in it.
  2. Set the file owner: chown wazuh:wazuh /var/ossec/etc/custom-sca-files/<file>.
  3. Add an <sca> block to the agent’s ossec.conf with a <policy enabled="yes"> entry and the full path.
  4. Restart the agent, which starts a scan (scan_on_start defaults to yes).
  5. 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:

RuleLevelEvent
190013Scan summary with the percentage score sca.score
19002, 19003, 19004, 190054, 5, 7, 9Score below 90, 80, 50 and 30 percent
19007, 19008, 190097, 3, 3Check: Failed, Passed, Not applicable
190103Change from Failed to Passed
190119Change from Passed to Failed
19012, 190135Change from Passed or Failed to Not applicable
190149Change from Not applicable to Failed
190153Change 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

↑↓ select Enter open Esc close