What analysisd -t and wazuh-logtest do not catch: gaps in Wazuh 4.14 rule validation in CI
What wazuh-analysisd -t checks in Wazuh 4.14 and what it only warns about: duplicate IDs, a missing if_sid, rule level. A bash script with xmllint for CI.
In a CI pipeline for Wazuh rules you can stop at wazuh-analysisd -t and a wazuh-logtest test, but the 4.14.11 code shows a gap. A duplicated ID and an if_sid pointing to a nonexistent rule end in -t only with a warning and exit code 0. A script of your own has to catch those before -t runs. Parallel requests will not speed up logtest through the API either, because they queue on both sides.
Behavior described as “in the 4.14.11 code” comes from reading the public wazuh/wazuh sources at tag v4.14.11, not from the documentation and not from a test on a live manager, so it may change in a newer version. Values in the examples are synthetic: rule IDs from 100000, names starting with example.
What ends analysisd -t with an error and what with a warning
The -t option in the wazuh-analysisd documentation is described as “Test configuration.” and nothing more. In the 4.14.11 code the main_analysisd function loads decoders, lists and rules, and after loading them, when test_config is set, calls exit(0). So the outcome depends on whether Rules_OP_ReadRules returned a negative value. In that case the process ends through merror_exit with the message (1220): Error loading the rules, and merror_exit calls exit(1) (src/shared/debug_op.c). Messages collected as warnings go only to mwarn. With -t analysisd does not switch to daemon mode, so mwarn and merror also print to standard error, next to the entry in ossec.log (src/shared/debug_op.c). A pipeline can grep stderr for codes 7612 and 7617–7620.
| Situation | Function in the 4.14.11 code | Result |
|---|---|---|
| Level outside the range 0-16 | getattributes | Error, analysisd exits |
| ID longer than 6 digits or non-numeric | getattributes | Error, analysisd exits |
Missing level or id in a rule | Rules_OP_ReadRules | Error, analysisd exits |
| Two rules with one ID | Rules_OP_ReadRules | Warning (7612), second rule skipped |
if_sid with characters other than digits, commas and spaces | Rules_OP_ReadRules | Warning (7618), rule skipped |
if_sid points to a nonexistent ID | OS_AddChild | Warning (7617), rule skipped when no parent was found |
if_matched_sid points to a nonexistent ID | OS_AddChild | Warning (7620), rule skipped |
The level range matches the rule syntax documentation (“0 to 16”). The aborted startup outside the range, duplicate IDs and missing parents come from the code.
Duplicates and a missing parent
On a duplicate, Rules_OP_ReadRules checks doesRuleExist(id). If the rule exists and the overwrite attribute is not yes, it logs Rule ID '%d' is duplicated. Only the first occurrence will be considered. and moves on to the next rule. So the first loaded definition wins. A rule with a taken ID in a later file looks deployed in the repository but never fires. The file order is covered in the post on rule loading.
On a missing parent, OS_AddChild splits if_sid into single IDs and calls _AddtoRule for each. Every ID not found gives the warning Signature ID '%d' was not found and will be ignored in the 'if_sid' option of rule '%d'. When no parent exists, the rule also gets the message Empty 'if_sid' value. Rule '%d' will be ignored., the function returns -1, and the caller removes the rule and continues. An if_sid list with one valid and one missing ID loads with the valid parent. For if_matched_sid, a missing parent always means the rule is skipped.
The same function shows that the parent is looked up in the tree built so far. Rules_OP_ReadRules calls OS_AddChild right after it reads each rule and makes no second pass, so a child rule loaded before its parent does not find it and is skipped (that comes from reading the code, not from a test).
The documentation adds a restriction that the validation misses: level 0 rules are discarded right away and do not work with if_matched_sid. The level must be at least 1, and hiding an alert requires <no_log>. At load time rules.c does not check the level of the rule an if_matched_sid points to, so -t will not report such a rule (that comes from reading the code, not from a test).
Logtest through the API processes requests one by one
In wazuh_apid.py, the function that starts the API creates a ProcessPoolExecutor(max_workers=1) as process_pool, a second such pool as events_pool, and, when the process has no access to /dev/shm, a ThreadPoolExecutor(max_workers=1) as thread_pool. In DistributedAPI, a local request goes to thread_pool if it exists. Authorization functions go to the authentication pool, send_event_to_analysisd goes to events_pool, and in all other cases the request goes to process_pool through loop.run_in_executor. run_logtest and end_logtest_session are in none of these groups, so they go through the single-process pool that they share with the rest of the local API requests.
In CI, parallel PUT /logtest requests queue up and do not speed up the run. If someone else uses the API of that manager meanwhile, their requests wait in the same queue. With a large number of fixtures, later requests may exceed the API timeout, 10 seconds by default (intervals.request_timeout), which ends in error 3021; the wait_for_complete=true parameter disables that limit. On the analysisd side, logtest sessions run on the threads set in <rule_test><threads>, but the default is one thread (LOGTEST_THREAD), the maximum is 128, and auto means the number of CPUs. Without a configuration change the queue is single-threaded on both sides. The remaining elements of testing through the API are covered in the posts on wazuh-logtest and on deploying rules through the API.
# One fixture per request, run sequentially; wait_for_complete removes the API timeout
curl -sk -X PUT "$API/logtest?wait_for_complete=true" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"event":"example login failed user=example-user","log_format":"syslog","location":"example-fixture"}'
The expected response is JSON with a data.output field, in which rule.id appears if a rule fired. The -k in curl -sk turns off verification of the API certificate. By default the API generates a self-signed server.crt in /var/ossec/api/configuration/ssl/, so on a CI runner pass that file with --cacert instead of -k; with your own API certificate, pass the CA that issued it.
EventChannel events in logtest
The API accepts a log_format with one of the values listed in the specification, including eventchannel, but in logtest.c the field is only checked as a non-empty string (w_logtest_check_input_request). The w_logtest_preprocessing_phase function builds the message in the form 1:<location>:<event>, and it first serializes a JSON object in the event field with cJSON_PrintUnformatted. So the event goes through OS_CleanMSG the same way for every log_format.
Logtest processes a single string from the request and does not reproduce the EventChannel path of a Windows agent. The code does not settle whether a Windows event stored as JSON decodes identically to the one the agent sends. Treat the logtest result for Windows rules as an indication and confirm it on a real event from a test agent.
Custom checks in CI
What follows is my proposal, not a vendor procedure. The built-in tools stay in the pipeline (xmllint, analysisd -t, logtest with an assertion on rule.id), and a script runs before them to catch the cases that end only with a warning in analysisd. xmllint --xpath extracts the fields from the files. Rule files may have several top-level groups, so the script wraps them in a root element.
#!/usr/bin/env bash
# extract.sh: print one TSV line per rule (file, id, level, if_sid, if_matched_sid)
set -euo pipefail
for f in "$@"; do
doc=$(mktemp)
{ echo '<root>'; cat "$f"; echo '</root>'; } > "$doc"
xmllint --noout "$doc" || { echo "XML_ERROR $f" >&2; rm -f "$doc"; exit 2; }
n=$(xmllint --xpath 'count(//rule)' "$doc")
for ((i = 1; i <= n; i++)); do
printf '%s\t%s\t%s\t%s\t%s\n' "$(basename "$f")" \
"$(xmllint --xpath "string(//rule[$i]/@id)" "$doc")" \
"$(xmllint --xpath "string(//rule[$i]/@level)" "$doc")" \
"$(xmllint --xpath "string(//rule[$i]/if_sid)" "$doc")" \
"$(xmllint --xpath "string(//rule[$i]/if_matched_sid)" "$doc")"
done
rm -f "$doc"
done
The second script reads this output and checks the rules. The file order in the call corresponds to the load order, and the known_ids.txt file holds the IDs of rules from the base set that may be referenced (here only 5700).
#!/usr/bin/env bash
# check-rules.sh known_ids.txt file.xml... exit 1 when any check fails
set -euo pipefail
export LC_ALL=C
known=$1; shift
tsv=$(mktemp); trap 'rm -f "$tsv"' EXIT
"$(dirname "$0")/extract.sh" "$@" > "$tsv"
awk -F'\t' -v known="$known" '
BEGIN { while ((getline l < known) > 0) ext[l] = 1 }
{ rows++; file[rows] = $1; id[rows] = $2; lvl[rows] = $3; sid[rows] = $4; msid[rows] = $5
if ($2 in first) dup[rows] = 1; else { first[$2] = $1; pos[$2] = rows; lv[$2] = $3 } }
END {
bad = 0
for (r = 1; r <= rows; r++) {
if (dup[r]) { printf "DUPLICATE_ID %s id=%s (first in %s)\n", file[r], id[r], first[id[r]]; bad = 1 }
if (lvl[r] !~ /^[0-9]+$/ || lvl[r] + 0 > 16) { printf "BAD_LEVEL %s id=%s level=%s\n", file[r], id[r], lvl[r]; bad = 1 }
n = split(sid[r], p, /[ ,]+/)
for (k = 1; k <= n; k++) if (p[k] != "" && !(p[k] in first) && !(p[k] in ext)) {
printf "UNRESOLVED_IF_SID %s id=%s -> %s\n", file[r], id[r], p[k]; bad = 1 }
else if (p[k] in pos && pos[p[k]] > r) { printf "PARENT_LOADED_LATER %s id=%s -> %s\n", file[r], id[r], p[k]; bad = 1 }
if (msid[r] != "") {
if (!(msid[r] in first) && !(msid[r] in ext)) { printf "UNRESOLVED_IF_MATCHED_SID %s id=%s -> %s\n", file[r], id[r], msid[r]; bad = 1 }
else if ((msid[r] in lv) && lv[msid[r]] == "0") { printf "LEVEL0_IF_MATCHED_SID %s id=%s -> %s\n", file[r], id[r], msid[r]; bad = 1 }
}
}
exit bad
}' "$tsv"
I ran the script (bash 5.3, libxml2 2.15, the macOS system awk) on two synthetic files, 100000-example_base.xml and 100100-example_extra.xml, with one deliberate error of each kind:
./check-rules.sh known_ids.txt rules/*.xml; echo "exit=$?"
LEVEL0_IF_MATCHED_SID 100000-example_base.xml id=100012 -> 100011
DUPLICATE_ID 100100-example_extra.xml id=100010 (first in 100000-example_base.xml)
UNRESOLVED_IF_SID 100100-example_extra.xml id=100020 -> 100999
BAD_LEVEL 100100-example_extra.xml id=100021 level=17
UNRESOLVED_IF_MATCHED_SID 100100-example_extra.xml id=100022 -> 100888
PARENT_LOADED_LATER 100100-example_extra.xml id=100031 -> 100030
exit=1
For simpler checks, rg and sort are enough. Duplicate IDs across the whole directory and levels above 16 in the same files:
LC_ALL=C rg -oN --no-filename '<rule\s[^>]*\bid="[0-9]+"' rules/ | rg -o '[0-9]+' | sort | uniq -d
rg -n '<rule\s[^>]*level="(1[7-9]|[2-9][0-9]|[0-9]{3,})"' rules/
100010
rules/100100-example_extra.xml:14: <rule id="100021" level="17">
Invalid XML ends with exit code 2, the xmllint parser message and an XML_ERROR line, before the other checks start. Level 17 would also be caught by analysisd in this set, but rg shows the file and line number, and the Invalid level message does not contain the rule ID.
The script does not check if_group, if_level or dependencies through CDB lists. Checking if_group would require a set of group names defined in the rules and a decision on how to treat built-in groups. Tests for a specific group of rules are shown in the post Testing Wazuh rules for WordPress traffic.
Sources
- https://documentation.wazuh.com/4.14/user-manual/reference/daemons/wazuh-analysisd.html
- https://documentation.wazuh.com/4.14/user-manual/reference/tools/wazuh-logtest.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/testing.html
- https://documentation.wazuh.com/4.14/user-manual/ruleset/ruleset-xml-syntax/rules.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/analysisd/analysisd.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/analysisd/rules.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/analysisd/rules_list.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/analysisd/logtest.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/error_messages/warning_messages.h
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/api/scripts/wazuh_apid.py
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/framework/wazuh/core/cluster/dapi/dapi.py
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/api/api/controllers/logtest_controller.py
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/shared/debug_op.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/config/logtest-config.c
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/src/config/logtest-config.h
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.11/api/api/configuration.py