Detection engineering in Wazuh: from a raw log to a tested rule
From raw log to Wazuh rule: decoder, local_rules.xml with IDs 100000-120000, three wazuh-logtest phases and overwrite for rule 5710 without editing the ruleset.
A Wazuh rule makes sense only when the log contains fields it can work on. Going from a raw entry through the decoder and the rule to wazuh-logtest lets you check what the system recognizes before a change starts generating alerts.
This description covers Wazuh 4.x. In Wazuh 5 (5.0-rc), detection is described by Sigma rules executed by detectors in the Wazuh indexer, and the result is a finding instead of an alert: Wazuh 5: Sigma rules, detectors and findings.
Start from the raw event
Rules analyze events using conditions. Every condition must be met for the rule to generate an alert. When the analysis starts from the rule, it is easy to mistake a matching error for a parsing problem. First take a representative log and establish whether Wazuh recognizes its initial fields and the decoder.
The example documentation uses an entry from the program example with text about a user logging in. In the example, the decoder first identifies the program, and a following child decoder extracts the user and the source address. For a deployment, use your own anonymized samples, because program names, format and available fields depend on the source.
Check the decoder before the rule
A new decoder can be added to /var/ossec/etc/decoders/local_decoder.xml. The documentation recommends separate files in /var/ossec/etc/decoders/ for larger changes; more on custom decoders in the article on custom decoders. The sample decoder shows program recognition and a regular expression that assigns two captured elements to the fields user and srcip.
<decoder name="example">
<program_name>^example</program_name>
</decoder>
<decoder name="example">
<parent>example</parent>
<regex>User '(\w+)' logged from '(\d+.\d+.\d+.\d+)'</regex>
<order>user, srcip</order>
</decoder>
A log sample for this decoder, with a synthetic host and address:
Dec 25 20:45:02 agent-01 example[12345]: User 'admin' logged from '192.0.2.10'
In the logtest result for this example, the documentation shows the fields dstuser and srcip, not user; compare names with phase 2, not with the order element.
Run /var/ossec/bin/wazuh-logtest and enter one log sample. In the pre-decoding phase, check full event, timestamp, hostname and program_name. The decoding phase should show the decoder name and the extracted values. If an expected field is missing, fix the decoder first. A rule cannot reliably test values that the decoder does not extract.
The section on the three phases describes restarting the manager. When modifying a default decoder, the documentation recommends copying its file to /var/ossec/etc/decoders/ and excluding the original from the load list. Editing a file in the ruleset directory can be lost on upgrade. Excluding the original, however, means its later updates will not reach the copy automatically.
Add a local rule
Minor rule changes go into /var/ossec/etc/rules/local_rules.xml. For larger changes you can create separate files in /var/ossec/etc/rules/. Custom rules should use identifiers from 100000 to 120000 to avoid conflicts with the rules shipped with the system. The file load order is described in the article on ruleset loading.
Below is a rule example from the documentation for the example decoder.
<group name="custom_rules_example,">
<rule id="100010" level="0">
<program_name>example</program_name>
<description>User logged</description>
</rule>
</group>
The program_name condition ties the rule to the program name recognized earlier. description should briefly explain the purpose of the rule. The custom_rules_example group ties the alert to a category. Level 0 means the event is not shown in the dashboard, although logtest still shows the rule in phase 3 with id and level.
You can put the MITRE ATT&CK mapping in the mitre field. The exact choice of conditions should follow from the fields present in the decoding phase and from the behavior of the source.
The table lists the elements needed in the flow shown. Allowed values and meaning come from the XML syntax reference.
| Element | Values | Meaning |
|---|---|---|
id | A number from 1 to 999999 | Sets the rule identifier |
level | From 0 to 16 | Sets the rule level used by alerts and responses |
if_sid | A list of rule identifiers | Matches when the indicated rule matched earlier |
match | Regular expression | Looks for a match in the log with the default sregex engine |
regex | Regular expression | Works like match, but uses the regex type by default |
field | Field name and regular expression | Compares a field extracted by the decoder |
description | Text | Explains the purpose of the rule; the documentation recommends always using it |
group | Group name | Categorizes the alert and allows filtering related alerts |
Values such as srcip, dstip, srcport, dstport, user, protocol, hostname and location also appear in the reference as condition labels. Their presence in the syntax table does not mean every source provides all of these fields. First confirm their availability in the decoder output.
Run the sample through three phases
/var/ossec/bin/wazuh-logtest checks decoders and rules against the samples you supply. It is available through the dashboard, the command-line tool or the Wazuh server API. It splits the analysis into three stages:
Phase 1: Completed pre-decoding.shows the full event and the initially recognized elements, such as the timestamp, host and program name.Phase 2: Completed decoding.shows the decoder and the fields extracted from the log body.Phase 3: Completed filtering (rules).presents the matched rule and its result, including the identifier, level, description, groups,firedtimesand the MITRE mapping, if present.
Result for the example decoder and rule 100010:
**Phase 1: Completed pre-decoding.
full event: 'Dec 25 20:45:02 agent-01 example[12345]: User 'admin' logged from '192.0.2.10''
timestamp: 'Dec 25 20:45:02'
hostname: 'agent-01'
program_name: 'example'
**Phase 2: Completed decoding.
name: 'example'
dstuser: 'admin'
srcip: '192.0.2.10'
**Phase 3: Completed filtering (rules).
id: '100010'
level: '0'
description: 'User logged'
groups: '['custom_rules_example']'
firedtimes: '1'
mail: 'False'
A positive sample should reach the expected decoder and rule. Also use a sample that differs in one significant condition, for example one that lacks the expected program name or has a different field value. wazuh-logtest shows which decoders and rules match the given logs, so a negative result can be judged by the target rule not matching. The documentation does not prescribe a separate negative testing procedure or a name for such a sample.
Change one condition at a time in the negative sample. If you change the program, the user field and the log text at once, a missing match does not show which condition worked. In the test report, keep the sample text, the phase where the result differs, and the identifier of the rule that matched or did not match. Do not assign Alert to be generated to a negative sample if the rule phase does not show it.
When you test in interactive mode, each wazuh-logtest session loads its own set of rules and decoders. The firedtimes counter keeps the number of matches for the duration of the session. This is useful for checking whether consecutive entries change the rule result, but do not mix results from different sessions. In a Wazuh cluster the master node processes events. The tester configuration is in the <rule_test> section of /var/ossec/etc/ossec.conf; default values:
<rule_test>
<enabled>yes</enabled>
<threads>1</threads>
<max_sessions>64</max_sessions>
<session_timeout>15m</session_timeout>
</rule_test>
A session inactive for longer than session_timeout is closed. When the number of open sessions reaches max_sessions, opening a new one closes the longest idle one.
The documentation example shows that firedtimes grows with consecutive repeats within a logtest session. Repeated logs can also trigger a correlation rule when the condition on event count and time is met: in the documentation example, the eighth SSH event for a non-existent user from one address within two minutes matches rule 5712 instead of 5710. More in the article on event correlation.
Saving the file is enough for wazuh-logtest; to generate alerts, you have to restart the Wazuh manager.
# systemd
systemctl restart wazuh-manager
# SysV init
service wazuh-manager restart
You can also call the tester through the Wazuh server API. The documentation provides the PUT /logtest endpoint, which checks whether an alert matches a log and returns related information. The request is passed as JSON. location in this interface describes the source of the log passed to the test. Do not automatically equate this value with location in any alert type.
| Request field | Meaning |
|---|---|
log_format | Log type, for example syslog |
event | Log to process |
location | Log origin |
token | Optional logtest session identifier |
The first processing request has no session token. When there is no active session, the request is sent to the analysis engine, and the response returns a logtest token. Later operations can pass it to continue the session. The API tester does not replace judging the result phases: you still have to check the decoder and the identifier of the matched rule.
Change a built-in rule without editing the ruleset
The documentation procedure is to copy the definition to a file in /var/ossec/etc/rules/, make the local change and add overwrite="yes" to the rule element. A copy in /var/ossec/etc/rules/ protects changes from being lost on upgrade. The source example changes the level of the SSH rule 5710 from 5 to 10.
The base rule contains the matching condition and the metadata (description, MITRE, groups), so copy the whole definition.
<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>
Wazuh does not allow overwriting if_sid, if_group, if_level, if_matched_sid or if_matched_group in a rule with overwrite="yes". If present, they are ignored and the original values stay active.
Before changing a built-in rule, save the wazuh-logtest result for a sample that should match it. After the edit, repeat the same sample and check that the identifier stays the same and the level changed as intended. Also add a sample that does not meet the <match> condition, to check that the rule has not started matching a wider set of entries. This is a check of the change scope through the available tester; the documentation does not specify a separate mechanism for comparing rule versions.
The full description of levels, groups and correlation is in the article on rule levels. Testing decoders and rules step by step is described in the article on wazuh-logtest.
What the documentation does not specify
The documentation explains how the tester works and shows examples, but it does not define a universal set of positive and negative samples for every detection. It also does not specify an acceptance criterion for a rule in a production environment. Choose samples for the specific source format and for the conditions the rule has to distinguish. The documentation does not give a procedure for reloading rules without a restart.
Sources
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/rules/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html
- https://documentation.wazuh.com/current/user-manual/ruleset/testing.html