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:
| Option | Requirement | Default | Meaning |
|---|---|---|---|
name | required | n/a | Name referenced in the reaction block |
executable | required | n/a | The file name only, no path needed |
expect | optional, deprecated since 4.2 | n/a | Alert fields passed as parameters: srcip, user, filename |
extra_args | optional | n/a | Arguments appended to the parameters sent to the script |
timeout_allowed | optional | no | yes 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:
| Option | Where | Default | Allowed values |
|---|---|---|---|
disabled | manager and agent | none, mandatory field | yes, no |
command | manager | n/a | Name of a defined command |
location | manager | n/a | local, server, defined-agent, all |
agent_id | manager | n/a | Agent ID, only with defined-agent |
level | manager | n/a | Minimum level from 1 to 16 |
rules_group | manager | n/a | Groups separated by a pipe, each ending with a comma |
rules_id | manager | n/a | Rule identifiers separated by commas |
timeout | manager | n/a | Positive number of seconds before the reverse command runs |
repeated_offenders | agent only | n/a | Up 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
location | Reaction target |
|---|---|
local | The agent that generated the event |
server | The Wazuh manager |
defined-agent | The agent given by agent_id |
all | All 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.
| System | Script | Action |
|---|---|---|
| Linux, macOS, Unix | disable-account | Disables a user account |
| Linux, macOS, Unix | firewall-drop | Adds the IP to the iptables block list |
| Linux, macOS, Unix | firewalld-drop | Adds the IP to the firewalld drop list, requires firewalld |
| Linux, macOS, Unix | host-deny | Adds the IP to /etc/hosts.deny |
| Linux, macOS, Unix | ip-customblock | Custom Wazuh block, easy to modify |
| Linux, macOS, Unix | ipfw, npf, pf | Equivalents of firewall-drop for IPFW, NPF and PF, require those firewalls |
| Linux, macOS, Unix | wazuh-slack | Slack notification, hook URL passed through extra_args |
| Linux, macOS, Unix | restart.sh, restart-wazuh | Restarts the agent or the manager |
| Linux, macOS, Unix | route-null | Adds the IP to the null route |
| Linux, macOS, Unix | kaspersky | Integration with Kaspersky Endpoint Security for Linux CLI |
| Windows | netsh.exe | Blocks the IP through netsh |
| Windows | restart-wazuh.exe | Restarts the agent |
| Windows | route-null.exe | Adds 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:
- The script reads a message with
commandequal toaddand picks from the alert the keys that identify the action, for examplesrcip. - 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"]
}
}
- It waits for the answer on STDIN. The value
continuein thecommandfield lets it proceed,abortstops 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:
| Option | Action |
|---|---|
-L | List available active responses |
-b with an IP address | Blocks the given IP address |
-f with a reaction identifier | With -b, selects which reaction to run |
-u with an agent ID | Runs the action on the given agent |
-s | CSV output |
-j | JSON 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
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/commands.html
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/active-response.html
- https://documentation.wazuh.com/current/user-manual/capabilities/active-response/how-it-works.html
- https://documentation.wazuh.com/current/user-manual/capabilities/active-response/default-active-response-scripts.html
- https://documentation.wazuh.com/current/user-manual/capabilities/active-response/custom-active-response-scripts.html
- https://documentation.wazuh.com/current/user-manual/capabilities/active-response/ar-use-cases/blocking-ssh-brute-force.html
- https://documentation.wazuh.com/current/user-manual/reference/tools/agent-control.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0015-ossec_rules.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0095-sshd_rules.xml