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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ is at [docs.gen0sec.com](https://docs.gen0sec.com).
private keys live in an HSM or other PKCS#11 token, beside a software key,
with the PIN in a file. The demo runs SoftHSM in docker. Needs Synapse 0.8.9
or later.
- [Lua snippets on requests](./use-cases/axon-access/): an axon snippet runs on
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.

### Being updated

Expand Down
1 change: 1 addition & 0 deletions use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ closest to what you want and copy it. Every set uses placeholder values
| [`header-rules`](./header-rules/) | proxy | no | none: the demo listens on port 8080 | Add and remove request and response headers, with a script that checks every rule |
| [`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 |

"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
2 changes: 2 additions & 0 deletions use-cases/axon-access/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logs/*
!logs/.gitkeep
65 changes: 65 additions & 0 deletions use-cases/axon-access/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# axon: a Lua snippet on every request

Run a short Lua script on every request the WAF allowed. It can tag a request
in the access log, and block it. This is axon, in the style of OpenResty's
`access_by_lua_file`. Needs Synapse 0.8.10 or later, **built with the `axon`
cargo feature**, which is off by default.

`access.lua` does three things:

| What | How | Result |
|---|---|---|
| Block a path | `axon.re.match(axon.ctx.path, "^/admin")` | 403 |
| Limit a client to 30 requests a minute | `axon.shared.hits:incr(ip, 1, 0, 60)` | 403 above 30 |
| Tag a scanner | the user agent starts with `sqlmap` | the request passes; `axon_tag` is `scanner-ua` in `access.log` |

Things to know:

- The snippet runs only for requests the WAF **allowed**. A request the WAF
blocked or challenged never reaches it.
- A snippet returns `nil`, `{ tag = "x" }` or `{ action = "block", tag = "x" }`.
A block is enforced only when the phase sets `allow_block: true`. Without it,
the block is logged and counted and nothing more. A snippet cannot allow a
request or change one.
- `axon.ctx.path` is the path the WAF judged: `/%61dmin`, `//admin` and
`/x/../admin` are all `/admin`, so a respelled path does not get past it.
- A shared dictionary must be declared in `snippet.shared_dicts`. It is
process-wide: every worker thread sees the same counters. It is sized in
entries, and a full dictionary drops the oldest.
- Time is bounded: a snippet that runs longer than the deadline (10 ms for
this phase) is stopped, and a phase that fails 10 times in a row switches
itself off until the next successful reload. A request is never failed by an
error in a snippet.
- A snippet is code that runs in the proxy. Anyone who can write the file can
run code there, so the loader refuses a file that is world-writable, that
another group can write, or that is owned by a user other than root and the
user Synapse runs as. In docker, make the mounted `access.lua` readable by
that user and owned by root or by it.
- `snippet.required: true` makes a start fail when the snippet cannot load, so
the proxy does not run without its rules. A reload that fails keeps the
last snippet that worked.
- This example counts the address Synapse sees. Behind a load balancer, set
`proxy.trusted_proxies` so that it counts the client's address.

## Run and test

Use an image of Synapse 0.8.10 that was built with the `axon` feature:

```sh
export SYNAPSE_IMAGE=<your axon image>
docker compose up -d
./test.sh # prints "ok" or "FAIL" per check and exits 1 if any fails
docker compose down
```

The rate limit counts a minute, so wait 60 seconds before you run `test.sh`
again. A binary without the feature starts, logs a warning that it was built
without `axon`, and runs no snippet (with `required: true` it refuses to
start); `test.sh` then fails on the first block.

To see the test fail, change `"^/admin"` to `"^/nothing"` in `access.lua`,
run `docker compose restart synapse` and `./test.sh` again.

`synapse --check-config` also validates the snippet: it reports a Lua syntax
error, a snippet that is not found or not safe to load, and a build without the
feature.
33 changes: 33 additions & 0 deletions use-cases/axon-access/access.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- access_by_axon: runs once for every request the WAF allowed.
--
-- A snippet can observe and annotate, and block when the phase sets
-- `allow_block: true`. It cannot allow a request the WAF blocked.
-- What it returns:
-- nil nothing happens
-- { tag = "x" } the tag goes to the access log
-- { action = "block", tag = "x" } the proxy answers 403

-- 1. A path nobody should reach from outside.
if axon.re.match(axon.ctx.path, "^/admin") then
return { action = "block", tag = "admin-path" }
end

-- 2. A client that makes more than LIMIT requests a minute. `incr` adds 1
-- to the counter of this address, starts it at 0 and lets it expire after
-- 60 seconds. The dict is nil when it is not declared in config.yaml.
local LIMIT = 30
local hits = axon.shared.hits
if hits then
local n = hits:incr(axon.ctx.client_ip, 1, 0, 60)
if n and n > LIMIT then
return { action = "block", tag = "rate-limit" }
end
end

-- 3. A scanner is only tagged. The tag shows in the access log.
local ua = axon.ctx.user_agent
if ua and axon.re.match(ua, "^sqlmap") then
return { tag = "scanner-ua" }
end

return nil
39 changes: 39 additions & 0 deletions use-cases/axon-access/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# axon-access: a Lua snippet that runs on every request the WAF allowed.
# Needs Synapse 0.8.10 or later, built with the `axon` feature.
mode: proxy

firewall:
mode: none
capture:
enabled: false

proxy:
listeners:
- { name: http, address: "0.0.0.0:8080", mode: http }
upstream:
conf: "/etc/synapse/upstreams.yaml"
acme:
enabled: false

logging:
level: info
# The snippet's tag is written to access.log, as performance.axon_tag.
file_logging_enabled: true
log_directory: /var/log/synapse

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

# The shared dictionary every worker thread sees. A dict that is not
# declared does not exist: axon.shared.hits is nil. Sized in entries.
shared_dicts:
- name: hits
entries: 10000

access_by_axon:
file: access.lua
# A relative path is read from the working directory; the compose file sets it.
# Without this a block is recorded as a log entry and never enforced.
allow_block: true
15 changes: 15 additions & 0 deletions use-cases/axon-access/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Run: docker compose up -d && ./test.sh
# SYNAPSE_IMAGE must be a Synapse 0.8.10 image built with the `axon` feature.
services:
synapse:
image: ${SYNAPSE_IMAGE:-registry.gen0sec.com/gen0sec/synapse:0.8.10}
working_dir: /etc/synapse
command: ["-c", "/etc/synapse/config.yaml"]
ports: ["8090:8080"]
volumes:
- ".:/etc/synapse:ro"
- "./logs:/var/log/synapse"
depends_on: [whoami]
whoami:
image: traefik/whoami:v1.10.3
hostname: whoami
Empty file.
28 changes: 28 additions & 0 deletions use-cases/axon-access/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Check the snippet against the running example. Exit 1 if any check fails.
# docker compose up -d && ./test.sh
# The rate limit counts a minute: run it again after 60 seconds.
set -u
cd "$(dirname "$0")" || exit 2
base=${BASE:-http://localhost:8090}
fail=0
ok() { echo "ok $1"; }
bad() { echo "FAIL $1"; fail=1; }
code() { curl -s -o /dev/null -w '%{http_code}' -H 'Host: app.example.com' "$@"; }

[ "$(code "$base/")" = 200 ] && ok "a plain request passes" || bad "a plain request did not pass"

[ "$(code "$base/admin/panel")" = 403 ] && ok "/admin is blocked" || bad "/admin was not blocked"

# A scanner is let through and tagged in the access log.
[ "$(code -A sqlmap/1.7 "$base/tagged")" = 200 ] && ok "a scanner user agent is not blocked" || bad "the scanner request failed"
sleep 1
grep -h '/tagged' logs/access.log 2>/dev/null | grep -q '"axon_tag":"scanner-ua"' \
&& ok "the scanner request carries the tag in access.log" || bad "no scanner-ua tag in logs/access.log"

# More than 30 requests in a minute from one address are blocked.
last=200
for _ in $(seq 1 40); do last=$(code "$base/burst"); done
[ "$last" = 403 ] && ok "the 40th request in a minute is blocked" || bad "the burst was not limited (last status $last)"

exit $fail
7 changes: 7 additions & 0 deletions use-cases/axon-access/upstreams.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

hosts:
app.example.com:
paths:
"/":
upstream: whoami:80