r/Wazuh • • 9d ago

Sequential correlation in Wazuh 4.x: detecting event A followed by event B

I've been writing a series about how Wazuh rules work, and I recently published the third part, which focuses on sequential correlation. The previous articles covered how Wazuh rules are connected and how to distinguish atomic conditions from temporal ones, while this one explores detecting events that occur in a specific order.

For instance, an event triggers rule A, followed by another event triggering rule B. We want rule C to detect that sequence, provided both events occur within 60 seconds and share the same source IP. It turns out we can do this with a single Wazuh rule:

<rule id="100575" level="12" timeframe="60">   <if_matched_sid>100570</if_matched_sid>
    <if_sid>100505</if_sid>
    <same_srcip />
    <description>Sequential correlation from $(srcip).</description>
</rule>

The frequency behaviour surprised me most. The rule does not need a frequency, because Wazuh defaults it internally to zero, which allows one previous matching event to satisfy the condition. Yet explicitly setting frequency="0" or frequency="1" is invalid, while frequency="2" produces the same internal value as omitting it.

I went through the Wazuh 4.14.10 source code to understand why, then tested the behaviour against a running manager. One result I did not expect was that A → B → B can trigger the correlation twice, because the earlier A remains eligible for subsequent matches.

I also looked into the distinction between if_sid and if_matched_sid, how Wazuh maintains historical matches, and what happens when events arrive in the wrong order or have different source IPs.

The implementation details and test results are in the article Understanding Wazuh rules, part III: sequential correlation, and the test project is available if anyone wants to reproduce the results.

I'm curious whether anyone here is using similar sequential correlations in production. Have you encountered limitations with Wazuh's built-in correlation, particularly when trying to connect different event types?

4 Upvotes

6 comments sorted by

3

u/Wazuh_Christian 8d ago

Nice write-up, and thanks for going to the source instead of guessing. I ran your example on a 4.14 manager and got the same results, including the A → B → B double fire. I also went through the code to see why it happens.

Why A → B → B fires twice. It's the frequency-0 path in Search_LastSids() (src/analysisd/eventinfo.c). With a real frequency, the older events that complete the count get marked as matched, and that mark is what keeps them from being reused. With frequency 0 the loop matches on the first candidate, so it never sets that pointer and A is never marked. Every B that shows up before A ages out of the timeframe will find it again.

If you want one alert per sequence, ignore="60" on rule C works. But the ignore window covers the whole rule, not one srcip. In my test, a second sequence from a different IP inside those 60 seconds was reported as plain B instead of C, because analysisd falls back to the parent rule while C is ignored. That may or may not be acceptable depending on the use case.

On limitations, these are the ones I see bite people most when they connect different event types:

  1. Same agent by default. The history lookup skips events from other agents unless the rule has <global_frequency />. So "the firewall (syslog) sees X, then the endpoint agent sees Y" won't correlate out of the box. I tested A on one agent and B on another with the same srcip: nothing without the option, and C fired with it.
  2. Per manager, not per cluster. The docs call this out for global_frequency. The history lives in memory in each node's analysisd, so if A and B land on different workers they never see each other, and a restart wipes it.
  3. Rule A must be the final rule for the event, with level > 0. Only the rule that ends up matching (the deepest child) is stored in the history, and analysisd stops before that step for level-0 rules. An if_matched_sid pointing at a level-0 rule, or at a parent whose child took the event, will never fire and won't log any error. This one catches a lot of people because the natural move is to make the "A" rule level 0 to keep the noise down. If you don't want A in your alerts, give it level 1+ with <options>no_log</options> (or a level below log_alert_level). It still gets stored for correlation.

And yes, this pattern is used in production. The stock ruleset ships it: rule 40112 ("Multiple authentication failures followed by a success") is the same shape as yours, if_matched_group + if_group + same_source_ip with a 240s timeframe and no frequency.

Useful docs if anyone wants to dig in:

- if_matched_sid / if_matched_group: https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html#if-matched-sid

- global_frequency (including the "manager level, not cluster level" note): https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html#global-frequency

- same_srcip: https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html#same-srcip

- timeframe / frequency / ignore attributes: https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html#rule

- Custom rules: https://documentation.wazuh.com/current/user-manual/ruleset/rules/custom.html

- Testing with wazuh-logtest: https://documentation.wazuh.com/current/user-manual/ruleset/testing.html

Regards

2

u/feldrim 8d ago

I updated the article to address these issues.

2

u/Wazuh_Christian 8d ago

Nice, thanks for updating it. Glad the details helped, looking forward to the rest of the series.

1

u/feldrim 8d ago

Since 5.x is coming in a year, I'm not sure if I'll actually have more regarding the rules. But I'll try writing Wazuh articles in other topics as well. 

2

u/Raxion75 8d ago

If i remember correctly, these type of sequential correlations don't work on many plugins/wodles that rely on batch fetching in intervals (such as s3/cloudtrail), or at least they don't reliably fire since the alerts arrives in same time within a batch.

Haven't found a good alternative or solution till this day

1

u/feldrim 8d ago

This is actually a side effect to be used as sequences. As you have properly mentioned, the timestamp is based on ingestion time, not the time of event. I should have noted it somewhere.