PL EN
WB-BC414DF6TLP:CLEARWazuh 4

Wazuh 4 training, part 9: CDB lists, Active Response and integrations

Part 9 of the Wazuh 4 training: a CDB list in an SSH rule, Active Response commands, firewall-drop with a timeout, a custom script and integrator filters.

Part 9 of the Wazuh 4 training moves from detection to response. After part 8 on inventory, vulnerabilities and rootkits I build a rule that uses a CDB list, attach Active Response with the firewall-drop command to it, and only then configure the integrator, which sends alerts to external services.

Exercise 1: a CDB list in an SSH rule

A CDB (constant database) list lets you check whether a field extracted by a decoder is on the list. The documentation names whitelists and blacklists of users, file hashes, IP addresses and domain names as the main use. The full format description, the file API and the lists shipped with the ruleset are in the post on CDB lists. Here I cover only what the exercise needs.

File format

The list file is a plain text file. Each line has a unique key ending with a colon, after which you can add a value:

EntryMeaning
key1:a key only, presence is checked
key2:value2a key with a value, the value can be used as a criterion in a rule
"a0:a0:a0:a0:a0:a0":a key containing a colon, for example a MAC address, entirely in quotation marks

Values may repeat, keys may not. For IP addresses a trailing dot in the key means a subnet:

KeyCIDRMatch
198.51.:198.51.0.0/16198.51.0.0 to 198.51.255.255
192.0.2.:192.0.2.0/24192.0.2.0 to 192.0.2.255
203.0.113.5:203.0.113.5/32only 203.0.113.5

The documentation recommends storing lists in /var/ossec/etc/lists. Lists are built and loaded when the analysis engine starts, so a manager restart is mandatory after every change to the file.

File, declaration and rule

The file /var/ossec/etc/lists/ssh-watch-list with the addresses to watch:

192.0.2.10:
192.0.2.77:
198.51.100.:
203.0.113.5:

I declare the list in the <ruleset> section of the manager /var/ossec/etc/ossec.conf, with a path relative to the installation directory. Next to the default entries (etc/lists/audit-keys, etc/lists/amazon/aws-eventnames, etc/lists/security-eventchannel) I add my own:

<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>
  <list>etc/lists/amazon/aws-eventnames</list>
  <list>etc/lists/security-eventchannel</list>
  <list>etc/lists/ssh-watch-list</list>

  <!-- User-defined ruleset -->
  <decoder_dir>etc/decoders</decoder_dir>
  <rule_dir>etc/rules</rule_dir>
</ruleset>

The standard rule 5710 from 0095-sshd_rules.xml has level 5, inherits from 5700 and matches illegal user|invalid user, that is, a login attempt for a non-existent account. A custom child rule in the user rules file, in the etc/rules directory, raises the priority when the source address is on the list:

<group name="local,sshd,">
  <rule id="100112" level="10">
    <if_sid>5710</if_sid>
    <list field="srcip" lookup="address_match_key">etc/lists/ssh-watch-list</list>
    <description>sshd: login attempt with a non-existent user from an address on ssh-watch-list</description>
  </rule>
</group>

The srcip field is an IP address, so I use address_match_key. Plain match_key is for text keys such as a user name.

Lookup attributes

lookup valueWhen the rule matches
match_key (default)the key from the field is on the list
address_match_keythe IP address from the field is on the list, with prefix support
not_match_keythe key from the field is not on the list
not_address_match_keythe IP address from the field is not on the list
match_key_valuethe key is on the list and its value matches the expression in check_value
address_match_key_valuethe same for an IP address

The entry with a value looks like this:

<list field="srcip" lookup="address_match_key_value" check_value="^reject">etc/lists/list-IP</list>

The documentation describes a scenario with a value: a list of accounts with a department name gives a criterion for an alert about a person from outside the finance department logging in to a finance server. There is no ready-made rule for it.

Rule test

After saving the list, the declaration and the rule, I restart the manager:

systemctl restart wazuh-manager

I check the rule with the tool described in the post on wazuh-logtest. I paste four sshd lines with different addresses, two of them on the list, and expect the lines with a listed address to trigger rule 100112 instead of 5710:

Jan 19 04:17:54 agent-01 sshd[2365]: Failed password for invalid user example-a from 192.0.2.10 port 55969 ssh2
Jan 19 04:17:55 agent-01 sshd[2365]: Failed password for invalid user example-b from 203.0.113.99 port 55970 ssh2
Jan 19 04:17:56 agent-01 sshd[2365]: Failed password for invalid user example-c from 198.51.100.40 port 55971 ssh2
Jan 19 04:17:57 agent-01 sshd[2365]: Failed password for invalid user example-d from 192.0.2.200 port 55972 ssh2

The address 198.51.100.40 matches through the entry 198.51.100.:. The addresses 203.0.113.99 and 192.0.2.200 are not on the list, so they stay with rule 5710. The higher level of rule 100112 can later be used for a stronger Active Response reaction.

Exercise 2: Active Response, commands and scripts

Active Response runs a script on the monitored endpoint when an alert meets a criterion: a specific rule ID, a level or a group. The documentation warns that badly designed rules and responses can increase the endpoint’s vulnerability. The full description of configuration and the script protocol is in the post on Active Response, and an example for web traffic is in the post on blocking attacks on a proxy.

Two blocks on the manager

The configuration consists of a <command> block, which says what to run, and an <active-response> block, which says when and where. Both are in the manager /var/ossec/etc/ossec.conf. For scripts shipped with Wazuh the <command> blocks are already present and you do not need to add them.

<command> optionDefaultMeaning
namenone, requiredthe name referenced in <active-response>
executablenone, requireda script or executable, no path needed; you give the extension only for scripts with the same name
extra_argsnoneextra arguments appended to the parameters sent to the script on the agent
timeout_allowednoyes means a stateful command that reverts the action after the time set in <active-response>
expectnonedeprecated since version 4.2

Executables are in /var/ossec/active-response/bin (Linux/Unix), /Library/Ossec/active-response/bin (macOS) and C:\Program Files (x86)\ossec-agent\active-response\bin (Windows).

<active-response> optionMeaning
disabledyes or no, required in every block
commandthe name of a defined command
locationlocal, server, defined-agent or all
agent_idthe agent ID with defined-agent
levelminimum rule level, from 1 to 16
rules_groupa rule group, several groups separated by the | character
rules_idrule IDs separated by commas
timeoutnumber of seconds before the reverse command runs
repeated_offendersincreasing timeouts in minutes, at most 5 entries; only in the agent ossec.conf, not on Windows agents

The four location values work like this:

ValueWhere the script runs
localon the agent that generated the alert
serveron the manager
defined-agenton the agent given in agent_id, regardless of the event source
allon all agents except the manager; use with caution

The documentation describes server as running the script on the Wazuh server and all as running it on every agent. For the reaction to work on the manager too, I add a separate block with location set to server.

The three filter options work together, not restrictively. When you set level, rules_group and rules_id together, the reaction runs if the rule meets any of them. For rules_group, add a comma at the end, for example authentication_failures,, because a string without a comma matches partially: authentication matches authentication_success and authentication_failure. In the default ruleset rule groups usually end with a comma, so the comma limits the match to a single group.

The disabled option set to yes on the manager turns off Active Response on the manager and on all agents, and on an agent only for that agent.

Stateless and stateful

A stateless reaction is a one-time action with no event that would revert it. A stateful reaction reverts its action after a set time. timeout_allowed in the command decides which, and timeout in <active-response> sets the time. When repeated_offenders is set, timeout applies only to the first offense. An example on the agent side:

<active-response>
  <disabled>no</disabled>
  <repeated_offenders>1,5,10</repeated_offenders>
</active-response>

Scripts shipped with Wazuh

ScriptDescription
disable-accountdisables a user account
firewall-dropadds an IP address to the iptables deny list
firewalld-dropadds an address to the firewalld drop list, requires firewalld to be installed
host-denyappends an address to /etc/hosts.deny
ip-customblocka custom Wazuh block, easy to modify
ipfw, npf, pfblock equivalents for IPFW, NPF and PF, they require those firewalls
wazuh-slackposts a notification to Slack, the hook URL as extra_args
restart.sh, restart-wazuhrestart the agent or the manager
route-nulladds an address to a null route
kasperskyintegration with Kaspersky Endpoint Security for Linux

On Windows the documentation lists netsh.exe (blocks an address through netsh), restart-wazuh.exe and route-null.exe.

firewall-drop on rule 5712

Rule 5712 from 0095-sshd_rules.xml has level 10, frequency 8, timeframe 120 and ignore 60. It correlates rule 5710 events from the same source address (same_source_ip) in a 120 second window, with a frequency threshold of 8, and describes brute force against non-existent accounts. The twin rule 5763 works the same way for failed authentication. On the manager I check that the command block exists, and then add the reaction block:

<command>
  <name>firewall-drop</name>
  <executable>firewall-drop</executable>
  <timeout_allowed>yes</timeout_allowed>
</command>

<active-response>
  <disabled>no</disabled>
  <command>firewall-drop</command>
  <location>local</location>
  <rules_id>5712,100112</rules_id>
  <timeout>120</timeout>
</active-response>

I added 100112 so that an address from the list in exercise 1 is also blocked. With location set to local the script runs on the agent that reported the attack, so the block applies to its firewall. After a manager restart the test goes like this:

  1. From another machine I check ping to the agent to confirm connectivity.
  2. I run a dictionary attack over SSH against non-existent accounts from that machine, in an environment meant for it. For this Wazuh documents the Hydra tool with a list of 10 passwords.
  3. After the attack ping to the agent stops responding, because the attacker address is on the iptables deny list.
  4. After timeout passes, the script receives the delete command and the address returns to normal traffic.

In the dashboard I look for alerts from rules 651 (block) and 652 (unblock).

The log and rules 651 and 652

Scripts write their results to /var/ossec/logs/active-responses.log (Linux), /Library/Ossec/logs/active-responses.log (macOS) and C:\Program Files (x86)\ossec-agent\active-response\active-responses.log (Windows). The manager monitors this file by default with this block:

<localfile>
  <log_format>syslog</log_format>
  <location>/var/ossec/logs/active-responses.log</location>
</localfile>

The rules from 0015-ossec_rules.xml turn the entries into alerts. The base rule 650 has level 0 and decoded_as equal to ar_log_json. Rule 651 (level 3) is triggered by the field parameters.program equal to firewall-drop with command equal to add, description “Host Blocked by firewall-drop Active Response”. Rule 652 does the same for delete and describes the unblock. Rule 653 handles host-deny. A custom script needs its own rule that will analyze its log entries.

A netsh block on Windows

For a Windows agent an analogous block calls netsh.exe. The documentation describes netsh.exe as a default script but gives no ready-made <command> block. Before you add it, check whether a netsh block already exists in ossec.conf:

<command>
  <name>netsh</name>
  <executable>netsh.exe</executable>
  <timeout_allowed>yes</timeout_allowed>
</command>

<active-response>
  <disabled>no</disabled>
  <command>netsh</command>
  <location>local</location>
  <rules_group>authentication_failures,</rules_group>
  <timeout>120</timeout>
</active-response>

The authentication_failures group is broad: it is the same group that rules 5712 and 5763 belong to. In production I narrow the trigger to specific IDs in rules_id.

Exercise 3: a custom Active Response script

A script can be written in any language. It receives one line of JSON with the full alert on STDIN:

{
  "version": 1,
  "origin": { "name": "", "module": "" },
  "command": "",
  "parameters": {
    "extra_args": [],
    "alert": {},
    "program": ""
  }
}

The command field has the value add or delete. alert holds the whole alert, extra_args the arguments from the command, and program the script path. A stateless script has to do four things: read STDIN, parse the JSON, confirm that command is add, and extract the data it needs. A stateful script does the same, and in addition:

  1. It handles both commands: on add it performs the action, on delete it reverts it.
  2. It builds a control message with keys from the alert, for example the srcip address, and prints it to STDOUT:
{
  "version": 1,
  "origin": { "name": "program-name", "module": "active-response" },
  "command": "check_keys",
  "parameters": { "keys": ["192.0.2.10"] }
}
  1. It waits for the response on STDIN. The command field of the response is continue or abort. Abort makes sense when the same action is already in progress and its timeout has not yet passed.

A warning from the documentation: the script reads STDIN up to the newline character, and the message on STDOUT must end with one. Otherwise a deadlock may occur.

A stateful sketch in Bash that only writes to the log the address to block. This is my example of the protocol, not a script from the documentation, and it does not perform any blocking:

#!/bin/bash
# Stateful active response sketch: logs the address it would block or unblock
LOG="/var/ossec/logs/active-responses.log"

read -r INPUT
CMD=$(printf '%s' "$INPUT" | jq -r '.command')
SRCIP=$(printf '%s' "$INPUT" | jq -r '.parameters.alert.data.srcip // empty')
[ -n "$SRCIP" ] || exit 1

case "$CMD" in
  add)
    # Ask the execd daemon whether the same key is already active
    printf '%s\n' "{\"version\":1,\"origin\":{\"name\":\"example-ar\",\"module\":\"active-response\"},\"command\":\"check_keys\",\"parameters\":{\"keys\":[\"$SRCIP\"]}}"
    read -r REPLY
    [ "$(printf '%s' "$REPLY" | jq -r '.command')" = "continue" ] || exit 0
    echo "$(date '+%Y/%m/%d %H:%M:%S') example-ar: block $SRCIP" >> "$LOG"
    ;;
  delete)
    echo "$(date '+%Y/%m/%d %H:%M:%S') example-ar: unblock $SRCIP" >> "$LOG"
    ;;
  *)
    exit 1
    ;;
esac

The documentation also gives a full Python example (custom-ar.py) that writes the file ar-test-result.txt on add and removes it on delete. Wazuh treats it as a template with three places to change: the key (Custom key), the add action and the delete action.

Deploying the script

On a Linux endpoint I put the script in the Active Response bin directory and set permissions:

sudo chmod 750 /var/ossec/active-response/bin/<CUSTOM_SCRIPT>
sudo chown root:wazuh /var/ossec/active-response/bin/<CUSTOM_SCRIPT>
sudo systemctl restart wazuh-agent

On macOS the path is /Library/Ossec/active-response/bin, and on Windows C:\Program Files (x86)\ossec-agent\active-response\bin without the chmod step. In the Python example the documentation says to restart the agent after copying. On the manager I add the command and the reaction. The documentation example for the Python script triggered by rule 503, with a 60 second timeout:

<command>
  <name>linux-custom-ar</name>
  <executable>custom-ar.py</executable>
  <timeout_allowed>yes</timeout_allowed>
</command>

<active-response>
  <disabled>no</disabled>
  <command>linux-custom-ar</command>
  <location>local</location>
  <rules_id>503</rules_id>
  <timeout>60</timeout>
</active-response>

After a manager restart the script runs on the endpoint every time rule 503 fires. For my sketch from the exercise I substitute <executable> with its file name and rules_id with rule 100112.

Exercise 4: the integrator

The integrator connects the manager to external APIs and alerting tools. The <integration> block is in the manager configuration, and the full description of built-in integrations and custom scripts is in the post on the integrator. Here I care about the options that decide how many alerts leave the system.

OptionDefaultMeaning
namenoneslack, pagerduty, virustotal, shuffle, maltiverse or any string starting with custom-
hook_urlnonethe service address; required for Slack, Shuffle and Maltiverse
api_keynonethe API key; required for PagerDuty, VirusTotal and Maltiverse
alert_formatnonejson, recommended for Slack, VirusTotal, Shuffle and Maltiverse
levelnoneonly alerts of this level or higher, from 0 to 16
rule_idnonerule IDs separated by commas
groupnonerule groups; for VirusTotal only rules from the syscheck group are available
event_locationnonea filter on where the event came from, an sregex expression
max_log165maximum length of an alert snippet, from 165 to 1024; does not apply when alert_format is json
optionsnonea JSON object that overrides or adds fields
timeout10seconds to wait for the service response
retries3maximum number of retries after a timeout

A documentation example for Slack with a level and group filter:

<integration>
  <name>slack</name>
  <hook_url>https://hooks.slack.com/services/...</hook_url> <!-- Replace with your Slack hook URL -->
  <level>10</level>
  <group>multiple_drops,authentication_failures</group>
  <alert_format>json</alert_format>
  <options>{"pretext": "Custom Title"}</options> <!-- Replace with your custom JSON object -->
</integration>

The VirusTotal example has group set to syscheck, timeout 30 and retries 5, and a custom integration must have a name starting with custom-, for example custom-integration.

For the reaction from exercise 2 the rule_id filter is useful, because it sends outside only alerts of the rules that Active Response reacts to:

<integration>
  <name>slack</name>
  <hook_url>https://hooks.slack.com/services/...</hook_url> <!-- Replace with your Slack hook URL -->
  <rule_id>5712,100112,651,652</rule_id>
  <alert_format>json</alert_format>
</integration>

This way the channel shows the attack as well as the block and the unblock.

Checklist after part 9

  • The file /var/ossec/etc/lists/ssh-watch-list has the key: format and unique keys, and <list> is declared in <ruleset>.
  • The manager was restarted after the list change, and rule 100112 fires instead of 5710 for addresses on the list.
  • I tell apart match_key, address_match_key, the not_ variants and the ..._value variants with check_value.
  • The <command> block has name, executable and timeout_allowed, and the <active-response> block has disabled.
  • firewall-drop on rule 5712 blocks the attacker address and releases it after timeout.
  • Alerts from rules 651 and 652 are visible in the dashboard.
  • I know how a stateless script differs from a stateful one and why the check_keys message must end with a newline.
  • The <integration> block has alert_format set to json and a level, rule_id or group filter.

In part 10 I move on to integrations with Osquery, Sysmon and Docker.

What the documentation does not specify

  • The documentation does not describe how long firewall-drop keeps an iptables rule after a manager restart during a timeout.
  • The command page does not say how timeout in <active-response> behaves when timeout_allowed is no.
  • The integration documentation gives no request limit for VirusTotal with a free key.
  • The CDB list page does not describe the evaluation order of two child rules of the same rule if both match the same address.

Sources

↑↓ select Enter open Esc close