Custom Wazuh decoders: syntax, regex, sibling and dynamic fields
Wazuh decoders in practice: tag table, OS_Regex and pcre2 syntax, sibling decoders, dynamic fields, and an example with the expected Phase 2 output in logtest.
A decoder splits a raw log into fields that rules work on. Custom decoders go to /var/ossec/etc/decoders/, and you can check how they work in wazuh-logtest without restarting the manager.
The paths and syntax below apply to Wazuh 4.x. In Wazuh 5 (5.0-rc) the decoder normalizes the log to the Wazuh Common Schema, and the XML decoders from 4.x do not migrate and have to be recreated: migration from 4.x to Wazuh 5, Sigma rules in Wazuh 5.
Where to write decoders
For a single decoder the documentation points to the file /var/ossec/etc/decoders/local_decoder.xml. For bigger changes, separate files in the same directory are assumed. The directory is loaded by the entry <decoder_dir>etc/decoders</decoder_dir> in the <ruleset> section, as described in the post Loading rules and decoders in Wazuh. Saving the file is enough for a test in wazuh-logtest. A manager restart is needed only to generate alerts:
systemctl restart wazuh-manager
# or on SysV init
service wazuh-manager restart
The full path from a syslog sample of a BMC controller to a decoder, a rule and a wazuh-logtest test is shown in the post on decoding BMC logs.
Decoder tags
A decoder is a <decoder name="..."> element. The documentation describes these tags:
| Tag | Values | Role |
|---|---|---|
parent | decoder name | The decoder becomes a child and runs only after the parent matches |
program_name | regex, sregex or pcre2 | Condition on the program name from the syslog header |
prematch | regex or pcre2 | Condition on the log content, without the syslog header |
regex | regex or pcre2 | Pattern with groups in parentheses, from which fields are created |
order | list of field names | Field names for successive regex groups |
fts | location and static fields | First time seen, an alert on the first match when a rule uses if_fts |
ftscomment | any text | Comment for fts |
plugin_decoder | PF_Decoder, SymantecWS_Decoder, SonicWall_Decoder, OSSECAlert_Decoder, JSON_Decoder | Decoding with a plugin when regex is impractical |
accumulate | none | Tracking events spread over many lines by the id field |
use_own_name | true | For child decoders: the name comes from their own name, not from the parent |
type | syslog (default), firewall, ids, web-log, squid, windows, host-information, ossec | The log type the decoder handles |
var | variable name | A variable used across the whole file, defined at its start |
For json_null_field the default value is string, and discard drops null fields. For json_array_structure the default is array, and csv turns arrays into CSV strings. Both work only with the JSON decoder.
Attributes of the match tags:
| Tag | Attribute | Values |
|---|---|---|
program_name | type | osmatch (default), osregex, pcre2 |
prematch | type | osregex (default), pcre2 |
prematch | offset | after_regex, after_parent |
regex | type | osregex (default), pcre2 |
regex | offset | after_regex, after_parent, after_prematch |
plugin_decoder | offset | after_parent, after_prematch |
offset discards the start of the log and begins matching from the indicated place. A tag repeated in one decoder concatenates the values, and the type value comes from the last occurrence.
Dependencies between tags:
regexrequiresorderin the same decoder.regexrequiresprematchorprogram_namein the same decoder or in the parent.- A child decoder cannot be a parent, so decoders have no grandchildren.
- After a decoder without a parent matches, Wazuh looks further only among its children. A parent condition that is too general therefore takes over logs from other decoders.
Regex syntax
The documentation distinguishes three types: regex (OS_Regex, the osregex value of the type attribute), sregex (OS_Match, osmatch) and PCRE2 (pcre2). The default type is osregex for prematch and regex, and osmatch for program_name.
| OS_Regex token | Meaning |
|---|---|
\w | Letters, digits, -, @, _ |
\d | Digit |
\s | ASCII space only (32) |
\t | Tab |
\p | Punctuation characters from the list in the documentation |
\W, \D, \S | Negations of \w, \d, \s |
\. | Any character |
. | Literal dot |
+, * | One or more, zero or more |
^, $ | Start and end of text |
The vertical bar works as a logical or between patterns. The characters $, (, ), \, the vertical bar and the less-than sign are preceded by a backslash. The \p character list is ()*+,-.:;<=>?[]!"'#$%&|{}. OS_Regex limitations from the documentation:
+and*work only on expressions with a backslash, so\d+is valid and0+is not.- Alternation in a group, for example
(foo|bar), is not allowed. - Complex backtracking is not supported:
\p*\d*\s*\w*:will not match a single colon, because\p*consumes it first. - There is no syntax for a literal caret, asterisk or plus, although
\pcovers the asterisk and plus.
sregex supports only simple text matching and ^, $, the vertical bar and ! (negation). You enable PCRE2 with the attribute type="pcre2". It is case sensitive by default, and (?i) turns that off. The Wazuh XML parser does not decode entities and treats < in a value as the start of an element, so literal angle brackets are written as \x3C and \x3E:
<regex type="pcre2">from=\x3C(.*?@(.*?))\x3E</regex>
The pattern \.+ from the documentation examples means any character repeated one or more times. You can check pattern matching in wazuh-logtest, as described in the post Wazuh logtest.
Sibling decoders
Sibling is a strategy for logs with a variable structure. The parent matches the program, and several child decoders with the same parent each extract one field. Instead of one long regex that requires a fixed field order, each decoder looks for its own fragment. The order of fragments in the log and the presence of some of them stop mattering, and Wazuh extracts as much information as it finds. The documentation also stresses the better readability of short patterns.
Static and dynamic fields
The static fields are fourteen names: user, srcuser, srcip, dstip, srcport, dstport, protocol, action, id, url, data, extra_data, status and system_name. The documentation also gives dstuser, and user is its alias (you can use only one of the names). A single event fills at most eight static fields, with priority for user, srcip, dstip, srcport, dstport, protocol, action and id. Any other name in <order> creates a dynamic field, and a name with a dot creates a nested field in JSON, for example audit.auid.
The default limit of fields from one <order> is 64. You change it through analysisd.decoder_order_size. Copy the value from /var/ossec/etc/internal_options.conf to /var/ossec/etc/local_internal_options.conf, because Wazuh updates overwrite the first file.
Example: parent and sibling decoders
The values in the example are synthetic. The application example-app writes lines with a variable set of fields:
Oct 30 12:00:00 agent-01 example-app: INFO: user="example-user" src=203.0.113.7 port=2222 reason="bad password"
Oct 30 12:00:05 agent-01 example-app: INFO: user="example-user" src=203.0.113.7
Decoders in /var/ossec/etc/decoders/local_decoder.xml:
<decoder name="example-app">
<program_name>^example-app</program_name>
</decoder>
<decoder name="example-app">
<parent>example-app</parent>
<regex>user="(\w+)"</regex>
<order>user</order>
</decoder>
<decoder name="example-app">
<parent>example-app</parent>
<regex>src=(\d+.\d+.\d+.\d+)</regex>
<order>srcip</order>
</decoder>
<decoder name="example-app">
<parent>example-app</parent>
<regex>port=(\d+)</regex>
<order>srcport</order>
</decoder>
<decoder name="example-app">
<parent>example-app</parent>
<regex>reason="(\.+)"</regex>
<order>example.reason</order>
</decoder>
Each child decoder has a regex with order, and the program_name condition sits in the parent, so the dependencies from the previous section are met. The example.reason field is dynamic. Test:
/var/ossec/bin/wazuh-logtest
Paste the first log line. The expected result, following the format from the documentation, looks like this:
**Phase 1: Completed pre-decoding.
full event: 'Oct 30 12:00:00 agent-01 example-app: INFO: user="example-user" src=203.0.113.7 port=2222 reason="bad password"'
timestamp: 'Oct 30 12:00:00'
hostname: 'agent-01'
program_name: 'example-app'
**Phase 2: Completed decoding.
name: 'example-app'
dstuser: 'example-user'
example.reason: 'bad password'
srcip: '203.0.113.7'
srcport: '2222'
Phase 1 shows data from the syslog header. In phase 2 the user field appears as dstuser, because user is its alias, which the documentation shows with the example case. The second line has no port or reason: decoders without a match are skipped, and dstuser and srcip are still extracted. A single decoder with a fixed-order regex would extract nothing from it. The documentation shows this situation with the securityapp example.
Variables and PCRE2
A var block at the start of the file defines variables used in its decoders, in values and attributes. An example from the documentation:
<var name="header">myprog</var>
<var name="offset">after_parent</var>
<var name="type">syscall</var>
<decoder name="syscall">
<prematch>^$header</prematch>
</decoder>
<decoder name="syscall-child">
<parent>syscall</parent>
<prematch offset="$offset">^: $type </prematch>
<regex offset="after_prematch">(\S+)</regex>
<order>syscall</order>
</decoder>
For PCRE2 the documentation lists, among others, \d, \h, \w, greedy quantifiers (*, +, ?, {n,m}), lazy (*?, +?, ??) and possessive (*+, ++, ?+) ones, and the character codes \xhh and \x{hh..}. The documentation also mentions look-ahead, look-behind and non-capturing groups.
First occurrence and accumulation
fts generates an alert on the first decoder match for a combination of the indicated fields, provided a rule uses if_fts. An example from the sudo decoder:
<decoder name="sudo-fields">
<parent>sudo</parent>
<prematch>\s</prematch>
<regex>^\s*(\S+)\s*:</regex>
<order>srcuser</order>
<fts>name,srcuser,location</fts>
<ftscomment>First time user executed the sudo command</ftscomment>
</decoder>
accumulate joins lines with the same id: Wazuh groups them by agent, decoder name and id value, and fills the empty fields srcip, dstip, srcport, dstport, srcuser, dstuser and data from earlier lines. Values expire 120 seconds after the last matching line. The option requires a regex that fills id. When one event already spans many lines in the file, see the post on localfile multiline.
Modifying a default decoder
The documentation recommends not editing files in /var/ossec/ruleset/decoders/. The procedure for 0310-ssh_decoders.xml:
cp /var/ossec/ruleset/decoders/0310-ssh_decoders.xml /var/ossec/etc/decoders/0310-ssh_decoders.xml
chown wazuh:wazuh /var/ossec/etc/decoders/0310-ssh_decoders.xml
chmod 660 /var/ossec/etc/decoders/0310-ssh_decoders.xml
Then exclude the original in the server’s ossec.conf (the rest of the block is the layout from the documentation):
<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>
After the changes you edit the copied file and restart the manager. The excluded original gets no updates, so fixes from new versions have to be carried over by hand. Fields from the decoder go on to the rules, including the lists described in CDB in Wazuh.
What the documentation does not specify
The documentation does not specify the order in which decoders without a parent or files in the directory are checked, the order of fields in the wazuh-logtest output, or the maximum length of a regex pattern.
Sources
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/decoders.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/regex.html
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/sibling-decoders.html
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/dynamic-fields.html