# Write a correlation rule

A rule turns raw events into a finding. It is a YAML file in `correlations/`,
embedded at build time.

```yaml
id: many_open_ports
version: 1
meta:
  name: Host exposes many open ports
  description: >
    A host was found with an unusually large number of open TCP ports, which
    widens its attack surface.
  risk: MEDIUM                 # HIGH | MEDIUM | LOW | INFO
collections:
  - collect:
      - method: exact
        field: type
        value: TCP_PORT_OPEN
aggregation:
  field: entity.data           # one finding per host/IP
analysis:
  - method: threshold          # fire only when a bucket has 8+ matches
    field: type
    minimum: 8
headline: "Host exposes many open ports: {entity.data}"
```

## How it evaluates

1. **collect** — keep events whose fields match. `method` is `exact` or `regex`;
   `field` is `type`, `data`, `module`, or a walked path (`source.*`, `entity.*`,
   `child.*`). Prefix a value with `not ` to negate. A collection can have several
   matchers (all must pass) and a rule several collections.
2. **aggregation** — bucket the surviving events by a field. You get one finding per
   bucket.
3. **analysis** — optional passes: `threshold` (bucket survives if a value occurs
   between `minimum` and `maximum` times), `outlier` (keep buckets below
   `maximum_percent` of the total), `first_collection_only`,
   `match_all_to_first_collection`.
4. **headline** — the finding title. `{field}` placeholders are filled from the
   first event, e.g. `{data}`, `{source.data}`, `{entity.data}`.

## Test it

Add a unit test in `crates/spiderfootplus-core/src/correlation.rs` that builds a small event
graph and asserts the rule fires (and does **not** fire below threshold). Then:

```sh
cargo test -p spiderfootplus-core correlation
spiderfootplus rules                  # confirm it loads with the right risk
spiderfootplus correlate <scan-id>    # re-run rules against a stored scan
```
