Clerks sorting mail into pigeonhole racks in a New York post office around 1918, with the article title about writing Wazuh rules and decoders with an LLM
Cover based on a photo by U.S. National Archives and Records Administration, via Wikimedia Commons, Public domain

You can write Wazuh rules and decoders with an LLM in seconds, and the XML will look right: a parent decoder, a child with a <regex> and an <order>, a rule with a level and a description. Then the manager refuses to load the rule, or it loads and quietly never fires. A model-written Wazuh rule is a draft until wazuh-logtest has loaded it and shown you, field by field, what it does to real log lines.

This guide runs that loop on one common job: a custom decoder for an in-house auth gateway, a failed-login rule, a brute-force correlation rule and an "admin login without MFA" rule. Every error and every alert below comes from wazuh-logtest on a Wazuh 4.14.10 manager in a lab, fed with synthetic log lines that use documentation IP ranges. The drafts were written for this guide to show mistakes models commonly make; they are not a captured model transcript. The tool output is the evidence, not the model's explanation.

If you already write detections in other formats, this sits next to our guides to Sigma and YARA rules with an LLM and Falco rules with an LLM. Wazuh is different enough to need its own loop: its decoders and rules are XML, its default regex engine is not PCRE, and many of its field names are reserved.

Authorized work only. Write and test detections on a manager and logs you own or are explicitly allowed to work with. Keep real hostnames, usernames, IPs and tokens out of prompts; see what to send the model.

Why write Wazuh rules and decoders with an LLM

Wazuh ships thousands of rules (the lab manager reported 8,454 enabled at startup), but your own applications are rarely covered. A custom service writes action=login result=failure user=alice and the stock ruleset sees an unknown syslog program. To alert on it you need two pieces:

  • A decoder that recognizes the program and extracts fields. Wazuh's custom decoders guide puts these in /var/ossec/etc/decoders/local_decoder.xml or a new file in that directory.
  • Rules that match on those fields and set a level. The custom rules guide puts them in /var/ossec/etc/rules/local_rules.xml and says to use IDs between 100000 and 120000.

An LLM is good at the boring part: turning a sample log line into a first decoder, naming fields, writing descriptions and mapping MITRE IDs. It is bad at the parts that only the manager knows: which regex syntax the default engine accepts, which field names are static, and which decoder name a rule actually sees. That is why the loop matters more than the prompt.

Diagram of the loop to write Wazuh rules and decoders with an LLM: the model drafts XML, wazuh-logtest loads it, load errors and decoding gaps go back to the model, lab log lines show which rule fires, and wazuh-logtest -U unit tests gate the change
Draft the XML, inspect load errors, decoded fields and matched rules, then pass unit tests before restarting the manager. Image: RedRouter, Original

The lab: one log source, four kinds of line

The service is a made-up gateway called authgw. These are the lab lines:

Oct 28 10:15:02 gw01 authgw[2210]: action=login result=failure user=alice src=203.0.113.45 mfa=none reason=bad_password
Oct 28 10:16:40 gw01 authgw[2210]: action=login result=success user=alice src=203.0.113.45 mfa=totp
Oct 28 10:18:05 gw01 authgw[2210]: action=login result=success user=admin src=198.51.100.7 mfa=none
Oct 28 10:19:00 gw01 authgw[2210]: action=login result=failure user=svc-probe src=127.0.0.1 mfa=none reason=probe

What we want from Wazuh:

  1. A level 5 alert on each failed login.
  2. A level 10 alert when one source IP fails eight times in two minutes.
  3. A level 12 alert when admin logs in without MFA.
  4. Silence for the local health-check probe, which fails on purpose every few seconds.

Each draft was copied into local_decoder.xml and local_rules.xml, and the four lines were piped into /var/ossec/bin/wazuh-logtest. Per the Wazuh testing docs, logtest uses the same rules as the analysis engine and loads them per session, so saving the files is enough to test. You only restart the manager when you want the rules to generate real alerts.

Draft 1: the rule won't load

This is the kind of first draft a model hands back:

<!-- local_decoder.xml -->
<decoder name="authgw">
  <program_name>authgw</program_name>
</decoder>

<decoder name="authgw-fields">
  <parent>authgw</parent>
  <regex>action=(\w+) result=(\w+) user=(\S+) src=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) mfa=(\w+)</regex>
  <order>action, status, user, srcip, mfa</order>
</decoder>
<!-- local_rules.xml -->
<group name="authgw,">
  <rule id="100100" level="5">
    <decoded_as>authgw-fields</decoded_as>
    <field name="status">failure</field>
    <description>authgw: login failure for $(dstuser)</description>
  </rule>

  <rule id="100101" level="10" frequency="8" timeframe="120">
    <if_matched_sid>100100</if_matched_sid>
    <same_srcip />
    <description>authgw: possible brute force from $(srcip)</description>
  </rule>

  <rule id="100102" level="12">
    <if_sid>100099</if_sid>
    <field name="dstuser">admin</field>
    <field name="mfa">none</field>
    <description>authgw: admin login without MFA from $(srcip)</description>
  </rule>
</group>

wazuh-logtest refused to open a session at all:

** Wazuh-logtest error -1:
	ERROR: Failure to read rule 100100. Field 'status' is static.
	ERROR: (7311): Failure to initializing session

status is one of Wazuh's static fields, along with srcip, dstip, srcuser, dstuser (and its alias user), action, url and a few others listed in the decoders syntax reference. Static fields are matched with their own rule options (<status>, <user>, <srcip>), and <field name="..."> is only for dynamic fields like our mfa. The rules syntax reference lists each option.

Models get this wrong constantly because <field name="status"> is perfectly reasonable XML. The same problem was waiting in rule 100102 (<field name="dstuser">), so we fixed both: <status>failure</status> and <user>admin</user>.

Draft 2: it loads, and nothing fires

With the static fields fixed, logtest loaded the session and printed two warnings first:

** Wazuh-Logtest: WARNING: (7617): Signature ID '100099' was not found and will be ignored in the 'if_sid' option of rule '100102'.
** Wazuh-Logtest: WARNING: (7619): Empty 'if_sid' value. Rule '100102' will be ignored.

The model invented a parent rule, 100099, that doesn't exist. Wazuh doesn't fail the load for this; it drops the rule and keeps going. On a running manager, that line goes to ossec.log and your "admin login without MFA" detection simply isn't there.

Then the first log line:

**Phase 2: Completed decoding.
	name: 'authgw'

**Phase 3: Completed filtering (rules).
	id: '1002'
	level: '2'
	description: 'Unknown problem somewhere in the system.'

Phase 2 shows the parent decoder matched (the program name) but no fields came out: no srcip, no status, nothing. The child regex never matched. The culprit is \d{1,3}. Wazuh's default <regex> engine is OS_Regex, and its regex reference lists exactly two modifiers, + and *. There are no {m,n} counts. It also says . is a literal dot and \. means "any character", the reverse of PCRE. A model trained mostly on PCRE writes the PCRE version every time.

Because nothing was decoded, the stock rule 1002 caught the line instead. That rule matches $BAD_WORDS, a list of words like "failure", and fires at level 2. If you only look at the alerts dashboard, you see something for these lines and might think the custom rule works.

Draft 3: fields decode, rules still silent

We changed the IP group to the form the Wazuh docs use, (\d+.\d+.\d+.\d+), and added the missing grouping rule 100099 with <decoded_as>authgw-fields</decoded_as>, hanging the other rules off it with <if_sid>. Now Phase 2 looked right:

**Phase 2: Completed decoding.
	name: 'authgw'
	action: 'login'
	dstuser: 'alice'
	mfa: 'none'
	srcip: '203.0.113.45'
	status: 'failure'

But only two of the four lines reached Phase 3 at all, and both were rule 1002 again. Look at name: 'authgw'. Even though the fields came from the child decoder called authgw-fields, the event is reported under the parent's name, and <decoded_as> compares against that name. Wazuh's own example in the custom decoders guide sidesteps this by giving the parent and child the same name. The model picked a "clearer" child name and broke every rule underneath it.

The fix is one word: <decoded_as>authgw</decoded_as>.

Draft 4: right alerts, and a false positive

<group name="authgw,">
  <rule id="100099" level="0">
    <decoded_as>authgw</decoded_as>
    <description>authgw: grouping rule</description>
  </rule>

  <rule id="100100" level="5">
    <if_sid>100099</if_sid>
    <status>failure</status>
    <description>authgw: login failure for $(dstuser)</description>
    <group>authentication_failed,</group>
  </rule>

  <rule id="100101" level="10" frequency="8" timeframe="120">
    <if_matched_sid>100100</if_matched_sid>
    <same_srcip />
    <description>authgw: possible brute force from $(srcip)</description>
    <mitre>
      <id>T1110</id>
    </mitre>
    <group>authentication_failures,</group>
  </rule>

  <rule id="100102" level="12">
    <if_sid>100099</if_sid>
    <status>success</status>
    <user>admin</user>
    <field name="mfa">none</field>
    <description>authgw: admin login without MFA from $(srcip)</description>
  </rule>
</group>

On the four lab lines, logtest now gave exactly what we asked for on three of them:

LineRuleLevelDescription
alice, failure1001005authgw: login failure for alice
alice, success, TOTP1000990authgw: grouping rule (no alert)
admin, success, no MFA10010212authgw: admin login without MFA from 198.51.100.7
svc-probe, failure, 127.0.0.11001005authgw: login failure for svc-probe

To test the correlation rule we piped in eight failures for alice from 203.0.113.45, seven seconds apart. Lines one to seven fired 100100; line eight fired 100101, level 10, "authgw: possible brute force from 203.0.113.45". Logtest keeps a firedtimes counter per session, so frequency rules can be tested from a file in one run.

Then we piped in eight failures from the health-check probe, ten seconds apart. Same result: seven level 5 alerts and a level 10 "possible brute force from 127.0.0.1". That's the false positive. In production the probe would page someone every couple of minutes, forever, and the team would learn to ignore rule 100101.

Draft 5: the narrow exclusion

The fix is a child rule at level 0 that matches only the probe, by user and source:

  <rule id="100103" level="0">
    <if_sid>100100</if_sid>
    <user>svc-probe</user>
    <srcip>127.0.0.1</srcip>
    <description>authgw: ignore the local health-check probe</description>
  </rule>

Results with the same inputs:

  • Eight probe failures from 127.0.0.1: every line ended on rule 100103, level 0, no alert, and the brute-force rule never fired, because 100100 is no longer the final match for those events.
  • Eight alice failures from 203.0.113.45: unchanged. Seven level 5 alerts, then rule 100101 at level 10.
  • One svc-probe failure from 203.0.113.99: still rule 100100, level 5. An attacker who guesses the probe's username from outside doesn't get a free pass.

That last line is the test models skip. When you ask a model to "stop the false positive," the easy answer is <user>svc-probe</user> alone, and it works on the lab data. Always add one line that should still alert after you tune.

Turn the lab lines into unit tests

wazuh-logtest has a unit-test switch, -U rule:alert:decoder, that exits 0 when a line ends on the expected rule, level and decoder and 1 when it doesn't. From the lab:

$ echo "<admin, mfa=none line>" | wazuh-logtest -U 100102:12:authgw
Unit test OK
exit=0

$ echo "<alice, mfa=totp line>" | wazuh-logtest -U 100102:12:authgw
Unit test FAIL. Expected ['100102', '12', 'authgw'] , Result ['100099', '0', 'authgw']
exit=1

Keep one expected-result line per rule (including the "should not alert" ones) and run them before every ruleset change. That gives the model a hard pass/fail target, which works much better than "does this look right?"

The model loop

The loop is: the model drafts, wazuh-logtest judges, and the exact output goes back. Here's a minimal sketch with the OpenAI Python SDK pointed at RedRouter. It assumes you've already copied the draft into place on a lab manager, and it only reads logtest output.

import os, subprocess
from openai import OpenAI

client = OpenAI(base_url="https://redrouter.org/v1",
                api_key=os.environ["REDROUTER_API_KEY"])

SYSTEM = ("You write Wazuh 4.x decoders and rules. Default <regex> is OS_Regex: "
          "only + and * modifiers, '.' is a literal dot. Static fields (status, user, "
          "srcip, action...) use their own rule options; <field name> is for dynamic "
          "fields only. <decoded_as> matches the parent decoder name. Custom rule IDs "
          "100000-120000. Return local_decoder.xml and local_rules.xml only.")

def logtest(path):
    with open(path) as f:
        return subprocess.run(["/var/ossec/bin/wazuh-logtest"], stdin=f,
                              capture_output=True, text=True, timeout=60).stdout

def revise(draft, output, goal):
    r = client.chat.completions.create(
        model="glm-5.3-flash-red",
        messages=[{"role": "system", "content": SYSTEM},
                  {"role": "user", "content": f"Goal:\n{goal}\n\nCurrent XML:\n{draft}\n\n"
                                               f"wazuh-logtest output:\n{output}\n\nFix it."}])
    return r.choices[0].message.content

Two notes from running these drafts by hand. First, put the engine rules in the system prompt, as above; it removes the two most common errors before the first run. Second, send logtest's Phase 2 block back, not just the final rule. "Name: authgw, no fields" tells the model the regex is the problem; "rule 1002 fired" alone sends it off rewriting rules.

What to send the model

Detection work often starts from production logs, so be careful about what goes into a prompt.

  • Send the shape of a line, not the line. Replace hostnames, usernames and IPs with placeholders or documentation ranges (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24) as we did here.
  • Send the XML and the logtest output. That's all the model needs to fix a decoder or rule.
  • Don't send tokens, session IDs, or full request bodies that appear in application logs.

On RedRouter, Zero Data Retention is an opt-in setting and off by default; the data handling docs explain what's stored and how API-key content storage works, and our post on what RedRouter keeps explains it in plain terms.

Which model to use

For drafting Wazuh decoders and rules, the default we'd start with is GLM 5.3 Flash Red (glm-5.3-flash-red). On docs.redrouter.org/models it lists at $0.25 per 1M input tokens, $0.13 per 1M cached input tokens and $1.00 per 1M output tokens. A full loop like the one above is a few thousand tokens per round. If the same work sits inside a broader authorized pentest or red-team workflow, red-cyber-1 (Beta) lists at $0.28 / $0.13 / $1.10 per 1M. See plans on /pricing, and check the docs for current rates before you budget.

RedRouter is an OpenAI-compatible API, so any client that takes a base URL works. Our OpenAI-compatible API guide for security agents covers the setup, and /security covers how we handle security workloads.

Checklist before a rule ships

  • wazuh-logtest opens a session with no ERROR lines and no (7617) or (7619) warnings.
  • Phase 2 shows every field you match on, under the decoder name your rules use.
  • Each rule has at least one line that should fire and one that shouldn't.
  • Every exclusion has a test line proving it doesn't silence the real thing.
  • Correlation rules are tested with a full burst from a file, not one line.
  • -U unit tests pass, then restart the manager to make the rules live.

FAQ

Can an LLM write Wazuh rules and decoders that work the first time?

Sometimes, but don't count on it. In this lab the first draft failed to load, the second loaded but decoded nothing, and the third decoded everything but fired nothing. Each fix was obvious once wazuh-logtest showed the output. Treat the model as a fast drafter and logtest as the judge.

Why does Wazuh say "Field 'status' is static"?

Because status is a reserved field, and reserved fields can't be matched with <field name="...">. Use the dedicated option (<status>, <user>, <srcip>, <action>) instead. <field name> is for dynamic fields you name yourself, like mfa.

Why does my Wazuh decoder match the program but extract no fields?

Usually the child <regex> uses PCRE syntax that OS_Regex doesn't support, like {1,3} counts or \. for a dot. Rewrite it with + and * only, or set type="pcre2" on the regex if you need PCRE features (the regex reference covers PCRE2).

Why doesn't my rule fire when the fields decode correctly?

Check <decoded_as>. It matches the decoder name shown in logtest's Phase 2, which is the parent's name. Also check that every <if_sid> points at a rule that exists; a missing parent drops your rule with only a warning.

How do I test Wazuh frequency rules like brute force detection?

Pipe a file of events into wazuh-logtest in one session. It keeps firedtimes across lines, so eight failures from one IP within the timeframe will fire a frequency="8" rule on the eighth line.

Do I need to restart the Wazuh manager after editing rules?

Not to test them; logtest loads the files per session. You do need to restart the manager for the rules to generate real alerts, per the Wazuh custom rules guide.

Is it safe to paste Wazuh logs into an LLM?

Not raw production logs. Send the shape of the line with placeholder values, plus the XML and logtest output. On RedRouter, Zero Data Retention is opt-in and off by default; see the data handling docs.

Related guides

Sources