How Wazuh loads rules: rule_dir, rule_exclude and file order
How Wazuh loads rules and decoders: the ruleset block, rule_dir with pattern, rule_exclude, alphabetical order, local_rules.xml, overwrite and manager restart.
The <ruleset> block in the manager’s ossec.conf decides which rule and decoder files reach the Wazuh analysis engine. It lists directories, single files, exclusions and CDB lists, and your own rules should go to a directory that updates do not overwrite.
Where rules and decoders live
Wazuh ships with more than 3000 rules and decoders. The documentation lists four locations:
| Path | Content | Wazuh update |
|---|---|---|
/var/ossec/ruleset/rules/ | Rules shipped with Wazuh | Files are overwritten or modified |
/var/ossec/ruleset/decoders/ | Decoders shipped with Wazuh | Files are overwritten or modified |
/var/ossec/etc/rules/local_rules.xml | Custom rules | A place for user changes |
/var/ossec/etc/decoders/local_decoder.xml | Custom decoders | A place for user changes |
All files in /var/ossec/ruleset/ are overwritten or modified during a Wazuh update. The documentation does not recommend editing or adding your own files there. It recommends changes in /var/ossec/etc/, where you can add your own rule and decoder files or use the default local_decoder.xml and local_rules.xml.
Options of the <ruleset> block
The documentation describes separate options for rules, decoders and CDB lists. n/a in the table means the documentation gives no default value.
| Option | Action | Allowed value | Default value |
|---|---|---|---|
rule_include | Loads a single rule file | Path and name of the rule file to load | n/a |
rule_dir | Loads a directory with rules | Path to a directory with rule files | ruleset/rules |
rule_exclude | Excludes a single rule file | Path and name of the rule file to exclude | n/a |
decoder_include | Loads a single decoder file | Path and name of the decoder file to load | n/a |
decoder_dir | Loads a directory with decoders | Path to a directory with decoder files | ruleset/decoders |
decoder_exclude | Excludes a single decoder file | Path and name of the decoder file to exclude | n/a |
list | Loads a single CDB list for use in rules | Path to the list file, which will be loaded and compiled | n/a |
rule_dir and decoder_dir accept an optional pattern attribute. The documentation describes it as a regular expression that decides whether a given file from the directory is loaded.
The example configuration from the reference page combines all the options:
<ruleset>
<rule_include>ruleset/rules/my_rules.xml</rule_include>
<rule_dir pattern="_rules.xml$">ruleset/rules</rule_dir>
<rule_exclude>0215-policy_rules.xml</rule_exclude>
<decoder_include>ruleset/decoders/my_decoder.xml</decoder_include>
<decoder_dir pattern=".xml$">ruleset/decoders</decoder_dir>
<decoder_exclude>ruleset/decoders/my_decoder.xml</decoder_exclude>
<list>etc/lists/blocked_hosts</list>
</ruleset>
In the example rule_exclude gives the bare file name, and decoder_exclude a relative path.
File order
The documentation for rule_dir and decoder_dir says: files are loaded in alphabetical order, and duplicate file names are skipped. Example names of files shipped with Wazuh are 0095-sshd_rules.xml and 0215-policy_rules.xml. When you name your own file in a directory, you also set its place in the order within that directory. For three example files in /var/ossec/etc/rules/ the order looks like this:
| File | Position in alphabetical order |
|---|---|
example-app_rules.xml | 1 |
local_rules.xml | 2 |
zz-example-overrides_rules.xml | 3 |
Default block and the user part
The page about custom decoders shows a <ruleset> block split into a default set and a user set. The XML comments come from the documentation. The last line, decoder_exclude, is part of a customization example in which the documentation disables the shipped SSH decoder.
<ruleset>
<!-- Default ruleset -->
<decoder_dir>ruleset/decoders</decoder_dir>
<rule_dir>ruleset/rules</rule_dir>
<rule_exclude>0215-policy_rules.xml</rule_exclude>
<list>etc/lists/audit-keys</list>
<!-- User-defined ruleset -->
<decoder_dir>etc/decoders</decoder_dir>
<rule_dir>etc/rules</rule_dir>
<decoder_exclude>ruleset/decoders/0310-ssh_decoders.xml</decoder_exclude>
</ruleset>
The documentation replaces rules and decoders shipped with Wazuh in two different ways:
| Element | Rule | Decoder |
|---|---|---|
| Scope of the change | A single rule | The whole decoder file |
| The copy goes to | /var/ossec/etc/rules/ | /var/ossec/etc/decoders/ |
| Mechanism | overwrite="yes" in the copied rule | decoder_exclude for the original file |
After the original decoder file is excluded, its updates do not reach the copy, so important changes have to be carried over by hand. The step-by-step procedure is in the article on custom decoders.
Custom rules and the ID range
For small changes the documentation points to the file /var/ossec/etc/rules/local_rules.xml. For larger changes it recommends creating new rule files in /var/ossec/etc/rules/. Custom rules should use IDs from the range 100000-120000, so they do not collide with rules shipped with Wazuh. The syntax of the id attribute allows numbers from 1 to 999999. For decoders the documentation gives an analogous split: new decoders in /var/ossec/etc/decoders/local_decoder.xml, and for larger changes new files in /var/ossec/etc/decoders/.
A custom rule example from the documentation, with an ID from the recommended range:
<group name="custom_rules_example,">
<rule id="100010" level="0">
<program_name>example</program_name>
<description>User logged</description>
</rule>
</group>
Changing a rule shipped with Wazuh: overwrite
To change a rule shipped with Wazuh, copy it to a file in /var/ossec/etc/rules/, make the changes and add overwrite="yes" to the rule. The overwrite attribute replaces a rule with local changes and takes the values yes or no.
The documentation shows this on rule 5710 from the file /var/ossec/ruleset/rules/0095-sshd_rules.xml, changing its level from 5 to 10. The steps from the documentation:
- Open
/var/ossec/ruleset/rules/0095-sshd_rules.xml. - Find and copy the definition of rule 5710.
- Paste it into
/var/ossec/etc/rules/local_rules.xml, changeleveland addoverwrite="yes".
The result from the documentation:
<group name="syslog,sshd,">
<rule id="5710" level="10" overwrite="yes">
<if_sid>5700</if_sid>
<match>illegal user|invalid user</match>
<description>sshd: Attempt to login using a non-existent user</description>
<mitre>
<id>T1110</id>
</mitre>
<group>invalid_login,authentication_failed,pci_dss_10.2.4,pci_dss_10.2.5,pci_dss_10.6.1,gpg13_7.1,gdpr_IV_35.7.d,gdpr_IV_32.2,hipaa_164.312.b,nist_800_53_AU.14,nist_800_53_AC.7,nist_800_53_AU.6,tsc_CC6.1,tsc_CC6.8,tsc_CC7.2,tsc_CC7.3,</group>
</rule>
</group>
The documentation notes that the labels if_sid, if_group, if_level, if_matched_sid and if_matched_group cannot be overwritten. In a rule with overwrite="yes" they are ignored and the original values apply. So changing if_sid in the example above will not change the parent rule. The effect of the rule level on alerts is described in the article on rule levels and thresholds.
CDB lists in <ruleset>
The list option loads a single CDB list. Do not give the file extension in the value. Since version 3.11.0 Wazuh builds and loads CDB lists itself when the analysis engine starts. The list format and their use in rules are described in the article on CDB lists.
Test and restart
To test rules in /var/ossec/bin/wazuh-logtest it is enough to save the changed files. For rules to generate alerts, you have to restart the Wazuh manager:
# Systemd
systemctl restart wazuh-manager
# SysV init
service wazuh-manager restart
A test session in this tool is described in the article on wazuh-logtest. How I deploy rule files from a git repository through the API, also in a cluster, is shown in the post on rules from a repository.
Checklist before a change
- Check whether you are changing a rule shipped with Wazuh or your own.
- Save changes in
/var/ossec/etc/rules/, not in/var/ossec/ruleset/rules/, which the documentation does not recommend. - For small changes use
local_rules.xml, for larger ones a separate file in/var/ossec/etc/rules/. - When changing a shipped rule, keep its ID and add
overwrite="yes". - Do not change the
if_*labels in an overriding rule, because they will be ignored. - Check the name of the new file: it decides its place in the alphabetical order in the directory.
- Check that
patterninrule_dircovers the name of the new file and thatrule_excludedoes not exclude it. - Test the rule in
wazuh-logtest, then restart the manager.
What the documentation does not specify
The documentation does not say whether skipping duplicate names works across different rule_dir entries, for example for the same name in ruleset/rules and etc/rules, or which copy stays in that case. It does not specify the load order between separate rule_dir entries. It gives no general rule on whether rule_exclude takes a bare name or a path, nor the dialect of the expressions in the pattern attribute. The pages described here also say nothing about what happens when if_sid points to a rule from a file loaded later, or when two rules have the same ID without overwrite="yes".
Sources
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/ruleset.html
- https://documentation.wazuh.com/current/user-manual/ruleset/rules/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/index.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html