- README rewritten for a first-time reader: the gate and its rule order, quick start, all 45 tools by group, transports, proxy and rewrite rules, findings/ assets/traffic/reports, state layout, security model, limits, build and release targets, install layout, environment, source layout, 1.x promise. - License changed to CNCL 1.1 (Could Not Care Less License). LICENSE carries the unchanged text; README, SECURITY.md and the man page point at it. make dist and make install ship LICENSE. - Scope gap tests: deny_ip beats allow_ip, port range endpoints and neighbours, /25 boundary, IPv6 CIDR accept/refuse, apex vs subdomain vs wildcard matching (*.name not matching the apex), embedded host:port, trailing FQDN dot, and a failed scope_reload closing the gate. scope.c 75.9% -> 82.8%, total 79.4%. 397 checks, analyze clean, release-check green including distcheck 1.0.4.
236 lines
9.1 KiB
Markdown
236 lines
9.1 KiB
Markdown
# Operating APEX TWIN
|
||
|
||
This is the run book: how to install it, what it writes, how the scope behaves,
|
||
how to run the proxy and the webhook, how to back up and upgrade, and what to
|
||
check when something refuses.
|
||
|
||
## What runs
|
||
|
||
| Mode | Command | Listener |
|
||
|---|---|---|
|
||
| MCP over stdio | `apex-twin` | none |
|
||
| MCP over HTTP | `apex-twin --http 8797` | 127.0.0.1:8797, `POST /mcp` |
|
||
| Forward proxy | `apex-twin --proxy 8798` | 127.0.0.1:8798 |
|
||
| Diagnostics | `apex-twin --check` | none, prints JSON and exits |
|
||
|
||
Both listeners bind loopback only on purpose. Reach them through an SSH tunnel
|
||
or a VPN; never put them on a LAN interface. The HTTP face serves at most
|
||
`APEX_HTTP_MAX_CHILDREN` connections at once (503 beyond) and closes a request
|
||
whose head or body does not arrive within `APEX_HTTP_HEAD_TIMEOUT_MS`.
|
||
|
||
## Install
|
||
|
||
```sh
|
||
make # build, zero warnings under -Werror
|
||
make test # 298 end-to-end checks
|
||
make install # binary, README and both systemd units
|
||
make install DESTDIR=/tmp/pkg PREFIX=/usr
|
||
```
|
||
|
||
`make install` writes:
|
||
|
||
```
|
||
$PREFIX/bin/apex-twin
|
||
$PREFIX/share/doc/apex-twin/{README,CHANGELOG,SECURITY,LICENSE,operations,artex-port}.md
|
||
$PREFIX/share/man/man1/apex-twin.1
|
||
$PREFIX/lib/systemd/system/apex-twin.service
|
||
$PREFIX/lib/systemd/system/apex-twin-proxy.service
|
||
```
|
||
|
||
The units hardcode `User=lotherk` and
|
||
`APEX_HOME=/home/lotherk/stack/data/apex-twin`; edit them before installing for
|
||
another account. `EnvironmentFile=-/home/lotherk/.config/apex-twin/env` is
|
||
optional and is the right place for `APEX_WEBHOOK`, `APEX_REWRITE` and the time
|
||
outs — keep secrets out of the unit file.
|
||
|
||
The radio unit adds `SupplementaryGroups=dialout`; the proxy unit does not, and
|
||
neither unit gets `CAP_NET_RAW` (connect scans need no privilege).
|
||
|
||
```sh
|
||
systemctl daemon-reload
|
||
systemctl enable --now apex-twin.service apex-twin-proxy.service
|
||
journalctl -u apex-twin -f
|
||
```
|
||
|
||
## State
|
||
|
||
Everything lives under `$APEX_HOME` (default
|
||
`~/.local/state/apex-twin`), created 0700:
|
||
|
||
```
|
||
scope.json the rules of engagement
|
||
audit.log one JSON line per decision, append-only
|
||
findings/f0001.json one finding per file, rewritten atomically on update
|
||
evidence/<ts>-<label>.bin raw proof, 0600
|
||
reports/report-<ts>.* md / csv / json, never overwritten
|
||
assets/a0001.json one record per target, merged on the next look
|
||
traffic/t0001.json|.http one capture: metadata plus the raw pair
|
||
rewrite.json proxy rules, read at start and on SIGHUP (optional)
|
||
```
|
||
|
||
`audit.log` is written **before** an action, so a process that dies mid-scan
|
||
still left its intent on disk. When it reaches `APEX_AUDIT_MAX_BYTES` (default
|
||
10 MiB, `0` disables) it is renamed to `audit.log.1` under `flock`; one previous
|
||
file is kept. Back up both. `audit_log` reads the current file only. Findings, assets and reports are written beside
|
||
their target and renamed; a reader never sees half a file.
|
||
|
||
## Scope lifecycle
|
||
|
||
Write `scope.json` before the first run. Start from `examples/scope.json`:
|
||
|
||
```json
|
||
{
|
||
"label": "acme-q1",
|
||
"expires": "2026-03-31T23:59:59Z",
|
||
"allow_private": false,
|
||
"serial_tx": false,
|
||
"allow_ip": ["203.0.113.0/24", "198.51.100.7"],
|
||
"deny_ip": ["203.0.113.66"],
|
||
"allow_domain": ["app.acme.example", "*.acme.example"],
|
||
"allow_port": ["80", "443", "8000-8100"]
|
||
}
|
||
```
|
||
|
||
Rules are applied in this order: no scope / expired / `deny_ip` / `allow_port` /
|
||
neither `allow_ip` nor `allow_domain` / private or loopback without
|
||
`allow_private` / a name outside `allow_domain` that does not resolve entirely
|
||
inside `allow_ip`. `deny_ip` beats `allow_ip`. A name in `allow_domain` is
|
||
trusted for its addresses but may not resolve private while `allow_private` is
|
||
false.
|
||
|
||
Before an engagement:
|
||
|
||
```sh
|
||
apex-twin --check # loaded? expired? tool count?
|
||
```
|
||
|
||
Ask the gate without touching the target (MCP tool `scope_check`):
|
||
|
||
```json
|
||
{"name":"scope_check","arguments":{"target":"203.0.113.9","port":443}}
|
||
```
|
||
|
||
Edit the file while the server runs and call `scope_reload` (or restart). The
|
||
proxy's rewrite rules are separate and reload on `kill -HUP`.
|
||
|
||
## Backup and restore
|
||
|
||
Stop the services, then copy the whole state directory:
|
||
|
||
```sh
|
||
systemctl stop apex-twin.service apex-twin-proxy.service
|
||
rsync -a ~/.local/state/apex-twin/ /backup/apex-twin-$(date +%F)/
|
||
systemctl start apex-twin.service apex-twin-proxy.service
|
||
```
|
||
|
||
Restore is the reverse. Reports are derivable from findings and assets;
|
||
`audit.log` and `evidence/` are not. Evidence and audit may contain credentials,
|
||
tokens or personal data from an engagement — treat the backup as sensitive.
|
||
|
||
## Proxy
|
||
|
||
```sh
|
||
curl -x http://127.0.0.1:8798 http://target.example/
|
||
```
|
||
|
||
Every request passes the scope gate before a socket opens. Plain HTTP is
|
||
forwarded and captured under `traffic/`. `CONNECT` is tunnelled blind and only
|
||
recorded as metadata: the binary links no TLS stack, so it does not inspect TLS.
|
||
|
||
Each accepted connection gets its own child process, so a `CONNECT` tunnel does
|
||
not block the next request and parallel captures cannot overwrite each other
|
||
(traffic ids are reserved with `O_EXCL`). At most `APEX_PROXY_MAX_CHILDREN`
|
||
(default 64, max 1024) children run at once; beyond that the parent answers
|
||
`503 Service Unavailable` (`proxy busy`) and audits the refusal. A SIGHUP in the parent reloads the
|
||
rewrite rules for connections accepted after it; children keep the rules they
|
||
inherited at fork.
|
||
|
||
`rewrite.json` can set or remove headers and block a path; first match wins:
|
||
|
||
```json
|
||
{"rules": [
|
||
{"match_host": "app.example.com", "match_path_prefix": "/api",
|
||
"set": [{"name": "X-Test", "value": "1"}],
|
||
"remove": ["User-Agent"], "note": "mark the request"},
|
||
{"match_host": "*", "match_path_prefix": "/admin",
|
||
"block": true, "note": "not part of this engagement"}
|
||
]}
|
||
```
|
||
|
||
`kill -HUP <pid>` reloads. An invalid file is rejected whole; the previous rules
|
||
stay active. Bodies: `Content-Length` and `Transfer-Encoding: chunked` are
|
||
forwarded; chunked payload is capped at 1 MiB (413 over), malformed framing is
|
||
400 and is decided before the origin is opened. Responses are capped at 1 MiB
|
||
per capture.
|
||
|
||
## Webhook
|
||
|
||
```sh
|
||
APEX_WEBHOOK=https://hooks.example/apex \
|
||
APEX_WEBHOOK_MIN_SEVERITY=high \
|
||
apex-twin
|
||
```
|
||
|
||
A finding at or above the floor is POSTed as JSON after it is on disk, and so is
|
||
the transition of a finding into `confirmed`. `notify_test` sends one event.
|
||
The webhook is operator infrastructure, not a target: it is **not** put through
|
||
the scope gate, and a failing webhook never fails the finding. The URL is read
|
||
from the environment; `--check` reports only `webhook_configured: true/false`.
|
||
|
||
## Upgrade and rollback
|
||
|
||
```sh
|
||
git pull
|
||
make && make test
|
||
make install
|
||
systemctl restart apex-twin.service apex-twin-proxy.service
|
||
```
|
||
|
||
State is forward compatible: older findings without `status` are read as
|
||
`open`, and the JSON formats only ever gained fields. To roll back, install the
|
||
previous binary and keep the state directory; if a newer build wrote a field the
|
||
older one does not know, the older build ignores it but will drop it on rewrite
|
||
of that file — back up first.
|
||
|
||
## Limits worth knowing
|
||
|
||
| Limit | Value |
|
||
|---|---|
|
||
| HTTP MCP request body | 8 MiB (`413` over) |
|
||
| Proxy request body (chunked) | 1 MiB payload |
|
||
| Proxy response per capture | 1 MiB (marked truncated) |
|
||
| Findings per report | 1024 |
|
||
| Default tool timeout | 60 s (hard cap 900 s) |
|
||
| Radio scan | 3–300 s, always ends with `stopscan` |
|
||
| Concurrent proxy connections | `APEX_PROXY_MAX_CHILDREN`, default 64, 503 beyond |
|
||
| Audit file size | `APEX_AUDIT_MAX_BYTES`, default 10 MiB, rotates to `.1` |
|
||
| Concurrent HTTP connections | `APEX_HTTP_MAX_CHILDREN`, default 64, 503 beyond |
|
||
| HTTP request deadline | `APEX_HTTP_HEAD_TIMEOUT_MS`, default 20000 |
|
||
| Evidence blob size | `APEX_EVIDENCE_MAX_BYTES`, default 16 MiB, `0` unlimited |
|
||
|
||
## Troubleshooting
|
||
|
||
- **Everything is refused.** No scope file, an expired scope, or a rule that
|
||
does not match. `apex-twin --check` and `scope_check` name the reason; the
|
||
refusal string is the contract, not just `isError`.
|
||
- **Port refused but host allowed.** `allow_port` is enforced for single-port
|
||
actions. `port <= 0` means "the caller enumerates ports", used by `portscan`.
|
||
- **A name resolves into a private net.** `allow_private` is false; either set
|
||
it or scope the name to public addresses.
|
||
- **The radio refuses to talk.** `/dev/ttyACM0` may be a console, not a
|
||
Marauder: the guard sends a bare CRLF and refuses on `login:`, `password:`,
|
||
`console (`, `mikronix`, `busybox`, `root@`. Set `APEX_SERIAL` to the real
|
||
device. Scans always send `stopscan`.
|
||
- **A gate decision is missing from the audit.** Look at `audit.log` ordering:
|
||
the deny line is written before the refusal; a missing line means the tool
|
||
never reached the gate, which is a bug worth reporting.
|
||
- **The webhook never fires.** `APEX_WEBHOOK` unset, severity below the floor, or
|
||
curl missing. `notify_test` says which; `audit.log` has `tool: notify` with the
|
||
curl exit code.
|
||
|
||
## What it does not do
|
||
|
||
No shell, no exploit, no post-exploitation, no database reads, no TLS
|
||
interception, no agent loop. The MCP client is the agent; this process is the
|
||
gate, the tools and the records.
|