mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-27 10:20:54 +08:00
fix(traffic): index and match the decoded text of compressed responses
The proxy records bodies as they crossed the wire. A response sent with Content-Encoding gzip, deflate or br (what Go tools such as nuclei, httpx and ffuf, Python requests and browsers ask for) was stored compressed, seen as binary and left out of the full-text index. Body search found nothing in it, and ResponseContaining could not tie a fact to it. Index the decoded text of such bodies, bounded to the index limit, and let ResponseContaining match the decoded response. The stored bytes are unchanged.
This commit is contained in:
@@ -4,6 +4,7 @@ go 1.26.11
|
||||
|
||||
require (
|
||||
github.com/Autumn-27/norma v0.4.11
|
||||
github.com/andybalholm/brotli v1.2.7
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.5
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.13
|
||||
@@ -26,7 +27,6 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.2.7 // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 // indirect
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"compress/gzip"
|
||||
"compress/zlib"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/andybalholm/brotli"
|
||||
)
|
||||
|
||||
// decodeBody undoes a single Content-Encoding layer (gzip, deflate or br) and
|
||||
// returns at most limit decoded bytes. The proxy records bodies as they crossed
|
||||
// the wire, so a compressed body has to be decoded before its text can be
|
||||
// indexed or matched. ok is false when the body is not encoded, the encoding is
|
||||
// unsupported or stacked, or the data does not decode. The output is bounded
|
||||
// because the body comes from the target.
|
||||
func decodeBody(header http.Header, body []byte, limit int) (out []byte, ok bool) {
|
||||
enc := strings.ToLower(strings.TrimSpace(header.Get("Content-Encoding")))
|
||||
if enc == "" || enc == "identity" || len(body) == 0 || strings.Contains(enc, ",") {
|
||||
return nil, false
|
||||
}
|
||||
var zr io.Reader
|
||||
switch enc {
|
||||
case "gzip", "x-gzip":
|
||||
gz, err := gzip.NewReader(bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
zr = gz
|
||||
case "deflate":
|
||||
// HTTP deflate is zlib (RFC 9110); some servers send a raw deflate stream.
|
||||
if z, err := zlib.NewReader(bytes.NewReader(body)); err == nil {
|
||||
zr = z
|
||||
} else {
|
||||
zr = flate.NewReader(bytes.NewReader(body))
|
||||
}
|
||||
case "br":
|
||||
zr = brotli.NewReader(bytes.NewReader(body))
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
out, err := io.ReadAll(io.LimitReader(zr, int64(limit)))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
// indexBody returns the full-text index text of a recorded body: the decoded
|
||||
// text of a compressed body, otherwise what spill computed.
|
||||
func indexBody(header http.Header, body []byte, spilled string) string {
|
||||
dec, ok := decodeBody(header, body, maxIndexBody)
|
||||
if !ok {
|
||||
return spilled
|
||||
}
|
||||
if isBinaryBody(header.Get("Content-Type"), dec) {
|
||||
return ""
|
||||
}
|
||||
return string(dec)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"compress/zlib"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/andybalholm/brotli"
|
||||
)
|
||||
|
||||
const compressedPage = "<html><body>Welcome admin. root:x:0:0:root:/root:/bin/bash</body></html>"
|
||||
|
||||
// TestCompressedResponseIsSearchable records a gzip response through the real
|
||||
// proxy, as a Go client (nuclei, httpx, ffuf) fetches it: Accept-Encoding gzip
|
||||
// is sent by default and the proxy keeps the body as it crossed the wire. Body
|
||||
// search and fact evidence must still see its text.
|
||||
func TestCompressedResponseIsSearchable(t *testing.T) {
|
||||
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
io.WriteString(w, compressedPage)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
io.WriteString(gz, compressedPage)
|
||||
gz.Close()
|
||||
}))
|
||||
defer target.Close()
|
||||
tr, _, proxyURL := startProxy(t)
|
||||
pu, _ := url.Parse(proxyURL)
|
||||
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(pu)}}
|
||||
resp, err := client.Get(target.URL + "/passwd")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
host := strings.TrimPrefix(target.URL, "http://")
|
||||
var rows []ExchangeMeta
|
||||
for deadline := time.Now().Add(5 * time.Second); len(rows) == 0 && time.Now().Before(deadline); {
|
||||
rows, _ = tr.query(host, "", "", 0, 10)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("exchange not recorded: %d rows", len(rows))
|
||||
}
|
||||
if _, r, _ := tr.Get(rows[0].ID); !strings.Contains(r, "Content-Encoding: gzip") {
|
||||
t.Fatalf("test needs a gzip capture, got response %q", r)
|
||||
}
|
||||
|
||||
hits, err := tr.query(host, "", "root:x:0", 0, 10)
|
||||
if err != nil || len(hits) != 1 {
|
||||
t.Fatalf("body search for text of a gzip response: %d rows, err=%v; want 1", len(hits), err)
|
||||
}
|
||||
id, ok, err := tr.ResponseContaining([]string{"root:x:0:0:root"}, 0, time.Now().Unix()+1)
|
||||
if err != nil || !ok || id != rows[0].ID {
|
||||
t.Fatalf("ResponseContaining on a gzip response = (%q, %v, %v), want (%q, true, nil)", id, ok, err, rows[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompressedBodyIndexEncodings(t *testing.T) {
|
||||
var br, zl bytes.Buffer
|
||||
bw := brotli.NewWriter(&br)
|
||||
io.WriteString(bw, compressedPage)
|
||||
bw.Close()
|
||||
zw := zlib.NewWriter(&zl)
|
||||
io.WriteString(zw, compressedPage)
|
||||
zw.Close()
|
||||
|
||||
for _, tc := range []struct {
|
||||
enc string
|
||||
body []byte
|
||||
}{{"br", br.Bytes()}, {"deflate", zl.Bytes()}} {
|
||||
t.Run(tc.enc, func(t *testing.T) {
|
||||
tr, _ := openTraffic(t)
|
||||
f := newFlow("api.example.com", "GET", "/"+tc.enc, nil, tc.body)
|
||||
f.Response.Header.Set("Content-Type", "text/html")
|
||||
f.Response.Header.Set("Content-Encoding", tc.enc)
|
||||
tr.record(f)
|
||||
hits, err := tr.query("api.example.com", "", "root:x:0", 0, 10)
|
||||
if err != nil || len(hits) != 1 {
|
||||
t.Fatalf("body search for text of a %s response: %d rows, err=%v; want 1", tc.enc, len(hits), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecodeBodyIsBounded keeps a compression bomb from a target from being
|
||||
// expanded beyond the index limit.
|
||||
func TestDecodeBodyIsBounded(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
gz := gzip.NewWriter(&buf)
|
||||
gz.Write(make([]byte, 4*maxIndexBody))
|
||||
gz.Close()
|
||||
out, ok := decodeBody(http.Header{"Content-Encoding": {"gzip"}}, buf.Bytes(), maxIndexBody)
|
||||
if !ok || len(out) != maxIndexBody {
|
||||
t.Fatalf("decodeBody returned %d bytes, ok=%v; want %d", len(out), ok, maxIndexBody)
|
||||
}
|
||||
}
|
||||
+21
-2
@@ -733,6 +733,8 @@ func (t *Traffic) recordViews(rq requestView, rs responseView, lines []rewrite.T
|
||||
// work and must not sit inside the SQLite write lock.
|
||||
reqB := t.spill(rq.body, rq.header.Get("Content-Type"))
|
||||
respB := t.spill(rs.body, ct)
|
||||
reqB.index = indexBody(rq.header, rq.body, reqB.index)
|
||||
respB.index = indexBody(rs.header, rs.body, respB.index)
|
||||
trace := t.spillTrace(lines)
|
||||
|
||||
// A rewritten exchange is logged by host, step and rule ids only: its URL
|
||||
@@ -1234,15 +1236,32 @@ ORDER BY ts DESC LIMIT 8`, fromTS, toTS, arg)
|
||||
}
|
||||
for _, cand := range ids {
|
||||
_, resp, gerr := t.Get(cand)
|
||||
if gerr != nil || !strings.Contains(resp, line) {
|
||||
if gerr != nil {
|
||||
continue
|
||||
}
|
||||
return cand, true, nil
|
||||
if strings.Contains(resp, line) || t.decodedResponseContains(cand, line) {
|
||||
return cand, true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
// decodedResponseContains reports whether the decoded body of a compressed
|
||||
// response contains line. Get returns the body as recorded, which for a
|
||||
// Content-Encoding response is the compressed bytes.
|
||||
func (t *Traffic) decodedResponseContains(id, line string) bool {
|
||||
_, _, respHead, respBody, err := t.loadFullExchange(id, "")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, _, headers := parseStoredResponseHead(respHead)
|
||||
h := http.Header{}
|
||||
h.Set("Content-Encoding", findHeaderValue(headers, "Content-Encoding"))
|
||||
dec, ok := decodeBody(h, respBody, maxIndexBody)
|
||||
return ok && strings.Contains(string(dec), line)
|
||||
}
|
||||
|
||||
// ftsFilter builds the SQL condition restricting rows to those whose indexed
|
||||
// text matches term. ok is false when full-text search cannot serve the term —
|
||||
// no FTS index on this build, or fewer than three characters, which the trigram
|
||||
|
||||
Reference in New Issue
Block a user