# External alert analysis in Wazuh: the result as a secondary alert

> Connect external alert analysis to Wazuh through the Integrator: custom- options, script arguments, the queue socket, and a decoder and rules for the result.

Published: 2026-10-20. Updated: 2026-11-12. Category: Guide. Tags: Wazuh 4. URL: https://wazuh.blog/en/blog/ai-triage-wazuh/ Polish original: https://wazuh.blog/blog/ai-triage-wazuh/

An external service that analyzes alerts, for example a classifier based on a language model, has operational value only when its result returns to Wazuh as an event that you can filter, alert on and investigate. The pattern is the same as in the VirusTotal integration: the Integrator passes the alert to a script, the script queries the service and sends the result to the manager's queue, and a decoder and rules turn it into a new alert.

## Data flow

The whole thing rests on four elements, each described in the Wazuh documentation or in a script shipped with Wazuh:

1. An `integration` block in `/var/ossec/etc/ossec.conf` selects alerts and points to a script whose name starts with `custom-`.
2. The Integrator writes the matching alert to a temporary file in JSON format and runs the script from `/var/ossec/integrations/`.
3. The script calls the external service and sends the response to the manager's queue socket.
4. The `json` decoder and a rule with the `integration` field turn the response into a new alert.

The original alert stays unchanged in `alerts.json`. The script reads a copy of the alert from the temporary file and writes nothing to it, and the analysis result is created as a separate event with its own `rule.id`. This design matches how the VirusTotal integration works, where the FIM alert stays and next to it appears an alert from the `virustotal` group that refers to the `source.alert_id` field.

## The integration block in ossec.conf

The reference documentation describes the `integration` section as the configuration of the manager's connection to external APIs and alerting tools. A custom integration uses a name starting with `custom-`. The full description of the options is in a separate post on the [integrator and notifications](/en/blog/wazuh-integrator-notifications/), here only the ones that matter for alert analysis.

| Option | Meaning according to the documentation | Default value |
| --- | --- | --- |
| name | For a custom integration, any string starting with custom- | none |
| hook_url | Address used to communicate with the integrated software | none |
| api_key | Key for the external API | none |
| alert_format | Writes the alert file in json format, from which the Integrator takes field values | none |
| level | Passes alerts of the given level or higher, values 0 to 16 | none |
| rule_id | Filter by rule IDs, comma-separated list | none |
| group | Filter by rule groups, comma-separated list | none |
| event_location | Filter by event source, any sregex expression | none |
| options | JSON object that overrides fields or adds custom ones | none |
| timeout | Time to wait for the external server's response, in seconds | 10 |
| retries | Maximum number of retries after a timeout | 3 |

An example for an analysis service exposed at `example.com`. It is an illustration, and the values must be matched to your own service:

```xml
<integration>
  <name>custom-example-triage</name>
  <hook_url>https://triage.example.com/api/v1/analyze</hook_url>
  <api_key>EXAMPLE_API_KEY</api_key>
  <group>authentication_failures</group>
  <alert_format>json</alert_format>
  <timeout>30</timeout>
  <retries>3</retries>
</integration>
```

The documentation says to restart the manager after a configuration change: `systemctl restart wazuh-manager`.

Design recommendation: select alerts with `rule_id` or `group` rather than `level` alone. The documentation does not say whether the Integrator processes alerts generated from an integration result, so a level filter could also cover the result rules if their level is high enough. A narrower filter rules that out by design.

## The integration script

Rules from the documentation:

- The script lives in `/var/ossec/integrations/` and has the same name as the `name` value.
- It must have execute permissions and belong to the root user in the wazuh group:

```bash
chmod 750 /var/ossec/integrations/custom-example-triage
chown root:wazuh /var/ossec/integrations/custom-example-triage
```

- The first line names the interpreter, for example `#!/usr/bin/env python`.
- The first argument is the path to the temporary file with the alert, written as `/tmp/<INTEGRATION_NAME>-<TIMESTAMP>-<RANDOM>.alert`. The second is `api_key`, the third is `hook_url`. If an option is not set, the argument is empty.
- With `alert_format` set to `json`, load the file content as a JSON object. The documentation shows access to fields such as `rule.level`, `rule.id`, `rule.description`, `agent.id` and `agent.name`.

The documentation recommends looking at `/var/ossec/logs/alerts/alerts.json` before writing the script, to know the real shape of an alert. The field structure is described in the post on [alert triage](/en/blog/soc-triage/).

The VirusTotal script from version 4.14.0 additionally reads the arguments at positions 6 and 7 as `timeout` and `retries`, and the argument at position 4 equal to `debug` turns on logging to `logs/integrations.log`. A custom script should check the number of arguments instead of assuming they are present. To record integration activity in `/var/ossec/logs/integrations.log`, the VirusTotal documentation says to set `integrator.debug=1` in `/var/ossec/etc/local_internal_options.conf` and restart the manager.

## Returning the result through the queue socket

The documentation for custom scripts ends at reading the alert. The way to send the result back can be seen in the `integrations/virustotal.py` script from the Wazuh repository (tag v4.14.0). The `send_msg` function connects to the datagram socket `queue/sockets/queue` in the installation directory (for a script in `/var/ossec/integrations/` this is `/var/ossec/queue/sockets/queue`) and sends one text message:

```text
1:virustotal:{"integration": "virustotal", ...}
1:[001] (agent-01) any->virustotal:{"integration": "virustotal", ...}
```

The first form is for manager alerts or events without an agent. The second is for an agent: the agent ID in square brackets, its name in round brackets, then an IP address or `any`. In the location, the `|` character is replaced with `||` and `:` with `|:`. After the arrow comes the name of the event location, and after the colon the JSON with the result. A sketch of an adaptation for a custom integration, based on this script:

```python
import json
from socket import AF_UNIX, SOCK_DGRAM, socket

SOCKET_ADDR = "/var/ossec/queue/sockets/queue"

def send_result(result: dict, agent: dict | None) -> None:
    # Same message layout as the shipped VirusTotal script
    if not agent or agent["id"] == "000":
        message = "1:example-triage:{0}".format(json.dumps(result))
    else:
        location = "[{0}] ({1}) {2}".format(
            agent["id"], agent["name"], agent.get("ip", "any")
        )
        location = location.replace("|", "||").replace(":", "|:")
        message = "1:{0}->example-triage:{1}".format(location, json.dumps(result))
    sock = socket(AF_UNIX, SOCK_DGRAM)
    sock.connect(SOCKET_ADDR)
    sock.send(message.encode())
    sock.close()
```

When the socket is missing, the VirusTotal script exits with code 5 (`ERR_SOCKET_OPERATION`). A custom script should do the same and write the cause to a log instead of silently losing the result.

## The JSON contract

The result must be a flat, predictable object, because rules match single fields. The contract below is a custom design:

```json
{
  "integration": "example-triage",
  "example-triage": {
    "classification": "requires-review",
    "recommended_action": "manual_review",
    "reason": "Insufficient context for attribution",
    "source": {
      "alert_id": "1700000000.12345",
      "rule_id": "100123"
    }
  }
}
```

The layout imitates the VirusTotal result: the `integration` field on top, the data under a key named after the integration, and `source.alert_id` as a reference to the source alert. The allowed `classification` values should be a closed list (`false-positive`, `true-positive`, `requires-review`), and the script should reject a response outside the list instead of passing it on. The lack of a confident classification has its own value, `requires-review`, so there is no need to guess. Omit the `confidence` field or leave it empty until quality has been measured.

## Decoder and rules for the result

The built-in `json` decoder from `0006-json_decoders.xml` has a `prematch` of `^{\s*"` and the `JSON_Decoder` plugin, so a separate decoder is usually not needed. Custom decoders are covered in the post on [decoders](/en/blog/wazuh-custom-decoders/). We build the result rules like the VirusTotal rules from `0490-virustotal_rules.xml`: a level 0 base rule matches `decoded_as` and the `integration` field, and child rules match field values:

```xml
<group name="example_triage,">

  <rule id="100800" level="0">
    <decoded_as>json</decoded_as>
    <field name="integration">example-triage</field>
    <description>Example triage: integration messages</description>
    <options>no_full_log</options>
  </rule>

  <rule id="100801" level="3">
    <if_sid>100800</if_sid>
    <field name="example-triage.classification">false-positive</field>
    <description>Example triage: $(example-triage.source.rule_id) classified as false-positive</description>
    <options>no_full_log</options>
  </rule>

  <rule id="100802" level="7">
    <if_sid>100800</if_sid>
    <field name="example-triage.classification">requires-review</field>
    <description>Example triage: $(example-triage.source.rule_id) requires review</description>
    <options>no_full_log</options>
  </rule>

  <rule id="100803" level="10">
    <if_sid>100800</if_sid>
    <field name="example-triage.classification">true-positive</field>
    <description>Example triage: $(example-triage.source.rule_id) classified as true-positive</description>
    <options>no_full_log</options>
  </rule>

</group>
```

Levels 3, 7 and 10 are a recommendation. The level is set by an explicit map in the rules, not by a number returned by the service. Choosing levels is covered in the post on [rule levels and thresholds](/en/blog/wazuh-rule-levels-and-thresholds/). Test the rule before deployment. The `wazuh-logtest` tool, described in the post on [testing rules](/en/blog/wazuh-logtest/), accepts a raw line, so for the test you pass only the JSON from the contract, without the `1:` prefix and the location. The test checks the decoder and rule matching, but not the queue socket or the script. Verify those separately: run the script on a saved alert file with `integrator.debug=1` turned on, and check that a new alert with the expected `rule.id` appeared in `alerts.json`.

## The model result is untrusted data

The response of an external service is input from outside the trust boundary, just like log content. Three design rules follow:

- The result does not trigger active response. Rules 100801 to 100803 should not end up in any `active-response` block, and the decision on a reaction belongs to an analyst or to the deterministic gate described in the [response approval gate](/en/blog/response-approval-gate/). The [active response](/en/blog/wazuh-active-response/) mechanism runs commands on agents, and it makes no sense to make it depend on an unverified classification.
- Fields from the response reach the alert only from a fixed list. The `reason` text is free-form and may contain any characters, so in the rule description I use only fields with a finite set of values, such as `classification` and `source.rule_id`.
- The `false-positive` classification does not remove or silence the source alert. Lowering the severity of the original requires a separate, explicit rule change.

## Measuring classification quality

The Wazuh documentation gives no quality measure for external analysis, so this is my own recommendation. Before the result affects escalation, compare it with analysts' decisions on a labeled sample of alerts from the same rules. A confusion table is enough: how many alerts judged `false-positive` were real incidents, because that error costs the most. Repeat the measurement after every change to the analysis service, and until you have it, the result rules stay informational, with no effect on the escalation queue.

## What the documentation does not specify

- The message format in the queue socket is not described on the documentation pages used. It comes from the `virustotal.py` script from version 4.14.0. The meaning of the leading digit `1` is not explained.
- There is no information on whether the Integrator processes alerts created from an integration result (a feedback loop), how retrying works beyond the `timeout` and `retries` options, or what happens on a nonzero script exit code.
- The custom script documentation does not say whether `timeout` and `retries` reach the script as arguments. Only the VirusTotal script does this.
- The JSON contract schema, the result field names and the rule levels are the author's design. The Wazuh specification does not define them.

## Sources

- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/integration.html
- https://documentation.wazuh.com/current/user-manual/manager/integration-with-external-apis.html
- https://documentation.wazuh.com/current/user-manual/capabilities/virustotal-scan/index.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/integrations/virustotal.py
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/rules/0490-virustotal_rules.xml
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.0/ruleset/decoders/0006-json_decoders.xml
