diff --git a/docs/traffic-socks5-udp.md b/docs/traffic-socks5-udp.md index 57651595..b05294f5 100644 --- a/docs/traffic-socks5-udp.md +++ b/docs/traffic-socks5-udp.md @@ -14,7 +14,7 @@ ARTEX extends the opt-in SOCKS5 inbound listener with **RFC 1928 `UDP ASSOCIATE` - 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. +- **Loopback clients only**: as described in [docs/traffic-socks5.md](traffic-socks5.md), the control connection that requests `UDP ASSOCIATE` is accepted from loopback clients only, whichever authentication method it offers. ## Protocol Scope & Lifetime diff --git a/docs/traffic-socks5.md b/docs/traffic-socks5.md index b60fa45a..bf224dcc 100644 --- a/docs/traffic-socks5.md +++ b/docs/traffic-socks5.md @@ -12,7 +12,7 @@ ARTEX ships an opt-in **RFC 1928 SOCKS5 `CONNECT`** inbound listener in `traffic - **Flag off**: no SOCKS5 listener is started and the HTTP MITM recording proxy is byte-identical. - **Requires traffic recorder**: the server only starts the SOCKS5 listener when the traffic recorder is enabled (inside the traffic proxy block in `server/manager.go`). If traffic capture is disabled, setting `ARTEX_SOCKS5_ENABLED` / `ARTEX_SOCKS5_ADDR` does not open a listener. - **Agents unchanged**: workers and tools keep `HTTP_PROXY`, `HTTPS_PROXY`, and `ALL_PROXY` pointing to the existing HTTP proxy; ARTEX does not silently switch agents to SOCKS5. -- **Non-loopback bind warning**: RFC 1929 username/password (`0x02`) accepts **any** username and password without verifying credentials (so the pace marker `artex-pace` can ride in the username). Binding `ARTEX_SOCKS5_ADDR` to a non-loopback address therefore exposes an **unauthenticated proxy** to anyone who can reach that port. The target gate and egress governor still apply to every connection, but keep the listener on loopback (`127.0.0.1`) or restrict access at the host firewall. +- **Loopback clients only**: RFC 1929 username/password (`0x02`) does not verify credentials (the username only carries the pace marker `artex-pace`), so it is offered on the same terms as no-auth (`0x00`): to loopback clients only. A client connecting from any other address is refused with `0xFF` (*no acceptable methods*) whichever method it offers, so binding `ARTEX_SOCKS5_ADDR` to a non-loopback address does not expose an open proxy; it simply accepts no remote clients. The target gate and egress governor still apply to every accepted connection. ## Protocol Scope (P4.1a) diff --git a/traffic/socks5.go b/traffic/socks5.go index ca735d5b..bdfb40b5 100644 --- a/traffic/socks5.go +++ b/traffic/socks5.go @@ -478,16 +478,23 @@ func (s *socks5Server) negotiateAuth(conn net.Conn) (paced bool, ok bool) { } } + // Credentials are never verified (the username only carries the pace marker), + // so username/password grants no more trust than no-auth: both are accepted + // from loopback clients only. A non-loopback client is refused whichever + // method it offers, so a non-loopback bind is not an open proxy. + if !isLoopbackRemote(conn.RemoteAddr()) { + _, _ = conn.Write([]byte{socks5Version, socks5AuthNoAccept}) + return false, false + } // Prefer username/password when offered so clients carrying the pace marker // (egress.PaceUser) can pass it through without requiring real credentials. - // Accept no-auth for loopback clients. if hasUserPass { if _, err := conn.Write([]byte{socks5Version, socks5AuthUserPass}); err != nil { return false, false } return readSOCKS5UserPass(conn) } - if hasNone && isLoopbackRemote(conn.RemoteAddr()) { + if hasNone { if _, err := conn.Write([]byte{socks5Version, socks5AuthNone}); err != nil { return false, false } @@ -498,8 +505,9 @@ func (s *socks5Server) negotiateAuth(conn net.Conn) (paced bool, ok bool) { } // readSOCKS5UserPass parses an RFC 1929 username/password subnegotiation. -// Credentials are never required (any username/password succeeds); a username -// equal to egress.PaceUser marks the connection as paced. +// Credentials are never verified (any username/password succeeds, which is why +// negotiateAuth only offers this method to loopback clients); a username equal +// to egress.PaceUser marks the connection as paced. func readSOCKS5UserPass(conn net.Conn) (paced bool, ok bool) { var head [2]byte if _, err := io.ReadFull(conn, head[:]); err != nil { diff --git a/traffic/socks5_auth_test.go b/traffic/socks5_auth_test.go new file mode 100644 index 00000000..51a803d7 --- /dev/null +++ b/traffic/socks5_auth_test.go @@ -0,0 +1,96 @@ +package traffic + +import ( + "io" + "net" + "testing" + "time" + + "github.com/Autumn-27/artex/egress" +) + +// remoteAddrConn reports a chosen remote address so negotiateAuth can be driven +// as if the client connected from that address. +type remoteAddrConn struct { + net.Conn + remote net.Addr +} + +func (c remoteAddrConn) RemoteAddr() net.Addr { return c.remote } + +// negotiateFrom runs negotiateAuth for a client at remote that offers methods +// and, when the server selects username/password, sends user/pass. It returns +// the server's method selection and negotiateAuth's results. +func negotiateFrom(t *testing.T, remote net.Addr, methods []byte, user, pass string) (selected byte, paced, ok bool) { + t.Helper() + server, client := net.Pipe() + defer server.Close() + defer client.Close() + _ = client.SetDeadline(time.Now().Add(5 * time.Second)) + + type result struct{ paced, ok bool } + done := make(chan result, 1) + go func() { + p, o := (&socks5Server{}).negotiateAuth(remoteAddrConn{Conn: server, remote: remote}) + done <- result{p, o} + }() + + if _, err := client.Write(append([]byte{socks5Version, byte(len(methods))}, methods...)); err != nil { + t.Fatalf("write greeting: %v", err) + } + var sel [2]byte + if _, err := io.ReadFull(client, sel[:]); err != nil { + t.Fatalf("read method selection: %v", err) + } + if sel[1] == socks5AuthUserPass { + msg := []byte{socks5UserPassVer, byte(len(user))} + msg = append(msg, user...) + msg = append(msg, byte(len(pass))) + msg = append(msg, pass...) + if _, err := client.Write(msg); err != nil { + t.Fatalf("write credentials: %v", err) + } + var resp [2]byte + if _, err := io.ReadFull(client, resp[:]); err != nil { + t.Fatalf("read auth response: %v", err) + } + } + r := <-done + return sel[1], r.paced, r.ok +} + +func TestSOCKS5NonLoopbackClientRefusedForEveryMethod(t *testing.T) { + remote := &net.TCPAddr{IP: net.ParseIP("203.0.113.7"), Port: 40000} + cases := []struct { + name string + methods []byte + }{ + {"userpass only", []byte{socks5AuthUserPass}}, + {"none and userpass", []byte{socks5AuthNone, socks5AuthUserPass}}, + {"none only", []byte{socks5AuthNone}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + sel, _, ok := negotiateFrom(t, remote, tc.methods, "anyone", "anything") + if ok || sel != socks5AuthNoAccept { + t.Fatalf("non-loopback client: selected=%#x ok=%v, want %#x and refused", sel, ok, socks5AuthNoAccept) + } + }) + } +} + +func TestSOCKS5LoopbackClientStillAccepted(t *testing.T) { + for _, ip := range []string{"127.0.0.1", "::1"} { + remote := &net.TCPAddr{IP: net.ParseIP(ip), Port: 40000} + + sel, paced, ok := negotiateFrom(t, remote, []byte{socks5AuthNone, socks5AuthUserPass}, egress.PaceUser, egress.PaceUser) + if !ok || sel != socks5AuthUserPass || !paced { + t.Fatalf("%s userpass with pace marker: selected=%#x paced=%v ok=%v", ip, sel, paced, ok) + } + + sel, paced, ok = negotiateFrom(t, remote, []byte{socks5AuthNone}, "", "") + if !ok || sel != socks5AuthNone || paced { + t.Fatalf("%s no-auth: selected=%#x paced=%v ok=%v", ip, sel, paced, ok) + } + } +}