Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com).
- [Lua snippets on flows](./use-cases/axon-flow/): an axon snippet runs on every
flow the smart firewall evaluated and tags it. Needs Synapse 0.8.10 built
with the `axon` feature.
- [Scan detection with a snippet pipeline](./use-cases/axon-scan-detection/):
four axon files in one pipeline count in shared dictionaries and tag
outgoing port scans and sweeps and an outside /24 that scans together, with
a script that runs each scan and a control. Needs Synapse 0.8.10 built with
the `axon` feature.
- [Blocking on a threat score plus evidence](./use-cases/axon-threat-block/): an
axon snippet blocks an outside source with a middling threat score once it
also trips the IDS, the traffic classifier or a port count, with a script
that checks what it blocks and what it leaves. Needs Synapse 0.8.10 built
with the `axon` feature and an API key.

### Being updated

Expand Down
2 changes: 2 additions & 0 deletions use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ closest to what you want and copy it. Every set uses placeholder values
| [`kubernetes-cert-manager`](./kubernetes-cert-manager/) | proxy | no | a privileged proxy pod, Kubernetes cluster | A `SynapseProxy` of the Synapse operator serving an Ingress with a certificate from cert-manager, and the proxy picking up a renewed certificate without a restart; needs Synapse operator 0.2.0 (not released yet) and Synapse 0.8.8 or later |
| [`axon-access`](./axon-access/) | proxy | no | none: the demo listens on port 8090 | A Lua snippet on every request the WAF allowed: block a path, rate-limit a client with a shared dictionary, tag a scanner in the access log; needs Synapse 0.8.10 built with the `axon` feature |
| [`axon-flow`](./axon-flow/) | agent | no | the same as `smart-firewall` | A Lua snippet on every flow the smart firewall evaluated: tag a flow with a named expression and by field; needs Synapse 0.8.10 built with the `axon` feature |
| [`axon-scan-detection`](./axon-scan-detection/) | agent | no | the same as `smart-firewall` | A pipeline of Lua snippets with shared dictionaries that tags outgoing port scans and sweeps and an outside /24 scanning together; tags only; needs Synapse 0.8.10 built with the `axon` feature |
| [`axon-threat-block`](./axon-threat-block/) | agent | yes | the same as `smart-firewall` | A Lua snippet that blocks an outside source with a threat score of 50-89 once it also trips the IDS, the traffic classifier or 10+ ports; needs Synapse 0.8.10 built with the `axon` feature |

"Privileges" lists what the set's own config needs; the installation guide
lists what the container image needs on top. Installation is covered in the
Expand Down
22 changes: 22 additions & 0 deletions use-cases/axon-scan-detection/00_common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Stage 0 of the pipeline: work out once per flow what every later stage
-- needs, and hand it down in axon.stash, a table that lives for this one
-- flow and is shared by every stage of the pipeline.
--
-- stash.syn a client's opening SYN (SYN without ACK)
-- stash.inbound an outside source reaching a HOME_NET address
-- stash.outbound a HOME_NET source reaching an outside address
--
-- "Outside" and "home" come from ids.address_vars.HOME_NET in config.yaml.
-- A stage returns nil or { tag = "..." }; tags add up across the stages.

local ctx = axon.ctx
local s = axon.stash

local flags = ctx.tcp_flags
s.syn = ctx.proto == 6 and flags ~= nil and (flags & 0x12) == 0x02

local src_home, dst_home = ctx.ids_src_home_net, ctx.ids_dst_home_net
s.inbound = src_home == false and dst_home == true
s.outbound = src_home == true and dst_home == false

return nil
34 changes: 34 additions & 0 deletions use-cases/axon-scan-detection/10_outbound_portscan.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- outbound-portscan: one of your hosts opening connections to many ports of
-- one outside host (a vertical scan), as a compromised or misused machine
-- does.
--
-- Counts the distinct destination ports of each (source, destination) pair
-- for WINDOW seconds from the pair's first connection, and tags the flow
-- that reaches PORTS, once per pair every REPEAT seconds.

local PORTS = 25
local WINDOW = 600
local REPEAT = 3600

local s = axon.stash
if not (s.syn and s.outbound) then
return nil
end
local seen, counts = axon.shared.scan_seen, axon.shared.scan_counts
if not seen or not counts then
return nil
end

local ctx = axon.ctx
local pair = ctx.src_ip .. ">" .. ctx.dst_ip
-- add() succeeds only for a key that is not there yet: a port not yet seen
-- for this pair in the window.
if not seen:add("vp|" .. pair .. "|" .. ctx.dst_port, true, WINDOW) then
return nil
end
local n = counts:incr("vp|" .. pair, 1, 0, WINDOW)
if n and n >= PORTS and seen:add("vp!" .. pair, true, REPEAT) then
axon.log.warn("outbound-portscan " .. pair .. " ports=" .. n)
return { tag = "outbound-portscan" }
end
return nil
42 changes: 42 additions & 0 deletions use-cases/axon-scan-detection/20_outbound_sweep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- outbound-sweep: one of your hosts opening connections to one port on many
-- outside hosts (a horizontal scan for SSH, RDP, SMB, databases, ...).
--
-- Web, DNS, mail, time, push and VPN ports are left out: browsers and apps
-- reach hundreds of hosts on those. Counts the distinct destinations of each
-- (source, port) for WINDOW seconds and tags the flow that reaches HOSTS,
-- once per (source, port) every REPEAT seconds.

local HOSTS = 40
local WINDOW = 600
local REPEAT = 3600

local COMMON = {
[53] = true, [80] = true, [123] = true, [443] = true, [853] = true,
[465] = true, [587] = true, [993] = true, [995] = true, [5223] = true,
[5228] = true, [3478] = true, [8080] = true, [8443] = true, [51820] = true,
}

local s = axon.stash
if not (s.syn and s.outbound) then
return nil
end
local ctx = axon.ctx
local port = ctx.dst_port
if COMMON[port] then
return nil
end
local seen, counts = axon.shared.scan_seen, axon.shared.scan_counts
if not seen or not counts then
return nil
end

local key = ctx.src_ip .. ":" .. port
if not seen:add("hs|" .. key .. "|" .. ctx.dst_ip, true, WINDOW) then
return nil
end
local n = counts:incr("hs|" .. key, 1, 0, WINDOW)
if n and n >= HOSTS and seen:add("hs!" .. key, true, REPEAT) then
axon.log.warn("outbound-sweep " .. key .. " hosts=" .. n)
return { tag = "outbound-sweep" }
end
return nil
48 changes: 48 additions & 0 deletions use-cases/axon-scan-detection/30_inbound_subnet_scan.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- inbound-subnet-scan: several addresses of one outside /24 that together
-- probe many ports of your network. Internet-wide scanners spread a scan
-- over dozens of addresses, each touching a few ports an hour, so no single
-- address reaches a per-source port-count rule. Summed per /24 they stand
-- out, and the tag names the network to block.
--
-- Counts the distinct destination ports and the distinct source addresses
-- of each /24 for WINDOW seconds from its first probe, and tags the flow
-- that reaches both PORTS and SOURCES, once per /24 every REPEAT seconds.
-- IPv4 only.

local PORTS = 15
local SOURCES = 3
local WINDOW = 3600
local REPEAT = 21600

local s = axon.stash
if not (s.syn and s.inbound) then
return nil
end
local seen, counts = axon.shared.scan_seen, axon.shared.scan_counts
if not seen or not counts then
return nil
end

local ctx = axon.ctx
local src = ctx.src_ip
local a, b, c = string.match(src, "^(%d+)%.(%d+)%.(%d+)%.%d+$")
if not a then
return nil
end
local net = a .. "." .. b .. "." .. c .. ".0/24"

local ports = counts:get("snp|" .. net)
if seen:add("snp|" .. net .. "|" .. ctx.dst_port, true, WINDOW) then
ports = counts:incr("snp|" .. net, 1, 0, WINDOW)
end
local sources = counts:get("sns|" .. net)
if seen:add("sns|" .. net .. "|" .. src, true, WINDOW) then
sources = counts:incr("sns|" .. net, 1, 0, WINDOW)
end

if ports and sources and ports >= PORTS and sources >= SOURCES
and seen:add("sn!" .. net, true, REPEAT) then
axon.log.warn("inbound-subnet-scan " .. net .. " ports=" .. ports .. " sources=" .. sources)
return { tag = "inbound-subnet-scan" }
end
return nil
76 changes: 76 additions & 0 deletions use-cases/axon-scan-detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# axon: scan detection with a snippet pipeline

Tag port scans and sweeps, outgoing and incoming, with a pipeline of short Lua
files that count over time windows in shared dictionaries. Needs Synapse
0.8.10 or later, **built with the `axon` cargo feature**, and with
`amygdala-reactor` (in the standard packages), on Linux. For one snippet
file and named expressions, start with [axon-flow](../axon-flow/).

The pipeline (`files:` in `config.yaml`) runs these files in order on every
flow the smart firewall evaluated:

| File | Tag | When |
|---|---|---|
| `00_common.lua` | none | Works out once per flow whether it is a client's opening SYN, and whether it goes from home to outside or from outside to home, and hands that to the other stages in `axon.stash` |
| `10_outbound_portscan.lua` | `outbound-portscan` | One home host opens connections to 25+ ports of one outside host within 10 minutes (a vertical scan) |
| `20_outbound_sweep.lua` | `outbound-sweep` | One home host opens connections to one port on 40+ outside hosts within 10 minutes (a horizontal scan). Web, DNS, mail, time, push and VPN ports are left out |
| `30_inbound_subnet_scan.lua` | `inbound-subnet-scan` | 3+ addresses of one outside /24 probe 15+ ports of your network within an hour. Internet-wide scanners spread a scan over many addresses, each touching a few ports, so no single address reaches a per-source rule |

Each tag is given once per pair, port or /24 per hour (six hours for the /24),
so a long scan is one entry, not one per connection.

Things to know:

- **Home and outside** come from `ids.address_vars.HOME_NET`. Put all your
networks there, public addresses included. The IDS does not have to be on.
- **A pipeline is one snippet in stages.** The files share one Lua VM, one time
limit and one error budget. Tags add up in order; an error in any stage
leaves the flow unannotated. At most 16 files.
- **`axon.stash`** is a table that lives for one flow and is shared by its
stages. Use it to work something out once (here, the direction).
- **Shared dictionaries** (`snippet.shared_dicts`) keep counts across flows.
`add` writes a key only when it is not there yet, so "a port not seen for
this pair" is one call. `incr` counts, and the key keeps the expiry it got
when it was created, so a window runs from its first event. `entries` is a
number of keys; a full dictionary drops its least recently used keys first.
The dictionaries live in the process: a restart empties them, a reload
(`SIGHUP`) keeps them.
- **Thresholds** are the constants at the top of each file. Change them there.
- **Every stage only tags.** A tag is an `axon:<tag>` log entry next to the
smart firewall's matches, in `events.log` and the CEF output. The stages also
log one line with the numbers behind a tag, such as
`outbound-portscan 10.0.0.5>198.51.100.1 ports=25`, to the application log.
To block, see [axon-threat-block](../axon-threat-block/).

The privileges, kernel and interface settings are those of the
[smart-firewall](../smart-firewall/) set, which this config is based on.

## Test

`test.sh` runs this set's `ids:` and `snippet:` blocks on live captured
traffic:

```sh
sudo SYNAPSE_BIN=/path/to/synapse ./test.sh # "ok" or "FAIL" per check, exit 1 if any fails
```

It needs root, `ip`, `python3` and `tcpdump` (the agent compiles its
AF_PACKET filter with `tcpdump -dd`), and a Synapse built with `axon`. It
makes two network namespaces joined by a veth pair and runs the agent in one,
with AF_PACKET capture and `firewall.mode: none`. From the other it runs each
scan and a control that must not be tagged:

| Traffic | Expected |
|---|---|
| 10.0.0.5 to 30 ports of 198.51.100.1 | one `outbound-portscan` |
| 10.0.0.5 to 20 ports of 198.51.100.2 | no tag |
| 10.0.0.5 to port 2222 on 50 hosts | one `outbound-sweep` |
| 10.0.0.5 to port 443 on the same 50 hosts | no tag |
| five addresses of 203.0.113.0/24, 20 ports in all, then two more addresses on 10 more ports | one `inbound-subnet-scan` |
| five addresses of 198.18.0.0/24, 10 ports in all | no tag |

The host's interfaces and firewall are not touched, and the namespaces are
removed when it ends.

To see the test fail, change `local PORTS = 25` to `local PORTS = 35` in
`10_outbound_portscan.lua` and run it again.
59 changes: 59 additions & 0 deletions use-cases/axon-scan-detection/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# axon-scan-detection: a pipeline of Lua snippets that tags port scans and
# sweeps on every flow the smart firewall evaluated. Needs Synapse 0.8.10 or
# later, built with the `axon` feature (and with `amygdala-reactor`, which the
# standard packages have).
mode: agent

firewall:
enabled: true
mode: xdp
amygdala:
enabled: true
backend: auto

capture:
enabled: true
interface: eth0

# The snippets read "home" and "outside" from HOME_NET
# (axon.ctx.ids_src_home_net / ids_dst_home_net). Put your own networks here,
# public addresses included. The IDS does not need to be on for this.
ids:
address_vars:
HOME_NET: "[10.0.0.0/8,172.16.0.0/12,192.168.0.0/16]"
EXTERNAL_NET: "!$HOME_NET"

logging:
level: info
file_logging_enabled: true
log_directory: "/var/log/synapse"
events_log_format: json
eventbridge_log:
enabled: true

snippet:
enabled: true
# Stop the start when the snippets cannot load, instead of running without.
required: true

# Process-wide dictionaries the stages count in. A name that is not declared
# here reads as nil in a snippet. `entries` is a number of keys, not bytes;
# a full dictionary drops its least recently used keys first.
shared_dicts:
- name: scan_seen
entries: 200000
- name: scan_counts
entries: 50000

flow_by_axon:
# A pipeline: the files run in this order on every flow, in one Lua VM,
# as one snippet split into stages. Tags add up; axon.stash carries
# values from one stage to the next for the same flow.
files:
- 00_common.lua
- 10_outbound_portscan.lua
- 20_outbound_sweep.lua
- 30_inbound_subnet_scan.lua
# Off: the stages only tag. A tag is an `axon:<tag>` log entry next to
# the smart firewall's own matches.
allow_block: false
Loading