diff --git a/v2/internal/runner/banner.go b/v2/internal/runner/banner.go index b440ac342..9bf0929ba 100644 --- a/v2/internal/runner/banner.go +++ b/v2/internal/runner/banner.go @@ -7,11 +7,11 @@ const banner = ` ____ __ _______/ /__ (_) / __ \/ / / / ___/ / _ \/ / / / / / /_/ / /__/ / __/ / - /_/ /_/\__,_/\___/_/\___/_/ v2.3.4 + /_/ /_/\__,_/\___/_/\___/_/ v2.3.5 ` // Version is the current version of nuclei -const Version = `2.3.4` +const Version = `2.3.5` // showBanner is used to show the banner to the user func showBanner() { diff --git a/v2/internal/runner/runner.go b/v2/internal/runner/runner.go index 7b7590a89..d0e6f60aa 100644 --- a/v2/internal/runner/runner.go +++ b/v2/internal/runner/runner.go @@ -20,6 +20,7 @@ import ( "github.com/projectdiscovery/nuclei/v2/pkg/protocols" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/clusterer" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/interactsh" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolinit" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/headless/engine" "github.com/projectdiscovery/nuclei/v2/pkg/reporting" "github.com/projectdiscovery/nuclei/v2/pkg/reporting/exporters/disk" @@ -237,6 +238,7 @@ func (r *Runner) Close() { if r.projectFile != nil { r.projectFile.Close() } + protocolinit.Close() } // RunEnumeration sets up the input layer for giving input nuclei. diff --git a/v2/pkg/protocols/common/protocolinit/init.go b/v2/pkg/protocols/common/protocolinit/init.go index 5f58cbf6b..b31958c9c 100644 --- a/v2/pkg/protocols/common/protocolinit/init.go +++ b/v2/pkg/protocols/common/protocolinit/init.go @@ -2,6 +2,7 @@ package protocolinit import ( "github.com/corpix/uarand" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/dns/dnsclientpool" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/httpclientpool" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/network/networkclientpool" @@ -12,6 +13,9 @@ import ( func Init(options *types.Options) error { uarand.Default = uarand.NewWithCustomList(userAgents) + if err := protocolstate.Init(options); err != nil { + return err + } if err := dnsclientpool.Init(options); err != nil { return err } @@ -58,3 +62,7 @@ var userAgents = []string{ "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/4E423F", } + +func Close() { + protocolstate.Dialer.Close() +} diff --git a/v2/pkg/protocols/common/protocolstate/state.go b/v2/pkg/protocols/common/protocolstate/state.go new file mode 100644 index 000000000..deb6b3e3f --- /dev/null +++ b/v2/pkg/protocols/common/protocolstate/state.go @@ -0,0 +1,31 @@ +package protocolstate + +import ( + "github.com/pkg/errors" + "github.com/projectdiscovery/fastdialer/fastdialer" + "github.com/projectdiscovery/nuclei/v2/pkg/types" +) + +var Dialer *fastdialer.Dialer + +func Init(options *types.Options) error { + opts := fastdialer.DefaultOptions + if options.SystemResolvers { + opts.EnableFallback = true + } + if options.ResolversFile != "" { + opts.BaseResolvers = options.InternalResolversList + } + dialer, err := fastdialer.NewDialer(opts) + if err != nil { + return errors.Wrap(err, "could not create dialer") + } + Dialer = dialer + return nil +} + +func Close() { + if Dialer != nil { + Dialer.Close() + } +} diff --git a/v2/pkg/protocols/headless/engine/engine.go b/v2/pkg/protocols/headless/engine/engine.go index 8da632688..6c8d613e9 100644 --- a/v2/pkg/protocols/headless/engine/engine.go +++ b/v2/pkg/protocols/headless/engine/engine.go @@ -76,10 +76,7 @@ func New(options *types.Options) (*Browser, error) { if customAgent == "" { customAgent = uarand.GetRandom() } - httpclient, err := newhttpClient(options) - if err != nil { - return nil, err - } + httpclient := newhttpClient(options) engine := &Browser{ tempDir: dataStore, customAgent: customAgent, diff --git a/v2/pkg/protocols/headless/engine/http_client.go b/v2/pkg/protocols/headless/engine/http_client.go index 8f69b71dd..c9ec6e0ce 100644 --- a/v2/pkg/protocols/headless/engine/http_client.go +++ b/v2/pkg/protocols/headless/engine/http_client.go @@ -5,25 +5,13 @@ import ( "net/http" "time" - "github.com/pkg/errors" - "github.com/projectdiscovery/fastdialer/fastdialer" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate" "github.com/projectdiscovery/nuclei/v2/pkg/types" ) // newhttpClient creates a new http client for headless communication with a timeout -func newhttpClient(options *types.Options) (*http.Client, error) { - opts := fastdialer.DefaultOptions - if options.SystemResolvers { - opts.EnableFallback = true - } - if options.ResolversFile != "" { - opts.BaseResolvers = options.InternalResolversList - } - dialer, err := fastdialer.NewDialer(opts) - if err != nil { - return nil, errors.Wrap(err, "could not create dialer") - } - +func newhttpClient(options *types.Options) *http.Client { + dialer := protocolstate.Dialer transport := &http.Transport{ DialContext: dialer.Dial, MaxIdleConns: 500, @@ -34,5 +22,5 @@ func newhttpClient(options *types.Options) (*http.Client, error) { InsecureSkipVerify: true, }, } - return &http.Client{Transport: transport, Timeout: time.Duration(options.Timeout*3) * time.Second}, nil + return &http.Client{Transport: transport, Timeout: time.Duration(options.Timeout*3) * time.Second} } diff --git a/v2/pkg/protocols/headless/engine/page_actions_test.go b/v2/pkg/protocols/headless/engine/page_actions_test.go index 8a5b6e0bd..e89fe5354 100644 --- a/v2/pkg/protocols/headless/engine/page_actions_test.go +++ b/v2/pkg/protocols/headless/engine/page_actions_test.go @@ -9,11 +9,14 @@ import ( "testing" "time" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate" "github.com/projectdiscovery/nuclei/v2/pkg/types" "github.com/stretchr/testify/require" ) func TestActionNavigate(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() @@ -46,6 +49,8 @@ func TestActionNavigate(t *testing.T) { } func TestActionScript(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() @@ -113,6 +118,8 @@ func TestActionScript(t *testing.T) { } func TestActionClick(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() @@ -151,6 +158,8 @@ func TestActionClick(t *testing.T) { } func TestActionRightClick(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() @@ -197,6 +206,8 @@ func TestActionRightClick(t *testing.T) { } func TestActionTextInput(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() @@ -236,6 +247,8 @@ func TestActionTextInput(t *testing.T) { } func TestActionHeadersChange(t *testing.T) { + _ = protocolstate.Init(&types.Options{}) + browser, err := New(&types.Options{ShowBrowser: false}) require.Nil(t, err, "could not create browser") defer browser.Close() diff --git a/v2/pkg/protocols/http/httpclientpool/clientpool.go b/v2/pkg/protocols/http/httpclientpool/clientpool.go index e25d6e026..67ecfe746 100644 --- a/v2/pkg/protocols/http/httpclientpool/clientpool.go +++ b/v2/pkg/protocols/http/httpclientpool/clientpool.go @@ -15,6 +15,7 @@ import ( "github.com/pkg/errors" "github.com/projectdiscovery/fastdialer/fastdialer" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate" "github.com/projectdiscovery/nuclei/v2/pkg/types" "github.com/projectdiscovery/rawhttp" "github.com/projectdiscovery/retryablehttp-go" @@ -97,14 +98,7 @@ func wrappedGet(options *types.Options, configuration *Configuration) (*retryabl var err error if Dialer == nil { - opts := fastdialer.DefaultOptions - if options.SystemResolvers { - opts.EnableFallback = true - } - if options.ResolversFile != "" { - opts.BaseResolvers = options.InternalResolversList - } - Dialer, err = fastdialer.NewDialer(opts) + Dialer = protocolstate.Dialer } if err != nil { return nil, errors.Wrap(err, "could not create dialer") diff --git a/v2/pkg/protocols/http/request.go b/v2/pkg/protocols/http/request.go index f46a72944..d1f17408f 100644 --- a/v2/pkg/protocols/http/request.go +++ b/v2/pkg/protocols/http/request.go @@ -388,6 +388,9 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, previ finalEvent := make(output.InternalEvent) outputEvent := r.responseToDSLMap(resp, reqURL, matchedURL, tostring.UnsafeToString(dumpedRequest), tostring.UnsafeToString(dumpedResponse), tostring.UnsafeToString(data), headersToString(resp.Header), duration, request.meta) + if i := strings.LastIndex(hostname, ":"); i != -1 { + hostname = hostname[:i] + } outputEvent["ip"] = httpclientpool.Dialer.GetDialedIP(hostname) outputEvent["redirect-chain"] = tostring.UnsafeToString(redirectedResponse) for k, v := range previous { diff --git a/v2/pkg/protocols/network/networkclientpool/clientpool.go b/v2/pkg/protocols/network/networkclientpool/clientpool.go index 4113d3a37..127282d24 100644 --- a/v2/pkg/protocols/network/networkclientpool/clientpool.go +++ b/v2/pkg/protocols/network/networkclientpool/clientpool.go @@ -1,8 +1,8 @@ package networkclientpool import ( - "github.com/pkg/errors" "github.com/projectdiscovery/fastdialer/fastdialer" + "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate" "github.com/projectdiscovery/nuclei/v2/pkg/types" ) @@ -16,18 +16,7 @@ func Init(options *types.Options) error { if normalClient != nil { return nil } - opts := fastdialer.DefaultOptions - if options.SystemResolvers { - opts.EnableFallback = true - } - if options.ResolversFile != "" { - opts.BaseResolvers = options.InternalResolversList - } - dialer, err := fastdialer.NewDialer(opts) - if err != nil { - return errors.Wrap(err, "could not create dialer") - } - normalClient = dialer + normalClient = protocolstate.Dialer return nil }