PL EN
WB-0269B133TLP:CLEARWazuh 4

Testing rules and decoders with wazuh-logtest

wazuh-logtest in practice: CLI options (-v, -U, -l), phases 1-3 and firedtimes, the PUT /logtest API with session closing, and rule_test defaults.

wazuh-logtest shows how the Wazuh analysis engine processes a given log entry: which decoder recognizes it, which fields it extracts, and which rule generates the alert. The tool shares rules with the analysis engine, so it tests them on a controlled event without waiting for traffic from an agent.

This article covers Wazuh 4.x. For detection in Wazuh 5 (5.0-rc), where Sigma rules replace XML rules, see Wazuh 5: Sigma rules, detectors and findings.

The examples are synthetic.

Three interfaces and sessions

The tool is available in the Wazuh dashboard (Tools > Ruleset test), as the CLI /var/ossec/bin/wazuh-logtest, and through the Wazuh server API. In a cluster the master node processes the events, so run the CLI there. The rule_test configuration applies there as well.

Each session loads its own set of rules and decoders. The tool keeps the firedtimes counter and the event history for the whole session. Frequency-based rules (more in event correlation) can therefore be tested only when consecutive entries go to the same session.

CLI options

OptionEffect
-dDebug mode, diagnostic output to the terminal
-hHelp message
-UReturns a zero exit code when the test result matches the criteria given in the argument; makes sense only for a single log line
-lSets a custom event location
-VWazuh and wazuh-logtest version and license
-qQuiet run
-vVerbose results; the documentation calls this option the most important one when diagnosing a rule or decoder problem

The -U argument has the form rule-id:alert-level:decoder-name. This makes the tool work as an assertion in a script. A call with the sample sshd entry from later in this post could look like this:

# Exit status 0 only if rule 5710 at level 5 and decoder sshd match the single line
echo "Oct 15 21:07:00 agent-01 sshd[29205]: Invalid user example-user from 192.0.2.10 port 48928" \
  | /var/ossec/bin/wazuh-logtest -U 5710:5:sshd
echo "exit: $?"

The documentation does not say whether input from standard input behaves the same as typing lines in an interactive session, so check this on your own version before using it in CI.

Example: three phases and the firedtimes counter

When started without arguments, the tool prints a banner (Starting wazuh-logtest, Type one log per line) and waits for log lines. For the entry:

Oct 15 21:07:00 agent-01 sshd[29205]: Invalid user example-user from 192.0.2.10 port 48928

the output has three phases:

**Phase 1: Completed pre-decoding.
   full event: 'Oct 15 21:07:00 agent-01 sshd[29205]: Invalid user example-user from 192.0.2.10 port 48928'
   timestamp: 'Oct 15 21:07:00'
   hostname: 'agent-01'
   program_name: 'sshd'

**Phase 2: Completed decoding.
   name: 'sshd'
   parent: 'sshd'
   srcip: '192.0.2.10'
   srcport: '48928'
   srcuser: 'example-user'

**Phase 3: Completed filtering (rules).
   id: '5710'
   level: '5'
   description: 'sshd: Attempt to login using a non-existent user'
   groups: '["syslog","sshd","authentication_failed","invalid_login"]'
   firedtimes: '1'
   mail: 'false'
   mitre.id: '["T1110.001","T1021.004","T1078"]'
**Alert to be generated.

The output is shortened to selected phase 3 keys. The full list also contains compliance mappings (gdpr, hipaa, nist_800_53, pci_dss, tsc) and MITRE ATT&CK.

Each phase narrows down where the error is:

PhaseQuestionWhat to check
1, Completed pre-decoding.Was the header recognizedtimestamp, hostname, program_name match the entry format
2, Completed decoding.Did the decoder extract the fields the rule needsname, parent and fields such as srcip, srcport, srcuser
3, Completed filtering (rules).Which rule matchedid, level, description, groups, firedtimes, frequency

If phase 2 lacks a field the rule requires, fix the decoder (custom decoders) before changing the rule condition. If the fields are correct and the expected rule does not appear, compare the level and groups of the matched rule and the file load order described in ruleset loading. The text Alert to be generated. ends the output when the processing is going to produce an alert.

Repeating an entry in one session

Pasting the same entry six more times matches rule 5710 each time, and firedtimes grows. On the next paste, the eighth, rule 5712 matches:

**Phase 3: Completed filtering (rules).
   id: '5712'
   level: '10'
   description: 'sshd: brute force trying to get access to the system. Non existent user.'
   groups: '["syslog","sshd","authentication_failures"]'
   firedtimes: '1'
   frequency: '8'
   mitre.id: '["T1110"]'
**Alert to be generated.

In the built-in rules from version 4.14.0, rule 5712 has frequency="8", timeframe="120" and ignore="60", and requires if_matched_sid 5710 and same_source_ip. That means eight matches of 5710 from the same source address within two minutes, so you have to send the repeats quickly. Thresholds and levels are covered in the post on rule levels. In the output, the firedtimes of rule 5712 is 1.

API: PUT /logtest

Calls require a JWT token. You get it through POST /security/user/authenticate?raw=true:

# Obtain a JWT for the Wazuh server API
TOKEN=$(curl -u <WAZUH_API_USER>:<PASSWORD> -k -X POST "https://localhost:55000/security/user/authenticate?raw=true")

Both endpoints require the logtest:run action in RBAC. The specification allows the query parameters pretty and wait_for_complete.

Request and fields

FieldRequiredDescription
eventyesLog to process
log_formatyesLog type
locationyesLog origin, for example master->/var/log/syslog
tokennoSession identifier; keeps the event history and the loaded rules and decoders

Allowed log_format values according to the API specification: syslog, json, snort-full, squid, eventlog, eventchannel, audit, mysql_log, postgresql_log, nmapg, iis, command, full_command, djb-multilog, multi-line.

The first request has no token, so it starts a new session:

LOGTEST_REQ='{
  "event": "Oct 15 21:07:00 agent-01 sshd[29205]: Invalid user example-user from 192.0.2.10 port 48928",
  "log_format": "syslog",
  "location": "master->/var/log/syslog"
}'

curl -k -X PUT "https://localhost:55000/logtest?pretty=true" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d "$LOGTEST_REQ"

Response (shortened, without compliance mappings):

{
  "error": 0,
  "data": {
    "messages": [
      "INFO: (7202): Session initialized with token '35604a22'"
    ],
    "token": "35604a22",
    "output": {
      "timestamp": "2023-04-25T13:50:43.764000Z",
      "rule": {
        "level": 5,
        "description": "sshd: Attempt to login using a non-existent user",
        "id": "5710",
        "firedtimes": 1,
        "mail": false,
        "groups": ["syslog", "sshd", "authentication_failed", "invalid_login"]
      },
      "agent": { "id": "000", "name": "example-manager" },
      "manager": { "name": "example-manager" },
      "id": "1682430643.3725",
      "full_log": "Oct 15 21:07:00 agent-01 sshd[29205]: Invalid user example-user from 192.0.2.10 port 48928",
      "predecoder": {
        "program_name": "sshd",
        "timestamp": "Oct 15 21:07:00",
        "hostname": "agent-01"
      },
      "decoder": { "parent": "sshd", "name": "sshd" },
      "data": {
        "srcip": "192.0.2.10",
        "srcport": "48928",
        "srcuser": "example-user"
      },
      "location": "master->/var/log/syslog"
    },
    "alert": true,
    "codemsg": 0
  }
}

The response has three levels:

PathFields
Top levelerror, data
datamessages, token, output, alert, codemsg
data.outputtimestamp, rule, agent, manager, id, full_log, predecoder, decoder, data, location

The data.output.data field holds the fields extracted by the decoder and is not the same thing as the parent data. The alert field says whether the event will generate an alert. In the specification example where the decoder did not match the entry ERROR, the response has "alert": false and "codemsg": 1.

Following requests and closing the session

The token from messages, added to the next requests, keeps the session. A missing or invalid token starts a new session and reloads the rules and decoders, which when testing thresholds means a reset history. In the eighth request with the same token, the response points to rule 5712, has rule.frequency equal to 8, and the additional previous_output field holds the seven earlier lines of the event.

# Replace the token with the one returned for your session
curl -k -X DELETE "https://localhost:55000/logtest/sessions/35604a22?pretty=true" \
  -H "Authorization: Bearer $TOKEN"
{
  "error": 0,
  "data": {
    "messages": [
      "INFO: (7206): The session '35604a22' was closed successfully"
    ],
    "codemsg": 0
  }
}

Closing releases the event history and the loaded rules and decoders. According to the specification, a 200 response also covers the case where no session with the given token was found. The documented error responses of both endpoints are 400, 401, 403, 405, 406 (wrong content type), 413 and 429.

The rule_test configuration

The tester configuration is in /var/ossec/etc/ossec.conf in the rule_test section and applies to both the API and the CLI. Default values:

<rule_test>
  <enabled>yes</enabled>
  <threads>1</threads>
  <max_sessions>64</max_sessions>
  <session_timeout>15m</session_timeout>
</rule_test>
ElementValuesDefaultBehavior
enabledyes, noyesEnables and disables the module
threads1 to 128 or auto1auto creates one thread per CPU
max_sessions1 to 50064After the limit is reached, opening a new session closes the longest idle one
session_timeouta number with the suffix s, m or h, at most 365d15mA session idle for longer than the limit is closed together with its resources

Because the session limit closes the longest idle session, a max_sessions that is too low with several people testing at once can silently interrupt someone else’s threshold test. Treat as safe the values where the number of concurrent sessions fits within the limit with a margin. This is a recommendation, not a value from the documentation. A shorter session_timeout frees resources but resets the session history before a series of repeats ends.

Verifying a new rule step by step

  1. Keep the input entry and the expected fields as a reference to compare against after each change.
  2. Run a single event with -v and check phases 1 and 2 before you touch the rule.
  3. For a rule with frequency, send a series in one session and watch firedtimes and the moment the parent rule appears.
  4. Close the session with DELETE so the next test starts with a clean history.
  5. When the result in the tool is correct and the alert does not appear in production, the cause lies outside the rules. The event may not have reached the analysis, which the post on analysisd drops covers.

When comparing two responses, keep the three input fields (log_format, event, location) apart. The response repeats location in output, and full_log, decoder and data describe the processing result. This lets you tell a change in the log text from a change in the decoder or rule result.

What the documentation does not specify

  • The -U exit code on a mismatch, the exact output of -v, -q and -d.
  • The meaning of codemsg values beyond the examples 0 and 1, and the content of the messages in the error field.
  • The previous_output format beyond the example, and the detailed predecoder structure in the API response.
  • The exact dashboard layout and how -l behaves relative to the location field in the API.
  • The CLI examples in the documentation show the version 4.12.0 banner, and the tests of 5710 and 5712 are described on the general page, so check the result on your own version.

Sources

↑↓ select Enter open Esc close