Detecting web attacks from proxy logs and active response in Wazuh
Build a correlation rule for repeated requests, scope active response to the resulting rule, and check fields, conditions and timeouts in logtest.
A reverse proxy log contains the source address, path and status, but their layout depends on the source. On a sample line I decode these fields, build a correlation rule and bind active response to its ID, and I check the whole chain in wazuh-logtest.
Sample and fields
All values are examples: agent-01, example-proxy, an address from the 192.0.2.0/24 range and the path /example-protected.
Nov 17 12:00:00 agent-01 example-proxy: method=GET src=192.0.2.44 path=/example-protected status=200
The decoder documentation defines fixed field names that rules recognize directly. The example uses three of them; two more are shown for comparison:
| Field | Description in the documentation | Use in the example |
|---|---|---|
srcip | source IP address | address from src= |
url | URL of the event | value from path= |
status | event status (success, failure etc.) | three-digit code from status= |
protocol | protocol | not used |
action | event action (deny, drop, accept etc.) | not used |
I do not map the HTTP method to protocol, because the documentation describes that field as a protocol. I store the method in the dynamic field http_method. A dynamic field is any string outside the list of static fields.
Decoder
The parent decoder recognizes the program from the syslog header, and the child extracts the fields. <regex> requires <order> and either <prematch> or <program_name> in the decoder or its parent.
<decoder name="example-proxy">
<program_name>^example-proxy</program_name>
</decoder>
<decoder name="example-proxy">
<parent>example-proxy</parent>
<regex type="pcre2">method=(GET|POST) src=(\S+) path=(\S+) status=(\d{3})</regex>
<order>http_method,srcip,url,status</order>
</decoder>
The pattern accepts two methods, because only those appear in the sample. The attribute type="pcre2" selects PCRE2 instead of the default osregex. I store custom decoders in /var/ossec/etc/decoders/local_decoder.xml or in separate files in that directory. The syntax and the parent-child dependencies are described in the custom decoders guide.
Rules: signal and correlation
The first-level rule describes a single request, and the second one counts its repetitions from one address. Attributes and conditions from the documentation:
<rule> attribute | Description | Allowed values |
|---|---|---|
level | rule level used by alerts and responses | 0 to 16 |
id | rule identifier | 1 to 999999 |
frequency | number of rule matches before an alert is generated | 2 to 9999 |
timeframe | time window in seconds, used with frequency | 1 to 99999 |
ignore | time in seconds during which the rule stays inactive after it fires | 1 to 999999 |
| Condition | Meaning |
|---|---|
decoded_as | event decoded by the given decoder |
field | compares a decoder field with a regular expression |
if_sid | matches when an earlier rule from the list matched |
if_matched_sid | matches when an alert with the given ID occurred within the given time, used with frequency and timeframe |
same_srcip | the decoded srcip must be the same |
different_srcip | the decoded srcip must be different |
For if_matched_sid the documentation states a limitation: level 0 rules are dropped immediately and are not suitable as the target of this condition. That is why if_matched_sid points to rule 100011 at level 5, and only the grouping rule 100010 has level 0. The identifier range 100000 to 120000 is reserved for custom rules.
<group name="example-proxy,">
<rule id="100010" level="0">
<decoded_as>example-proxy</decoded_as>
<description>Example proxy: request</description>
</rule>
<rule id="100011" level="5">
<if_sid>100010</if_sid>
<field name="url">^/example-protected$</field>
<field name="status">^200$</field>
<description>Example proxy: request to protected path</description>
</rule>
<rule id="100012" level="10" frequency="5" timeframe="60">
<if_matched_sid>100011</if_matched_sid>
<same_srcip />
<description>Example proxy: repeated requests from one source</description>
</rule>
</group>
Rule 100012 sets frequency="5", the number of matches the rule must see before it generates an alert, within a window of timeframe="60" seconds and for the same srcip. The documentation shows this syntax on rule 30201: a rule with frequency="10" and timeframe="120" fires when rule 30201 matches 10 times in 120 seconds. In the logtest session from the documentation, rule 5712 with frequency="8" appears at the eighth pasted event. field elements in one rule work as a conjunction. Time-dependent rules are covered in the event correlation post.
Active response for the resulting rule
Active response binds an existing command to rules and extra criteria. First I define the command. The documentation of the SSH brute force blocking case tells you to check that the server’s /var/ossec/etc/ossec.conf contains a <command> block named firewall-drop:
<command>
<name>firewall-drop</name>
<executable>firewall-drop</executable>
<timeout_allowed>yes</timeout_allowed>
</command>
<name> gives the command its name, <executable> points to the script, and timeout_allowed set to yes means stateful active response, that is, the action is reverted after a time. The firewall-drop script adds the IP address to the iptables deny list.
The <active-response> block connects the command to the rule:
<active-response>
<disabled>no</disabled>
<command>firewall-drop</command>
<location>local</location>
<rules_id>100012</rules_id>
<timeout>60</timeout>
</active-response>
| Option | Description in the documentation | Values |
|---|---|---|
disabled | required in every block | yes, no |
command | name of a defined command | any defined name |
location | where to run the command | local, server, defined-agent, all |
agent_id | agent for defined-agent | agent number |
level | minimum alert level | 1 to 16 |
rules_group | rule group | name, several groups separated by | |
rules_id | triggering rules | ID, several separated by commas |
timeout | seconds until the reverting command | positive number |
repeated_offenders | increasing limits in minutes, up to 5 entries | positive number |
The location values mean: local runs the command on the agent that generated the event, server on the manager, defined-agent on the agent given in agent_id, and all on all agents without the manager. When level, rules_group and rules_id appear together, they are cumulative options, not restrictive ones: the response runs when any of them matches. To limit the response to one rule I therefore use only rules_id.
The repeated_offenders option sets increasing limits in minutes for returning sources. The documentation requires setting it in the agent’s ossec.conf, in the top-level <active-response> section, and does not support Windows agents. When it is used, timeout applies only to the first offense (vendor wording: “timeout only applies to the first offense”):
<active-response>
<disabled>no</disabled>
<repeated_offenders>1,5,10</repeated_offenders>
</active-response>
The active response script receives a JSON message with the full alert on STDIN. A shortened layout from the documentation, with my values:
{
"version":1,
"origin":{
"name":"agent-01",
"module":"wazuh-execd"
},
"command":"add",
"parameters":{
"extra_args":[],
"alert":{},
"program":"/var/ossec/active-response/bin/firewall-drop"
}
}
A stateful script handles the add and delete commands. It reads messages up to the newline character and ends every message it writes to STDOUT with that character, because otherwise a deadlock can occur. Configuration and scripts are covered in the active response article.
Testing in wazuh-logtest
/var/ossec/bin/wazuh-logtest uses the same rules as the analysis engine, and every session has a firedtimes counter. The phases are pre-decoding (syslog header), decoding (decoder and fields) and filtering (rule). For the first sample, phase 2 should show the decoder example-proxy and the fields http_method, srcip, url, status, and phase 3 rule 100011 at level 5. The result depends on the rules loaded on the manager, so I treat these values as expected, not recorded.
The test set changes one element at a time:
| Input | Expected result |
|---|---|
| unchanged sample | fields from phase 2, rule 100011 |
status=404 | fields decoded, 100011 does not match |
path=/example-other | fields decoded, 100011 does not match |
sample without src= | no field decoding, so 100011 and 100012 do not match |
five samples with one src in one session | 100012 appears, firedtimes grows |
samples with different src | same_srcip separates the series by address |
The sample without an address does not pass the pattern, because src=(\S+) is mandatory in it. The srcip field is not created, so rules based on decoded fields do not match. Before connecting active response I check the 100012 match alone in a separate session, and then the command behavior on a test system. The tester phases are described in the wazuh-logtest guide.
What the documentation does not define
The Wazuh documentation does not define the reverse proxy log format or the values that indicate an attack. It gives no universal thresholds for frequency, timeframe and timeout. It does not settle whether srcip in a given source is the client address or that of an intermediate proxy, and blocking a proxy address would affect many independent requests. It also does not describe whether decoded_as matches on the parent decoder alone when the child decoder did not match the fields. The status description in the documentation gives success and failure as examples, so I store the HTTP code as I decoded it. I choose the levels, thresholds and times in the examples only to show the syntax.
Sources
- https://documentation.wazuh.com/current/user-manual/ruleset/decoders/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/rules/custom.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/decoders.html
- https://documentation.wazuh.com/current/user-manual/ruleset/ruleset-xml-syntax/rules.html
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/active-response.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/ruleset/testing.html