diff --git a/docs/checks/index.mdx b/docs/checks/index.mdx index db8270c..2ecef63 100644 --- a/docs/checks/index.mdx +++ b/docs/checks/index.mdx @@ -17,6 +17,7 @@ import { Card, CardGrid, LinkCard } from '@/components/docs'; + ## Severity diff --git a/docs/checks/smuggling.mdx b/docs/checks/smuggling.mdx new file mode 100644 index 0000000..ad59fd5 --- /dev/null +++ b/docs/checks/smuggling.mdx @@ -0,0 +1,52 @@ +--- +title: Request Smuggling / Desync +description: Whether the proxy rejects ambiguous request framing — the precondition for CL.TE / TE.CL / TE.TE smuggling. +--- + +import { Aside } from '@/components/docs'; + +Every classic HTTP request-smuggling attack starts from the same flaw: a +message whose length is ambiguous (conflicting `Content-Length`, or +`Content-Length` together with `Transfer-Encoding`, or an obfuscated +`Transfer-Encoding`) that a front-end proxy and a back-end resolve +differently. RFC 9112 requires rejecting such a message; a proxy that +accepts it supplies the desync primitive. + + + +| Check ID | Title | CVSS 4.0 | CWE | OWASP | +| --- | --- | --- | --- | --- | +| `smuggling.ambiguous_framing` | Ambiguous Request Framing Accepted (Smuggling Precondition) | 7.3 | [CWE-444](https://cwe.mitre.org/data/definitions/444.html) | API8:2023 | + +## `smuggling.ambiguous_framing` + +Sends four RFC-violating framings and flags any the proxy answers with a +2xx/3xx instead of rejecting (4xx/5xx or a dropped connection): + +- **conflicting duplicate `Content-Length`** — two different values (CL.CL), +- **`Content-Length` + `Transfer-Encoding: chunked`** — the CL.TE / TE.CL ambiguity, +- **duplicate `Transfer-Encoding` with a non-chunked final encoding** — TE.TE, +- **whitespace-obfuscated header name** (`Transfer-Encoding : chunked`). + +Each body is bounded and self-contained so no probe can stall a +length-honoring parser. The check first confirms a well-formed POST is +accepted; if the endpoint rejects even that, it skips. + +**Remediate:** run a front-end that strictly conforms to RFC 9112 — reject +messages with conflicting `Content-Length`, with both `Content-Length` and +`Transfer-Encoding`, or with an unrecognized/obfuscated `Transfer-Encoding` +— and ensure the front-end and back-end share the same parsing rules. + +## Not yet covered + +HTTP/2→1.1 downgrade smuggling (H2.CL / H2.TE), raw H2C smuggling, and +connection-reuse boundary isolation (checklist items 2, 3, 5) require +HTTP/2 framing control and are tracked separately; the downgrade path ties +to the HTTP/2 & HTTP/3 protocol work. diff --git a/proxy/checks.go b/proxy/checks.go index 70427a0..3a80a55 100644 --- a/proxy/checks.go +++ b/proxy/checks.go @@ -37,6 +37,7 @@ import ( xforwardedfor "github.com/cerberauth/proxyaudit/proxy/checks/headertrust/x_forwarded_for" xforwardedhost "github.com/cerberauth/proxyaudit/proxy/checks/headertrust/x_forwarded_host" xrealip "github.com/cerberauth/proxyaudit/proxy/checks/headertrust/x_real_ip" + ambiguousframing "github.com/cerberauth/proxyaudit/proxy/checks/smuggling/ambiguous_framing" internaltargetaccess "github.com/cerberauth/proxyaudit/proxy/checks/ssrf/internal_target_access" openforwardproxy "github.com/cerberauth/proxyaudit/proxy/checks/ssrf/open_forward_proxy" headerssti "github.com/cerberauth/proxyaudit/proxy/checks/templateinjection/header_ssti" @@ -117,6 +118,8 @@ func AllChecks() ([]harnessx.Check, map[harnessx.CheckID]checkdef.CheckDef) { // Open Proxy / SSRF via the Proxy (Agg-tier, --aggressive). openforwardproxy.Check, internaltargetaccess.Check, + // Request Smuggling / Desync (Agg-tier, --aggressive). + ambiguousframing.Check, } defs := map[harnessx.CheckID]checkdef.CheckDef{ @@ -166,6 +169,8 @@ func AllChecks() ([]harnessx.Check, map[harnessx.CheckID]checkdef.CheckDef) { openforwardproxy.Check.ID: openforwardproxy.Def, internaltargetaccess.Check.ID: internaltargetaccess.Def, + + ambiguousframing.Check.ID: ambiguousframing.Def, } return checks, defs diff --git a/proxy/checks/smuggling/ambiguous_framing/check.go b/proxy/checks/smuggling/ambiguous_framing/check.go new file mode 100644 index 0000000..af53b38 --- /dev/null +++ b/proxy/checks/smuggling/ambiguous_framing/check.go @@ -0,0 +1,151 @@ +// Package ambiguousframing tests how the proxy handles requests whose +// message length is ambiguous — the precondition for every classic +// request-smuggling / desync attack (CL.TE, TE.CL, TE.TE). RFC 9112 +// requires a server to reject a message that carries conflicting +// Content-Length values, or both Content-Length and Transfer-Encoding, or +// an unrecognized Transfer-Encoding. A front-end that instead accepts such +// a message, and a back-end that resolves the length differently, is a +// desync waiting to happen. +// +// The probe never performs an actual desync: it sends only benign, +// self-contained bodies and never tries to smuggle a second request, so it +// cannot poison another client's connection (each probe uses its own +// Connection: close socket). It reports purely on whether the ambiguous +// message was rejected (a 4xx/5xx or a dropped connection) or accepted (a +// 2xx/3xx) — acceptance being the smuggling precondition. +// +// Requires --aggressive: it puts deliberately malformed requests on the +// wire, part of the opt-in attack-simulation suite. +package ambiguousframing + +import ( + "context" + _ "embed" + "net/http" + "net/url" + "strconv" + + "github.com/cerberauth/harnessx" + "github.com/cerberauth/harnessx/checkdef" + "github.com/cerberauth/proxyaudit/proxy/checks/smuggling/smugprobe" + "github.com/cerberauth/proxyaudit/proxy/probebase" + "github.com/cerberauth/proxyaudit/proxy/scanctx" +) + +//go:embed check.yaml +var checkYAML []byte + +var Def = checkdef.MustParseCheckDefYAML("ambiguous_framing", checkYAML) + +const crlf = "\r\n" + +// variant is one ambiguous-framing message shape. +type variant struct { + id string + title string + why string + headers string // header lines (each ending in CRLF) after Host/Connection + body string +} + +// variants are RFC-violating framings a conformant proxy must reject. Each +// body is self-contained and bounded so no probe can hang a length-honoring +// parser waiting for bytes that never arrive. +var variants = []variant{ + { + id: "duplicate_content_length", + title: "conflicting duplicate Content-Length", + why: "two Content-Length headers with different values — a front-end and back-end that pick different ones disagree on where the request ends", + headers: "Content-Length: 6" + crlf + "Content-Length: 5" + crlf, + body: "AAAAAA", // 6 bytes; both lengths are satisfiable, so no parser stalls + }, + { + id: "cl_te", + title: "both Content-Length and Transfer-Encoding", + why: "Content-Length and Transfer-Encoding: chunked together — the CL.TE / TE.CL ambiguity, where one hop honors the length and the other the chunking", + headers: "Content-Length: 6" + crlf + "Transfer-Encoding: chunked" + crlf, + body: "0" + crlf + crlf + "X", // valid empty chunk (5 bytes) + 1 pad byte = 6, satisfies CL too + }, + { + id: "te_obfuscated_value", + title: "Transfer-Encoding with an unrecognized final encoding", + why: "two Transfer-Encoding headers where the final one isn't chunked — a hop that reads the first chunks the body, a hop that reads the last doesn't (TE.TE)", + headers: "Transfer-Encoding: chunked" + crlf + "Transfer-Encoding: identity" + crlf, + body: "0" + crlf + crlf, + }, + { + id: "te_obfuscated_name", + title: "whitespace-obfuscated Transfer-Encoding header name", + why: "a space before the colon in \"Transfer-Encoding : chunked\" — a strict parser ignores the header, a lenient one honors the chunking, so the two hops frame the body differently", + headers: "Transfer-Encoding : chunked" + crlf, + body: "0" + crlf + crlf, + }, +} + +var Check = checkdef.NewCheck(Def, func(ctx context.Context, target harnessx.Target, _ harnessx.ResultStore) (harnessx.Result, error) { + sctx := scanctx.TargetContext(target) + if !sctx.Aggressive { + return harnessx.Result{Skipped: true, SkipReason: "aggressive-suite check — rerun with --aggressive to send ambiguous-framing probes"}, nil + } + + tgt, err := smugprobe.NewTarget(sctx, target.URL) + if err != nil { + return harnessx.Result{Err: err}, nil + } + path := requestPath(target.URL) + + // Baseline: a well-formed POST with a correct Content-Length must be + // accepted, or the endpoint rejects everything and acceptance of a + // malformed variant can't be read as a framing weakness. + control := tgt.Send(ctx, build(path, tgt.Host(), "Content-Length: 6"+crlf, "AAAAAA")) + if control.Rejected() { + return harnessx.Result{Skipped: true, SkipReason: "endpoint rejects a well-formed POST (" + describe(control) + ") — can't establish a baseline to judge ambiguous framing against"}, nil + } + + var observations []harnessx.Observation + for _, v := range variants { + resp := tgt.Send(ctx, build(path, tgt.Host(), v.headers, v.body)) + if resp.Rejected() { + continue + } + observations = append(observations, harnessx.Observation{ + Title: "Proxy accepts ambiguous request framing (" + v.title + ")", + Description: "A request with " + v.why + " was accepted by the proxy (" + describe(resp) + ") instead of being rejected as RFC 9112 requires. If the back-end resolves the message length differently from this front-end, the mismatch is a request-smuggling / desync primitive. This check does not attempt the desync itself — it reports only that the malformed framing was not rejected.", + Evidence: "POST " + path + " with " + v.title + " -> " + describe(resp) + "; well-formed control -> " + describe(control), + Metadata: map[string]string{"variant": v.id, "status": statusText(resp)}, + }) + } + return harnessx.Result{Observations: observations}, nil +}, checkdef.WithSkip(probebase.SkipUnlessFetched)) + +// build assembles a raw HTTP/1.1 request from a path, host, extra header +// lines (each CRLF-terminated), and a body sent verbatim. +func build(path, host, headers, body string) string { + return "POST " + path + " HTTP/1.1" + crlf + + "Host: " + host + crlf + + "User-Agent: proxyaudit" + crlf + + "Connection: close" + crlf + + headers + crlf + body +} + +func requestPath(rawURL string) string { + u, err := url.Parse(rawURL) + if err != nil || u.RequestURI() == "" { + return "/" + } + return u.RequestURI() +} + +func describe(r *smugprobe.Response) string { + if r.Err != nil { + return "connection dropped/failed" + } + return statusText(r) +} + +func statusText(r *smugprobe.Response) string { + if r.Err != nil { + return "no response" + } + return strconv.Itoa(r.StatusCode) + " " + http.StatusText(r.StatusCode) +} diff --git a/proxy/checks/smuggling/ambiguous_framing/check.yaml b/proxy/checks/smuggling/ambiguous_framing/check.yaml new file mode 100644 index 0000000..5ed18d3 --- /dev/null +++ b/proxy/checks/smuggling/ambiguous_framing/check.yaml @@ -0,0 +1,13 @@ +id: "smuggling.ambiguous_framing" +name: "Ambiguous Request Framing Accepted (Smuggling Precondition)" +description: "Sends requests with RFC-violating, ambiguous message length — conflicting duplicate Content-Length, Content-Length together with Transfer-Encoding (CL.TE/TE.CL), and obfuscated Transfer-Encoding (TE.TE) — and flags any the proxy accepts instead of rejecting. Acceptance is the precondition for request smuggling / desync when a back-end resolves the length differently. Non-destructive: it sends only benign self-contained bodies and never attempts the desync itself. Requires --aggressive." +link: "https://portswigger.net/web-security/request-smuggling" +tags: + - smuggling + - desync +depends_on: + - "probe.http_fetch" +cvss_vector: "CVSS:4.0/AV:N/AC:H/AT:P/PR:N/UI:N/VC:H/VI:H/VA:L/SC:N/SI:N/SA:N" +cvss_score: 7.3 +cwe_id: "CWE-444" +owasp: "API8:2023" diff --git a/proxy/checks/smuggling/smuggling_test.go b/proxy/checks/smuggling/smuggling_test.go new file mode 100644 index 0000000..839edb4 --- /dev/null +++ b/proxy/checks/smuggling/smuggling_test.go @@ -0,0 +1,124 @@ +// Package smuggling_test exercises the checks/smuggling scanners against a +// raw TCP server (net/http's own server rewrites malformed framing, so a +// hand-rolled listener is needed to model a proxy that accepts or rejects +// it) driven through a harnessx.Engine as proxyaudit would. +package smuggling_test + +import ( + "context" + "fmt" + "net" + "net/http" + "strings" + "testing" + "time" + + "github.com/cerberauth/harnessx" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + ambiguousframing "github.com/cerberauth/proxyaudit/proxy/checks/smuggling/ambiguous_framing" + "github.com/cerberauth/proxyaudit/proxy/probebase" + "github.com/cerberauth/proxyaudit/proxy/scanctx" +) + +// behaviour decides how the stub proxy answers a raw request. +type behaviour func(raw string) int + +// permissive accepts everything. +func permissive(string) int { return http.StatusOK } + +// strict rejects RFC-violating ambiguous framing, accepts the rest — +// a conformant proxy. +func strict(raw string) int { + lower := strings.ToLower(raw) + switch { + case strings.Count(lower, "content-length:") >= 2, + strings.Contains(lower, "content-length:") && strings.Contains(lower, "transfer-encoding"), + strings.Count(lower, "transfer-encoding") >= 2, + strings.Contains(raw, "Transfer-Encoding :"): + return http.StatusBadRequest + } + return http.StatusOK +} + +// rejectAll models an endpoint that 400s even a well-formed request. +func rejectAll(string) int { return http.StatusBadRequest } + +// rawServer serves one response per connection, its status chosen by fn +// after reading the request bytes. +func rawServer(t *testing.T, fn behaviour) string { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = ln.Close() }) + + go func() { + for { + conn, err := ln.Accept() + if err != nil { + return + } + go func(c net.Conn) { + defer c.Close() + _ = c.SetReadDeadline(time.Now().Add(time.Second)) + // The whole request arrives in one write before the client + // reads, so one Read returns it — no need to drain to EOF + // (the client holds the connection open) and wait out the + // deadline. + buf := make([]byte, 8192) + n, _ := c.Read(buf) + code := fn(string(buf[:n])) + body := "OK" + _, _ = fmt.Fprintf(c, "HTTP/1.1 %d %s\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", + code, http.StatusText(code), len(body), body) + }(conn) + } + }() + return "http://" + ln.Addr().String() +} + +func run(t *testing.T, url string, aggressive bool) harnessx.ScanSummary { + t.Helper() + target := scanctx.NewTarget(url, &scanctx.ScanContext{HTTPClient: http.DefaultClient, Aggressive: aggressive}) + engine := harnessx.New(harnessx.WithChecks(probebase.HTTPFetchCheck, ambiguousframing.Check)) + summary, err := engine.Run(context.Background(), target) + require.NoError(t, err) + return summary +} + +func TestAmbiguousFraming_PermissiveProxy_FlagsEveryVariant(t *testing.T) { + summary := run(t, rawServer(t, permissive), true) + variants := map[string]bool{} + for _, o := range summary.Observations { + variants[o.Metadata["variant"]] = true + } + for _, want := range []string{"duplicate_content_length", "cl_te", "te_obfuscated_value", "te_obfuscated_name"} { + assert.True(t, variants[want], "expected variant %q flagged, got %v", want, variants) + } +} + +func TestAmbiguousFraming_StrictProxy_NoFindings(t *testing.T) { + assert.Empty(t, run(t, rawServer(t, strict), true).Observations) +} + +func TestAmbiguousFraming_EndpointRejectsBaseline_Skipped(t *testing.T) { + summary := run(t, rawServer(t, rejectAll), true) + assert.Empty(t, summary.Observations) + assert.True(t, skipped(summary)) +} + +func TestAmbiguousFraming_NotAggressive_Skipped(t *testing.T) { + summary := run(t, rawServer(t, permissive), false) + assert.Empty(t, summary.Observations) + assert.True(t, skipped(summary)) +} + +func skipped(summary harnessx.ScanSummary) bool { + for _, r := range summary.Results { + if r.CheckID == ambiguousframing.Check.ID { + return r.Skipped + } + } + return false +} diff --git a/proxy/checks/smuggling/smugprobe/smugprobe.go b/proxy/checks/smuggling/smugprobe/smugprobe.go new file mode 100644 index 0000000..973ae91 --- /dev/null +++ b/proxy/checks/smuggling/smugprobe/smugprobe.go @@ -0,0 +1,120 @@ +// Package smugprobe sends a byte-for-byte request over a raw TCP (or TLS) +// connection, bypassing net/http's request writer entirely so a check can +// put duplicate Content-Length headers, conflicting framing, and +// obfuscated Transfer-Encoding on the wire — things the standard client +// sanitizes away. Each send uses its own connection with Connection: +// close, so an ambiguous message can only desync the probe's own socket, +// never a reused one serving another client. +package smugprobe + +import ( + "bufio" + "context" + "crypto/tls" + "io" + "net" + "net/http" + "net/url" + "time" + + "github.com/cerberauth/proxyaudit/proxy/scanctx" +) + +const maxBodyBytes = 1 << 15 // 32 KiB + +// Response is one raw send's outcome. Err is set when the connection +// failed or no response could be read (e.g. the proxy dropped the +// connection on a malformed message, which counts as a rejection). +type Response struct { + StatusCode int + Header http.Header + Err error +} + +// Rejected reports whether the message was refused: a dropped/failed +// connection, or a 4xx/5xx status. A sub-400 status means the proxy +// accepted the message and passed it on. +func (r *Response) Rejected() bool { + return r == nil || r.Err != nil || r.StatusCode >= 400 +} + +// Target carries the parsed destination so a check can send many messages +// to the same host cheaply. +type Target struct { + sctx *scanctx.ScanContext + addr string + host string + tls bool +} + +// NewTarget parses baseURL into a reusable send Target. +func NewTarget(sctx *scanctx.ScanContext, baseURL string) (*Target, error) { + u, err := url.Parse(baseURL) + if err != nil { + return nil, err + } + host := u.Hostname() + port := u.Port() + if port == "" { + if u.Scheme == "https" { + port = "443" + } else { + port = "80" + } + } + return &Target{sctx: sctx, addr: net.JoinHostPort(host, port), host: host, tls: u.Scheme == "https"}, nil +} + +// Host returns the authority to use in a Host header for this target. +func (t *Target) Host() string { return t.addr } + +// Send writes raw verbatim and reads the response. +func (t *Target) Send(ctx context.Context, raw string) *Response { + timeout := t.sctx.HTTPClient.Timeout + if timeout <= 0 { + timeout = 10 * time.Second + } + dialCtx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + + conn, err := (&net.Dialer{}).DialContext(dialCtx, "tcp", t.addr) + if err != nil { + return &Response{Err: err} + } + defer conn.Close() + + if t.tls { + tlsConn := tls.Client(conn, t.tlsConfig()) + if err := tlsConn.HandshakeContext(dialCtx); err != nil { + return &Response{Err: err} + } + conn = tlsConn + } + if deadline, ok := dialCtx.Deadline(); ok { + _ = conn.SetDeadline(deadline) + } + + if _, err := io.WriteString(conn, raw); err != nil { + return &Response{Err: err} + } + + resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: http.MethodPost}) + if err != nil { + return &Response{Err: err} + } + defer resp.Body.Close() + _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxBodyBytes)) + return &Response{StatusCode: resp.StatusCode, Header: resp.Header} +} + +func (t *Target) tlsConfig() *tls.Config { + cfg := &tls.Config{ServerName: t.host, MinVersion: tls.VersionTLS12} + if tr, ok := t.sctx.HTTPClient.Transport.(*http.Transport); ok && tr.TLSClientConfig != nil { + cfg.RootCAs = tr.TLSClientConfig.RootCAs + cfg.InsecureSkipVerify = tr.TLSClientConfig.InsecureSkipVerify + } + if t.sctx.RootCAs != nil { + cfg.RootCAs = t.sctx.RootCAs + } + return cfg +}