ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
docs: document constrained Go tampering hooks subset, API, and trace (P4.2 PR1)
BaaRai680227b1ea5e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7fc2348..67e0dfd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,6 +23,7 @@ All notable changes to this project are documented in this file. The format is b- **`ask_operator` target warning**: a request whose question names a host or URL outside the goal targets is tagged as containing target-derived text (#197).- **Opt-in update banner**: `ARTEX_UPDATE_CHECK` makes the backend check the latest GitHub release at most once per 24 hours and the UI show a banner; off by default (#202).- **Release provenance**: release archives carry GitHub build provenance attestations and a CycloneDX SBOM (`gh attestation verify <zip> --repo BaaRai/ARTEX`); release notes are the matching `CHANGELOG.md` section (#219, #201).+- **Opt-in constrained Go tampering hooks — backend, API & trace (P4.2 PR1)**: opt-in behind `ARTEX_TAMPER_HOOKS_ENABLED=1` (off by default; flag off loads zero hooks and keeps the no-hook baseline unchanged). Adds a small, deterministic, zero-dependency Go-subset evaluator (`traffic/hookvm`, parsed via `go/parser` with sorted map iteration, hard 100k step / 25 ms per-hook / 100 ms per-phase / 4 MiB memory bounds, and AST rejection of FS/network/exec/env/time/rand/goroutine/channel/defer/goto/global-var/pointer/switch constructs). Operator-only hooks (never an agent tool) are stored sealed at rest in `traffic_tamper_hooks` and `traffic_tamper_hook_versions` (migration `0011_traffic_tamper_hooks.sql`), managed via `/api/traffic/hooks*` (CRUD, toggle, version history, rollback, dry-run), prefiltered with `traffic/rewrite` matchers, executed after declarative rewrite rules per phase (fail-open only in 0.7; binary/compressed/>1 MiB bodies skipped; tainted sensitive fields stay HMAC-masked), and traced in SQLite `exchange_rewrites` (`source_kind`, `hook_id`, `hook_version`). See `docs/traffic-tamper-hooks.md`.- **Opt-in SOCKS5 UDP ASSOCIATE & raw UDP recording (P4.1c)**: when traffic capture is enabled, setting `ARTEX_SOCKS5_UDP_ENABLED=1` enables RFC 1928 `UDP ASSOCIATE` (`0x03`, IPv4 first) on the SOCKS5 listener (off by default: with flags off no listener starts; with SOCKS5 TCP on and UDP off, `UDP ASSOCIATE` returns `0x07` and no UDP socket is opened). Enforces per-datagram destination policy before forwarding using the existing target gate (`intercept.Interceptor.ResolveUDP4` / `intercept.PinnedUDP4Resolver`) and egress governor (`egress.Do`), dropping denied datagrams without passthrough; bounds associations (`UDPMaxDatagrams`, `UDPMaxBytes`, `UDPIdleTimeout`) and tears them down with the TCP control connection; drops `FRAG != 0` with no fragmentation reassembly; and records each forwarded datagram individually in SQLite `raw_udp_datagrams` with capped previews and atomic `_blobs` refs without writing HTTP `exchanges` rows or PG migrations. Transparent UDP (`IP_RECVORIGDSTADDR`) is deferred to C2. See `docs/traffic-socks5-udp.md`.- **Opt-in Linux transparent TCP inbound & raw TCP recording (P4.1b)**: on Linux when traffic capture is enabled, setting `ARTEX_TRANSPARENT_TCP_ENABLED=1` (loopback `127.0.0.1:1081` by default) or `ARTEX_TRANSPARENT_TCP_ADDR=<ipv4:port>` starts an IPv4 transparent TCP listener fed by operator-managed `iptables`/`nftables` `REDIRECT` rules (off by default; ARTEX never touches the host firewall; root/`CAP_NET_ADMIN` is required only for the operator's redirect rules; IPv6 is not covered). Recovers original IPv4 destinations via `SO_ORIGINAL_DST` with self-redirect loop protection, reuses the P4.1a target gate (`intercept.Interceptor.DialSOCKS5`), egress governor (`egress.Do`), half-close relay bounds, and SQLite `raw_tcp_streams` store (atomic `_blobs` spill, no HTTP `exchanges` row, no PostgreSQL migration). See `docs/traffic-transparent.md`.- **Opt-in SOCKS5 inbound proxy & raw TCP recording (P4.1a)**: when traffic capture is enabled, setting `ARTEX_SOCKS5_ENABLED=1` (loopback `127.0.0.1:1080` by default) or `ARTEX_SOCKS5_ADDR=<addr>` starts an RFC 1928 SOCKS5 `CONNECT` listener alongside the HTTP recording proxy (off by default; HTTP proxy and agent proxy env vars are unchanged; listener does not start if traffic recording is disabled). Accepts loopback no-auth and RFC 1929 username/password without verifying credentials (carrying `artex-pace` in the username; binding to a non-loopback address therefore exposes an unauthenticated proxy, though the target gate and governor still apply), rejects `BIND`/`UDP ASSOCIATE` with `0x07`, enforces the existing target gate (`intercept.Interceptor.DialSOCKS5` / `intercept.PinnedDialer`) and egress governor (`egress.Do`) before dialing, relays half-close-aware TCP with concurrency/byte/idle/handshake bounds, and records one row per connection in SQLite `raw_tcp_streams` with capped directional previews and atomic (temp+rename) `_blobs` refs without writing HTTP `exchanges` rows. See `docs/traffic-socks5.md`.diff --git a/docs/traffic-tamper-hooks.md b/docs/traffic-tamper-hooks.mdnew file mode 100644index 0000000..0f17955--- /dev/null+++ b/docs/traffic-tamper-hooks.md@@ -0,0 +1,110 @@+# Constrained Go Tampering Hooks (P4.2 — PR1 Backend, API & Trace)++ARTEX supports opt-in, **operator-only** HTTP tampering hooks written in a constrained, deterministic subset of Go—with **no Python runtime** and **no external interpreter dependency**. Hooks run inside the capture proxy after declarative rewrite rules in each phase (`request` or `response`), fail open on any error, and record field-level diffs into SQLite `exchange_rewrites`.++## Kill Switch & Authority++- **Kill switch (`ARTEX_TAMPER_HOOKS_ENABLED=1`, off by default)**: when unset or false, **zero hooks are loaded** onto the capture proxy and the proxy follows the unchanged baseline path.+- **Operator-only**: hooks are global admin configuration managed only via `/api/traffic/hooks*` and are **never exposed as an agent tool**.+- **Created disabled**: every hook is created with `enabled: false` and must be explicitly enabled via toggle or update.+- **Sealed at rest**: `source` and `matchers` on both `traffic_tamper_hooks` and `traffic_tamper_hook_versions` (migration `0011_traffic_tamper_hooks.sql`) are encrypted at rest with `db/secrets.go` (AES-256-GCM) because a hook may contain hardcoded signing keys or tokens.++## Execution Order, Failure Mode & Limits++- **Order**: per phase (`priority ASC, id ASC`), **declarative rewrite rules run first**, then **tampering hooks** see the post-rule message.+- **Matchers prefilter**: hooks reuse the exact `traffic/rewrite` matchers (`methods`, `host`, `url_regex`, `headers`, `body_regex`, `status`, `resp_headers`, `resp_body_regex`). Non-matching exchanges never invoke the VM.+- **Fail-open only (0.7)**: any returned error, panic, timeout, step-limit, memory-limit, or forbidden HTTP mutation **rolls back all mutations of that hook atomically**, passes the pre-hook message unchanged, records an `outcome='error'` trace row in `exchange_rewrites` with the reason (`panic`, `timeout`, `step_limit`, `memory_limit`, `forbidden_mutation`, `forbidden_method`, `invalid_url`, `invalid_status`, `forbidden_header`, `invalid_header`, `body_too_large`, `hook_error`), and updates in-memory `last_error` (`class`, `at`, `count`).+- **Skipped messages**:+ - **Binary bodies are not modified in 0.7** (`not rewritten: binary`).+ - Bodies larger than **1 MiB** (`not rewritten: body too large`), compressed bodies (`Content-Encoding` other than `identity`), WebSocket upgrades, SSE response bodies, and responses ≥ 5 MiB (`StreamLargeBodies`) are skipped.+- **Tainted fields stay masked**: if a `sensitive: true` rewrite rule wrote a field earlier in the phase, any subsequent hook trace line for that field emits only keyed HMAC fingerprints and never stores plaintext `after_inline`/`after_blob`.++### Resource & Storage Bounds++| Limit | Default |+|---|---|+| Per-hook wall-clock timeout | `25 ms` |+| Per-phase cumulative hook timeout | `100 ms` |+| CPU step budget per invocation | `100,000` steps |+| Memory allocation budget per invocation | `4 MiB` |+| Call depth | `32` |+| Regexp pattern size | `1 KiB` (RE2) |+| Source size | `64 KiB` |+| Body size | `1 MiB` |+| Stored / enabled hooks | `50` total / `20` enabled |+| Retained version history | `20` newest versions per hook |++## Go Subset Specification++Hooks are parsed with Go's standard `go/parser` and must declare `package hook`. They are **not** full idiomatic Go; only the exact syntax, fields, and virtual stdlib functions below are supported.++### Entrypoints++- Request phase (`phase: "request"`):+ ```go+ package hook+ func OnRequest(req *Request) error+ ```+- Response phase (`phase: "response"`):+ ```go+ package hook+ func OnResponse(req *Request, resp *Response) error+ ```+ In `OnResponse`, `req` is **read-only** and holds the request as sent to the target.++### Allowed Syntax++- Package-level `import`, `const` (basic literals / bool / unary `-`), and helper `func` definitions (no receivers, no `init()`, no package-level `var`, no `type` declarations).+- Statements: `:=`, `=`, `+=`, `-=`, `++`, `--`, local `var`, `if` / `else if` / `else`, `for` (C-style, condition-only, infinite `for {}`, and `for k, v := range ...` with **lexicographically sorted map keys**), `break`, `continue`, `return`.+- Expressions: literals (`int`, `float`, `string`, `rune`, `bool`, `nil`), unary (`!`, `-`, `+`), binary (`+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`), indexing (`x[i]`, `m[k]`, comma-ok `v, ok := m[k]`), slicing (`x[lo:hi]`), composite literals (`[]byte{...}`, `[]any{...}`, `map[string]any{...}`), type conversions (`[]byte(s)`, `string(b)`, `int(x)`, `int64(x)`, `float64(x)`, `byte(x)`), type assertions (`v, ok := x.(T)` for `string`, `bool`, `int`/`int64`, `float64`, `[]byte`, `[]any`, `map[string]any`).+- Collections: `[]byte`, `[]any`, `map[string]any` (plus `map[string][]string` for `Headers` and `url.ParseQuery`).+- Allowed builtins: `len`, `cap`, `append`, `make`, `delete`, `string`, `int`, `int64`, `float64`, `byte`, `panic`.++### Forbidden Constructs (Rejected with HTTP 400 at Validation)++- Forbidden imports: `os`, `io`, `io/fs`, `net`, `net/http`, `os/exec`, `syscall`, `unsafe`, `reflect`, `time`, `math/rand`, `crypto/rand`, `fmt`, `sort`, or any package outside the closed allowlist below; dot (`.`) and blank (`_`) imports.+- Forbidden syntax: `go` (goroutines), `chan` / `select`, `defer`, `goto`, `switch`, `func(...)` anonymous literals, `&` (address-of), `init()`, package-level `var`, custom `type` / methods.+- Forbidden builtins: `recover`, `print`, `println`, `close`, `complex`, `real`, `imag`, `new`.++### `Request` and `Response` API++| Type | Field / Method | Access | Notes |+|---|---|---|---|+| `*Request` | `Scheme`, `Host`, `Port` (`string`) | **Read-only** | Attempting to assign fails atomically (`forbidden_mutation`) |+| `*Request` | `Method`, `Path`, `RawQuery` (`string`) | R/W (`OnRequest`), R/O (`OnResponse`) | `CONNECT` rejected; `Path` must start with `/` |+| `*Request` | `Headers` (`map[string][]string`) | R/W (`OnRequest`), R/O (`OnResponse`) | Forbidden headers (`Host`, `Content-Length`, `Transfer-Encoding`, `Connection`, `Keep-Alive`, `TE`, `Trailer`, `Expect`, `Upgrade`, `Proxy-*`, `X-ARTEX-Replay-Nonce`) and CRLF/NUL values rejected atomically |+| `*Request` | `Body` (`[]byte`) | R/W (`OnRequest`), R/O (`OnResponse`) | Capped at 1 MiB; mutating strips `Content-Length`, `ETag`, `Content-MD5`, `Content-Encoding` |+| `*Request` / `*Response` | `GetHeader(name) string`, `SetHeader(name, val)`, `AddHeader(name, val)`, `DelHeader(name)` | Methods | Canonicalizes header keys |+| `*Response` | `Status` (`int`) | R/W (`OnResponse`) | Must be `200..599` excluding `204` and `304` |+| `*Response` | `Headers` (`map[string][]string`), `Body` (`[]byte`) | R/W (`OnResponse`) | Same header & 1 MiB body invariants |++### Allowed Virtual Stdlib Packages & Functions++- `strings`: `Contains`, `HasPrefix`, `HasSuffix`, `Index`, `TrimPrefix`, `TrimSuffix`, `ToLower`, `ToUpper`, `TrimSpace`, `ReplaceAll`, `Split` (returns `[]any` of strings), `Join`, `Repeat`.+- `bytes`: `Contains`, `HasPrefix`, `HasSuffix`, `Equal`, `Index`, `TrimSpace`, `ToLower`, `ToUpper`, `ReplaceAll`, `Repeat`.+- `encoding/json`: `Decode(b []byte) (any, error)`, `Marshal(v any) ([]byte, error)` (sorts object keys deterministically).+- `encoding/base64`: `EncodeToString`, `DecodeString`, `URLEncodeToString`, `URLDecodeString`.+- `encoding/hex`: `EncodeToString`, `DecodeString`.+- `net/url`: `QueryEscape`, `QueryUnescape`, `ParseQuery`, `EncodeQuery`.+- `regexp`: `MatchString`, `FindString`, `ReplaceAllString` (RE2, pattern ≤ 1 KiB).+- `crypto/sha256`: `Sum256`, `Sum256Hex`.+- `crypto/hmac`: `SHA256(key, data)`, `SHA256Hex(key, data)`.+- `strconv`: `Atoi`, `Itoa`, `ParseBool`, `FormatBool`.+- `errors`: `New`.++## Admin API & SQLite Trace++All routes require the administrator token:++| Route | Behavior |+|---|---|+| `GET /api/traffic/hooks` | Lists hooks in execution order (`priority ASC, id ASC`) with `version`, `load_error`, `last_error`, and `enabled_by_env`. |+| `POST /api/traffic/hooks` | Validates AST + matchers (400 on error) and creates a disabled hook (`version: 1`). |+| `PUT /api/traffic/hooks/{id}` | Validates and updates the hook, incrementing `version` and appending to `traffic_tamper_hook_versions`. |+| `DELETE /api/traffic/hooks/{id}` | Deletes hook and cascades its version history. |+| `POST /api/traffic/hooks/{id}/toggle` | Body `{"enabled": true|false}`. |+| `GET /api/traffic/hooks/{id}/versions` | Lists stored revisions newest-first. |+| `POST /api/traffic/hooks/{id}/rollback` | Body `{"version": N}`; restores revision `N` as a new monotonic version. |+| `POST /api/traffic/hooks/dry-run` | Executes a stored (`hook_id`) or inline (`phase`, `matchers`, `source`) hook against synthetic `request`/`response` without touching live traffic. |++In SQLite, `exchange_rewrites` adds `source_kind` (`'rule'` | `'hook'`), `hook_id`, and `hook_version`. Hook trace rows store compact metadata JSON (`{"kind":"hook","id":...,"name":...,"version":...,"sha256":...}`) in `rule_snapshot` (never full source per exchange) and preserve original client/target data in `exchanges`/`exchange_bodies`.