From 6c617a092f8ce60e4ef53528a4c238203e98ac5a Mon Sep 17 00:00:00 2001 From: RicYaben Date: Thu, 1 Aug 2024 09:59:15 +0100 Subject: [PATCH] fixes issue where the token was appended to the query (pointer mistake) --- modules/webproxy/request/builder.go | 21 ++++++--------------- modules/webproxy/webproxy_test.go | 25 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/modules/webproxy/request/builder.go b/modules/webproxy/request/builder.go index 03bb048b..1b860451 100644 --- a/modules/webproxy/request/builder.go +++ b/modules/webproxy/request/builder.go @@ -95,30 +95,21 @@ func (builder *httpProxyRequestBuilder) setHeaders(headers http.Header) { } func (builder *httpProxyRequestBuilder) Build(token string) (*http.Request, error) { - - // Add the body - var b *strings.Reader - if len(token) > 0 { - b = strings.NewReader(token) + // Create the request + req, err := http.NewRequest(builder.method, builder.url.String(), strings.NewReader(token)) + if err != nil { + return nil, err } // Slug token if needed - uri := builder.url if builder.slug { - q := uri.Query() + q := req.URL.Query() q.Add("token", token) - uri.RawQuery = q.Encode() - } - - // Create the request - req, err := http.NewRequest(builder.method, uri.String(), b) - if err != nil { - return nil, err + req.URL.RawQuery = q.Encode() } // Add the headers // The constructor does not add any header. req.Header = builder.headers - return req, nil } diff --git a/modules/webproxy/webproxy_test.go b/modules/webproxy/webproxy_test.go index 9a46c6e6..551ec54e 100644 --- a/modules/webproxy/webproxy_test.go +++ b/modules/webproxy/webproxy_test.go @@ -4,12 +4,13 @@ import ( "fmt" "io" "net" - "net/http" "testing" "time" "github.com/golang-jwt/jwt" "github.com/zmap/zgrab2" + "github.com/zmap/zgrab2/lib/http" + "github.com/zmap/zgrab2/modules/webproxy/request" ) type webproxyTester struct { @@ -72,6 +73,7 @@ func (cfg *webproxyTester) runTest(t *testing.T, testName string) { // Run the server and start the scan go cfg.runHTTPServer(t) + _, _, err = scanner.Scan(target) if err != nil { t.Fatalf("[%s] error while sending: %v", testName, err) @@ -102,12 +104,11 @@ func (cfg *webproxyTester) runTest(t *testing.T, testName string) { var tests = map[string]*webproxyTester{ "success": { paddress: "10.176.21.85", - laddress: "10.176.21.141", + laddress: "10.253.211.188", pport: 8080, lport: 8081, bChan: make(chan string, 1), hmackey: "gz13WcqhVBy09Mnw7ZZYNCqqlWvyRfJx", - slug: true, }, } @@ -136,3 +137,21 @@ func TestProxy(t *testing.T) { cfg.runTest(t, tname) } } + +func TestRequestBuilder(t *testing.T) { + b, err := request.NewHttpRequestBuilder("POST", "localhost:8080", http.Header{"cookie": {"123test"}}, true) + if err != nil { + t.Fatal(err) + } + + var times = 3 + for range times { + r, err := b.Build("123test") + if err != nil { + t.Fatal(err) + } + if r.URL.RawQuery != "token=123test" { + t.Fatalf("unexpected query: %s", r.URL.RawQuery) + } + } +}