Wazuh 4 training, part 5: decoders
Wazuh 4 training part 5: decoder options, a custom decoder, sibling decoders, the dynamic JSON decoder with Suricata logs and changing a default decoder.
The fifth part of the Wazuh 4 training enters the second stage of event analysis from part 4: decoding. There I read ready-made decoders, here I write my own. The exercises cover a decoder with a parent and a child, sibling decoders for logs with a variable structure and the dynamic JSON decoder, using Suricata events as the example. The description applies to Wazuh 4.14. A detailed syntax description is also in a separate post on custom decoders, and the testing tool in the post on wazuh-logtest.
What a decoder does
Decoders extract information from a received event and split it into blocks that rules work on later. The documentation shows this on an sudo log:
Sep 08 11:25:52 agent-01 sudo[4543]: pam_unix(sudo-i:session): session opened for user root(uid=0) by example-user(uid=1000)
According to the documentation example, after pasting it into /var/ossec/bin/wazuh-logtest the result has two phases. Phase 1 (pre-decoding) extracts the timestamp Sep 08 11:25:52, the host name agent-01 and the program name sudo from the syslog-style header. Phase 2 (decoding) analyzes the rest of the message, pam_unix(sudo-i:session): session opened for user root(uid=0) by example-user(uid=1000). The pam decoder extracts dstuser: 'root(uid=0)', srcuser: 'example-user' and uid: '1000' here, and the result shows name: 'pam' and parent: 'pam'.
The documentation recommends always running logtest on a sample log before writing your own decoder, to see what the loaded decoders already extract. Sometimes a ready-made decoder is enough or needs only a small fix.
Decoder options
A decoder is a <decoder name="..."> element with a set of sub-elements. The full list from the documentation:
| Option | Values | Description |
|---|---|---|
decoder | decoder name | defines the decoder |
parent | any decoder’s name | references a parent, the current decoder becomes a child |
accumulate | none | tracking events spread over several messages |
program_name | regex, sregex or pcre2 | condition: the log header must contain a matching program name |
prematch | regex or pcre2 | condition: the log (without the syslog header) must match the expression |
regex | regex or pcre2 | expression that extracts fields |
order | field names | names for the groups from the regex expression |
fts | list of fields | “first time seen”, an alert on the first match |
ftscomment | any text | comment for fts |
plugin_decoder | plugin name | decoding by a plugin when regex is too weak |
use_own_name | true | children only, the child’s name comes from its own name attribute |
json_null_field | string or discard | how null values from JSON are stored |
json_array_structure | array or csv | how arrays from JSON are stored |
var | variable name | variable for use in the same file |
type | see below | type of log the decoder should match |
parent and the hierarchy
parent links a subordinate decoder to its parent. A parent can have many children, but a child cannot be a parent: decoders, unlike rules, have no grandchildren. A child decoder fires only if its parent matched earlier.
The engine checks decoders without a parent one by one. After the first match it stops going through the other top-level decoders and examines only the children of that decoder. That is why the parent’s matching conditions must not be too general: an overly broad parent captures logs that should go to other decoders.
program_name
program_name sets a condition for the program name from the header. It accepts regex, sregex or pcre2, and the type attribute selects the expression type:
| Attribute | Values | Default |
|---|---|---|
type | osmatch, osregex, pcre2 | osmatch |
When the program_name label appears several times in a decoder, the value is their concatenation and the type comes from the last label. The documentation example matches test, TEST and every letter case variant:
<decoder name="test_decoder">
<program_name type="pcre2">(?i)test</program_name>
</decoder>
prematch
prematch is a condition the log must meet. For a syslog-style log the part after the header is analyzed, for any other log the whole text. The documentation advises making the expression as precise as possible. Attributes:
| Attribute | Values | Default |
|---|---|---|
offset | after_regex, after_parent | none |
type | osregex, pcre2 | osregex |
Several prematch labels in one decoder combine the same way as program_name.
regex and order
regex extracts fields, but only those written in parentheses. Using it requires the order label in the same decoder. It also requires prematch or program_name in that decoder or in its parent. Attributes:
| Attribute | Values | Default |
|---|---|---|
offset | after_regex, after_parent, after_prematch | none |
type | osregex, pcre2 | osregex |
order names the consecutive groups from the parentheses. The static fields Wazuh knows in advance:
| Field | Meaning |
|---|---|
srcuser | source user |
dstuser | destination user |
user | alias of dstuser (only one of the two can be used) |
srcip | source address |
dstip | destination address |
srcport | source port |
dstport | destination port |
protocol | protocol |
system_name | system name |
id | event ID |
url | event URL |
action | action (deny, drop, accept and so on) |
status | status (success, failure and so on) |
data | data |
extra_data | extra data |
Any other string is a dynamic field. Dynamic fields land in the alert under their own name and rules can refer to them. The JSON decoder in exercise 3 relies on this.
fts, plugin_decoder, use_own_name, var and type
fts marks a decoder that should trigger an alert on its first match. Allowed values are location and all the static fields from the table above. The option works when the decoded event triggers a rule with if_fts. ftscomment adds a comment to it:
<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>
plugin_decoder hands decoding over to a plugin. Allowed values are PF_Decoder, SymantecWS_Decoder, SonicWall_Decoder, OSSECAlert_Decoder and JSON_Decoder. The optional offset attribute (after_parent or after_prematch) moves the start of decoding. The JSON_Decoder plugin has its own section in exercise 3.
use_own_name accepts only the value true and works only in subordinate decoders: the child’s name then comes from its name attribute, not from the parent’s name.
var defines a variable that I use throughout the file as $name. The variable must stand at the beginning of the file, outside any <decoder> element:
<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>
type sets the log kind: syslog (default), firewall, ids, web-log, squid, windows, host-information or ossec.
Two options apply only to the JSON decoder. json_null_field has the values string (default, stores null as text) and discard (skips null fields). json_array_structure has the values array (default, JSON arrays) and csv (arrays as comma-separated text).
Expression syntax
Decoders use three kinds of regular expressions: regex (OS_Regex), sregex (OS_Match) and PCRE2. The default type in prematch and regex is osregex, and in program_name it is osmatch.
OS_Regex is a fast, simple library in C. It supports these character classes:
| Expression | Meaning |
|---|---|
\w | A-Z, a-z, 0-9 and -, @, _ |
\d | digits 0-9 |
\s | space |
\t | tab |
\p | the characters ()*+,-.:;<=>?[]!"'#$%&|{} |
\W, \D, \S | negations of \w, \d, \s |
\. | any character |
The modifiers are + (one or more) and * (zero or more). The special characters are ^ (start of text), $ (end of text) and | (alternation). I precede the characters $, (, ), \, | and < with a backslash. The limitations described in the documentation decide how I write expressions:
*and+work only on backslash expressions, so\d+is valid and0+is not.- Alternation inside a group, for example
(foo|bar), is not allowed. - Complex backtracking does not work:
\p*\d*\s*\w*:does not match a lone colon, because\p*consumes the colon. - The dot
.means a literal dot, and\.means any character. \smeans only the ASCII space, not other whitespace.- There is no syntax for a literal caret, asterisk or plus (
\pmatches the asterisk and the plus along with other characters).
sregex is faster, but supports only simple string matching and the characters ^, $, | and ! (negation).
PCRE2 is case-sensitive, and (?i) turns on case-insensitive matching. The Wazuh XML parser treats < inside a value as the start of a new element and does not decode entities. In PCRE2 I write < as \x3C and > as \x3E:
<regex type="pcre2">from=\x3C(.*?@(.*?))\x3E</regex>
This pattern extracts the address and the domain from the fragment from=<[email protected]>.
Where to store a custom decoder
Custom decoders go to /var/ossec/etc/decoders/local_decoder.xml. For larger changes the documentation recommends separate files in the /var/ossec/etc/decoders/ directory. The /var/ossec/ruleset/decoders/ directory belongs to Wazuh and changes there disappear on upgrade, as I wrote in part 4.
Testing works without a restart: wazuh-logtest sees the new files once they are saved. The manager, however, has to be restarted to generate alerts that use the changes:
systemctl restart wazuh-manager
Exercise 1: a custom decoder
The task: write a decoder that extracts the login result, the user and the source address from an application log. A sample log from the exampleapp program:
Dec 4 17:07:01 agent-01 exampleapp: Accepted password for user example-user, IP: 203.0.113.10
Pre-decoding extracts timestamp, hostname (agent-01) and program_name (exampleapp) from it. I append a parent and a child to /var/ossec/etc/decoders/local_decoder.xml:
<decoder name="exampleapp-login">
<program_name>exampleapp</program_name>
</decoder>
<decoder name="exampleapp-login">
<parent>exampleapp-login</parent>
<prematch> password for user </prematch>
<regex>^(\w+) password for user (\w+), IP: (\S+)</regex>
<order>result, user, srcip</order>
</decoder>
The parent catches every event whose program name is exampleapp. The child matches when the content contains password for user. The regex expression has three groups in parentheses and order names them result, user and srcip in turn. The result field is dynamic, user is an alias of dstuser and srcip is a static field. In OS_Regex \w also covers the hyphen, so example-user matches (\w+). The documentation shows an analogous case: for a decoder with <order>user, srcip</order>, logtest prints dstuser.
After saving the file I paste the log into wazuh-logtest. Expected result: in phase 2 the decoder name exampleapp-login and the fields result: 'Accepted', dstuser: 'example-user' and srcip: '203.0.113.10'. No rule is based on this decoder yet, so phase 3 matches nothing. If phase 2 is empty, I check in turn: the program name in the header, the prematch condition and whether the regex expression matches the text after the header.
The example in the documentation has the same shape. For the log Dec 25 20:45:02 agent-01 example[12345]: User 'admin' logged from '192.0.2.100' the parent has <program_name>^example</program_name>, and the child has <regex>User '(\w+)' logged from '(\d+.\d+.\d+.\d+)'</regex> with the order user, srcip. I describe the rules built on such a decoder in part 6.
Exercise 2: sibling decoders
Logs often reorder fields or omit some. One long regex expression then has to match all variants at once. The documentation shows this with an exampleapp application and two messages. First, a simple decoder with a single expression:
<decoder name="exampleapp">
<program_name>exampleapp</program_name>
<regex>(\w+): srcuser="(\.+)" action="(\.+)" dstusr="(\.+)"</regex>
<order>type,srcuser,action,dstuser</order>
</decoder>
The first log:
Apr 12 14:31:38 agent-01 exampleapp: INFO: srcuser="example-user" action="called" dstusr="example-admin"
gives a full set of fields: type: 'INFO', srcuser, action and dstuser. The second log, where the fields come in a different order and dstusr is missing:
Apr 01 19:21:24 agent-01 exampleapp: INFO: action="logged on" srcuser="example-user"
gives no fields, because the expression requires all four elements in a fixed order.
The solution is sibling decoders: several decoders with the same name, each having the same base decoder as its parent and extracting one piece of information. All of them sit at the same level and work independently of each other:
<decoder name="exampleapp">
<program_name>exampleapp</program_name>
</decoder>
<decoder name="exampleapp">
<parent>exampleapp</parent>
<regex>^(\w+):</regex>
<order>type</order>
</decoder>
<decoder name="exampleapp">
<parent>exampleapp</parent>
<regex>srcuser="(\.+)"</regex>
<order>srcuser</order>
</decoder>
<decoder name="exampleapp">
<parent>exampleapp</parent>
<regex>action="(\.+)"</regex>
<order>action</order>
</decoder>
<decoder name="exampleapp">
<parent>exampleapp</parent>
<regex>dstusr="(\.+)"</regex>
<order>dstuser</order>
</decoder>
The documentation explains the mechanism: when one of these decoders matches, the engine also checks the others, and each extracts one field. The order of fields in the log, extra fields and missing fields stop mattering. Both logs now produce a result. The first returns type, srcuser, action and dstuser, the second type: 'INFO', action: 'logged on' and srcuser, without dstuser.
Siblings have one more advantage: a long pattern splits into short pieces, so the decoder is easier to read and fix.
Event accumulation
Some applications write one event over several lines, of which only some contain the address or the user. The accumulate option tracks such events by the decoded id field. It requires an expression that fills id. Wazuh groups lines by agent, decoder name and id value, and then fills the empty fields of the current event with data from earlier lines. It fills only srcip, dstip, srcport, dstport, srcuser, dstuser and data. The collected values expire 120 seconds after the last matching line.
A ready-made example is OpenLDAP from the file /var/ossec/ruleset/decoders/0185-openldap_decoders.xml. The slapd daemon writes one connection over many lines with the same conn= number:
Jan 11 09:26:57 agent-01 slapd[20872]: conn=999999 fd=64 ACCEPT from IP=198.51.100.27:33957 (IP=203.0.113.77:389)
Jan 11 09:26:57 agent-01 slapd[20872]: conn=999999 op=0 BIND dn="uid=example,ou=People,dc=example,dc=com" method=128
Jan 11 09:26:57 agent-01 slapd[20872]: conn=999999 op=0 RESULT tag=97 err=49 text=
Each decoder from this file extracts the connection number as id and has <accumulate/>:
<decoder name="openldap">
<program_name>^slapd</program_name>
<accumulate/>
</decoder>
<decoder name="openldap-connect_ipv4">
<parent>openldap</parent>
<prematch>ACCEPT from IP=</prematch>
<regex>^conn=(\d+) fd=\d+ ACCEPT from IP=(\S+):\d+ \(IP=(\S+):</regex>
<order>id, srcip, dstip</order>
<accumulate/>
</decoder>
<decoder name="openldap-bind">
<parent>openldap</parent>
<prematch>BIND </prematch>
<regex>^conn=(\d+) op=\d+ BIND dn="\w+=(\S+),</regex>
<order>id, dstuser</order>
<accumulate/>
</decoder>
<decoder name="openldap-result">
<accumulate/>
<parent>openldap</parent>
<prematch> RESULT </prematch>
<regex>^conn=(\d+) op=\d+ RESULT </regex>
<order>id</order>
</decoder>
The RESULT line contains only the connection number. Because the earlier lines have the same id, Wazuh fills its empty fields, so the decoded event of the failed login (err=49) carries srcip 198.51.100.27, dstip 203.0.113.77 and dstuser example. A rule on such an event can say who tried to log in and from where.
Exercise 3: the dynamic JSON decoder
Wazuh has a built-in decoder for logs in JSON format, which extracts data from any source in that format. Suricata events in the eve.json file are good material, because the NIDS writes one JSON event per line. Installing Suricata is not the topic of this part. Any source that appends one JSON event per line to a file is enough for me. Suricata alerts from OPNsense sent over syslog to Wazuh 5 are covered in the post on Suricata from OPNsense.
Collecting JSON logs
On the agent I add a localfile block with the json format to ossec.conf, which is meant for JSON files with one event per line:
<localfile>
<log_format>json</log_format>
<location>/var/log/suricata/eve.json</location>
</localfile>
The label option adds custom data to JSON events, as long as log_format is json. Keys are nested with a period, for example agent.type. The example from the documentation:
<localfile>
<location>/var/log/myapp/log.json</location>
<log_format>json</log_format>
<label key="@source">myapp</label>
<label key="agent.type">webserver</label>
</localfile>
The built-in decoder
The json decoder lives in the file /var/ossec/ruleset/decoders/0006-json_decoders.xml (tag v4.14.0), together with the json-msgraph decoder, which discards null fields:
<decoder name="json-msgraph">
<prematch>"integration":"ms-graph"</prematch>
<plugin_decoder>JSON_Decoder</plugin_decoder>
<json_null_field>discard</json_null_field>
</decoder>
<decoder name="json">
<prematch>^{\s*"</prematch>
<plugin_decoder>JSON_Decoder</plugin_decoder>
</decoder>
The json decoder matches every log that starts with {, optional whitespace and a quote. The JSON_Decoder plugin handles the rest. It extracts numbers, strings, booleans, null values, arrays and objects. An array of objects is not supported. The extracted fields become dynamic fields, so there is no need to write a separate decoder for each source. The json-msgraph decoder handles events from the ms-graph module, covered in the post on Microsoft Graph and Defender XDR.
A Suricata alert event on one line, because wazuh-logtest processes one log line at a time:
{"timestamp":"2026-12-04T10:15:30.000000+0000","flow_id":1234,"in_iface":"eth0","event_type":"alert","src_ip":"198.51.100.7","src_port":5555,"dest_ip":"203.0.113.10","dest_port":80,"proto":"TCP","alert":{"action":"allowed","gid":1,"signature_id":2019236,"rev":3,"signature":"ET WEB_SERVER Possible CVE-2014-6271 Attempt in HTTP Version Number","category":"Attempted Administrator Privilege Gain","severity":1},"payload":"abcde","payload_printable":"example","stream":0,"host":"example.com"}
According to the documentation example, after pasting it into logtest phase 1 is empty and phase 2 prints name: 'json' and every field separately. Nested objects get names with periods: alert.action, alert.category, alert.gid, alert.rev, alert.severity, alert.signature and alert.signature_id. The remaining fields keep the names from the original: dest_ip, dest_port, event_type, flow_id, host, in_iface, payload, payload_printable, proto, src_ip, src_port, stream and timestamp. Numbers are stored as text, for example src_port: '5555'.
To picture this flattening, jq helps, which is not part of Wazuh. The command below, my recommendation, prints the field names in the form a rule will see them:
# Print the dotted field names of every alert event in a JSON lines file
jq -c 'select(.event_type=="alert") | [paths(scalars) | join(".")]' /var/log/suricata/eve.json
Rules 86600 and 86601
Decoding ends when a rule matches the fields. Suricata events are handled by the file /var/ossec/ruleset/rules/0475-suricata_rules.xml (tag v4.14.0) with the ID range 86600-86699 and the groups ids and suricata:
<rule id="86600" level="0">
<decoded_as>json</decoded_as>
<field name="timestamp">\.+</field>
<field name="event_type">\.+</field>
<description>Suricata messages.</description>
<options>no_full_log</options>
</rule>
<rule id="86601" level="3">
<if_sid>86600</if_sid>
<field name="event_type">^alert$</field>
<description>Suricata: Alert - $(alert.signature)</description>
<options>no_full_log</options>
</rule>
Rule 86600 has level 0 and catches everything the json decoder decoded that has the fields timestamp and event_type. Rule 86601 checks the dynamic field event_type and creates a level 3 alert for the value alert. The description contains $(alert.signature): Wazuh substitutes the value of the dynamic field there. For the log above, phase 3 in logtest prints rule 86601 with level 3 and the description Suricata: Alert - ET WEB_SERVER Possible CVE-2014-6271 Attempt in HTTP Version Number, the groups ids and suricata, and firedtimes: '1'. The http and dns events have separate rules 86602 and 86603 with level 0, so they create no alerts on their own.
Offset: JSON embedded in text
Not every log is pure JSON. The offset="after_prematch" attribute in plugin_decoder cuts off the start of the line and hands only the JSON to the plugin. The documentation shows this on a log with a prefix:
2018 Apr 04 13:11:52 examplejson: this_is_an_example: " player_information: "{ "name": "example-user", "surname": "example", "team": "example-team", "number": 30, "position": "example-position"}
The decoder looks for the text player_information: " and decodes JSON from that point:
<decoder name="raw_json">
<program_name>examplejson</program_name>
<prematch>player_information: "</prematch>
<plugin_decoder offset="after_prematch">JSON_Decoder</plugin_decoder>
</decoder>
In the documentation example logtest shows name, surname, team, number and position as dynamic fields. The plugin ignores data after a valid JSON object, so any characters after the closing brace do no harm.
Plugin together with regex
Subordinate decoders of one parent can mix plugins and expressions. The log:
2018 Jun 08 13:11:52 exampledb: json_data: { "name": "example-user", "surname": "example", "email": "[email protected]"}
is handled by three decoders: a parent by program name, a child with JSON_Decoder and a second child with an expression that extracts the domain of the address:
<decoder name="json_parent">
<program_name>exampledb</program_name>
</decoder>
<decoder name="json_child">
<parent>json_parent</parent>
<prematch>json_data: </prematch>
<plugin_decoder offset="after_prematch">JSON_Decoder</plugin_decoder>
</decoder>
<decoder name="json_child">
<parent>json_parent</parent>
<regex>@(\S+)"</regex>
<order>email_domain</order>
</decoder>
In the documentation example phase 2 contains the fields from the plugin (email, name, surname) and the email_domain field from the expression, that is example.com. This is the same principle as with sibling decoders: the parent has several children and each extracts its own part.
Changing a decoder shipped with Wazuh
A default decoder can process an unusual log format badly. Editing the file in /var/ossec/ruleset/decoders/ is out, because an upgrade overwrites it. The documentation describes another way: copy the file to /var/ossec/etc/decoders/ and exclude the original from loading. The example is for /var/ossec/ruleset/decoders/0310-ssh_decoders.xml, the same file I read in part 4:
- I copy the file to
/var/ossec/etc/decoders/. - I set the owner and permissions of the copy so the manager can read it:
chown wazuh:wazuh /var/ossec/etc/decoders/0310-ssh_decoders.xml
chmod 660 /var/ossec/etc/decoders/0310-ssh_decoders.xml
- In
/var/ossec/etc/ossec.confI add<decoder_exclude>, which removes the original from the loading list:
<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>
- I edit the copy and restart the manager:
systemctl restart wazuh-manager
The documentation warns about the cost: after excluding the original I no longer get its updates. My file stays untouched on upgrade, so I have to carry over important changes from newer versions by hand. The rules for loading rules and decoders are described in the post on loading rules.
Checklist after part 5
- I know how pre-decoding differs from decoding and what each phase extracts.
- I know the decoder options:
parent,program_name,prematch,regex,order,fts,plugin_decoder,use_own_name,var,type. - I tell
regex,sregexand PCRE2 apart and know the limitations of OS_Regex. - My custom decoder
exampleapp-loginsaved in/var/ossec/etc/decoders/local_decoder.xmlreturns the fieldsresult,dstuserandsrcipin logtest. - The sibling decoders for
exampleappdecode both logs, including the one with different fields. - A Suricata JSON event passes through the
jsondecoder and triggers rule 86601 with a description containing the signature. - I know how to exclude a decoder shipped with Wazuh using
<decoder_exclude>and what that costs on upgrades.
In part 6 I write rules that work on the fields from decoders: levels, groups, custom rules and modifying a built-in rule.
What the documentation does not define
- The order in which the engine tries several top-level decoders that match the same program name. The documentation says only that it checks them sequentially.
- The range of characters for a
program_nameof typeosmatch: the list of special characters covers^,$,|and!, and the page says nothing about a hyphen in a program name. - A limit on the number of sibling decoders and their performance cost.
- The behavior of
ftsbeyond the statement that it triggers an alert on the first match. - The load order of files from
/var/ossec/etc/decoders/.
Sources
- https://documentation.wazuh.com/4.14/user-manual/ruleset/ruleset-xml-syntax/decoders.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/ruleset-xml-syntax/regex.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/decoders/json-decoder.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/decoders/custom.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/decoders/sibling-decoders.html
- https://documentation.wazuh.com/4.14/user-manual/reference/ossec-conf/localfile.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/decoders/0006-json_decoders.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0475-suricata_rules.xml