From d95017b95c614e37522773e32491c4d85e38a1cd Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Sun, 17 Dec 2023 15:58:49 +0700 Subject: [PATCH 1/2] network proto: revert full buffer size read --- pkg/protocols/network/request.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/protocols/network/request.go b/pkg/protocols/network/request.go index e66524760e..8d0684d1e6 100644 --- a/pkg/protocols/network/request.go +++ b/pkg/protocols/network/request.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "net/url" + "os" "strings" "time" @@ -30,7 +31,6 @@ import ( templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types" errorutil "github.com/projectdiscovery/utils/errors" mapsutil "github.com/projectdiscovery/utils/maps" - "github.com/projectdiscovery/utils/reader" ) var ( @@ -265,7 +265,7 @@ func (request *Request) executeRequestWithPayloads(variables map[string]interfac } if input.Read > 0 { - buffer, err := reader.ConnReadNWithTimeout(conn, int64(input.Read), DefaultReadTimeout) + buffer, err := ConnReadNWithTimeout(conn, int64(input.Read), DefaultReadTimeout) if err != nil { return errorutil.NewWithErr(err).Msgf("could not read response from connection") } @@ -315,7 +315,7 @@ func (request *Request) executeRequestWithPayloads(variables map[string]interfac bufferSize = -1 } - final, err := reader.ConnReadNWithTimeout(conn, int64(bufferSize), DefaultReadTimeout) + final, err := ConnReadNWithTimeout(conn, int64(bufferSize), DefaultReadTimeout) if err != nil { request.options.Output.Request(request.options.TemplatePath, address, request.Type().String(), err) return errors.Wrap(err, "could not read from server") @@ -412,3 +412,21 @@ func getAddress(toTest string) (string, error) { } return toTest, nil } + +func ConnReadNWithTimeout(conn net.Conn, n int64, timeout time.Duration) ([]byte, error) { + if timeout == 0 { + timeout = DefaultReadTimeout + } + b := make([]byte, n) + _ = conn.SetDeadline(time.Now().Add(timeout)) + count, err := conn.Read(b) + _ = conn.SetDeadline(time.Time{}) + if err != nil && os.IsTimeout(err) && count > 0 { + // in case of timeout with some value read, return the value + return b[:count], nil + } + if err != nil { + return nil, err + } + return b[:count], nil +} From 3e743a6d59f2964d9487ba661aa5d92cdd8c0223 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Sun, 17 Dec 2023 17:35:03 +0700 Subject: [PATCH 2/2] fix read-all in network protocol --- pkg/protocols/network/request.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/protocols/network/request.go b/pkg/protocols/network/request.go index 8d0684d1e6..c896b6551b 100644 --- a/pkg/protocols/network/request.go +++ b/pkg/protocols/network/request.go @@ -31,6 +31,7 @@ import ( templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types" errorutil "github.com/projectdiscovery/utils/errors" mapsutil "github.com/projectdiscovery/utils/maps" + "github.com/projectdiscovery/utils/reader" ) var ( @@ -417,6 +418,12 @@ func ConnReadNWithTimeout(conn net.Conn, n int64, timeout time.Duration) ([]byte if timeout == 0 { timeout = DefaultReadTimeout } + if n == -1 { + // if n is -1 then read all available data from connection + return reader.ConnReadNWithTimeout(conn, -1, timeout) + } else if n == 0 { + n = 4096 // default buffer size + } b := make([]byte, n) _ = conn.SetDeadline(time.Now().Add(timeout)) count, err := conn.Read(b)