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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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`.
- **Opt-in worker cache-prefix split (P3.5 Phase C)**: setting `ARTEX_CACHE_PREFIX=1` (off by default) splits the `worker` system prompt at `DynamicBoundary = 1` into a sibling-shareable static prefix (`body + untrustedContentTail + workerTrafficBlock`) and a per-intent dynamic suffix (`workerArtifactSpec(runDir)` through `<available-deferred-tools>`) without reordering sections or altering the visible joined prompt text. Planner, mainagent, chat, and goals are unchanged; custom worker prompts referencing `{{.Now}}` remain volatile in the static segment. See `docs/context-telemetry.md`.
Expand Down
63 changes: 63 additions & 0 deletions docs/traffic-socks5-udp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Opt-in SOCKS5 UDP ASSOCIATE & Raw UDP Recording (P4.1c)

ARTEX extends the opt-in SOCKS5 inbound listener with **RFC 1928 `UDP ASSOCIATE` (`0x03`)**, **IPv4 first**, enforcing per-datagram destination policy and recording forwarded datagrams separately from HTTP traffic in SQLite. Transparent UDP (`IP_RECVORIGDSTADDR`) is out of scope here and planned separately (C2).

## Opt-in Configuration (Off by Default)

| Environment Variable | Default | Behavior |
|---|---|---|
| `ARTEX_SOCKS5_UDP_ENABLED` | unset (off) | Set to `1` / `true` / `on` / `yes` to enable RFC 1928 `UDP ASSOCIATE` on the SOCKS5 listener (starting the SOCKS5 listener on `ARTEX_SOCKS5_ADDR` or `127.0.0.1:1080` unless `ARTEX_SOCKS5_ENABLED` is explicitly `0`/`false`/`off`/`no`). |
| `ARTEX_SOCKS5_ENABLED` | unset (off) | Explicit `0` / `false` / `off` / `no` disables the entire SOCKS5 listener (TCP and UDP) even if `ARTEX_SOCKS5_UDP_ENABLED` is set. |
| `ARTEX_SOCKS5_ADDR` | `127.0.0.1:1080` | Bind address for the SOCKS5 TCP control listener; per-association IPv4 UDP relay sockets bind on the same local IPv4 interface on an ephemeral port (`:0`). |

- **Flag off means no listener**:
- With all SOCKS5 flags unset/off, no SOCKS5 listener and no UDP relay socket are started.
- With SOCKS5 TCP enabled and `ARTEX_SOCKS5_UDP_ENABLED` off, `UDP ASSOCIATE` (`0x03`) is refused with RFC 1928 reply `0x07` (*Command not supported*) and no UDP socket is opened.
- **Requires traffic recorder**: like P4.1a and P4.1b, the server only starts the SOCKS5 listener when the traffic recorder is enabled (inside the traffic proxy block in `server/manager.go`).
- **Non-loopback warning**: the same authentication caveat from [docs/traffic-socks5.md](traffic-socks5.md) applies—RFC 1929 username/password accepts any credentials, so keep the listener bound to loopback (`127.0.0.1`) or restrict access at the host firewall.

## Protocol Scope & Lifetime

- **IPv4 first**:
- `UDP ASSOCIATE` accepts IPv4 (`0x01`) and domain (`0x03`) requests (including `0.0.0.0:0` per RFC 1928 §4). IPv6 (`0x04`) `UDP ASSOCIATE` requests return `0x08` (*Address type not supported*); IPv6 UDP datagram destinations are dropped.
- Each client UDP datagram header (`RSV=0x0000`, `FRAG=0x00`, `ATYP=0x01` IPv4 or `ATYP=0x03` domain resolved to IPv4) is parsed individually.
- **No fragmentation reassembly**:
- Per RFC 1928 §7, datagrams with `FRAG != 0` are dropped without reassembly.
- Each forwarded `FRAG == 0` datagram (and each upstream reply datagram) is recorded as it arrives without cross-datagram reassembly.
- **Client source pinning**:
- Incoming client UDP datagrams on the ephemeral relay port are accepted only from the expected IPv4 client address (the `DST.ADDR`/`DST.PORT` given in `UDP ASSOCIATE`, or the TCP control connection's peer IP pinned on the first valid datagram).
- Upstream reply datagrams are accepted only from destinations (`ip:port`) to which the association previously forwarded an allowed datagram.
- **Tied to the TCP control connection**:
- Closing the SOCKS5 TCP control connection immediately closes both UDP sockets and terminates the association.
- Exceeding `UDPIdleTimeout`, `UDPMaxDatagrams`, or `UDPMaxBytes`, or calling `Traffic.Close()`, also closes the association and its TCP control connection cleanly.

## Policy Enforcement Per Datagram Destination

Before forwarding **every** client-to-server UDP datagram to `(host, port)`:

1. **Target gate (`intercept.Interceptor.ResolveUDP4` / `intercept.PinnedUDP4Resolver`)**: enforces global asset block rules (domain, IP, and CIDR, before and after fresh IPv4 DNS resolution) and any enabled `intercept.TargetScope` pins/exemptions, returning the verified IPv4 `*net.UDPAddr`.
2. **Egress governor (`egress.Do(egress.Current(), ...)`)**: enforces the active noise profile (`PaceTimeout`, default `30s`) before sending.
3. **Fail-closed drop (no silent passthrough)**: any datagram whose destination is denied by the target gate, fails DNS/IPv4 resolution, or is refused by the egress governor is **dropped** without sending bytes upstream and without recording a forwarded datagram. Subsequent allowed datagrams on the same association continue normally.

## Raw UDP Storage (SQLite)

Each forwarded datagram writes one row to the `raw_udp_datagrams` table in the SQLite traffic index (`traffic/traffic.go`, `indexSchema` — no PostgreSQL migration):

- Columns: `id` (`udp-<ms>-<seq>`), `association_id` (`udpa-<ms>-<seq>`), `protocol` (`'udp'`), `host`, `port`, `direction` (`'c2s'` | `'s2c'`), `ts` (unix milliseconds), `size`, `preview`, `blob`.
- **No HTTP exchange row**: raw UDP datagrams never insert into `exchanges`.
- **Preview & atomic blob spilling**:
- Payloads up to `UDPPreviewBytes` (default `8 KiB`) stay inline in `preview`.
- Payloads above `UDPPreviewBytes` keep the leading `UDPPreviewBytes` inline and spill the full datagram payload atomically (temp file + rename) into `_blobs/sha256/` with a reference registered in `blob_refs` under the datagram's `udp-*` ID.

## Operational Bounds

| Parameter | Default |
|---|---|
| `MaxConcurrent` | `64` active TCP streams + UDP associations combined |
| `UDPMaxDatagrams` | `1000` forwarded datagrams (`c2s` + `s2c`) per association |
| `UDPMaxBytes` | `4 MiB` forwarded payload bytes (`c2s` + `s2c`) per association |
| `UDPPreviewBytes` | `8 KiB` inline preview per datagram before blob spill |
| `UDPIdleTimeout` | `2m` inactivity timeout per association |
| `HandshakeTimeout` | `10s` for greeting, auth, and `UDP ASSOCIATE` request |
| `DialTimeout` | `30s` per-datagram target gate / DNS lookup bound |
| `PaceTimeout` | `30s` per-datagram egress governor wait bound |
2 changes: 1 addition & 1 deletion docs/traffic-socks5.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ARTEX ships an opt-in **RFC 1928 SOCKS5 `CONNECT`** inbound listener in `traffic

## Protocol Scope (P4.1a)

- **Commands**: `CONNECT` (`0x01`) only. `BIND` (`0x02`), `UDP ASSOCIATE` (`0x03`, planned for P4.1c), and unknown commands return RFC 1928 reply `0x07` (*Command not supported*).
- **Commands**: `CONNECT` (`0x01`) by default. `BIND` (`0x02`) and unknown commands return RFC 1928 reply `0x07` (*Command not supported*). `UDP ASSOCIATE` (`0x03`) also returns `0x07` unless `ARTEX_SOCKS5_UDP_ENABLED=1` is set (see [docs/traffic-socks5-udp.md](traffic-socks5-udp.md)).
- **Addressing**: IPv4 (`0x01`), domain name (`0x03`), and IPv6 (`0x04`). Unknown address types return `0x08` (*Address type not supported*).
- **Authentication**:
- Loopback peers (`net.IP.IsLoopback()`) may connect with **No Auth** (`0x00`). Non-loopback peers offering only `0x00` are rejected with `0x05 0xFF`.
Expand Down
150 changes: 150 additions & 0 deletions intercept/target_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,23 @@ func (i *Interceptor) dialTarget(ctx context.Context, scope TargetScope, toolNam
func addrTarget(network, addr string) (Target, error) {
switch network {
case "tcp", "tcp4", "tcp6":
return parseTargetAddr(addr)
default:
return Target{}, fmt.Errorf("network %q is not tcp", network)
}
}

// addrTargetUDP reads the address a UDP relay datagram names.
func addrTargetUDP(network, addr string) (Target, error) {
switch network {
case "udp", "udp4", "udp6":
return parseTargetAddr(addr)
default:
return Target{}, fmt.Errorf("network %q is not udp", network)
}
}

func parseTargetAddr(addr string) (Target, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return Target{}, err
Expand Down Expand Up @@ -237,3 +251,139 @@ func (i *Interceptor) DialSOCKS5(ctx context.Context, network, addr string) (net
}
return i.dialAddrs(ctx, network, ct.ips, t.Port)
}

// PinnedUDP4Resolver returns a per-datagram IPv4 destination resolver that
// enforces the platform's target gate identically to PinnedDialer. When scoped
// is true, a missing or incomplete TargetScope fails closed with
// TARGET_GATE_UNAVAILABLE.
func PinnedUDP4Resolver(toolName string, scoped bool) func(ctx context.Context, addr string) (*net.UDPAddr, error) {
return func(ctx context.Context, addr string) (*net.UDPAddr, error) {
scope, ok := TargetScopeFromContext(ctx)
switch {
case !ok && scoped:
return nil, &dialError{code: TargetGateUnavailable, target: addr, reason: "the target scope of the session did not reach the connection"}
case !ok:
return resolvePlainUDP4(ctx, addr)
case scope.Enabled == nil || !scope.Enabled():
if scope.Interceptor != nil {
return scope.Interceptor.ResolveUDP4(ctx, addr)
}
return resolvePlainUDP4(ctx, addr)
case scope.Interceptor == nil:
return nil, &dialError{code: TargetGateUnavailable, target: addr, reason: "the session's target scope carries no gate"}
}
return scope.Interceptor.resolveTargetUDP4(ctx, scope, toolName, addr)
}
}

// ResolveUDP4 enforces the platform's target gate before forwarding a SOCKS5
// UDP ASSOCIATE datagram, reusing the exact same asset block rules, fresh DNS
// resolution, and TargetScope pin/exemption logic as DialSOCKS5 / PinnedDialer.
// Returns the first verified IPv4 *net.UDPAddr.
func (i *Interceptor) ResolveUDP4(ctx context.Context, addr string) (*net.UDPAddr, error) {
if i == nil {
return resolvePlainUDP4(ctx, addr)
}
if scope, ok := TargetScopeFromContext(ctx); ok && scope.Enabled != nil && scope.Enabled() {
return i.resolveTargetUDP4(ctx, scope, "socks5-udp", addr)
}
t, err := addrTargetUDP("udp4", addr)
if err != nil {
return nil, &dialError{code: TargetGateUnavailable, target: addr, reason: "the address of the datagram could not be read (" + err.Error() + ")"}
}
if i.targets == nil {
return resolvePlainUDP4(ctx, addr)
}
rules, err := i.targets.BlockRules(db.TargetScopeRef{})
if err != nil {
return nil, &dialError{code: TargetGateUnavailable, target: t.String(), reason: "the asset rules could not be read: " + err.Error()}
}
ct := &checkedTarget{Target: t}
if t.Kind != TargetDomain {
ct.ips, ct.resolver, ct.resolvedAt = []string{t.Host}, literalResolver, time.Now().UTC()
}
if rule, hit := blockRule(rules, ct); hit {
return nil, &dialError{code: TargetRuleBlocked, target: t.String(), reason: fmt.Sprintf("%s is blocked by an asset rule. %s", t, rule.Reason())}
}
if t.Kind == TargetDomain {
c := &targetCheck{i: i, ctx: ctx, tool: "socks5-udp", fresh: true}
c.resolve(ct, true)
if ct.err != nil {
return nil, ct.err
}
if rule, hit := blockRule(rules, ct); hit {
return nil, &dialError{code: TargetRuleBlocked, target: t.String(), reason: fmt.Sprintf("%s is blocked by an asset rule. %s", t, rule.Reason())}
}
}
return firstIPv4UDPAddr(ct.ips, t.Port)
}

func (i *Interceptor) resolveTargetUDP4(ctx context.Context, scope TargetScope, toolName, addr string) (*net.UDPAddr, error) {
input, _ := json.Marshal(map[string]string{"udp": addr})
ctx = WithCall(ctx, toolName, input)
t, err := addrTargetUDP("udp4", addr)
if err != nil {
res := undeterminedTarget(ctx, scope, toolName, input, "the address of the datagram could not be read ("+err.Error()+")")
return nil, &dialError{code: res.Code, target: addr, reason: res.Reason}
}
blocked := func(res TargetResult) error {
return &dialError{code: res.Code, target: t.String(), reason: res.Reason}
}
c := &targetCheck{i: i, ctx: ctx, scope: scope, tool: toolName, input: input, fresh: true}
unpinned, res := c.run([]Target{t})
if res.Code != "" {
return nil, blocked(res)
}
if len(unpinned) > 0 {
scope.WaitBound = 0
res = i.RequestTargets(ctx, scope, toolName, input, unpinned, nil)
if res.Code == TargetApprovalPending {
res.Reason = notCovered(unpinned[0], scope)
}
return nil, blocked(res)
}
switch ct := c.checked[0]; ct.cover {
case coverDerived, coverNameOnly:
return resolvePlainUDP4(ctx, addr)
default:
return firstIPv4UDPAddr(ct.ips, t.Port)
}
}

func firstIPv4UDPAddr(ips []string, port int) (*net.UDPAddr, error) {
for _, raw := range ips {
a, err := netip.ParseAddr(raw)
if err == nil && a.Is4() {
return &net.UDPAddr{IP: net.IP(a.AsSlice()), Port: port}, nil
}
}
return nil, &net.AddrError{Err: "no IPv4 address found", Addr: strings.Join(ips, ", ")}
}

func resolvePlainUDP4(ctx context.Context, addr string) (*net.UDPAddr, error) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil || port <= 0 || port > 65535 {
return nil, fmt.Errorf("invalid udp port %q", portStr)
}
if ip := net.ParseIP(strings.Trim(host, "[]")); ip != nil {
ip4 := ip.To4()
if ip4 == nil {
return nil, &net.AddrError{Err: "IPv6 UDP destination not supported", Addr: addr}
}
return &net.UDPAddr{IP: ip4, Port: port}, nil
}
ips, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
if err != nil {
return nil, err
}
for _, ip := range ips {
if ip4 := ip.To4(); ip4 != nil {
return &net.UDPAddr{IP: ip4, Port: port}, nil
}
}
return nil, &net.AddrError{Err: "no IPv4 address found", Addr: addr}
}
3 changes: 3 additions & 0 deletions server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ func NewManager(dir, proxyAddr string) (*Manager, error) {
m.reloadRewriteRules() // in place before the proxy accepts a connection
if scfg, ok := traffic.SOCKS5ConfigFromEnv(); ok {
scfg.DialContext = m.interceptor.DialSOCKS5
scfg.ResolveUDP4 = m.interceptor.ResolveUDP4
if bound, serr := tr.StartSOCKS5(scfg); serr != nil {
log.Printf("[traffic/socks5] disabled: %v", serr)
} else if scfg.EnableUDP {
log.Printf("[traffic/socks5] opt-in SOCKS5 CONNECT + UDP ASSOCIATE listener on %s", bound)
} else {
log.Printf("[traffic/socks5] opt-in SOCKS5 CONNECT listener on %s", bound)
}
Expand Down
Loading
Loading