PL EN
WB-9247CD1BTLP:CLEARWazuh 4

Wazuh active response: configuration, scripts and SSH brute force blocking

Wazuh 4.14 active response: command and active-response options, the script JSON protocol, stateful vs stateless, check_keys and an SSH block via firewall-drop.

Active response runs a script on the endpoint when an alert meets a rule, level or group condition. The configuration splits into a command definition, trigger conditions and an execution location, and the script receives the whole alert as JSON on standard input.

Two configuration blocks

Each command has its own <command> block, and each reaction has its own <active-response> block. The documentation sets no limit on the number of either. The reaction block points to the command through <command>, so when something fails you check two links: whether the reaction points to an existing command name, and whether the command points to an existing file.

Options of the <command> block:

OptionRequirementDefaultMeaning
namerequiredn/aName referenced in the reaction block
executablerequiredn/aThe file name only, no path needed
expectoptional, deprecated since 4.2n/aAlert fields passed as parameters: srcip, user, filename
extra_argsoptionaln/aArguments appended to the parameters sent to the script
timeout_allowedoptionalnoyes makes the reaction stateful

A field from the expect list that is missing from the alert is passed as -. When the alert contains none of the expected fields, the reaction is skipped.

Options of the <active-response> block:

OptionWhereDefaultAllowed values
disabledmanager and agentnone, mandatory fieldyes, no
commandmanagern/aName of a defined command
locationmanagern/alocal, server, defined-agent, all
agent_idmanagern/aAgent ID, only with defined-agent
levelmanagern/aMinimum level from 1 to 16
rules_groupmanagern/aGroups separated by a pipe, each ending with a comma
rules_idmanagern/aRule identifiers separated by commas
timeoutmanagern/aPositive number of seconds before the reverse command runs
repeated_offendersagent onlyn/aUp to 5 increasing values in minutes

disabled set on the agent turns the reaction off only on that agent. Set in the manager’s ossec.conf, it turns the reaction off on the manager and on all agents.

Location and trigger conditions

locationReaction target
localThe agent that generated the event
serverThe Wazuh manager
defined-agentThe agent given by agent_id
allAll agents, without the manager; the documentation says to use it with care

To run a reaction on every agent and on the manager, the documentation recommends two similar blocks: one with all, the other with server.

level, rules_group and rules_id are cumulative options, not restrictive ones: the reaction starts when the rule meets any of them. Matching the level alone therefore runs the script also for a rule outside the listed group. To narrow the behavior, use a single filter.

The trailing comma on a name in rules_group prevents a partial match. The string authentication matches the groups authentication, authentication_success and authentication_failure, while authentication, matches only the first. The group in the rule definition must also end with a comma, which is the norm in the default ruleset.

<rules_group>example-auth,|example-network,</rules_group>

Stateless and stateful

A stateless reaction is a one-time action with no undo event. A stateful reaction reverses or stops the action after a time. The mode is set by timeout_allowed in the command, and the time by timeout in the reaction block. timeout alone, without timeout_allowed, does not make a command stateful.

repeated_offenders extends timeout for repeat offenders. When it is used, timeout applies only to the first violation. You set the option in the agent’s ossec.conf, in the top-level <active-response> block, also when the configuration is centralized through agent.conf. Windows agents do not support it.

<!-- Agent ossec.conf: 1, 5 and 10 minutes for repeat offenders -->
<active-response>
  <disabled>no</disabled>
  <repeated_offenders>1,5,10</repeated_offenders>
</active-response>

Default scripts

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

SystemScriptAction
Linux, macOS, Unixdisable-accountDisables a user account
Linux, macOS, Unixfirewall-dropAdds the IP to the iptables block list
Linux, macOS, Unixfirewalld-dropAdds the IP to the firewalld drop list, requires firewalld
Linux, macOS, Unixhost-denyAdds the IP to /etc/hosts.deny
Linux, macOS, Unixip-customblockCustom Wazuh block, easy to modify
Linux, macOS, Unixipfw, npf, pfEquivalents of firewall-drop for IPFW, NPF and PF, require those firewalls
Linux, macOS, Unixwazuh-slackSlack notification, hook URL passed through extra_args
Linux, macOS, Unixrestart.sh, restart-wazuhRestarts the agent or the manager
Linux, macOS, Unixroute-nullAdds the IP to the null route
Linux, macOS, UnixkasperskyIntegration with Kaspersky Endpoint Security for Linux CLI
Windowsnetsh.exeBlocks the IP through netsh
Windowsrestart-wazuh.exeRestarts the agent
Windowsroute-null.exeAdds the IP to the null route

Pick the script that matches the firewall actually running on the endpoint.

Custom script protocol

A script in any language receives one JSON line with the full alert on STDIN. Message structure:

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

extra_args holds additional arguments, alert the full alert that triggered the script, and program the script to run. A stateless script must: read STDIN, parse the JSON, check that command has the value add, and extract the data it needs.

A stateful script also handles delete, which reverses the action, and before add it confirms with execd that it may act. The check_keys flow:

  1. The script reads a message with command equal to add and picks from the alert the keys that identify the action, for example srcip.
  2. It sends a control message 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 answer on STDIN. The value continue in the command field lets it proceed, abort stops it, for example when the same action is already running and has not expired.
{
  "version":1,
  "origin":{
      "name":"node01",
      "module":"wazuh-execd"
  },
  "command":"continue",
  "parameters":{}
}

Reading from STDIN ends at a newline character, and a message written to STDOUT must end with one. Otherwise a deadlock can occur.

The reference Python script from the documentation (custom-ar.py) has three sections to modify: the key (keys = [alert["rule"]["id"]]), the add action and the delete action. A stateless reaction needs the key and add. A stateful one also needs delete and <timeout> in the <active-response> block in the server’s /var/ossec/etc/ossec.conf. After you copy it to the bin directory, set permissions and restart the agent:

sudo chmod 750 /var/ossec/active-response/bin/custom-ar.py
sudo chown root:wazuh /var/ossec/active-response/bin/custom-ar.py
sudo systemctl restart wazuh-agent

For macOS the documentation gives similar commands (/Library/Ossec/active-response/bin, restart through /Library/Ossec/bin/wazuh-control restart), and for Windows two methods (conversion to an executable file or a batch launcher).

The active-responses.log file

Scripts write their actions to /var/ossec/logs/active-responses.log (macOS /Library/Ossec/logs/active-responses.log, Windows C:\Program Files (x86)\ossec-agent\active-response\active-responses.log). The reference script writes a timestamp, the script name and a message there. The default configuration includes a read of this file:

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

In version 4.14 the ruleset file 0015-ossec_rules.xml has rule 650 (decoder ar_log_json, group active_response). Below it sit rule 651 “Host Blocked by firewall-drop Active Response” for command equal to add, and rule 652 “Host Unblocked by firewall-drop Active Response” for delete. The documentation notes that a custom script needs its own rule that parses its log. The rules for writing such rules and loading files are covered in the posts Wazuh: loading rules and decoders and wazuh-logtest.

Example: SSH brute force block

The example from the documentation uses rule 5763 (level 10, frequency 8, timeframe 120, same_source_ip) and the firewall-drop script. The sample values (addresses, identifiers) are synthetic. On the server, check that this command exists in /var/ossec/etc/ossec.conf:

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

Then add the reaction and restart the manager:

<ossec_config>
  <active-response>
    <disabled>no</disabled>
    <command>firewall-drop</command>
    <location>local</location>
    <rules_id>5763</rules_id>
    <timeout>180</timeout>
  </active-response>
</ossec_config>
sudo systemctl restart wazuh-manager

location set to local runs the block on the endpoint where the login attempts happened, and timeout 180 means the block is lifted after 180 seconds. The test needs an attacker with an SSH client and a password guessing tool. The documentation uses Hydra, run from the attacker host against the endpoint with the SSH server:

sudo hydra -t 4 -l <USERNAME> -P <PASSWD_LIST.txt> <TARGET_IP> ssh

After the attack, the dashboard shows an alert from rule 5763, then from rule 651. Within 3 minutes of the attack, a ping from the attacker host to the target gets no reply, which confirms the block. After timeout expires, the block is removed. The entry with command equal to delete in active-responses.log is matched by rule 652.

Silencing noise from sshd rules is covered in the post sshd rules: alert suppression, and approval before a response runs is covered in the post Response approval gate.

The agent_control tool

agent_control triggers reactions manually. Options related to active response:

OptionAction
-LList available active responses
-b with an IP addressBlocks the given IP address
-f with a reaction identifierWith -b, selects which reaction to run
-u with an agent IDRuns the action on the given agent
-sCSV output
-jJSON output

The identifier for -f is the command name plus the timeout value from the reaction block, for example firewall-drop180 for the example above. When timeout_allowed is no or timeout is not set, the number is 0. You can read the identifier in the first column of /var/ossec/etc/shared/ar.conf. A manual block assembled from the option descriptions, with no ready example in the documentation:

/var/ossec/bin/agent_control -L
/var/ossec/bin/agent_control -b 192.0.2.10 -f firewall-drop180 -u 001

Verifying the configuration

Check the command name, the executable file, the location, the filters and timeout. For a reaction with undo, the command must have timeout_allowed set to yes. Compare disabled on the manager and the agent with the scope the reaction should have, and check the permissions of a custom script (chmod 750, chown root:wazuh).

What the documentation does not specify

The documentation does not specify the syntax for undoing a block in agent_control, the shell or interpreter for custom scripts, error handling and retries, the behavior with timeout equal to zero, the maximum timeout value, or the exact interaction of repeated_offenders with timeout beyond the statement that timeout applies to the first violation.

Sources

↑↓ select Enter open Esc close