diff --git a/lib/sdk.go b/lib/sdk.go index 04b084f244..daeb68c14b 100644 --- a/lib/sdk.go +++ b/lib/sdk.go @@ -178,7 +178,8 @@ func (e *NucleiEngine) SignTemplate(tmplSigner *signer.TemplateSigner, data []by if err != nil { return data, err } - buff := bytes.NewBuffer(signer.RemoveSignatureFromData(data)) + _, content := signer.ExtractSignatureAndContent(data) + buff := bytes.NewBuffer(content) buff.WriteString("\n" + signatureData) return buff.Bytes(), err } diff --git a/pkg/protocols/common/protocolstate/state.go b/pkg/protocols/common/protocolstate/state.go index b7c7796fa8..5c56fdf77e 100644 --- a/pkg/protocols/common/protocolstate/state.go +++ b/pkg/protocols/common/protocolstate/state.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/url" + "sync" "github.com/go-sql-driver/mysql" "github.com/pkg/errors" @@ -19,9 +20,17 @@ import ( // Dialer is a shared fastdialer instance for host DNS resolution var ( - Dialer *fastdialer.Dialer + muDialer sync.RWMutex + Dialer *fastdialer.Dialer ) +func GetDialer() *fastdialer.Dialer { + muDialer.RLock() + defer muDialer.RUnlock() + + return Dialer +} + func ShouldInit() bool { return Dialer == nil } @@ -210,10 +219,12 @@ func interfaceAddresses(interfaceName string) ([]net.Addr, error) { // Close closes the global shared fastdialer func Close() { + muDialer.Lock() + defer muDialer.Unlock() + if Dialer != nil { Dialer.Close() Dialer = nil } - Dialer = nil StopActiveMemGuardian() } diff --git a/pkg/protocols/http/httpclientpool/clientpool.go b/pkg/protocols/http/httpclientpool/clientpool.go index 2c244556ae..65879818f6 100644 --- a/pkg/protocols/http/httpclientpool/clientpool.go +++ b/pkg/protocols/http/httpclientpool/clientpool.go @@ -29,6 +29,7 @@ import ( var ( rawHttpClient *rawhttp.Client + rawHttpClientOnce sync.Once forceMaxRedirects int normalClient *retryablehttp.Client clientPool *mapsutil.SyncLockMap[string, *retryablehttp.Client] @@ -102,6 +103,22 @@ type Configuration struct { ResponseHeaderTimeout time.Duration } +func (c *Configuration) Clone() *Configuration { + clone := *c + if c.Connection != nil { + cloneConnection := &ConnectionConfiguration{ + DisableKeepAlive: c.Connection.DisableKeepAlive, + } + if c.Connection.HasCookieJar() { + cookiejar := *c.Connection.GetCookieJar() + cloneConnection.SetCookieJar(&cookiejar) + } + clone.Connection = cloneConnection + } + + return &clone +} + // Hash returns the hash of the configuration to allow client pooling func (c *Configuration) Hash() string { builder := &strings.Builder{} @@ -131,7 +148,7 @@ func (c *Configuration) HasStandardOptions() bool { // GetRawHTTP returns the rawhttp request client func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client { - if rawHttpClient == nil { + rawHttpClientOnce.Do(func() { rawHttpOptions := rawhttp.DefaultOptions if types.ProxyURL != "" { rawHttpOptions.Proxy = types.ProxyURL @@ -142,7 +159,7 @@ func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client { } rawHttpOptions.Timeout = options.Options.GetTimeouts().HttpTimeout rawHttpClient = rawhttp.NewClient(rawHttpOptions) - } + }) return rawHttpClient } @@ -233,7 +250,7 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl transport := &http.Transport{ ForceAttemptHTTP2: options.ForceAttemptHTTP2, - DialContext: protocolstate.Dialer.Dial, + DialContext: protocolstate.GetDialer().Dial, DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { if options.TlsImpersonate { return protocolstate.Dialer.DialTLSWithConfigImpersonate(ctx, network, addr, tlsConfig, impersonate.Random, nil) @@ -241,7 +258,7 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl if options.HasClientCertificates() || options.ForceAttemptHTTP2 { return protocolstate.Dialer.DialTLSWithConfig(ctx, network, addr, tlsConfig) } - return protocolstate.Dialer.DialTLS(ctx, network, addr) + return protocolstate.GetDialer().DialTLS(ctx, network, addr) }, MaxIdleConns: maxIdleConns, MaxIdleConnsPerHost: maxIdleConnsPerHost, diff --git a/pkg/protocols/http/request.go b/pkg/protocols/http/request.go index 98e1932477..5a1219b83f 100644 --- a/pkg/protocols/http/request.go +++ b/pkg/protocols/http/request.go @@ -770,7 +770,7 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ // check for cookie related configuration if input.CookieJar != nil { - connConfiguration := request.connConfiguration + connConfiguration := request.connConfiguration.Clone() connConfiguration.Connection.SetCookieJar(input.CookieJar) modifiedConfig = connConfiguration } @@ -778,7 +778,8 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ updatedTimeout, ok := generatedRequest.request.Context().Value(httpclientpool.WithCustomTimeout{}).(httpclientpool.WithCustomTimeout) if ok { if modifiedConfig == nil { - modifiedConfig = request.connConfiguration + connConfiguration := request.connConfiguration.Clone() + modifiedConfig = connConfiguration } modifiedConfig.ResponseHeaderTimeout = updatedTimeout.Timeout } @@ -941,7 +942,11 @@ func (request *Request) executeRequest(input *contextargs.Context, generatedRequ if input.MetaInput.CustomIP != "" { outputEvent["ip"] = input.MetaInput.CustomIP } else { - outputEvent["ip"] = protocolstate.Dialer.GetDialedIP(hostname) + dialer := protocolstate.GetDialer() + if dialer != nil { + outputEvent["ip"] = dialer.GetDialedIP(hostname) + } + // try getting cname request.addCNameIfAvailable(hostname, outputEvent) }