diff --git a/README.md b/README.md index 19eb10f..aa8308f 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,9 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com). every request the WAF allowed, and blocks a path, rate-limits a client and tags a scanner, with a script that checks each. Needs Synapse 0.8.10 built with the `axon` feature. +- [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. ### Being updated diff --git a/use-cases/README.md b/use-cases/README.md index be1417c..c275400 100644 --- a/use-cases/README.md +++ b/use-cases/README.md @@ -29,6 +29,7 @@ closest to what you want and copy it. Every set uses placeholder values | [`hsm-tls-keys`](./hsm-tls-keys/) | proxy | no | none: the demo listens on ports 8085 and 8443 | TLS private keys in an HSM over PKCS#11 (RSA and EC), served beside a software key, with the PIN in a file; the demo uses SoftHSM in docker; needs Synapse 0.8.9 or later | | [`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 | "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 diff --git a/use-cases/axon-flow/README.md b/use-cases/axon-flow/README.md new file mode 100644 index 0000000..0612541 --- /dev/null +++ b/use-cases/axon-flow/README.md @@ -0,0 +1,56 @@ +# axon: a Lua snippet on every flow + +Run a short Lua script on every flow the smart firewall evaluated, and tag the +flows you care about. Needs Synapse 0.8.10 or later, **built with the `axon` +cargo feature** (off by default) and with `amygdala-reactor` (in the standard +packages), on Linux. For the proxy-side phase, see [axon-access](../axon-access/). + +`flow.lua` does two things: + +| What | How | Result | +|---|---|---| +| Tag a flow to port 443 with a high threat score | the named expression `tls_to_risky`, asked for with `axon.filter("tls_to_risky")` | `axon:tls-to-risky` log-only entry | +| Tag SSH from a public address | `axon.ctx.dst_port == 22 and not axon.ip.is_private(axon.ctx.src_ip)` | `axon:ssh-from-public` log-only entry | + +Things to know: + +- The snippet sees the whole field set the smart firewall evaluates: packet + facts, the JA4 family, TLS, DNS and HTTP fields, geography, threat, IDS and + the smart firewall's own verdicts in `axon.ctx.matches`. A value the capture + did not see reads as `nil`, never as zero. +- A tag is recorded as an `axon:` log entry next to the smart firewall's + matches. A tag is printable ASCII, at most 64 bytes, without `,`, `"` or `\`. +- `allow_block` is off here. Set it to `true` and a snippet that returns + `{ action = "block", tag = "x" }` makes a smart-firewall drop of the source + address, through the same never-ban guards as a smart-firewall rule. It never + overrides an explicit allow. +- `filters:` are compiled when the file loads. A bad expression rejects the + file, and the snippet that was serving keeps serving; asking for a name that + is not declared is a snippet error. +- Shared dictionaries (`snippet.shared_dicts`) are process-wide. The proxy and + agent phases see the same dictionary only inside one process. +- The same safety rules as in [axon-access](../axon-access/) apply: a time + limit per call, a circuit breaker, and a loader that refuses a file that is + world-writable or owned by another user. + +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 `snippet:` block 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` and `python3`, and a Synapse built with `axon`. It makes +two network namespaces joined by a veth pair, runs the agent in one with +AF_PACKET capture and `firewall.mode: none`, and connects from a public and a +private address in the other. The host's interfaces and firewall are not +touched, and the namespaces are removed when it ends. The threat database is +[`test/threat.mmdb`](./test/threat.mmdb), which scores the documentation range +203.0.113.0/24 at 90 and knows no other address. + +To see the test fail, change `not axon.ip.is_private` to `axon.ip.is_private` +in `flow.lua` and run it again. diff --git a/use-cases/axon-flow/config.yaml b/use-cases/axon-flow/config.yaml new file mode 100644 index 0000000..62bf8fe --- /dev/null +++ b/use-cases/axon-flow/config.yaml @@ -0,0 +1,39 @@ +# axon-flow: a Lua snippet that runs 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 + +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 snippet cannot load, instead of running without it. + required: true + + flow_by_axon: + file: flow.lua + # Off: a block the snippet returns is recorded as a log entry and never + # enforced. This example only tags. + allow_block: false + # Named wirefilter expressions, in the smart firewall's field vocabulary. + # The snippet asks for one with axon.filter("name"). A bad expression + # rejects the file, and the snippet that was serving keeps serving. + filters: + tls_to_risky: 'tcp.dst_port eq 443 and threat.score ge 80' diff --git a/use-cases/axon-flow/flow.lua b/use-cases/axon-flow/flow.lua new file mode 100644 index 0000000..c51eaa6 --- /dev/null +++ b/use-cases/axon-flow/flow.lua @@ -0,0 +1,25 @@ +-- flow_by_axon: runs once for every flow the smart firewall evaluated. +-- +-- It returns nil, or { tag = "x" }. The agent records the tag as an +-- `axon:` log-only entry next to the smart firewall's own matches. +-- This snippet never blocks: allow_block is off. + +-- Leave a flow alone that is already being dropped. +for _, m in ipairs(axon.ctx.matches) do + if m.action == "drop" then + return nil + end +end + +-- A named expression from config.yaml, judged on this flow. +if axon.filter("tls_to_risky") then + return { tag = "tls-to-risky" } +end + +-- SSH from an address outside the private ranges. Helpers are pure and +-- bounded; a value that is not an address yields false, never an error. +if axon.ctx.dst_port == 22 and not axon.ip.is_private(axon.ctx.src_ip) then + return { tag = "ssh-from-public" } +end + +return nil diff --git a/use-cases/axon-flow/test.sh b/use-cases/axon-flow/test.sh new file mode 100755 index 0000000..cbe667e --- /dev/null +++ b/use-cases/axon-flow/test.sh @@ -0,0 +1,121 @@ +#!/bin/sh +# Check flow.lua on live captured traffic. Exit 1 if any check fails. +# sudo SYNAPSE_BIN=/path/to/synapse ./test.sh +# +# Needs root, ip (iproute2) and python3. Everything runs in two network +# namespaces made for the run and joined by a veth pair: the agent captures +# on its end with AF_PACKET and `firewall.mode: none`, so the host's +# interfaces and firewall are not touched. The namespaces are removed on exit. +# The agent runs this set's `snippet:` block as it is, with test/threat.mmdb +# as its threat database (203.0.113.0/24 scored 90). +set -u +cd "$(dirname "$0")" || exit 2 +here=$(pwd) +bin=${SYNAPSE_BIN:-synapse} +bin=$(command -v "$bin") || { echo "error: synapse binary not found; set SYNAPSE_BIN" >&2; exit 2; } +[ "$(id -u)" -eq 0 ] || { echo "error: run as root (AF_PACKET capture and network namespaces)" >&2; exit 2; } +for t in ip python3; do command -v $t >/dev/null || { echo "error: $t is required" >&2; exit 2; }; done + +id=$$ +cli=axf-cli-$id srv=axf-srv-$id vc=axfc$id vs=axfs$id +work=$(mktemp -d) || exit 2 +apid='' lpid='' +cleanup() { + for p in "$apid" "$lpid"; do + if [ -n "$p" ] && [ "$p" -gt 1 ] && kill -0 "$p" 2>/dev/null; then kill -TERM "$p"; fi + done + sleep 1 + for p in "$apid" "$lpid"; do + if [ -n "$p" ] && [ "$p" -gt 1 ] && kill -0 "$p" 2>/dev/null; then kill -KILL "$p"; fi + done + ip netns del "$cli" 2>/dev/null + ip netns del "$srv" 2>/dev/null + rm -rf -- "${work:?}" +} +trap cleanup EXIT INT TERM + +# --- two namespaces: the client has a public and a private address +ip netns add "$cli" && ip netns add "$srv" && ip link add "$vc" type veth peer name "$vs" || exit 2 +ip link set "$vc" netns "$cli" && ip link set "$vs" netns "$srv" || exit 2 +ip -n "$cli" addr add 203.0.113.5/24 dev "$vc" +ip -n "$cli" addr add 10.0.0.5/24 dev "$vc" +ip -n "$srv" addr add 198.51.100.1/24 dev "$vs" +for n in "$cli" "$srv"; do ip -n "$n" link set lo up; done +ip -n "$cli" link set "$vc" up +ip -n "$srv" link set "$vs" up +ip -n "$cli" route add 198.51.100.0/24 dev "$vc" +ip -n "$srv" route add 203.0.113.0/24 dev "$vs" +ip -n "$srv" route add 10.0.0.0/24 dev "$vs" + +# --- listeners on 22, 443 and 8080 that accept and close +ip netns exec "$srv" python3 -I -c ' +import socket, threading +def serve(port): + s = socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind(("198.51.100.1", port)); s.listen(64) + while True: + c, _ = s.accept(); c.close() +for p in (22, 443, 8080): + threading.Thread(target=serve, args=(p,), daemon=True).start() +threading.Event().wait()' & +lpid=$! + +# --- the agent: this set's snippet block, captured on the veth +cp flow.lua test/threat.mmdb "$work"/ +chmod 644 "$work/flow.lua" +{ + cat < "$work/config.yaml" +(cd "$work" && exec ip netns exec "$srv" "$bin" -c config.yaml --firewall-mode none >"$work/agent.out" 2>&1) & +apid=$! + +logs() { cat "$work/agent.out" "$work"/logs/*.log 2>/dev/null; } +i=0 +until logs | grep -q "AF_PACKET capture thread running on '$vs'"; do + i=$((i + 1)) + [ $i -le 60 ] || { echo "FAIL the capture did not start"; logs | tail -20; exit 1; } + sleep 1 +done + +connect() { + ip netns exec "$cli" python3 -I -c " +import socket +for _ in range(5): + s = socket.socket(); s.bind(('$1', 0)); s.settimeout(2) + try: s.connect(('198.51.100.1', $2)) + except OSError: pass + s.close()" +} +n=0 +while [ $n -lt 10 ]; do + connect 203.0.113.5 22; connect 10.0.0.5 22; connect 203.0.113.5 443; connect 203.0.113.5 8080 + sleep 1; n=$((n + 1)) +done +sleep 2 + +fail=0 +ok() { echo "ok $1"; } +bad() { echo "FAIL $1"; fail=1; } +has() { logs | grep -q "rule 'axon:$1' matched src $2 .*LOG only"; } + +logs | grep -q "generation 1 loaded" && ok "the snippet loaded" || bad "the snippet did not load" +has ssh-from-public 203.0.113.5 && ok "SSH from a public address is tagged ssh-from-public" || bad "no ssh-from-public tag for 203.0.113.5" +has ssh-from-public 10.0.0.5 && bad "SSH from a private address was tagged" || ok "SSH from a private address is not tagged" +has tls-to-risky 203.0.113.5 && ok "443 from an address scored 90 is tagged tls-to-risky (filters:)" || bad "no tls-to-risky tag for 203.0.113.5" +logs | grep "rule 'axon:" | grep -qv "LOG only" && bad "a tag was not log only" || ok "every tag is log only" +logs | grep -q "axon\[flow\]" && bad "the snippet logged an error" || ok "no snippet error" + +exit $fail diff --git a/use-cases/axon-flow/test/threat.mmdb b/use-cases/axon-flow/test/threat.mmdb new file mode 100644 index 0000000..288ca8f Binary files /dev/null and b/use-cases/axon-flow/test/threat.mmdb differ