From 2e62517ba6ac0bcd16559a5baf930a8a495bcddd Mon Sep 17 00:00:00 2001 From: mzack9999 Date: Wed, 8 May 2024 14:53:10 +0200 Subject: [PATCH] automatically skip redundant host discovery --- v2/pkg/runner/runner.go | 13 ++++++++----- v2/pkg/runner/validate.go | 8 +++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/v2/pkg/runner/runner.go b/v2/pkg/runner/runner.go index 28d4c335..270d5647 100644 --- a/v2/pkg/runner/runner.go +++ b/v2/pkg/runner/runner.go @@ -65,7 +65,13 @@ type Target struct { func NewRunner(options *Options) (*Runner, error) { options.configureOutput() - options.configureHostDiscovery() + // automatically disable host discovery when less than two ports for scan are provided + ports, err := ParsePorts(options) + if err != nil { + return nil, fmt.Errorf("could not parse ports: %s", err) + } + + options.configureHostDiscovery(ports) // default to ipv4 if no ipversion was specified if len(options.IPVersion) == 0 { @@ -131,10 +137,7 @@ func NewRunner(options *Options) (*Runner, error) { } runner.scanner = scanner - runner.scanner.Ports, err = ParsePorts(options) - if err != nil { - return nil, fmt.Errorf("could not parse ports: %s", err) - } + runner.scanner.Ports = ports if options.EnableProgressBar { defaultOptions := &clistats.DefaultOptions diff --git a/v2/pkg/runner/validate.go b/v2/pkg/runner/validate.go index 2aa3862b..5857469f 100644 --- a/v2/pkg/runner/validate.go +++ b/v2/pkg/runner/validate.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/pkg/errors" + "github.com/projectdiscovery/naabu/v2/pkg/port" "github.com/projectdiscovery/naabu/v2/pkg/privileges" "github.com/projectdiscovery/naabu/v2/pkg/scan" fileutil "github.com/projectdiscovery/utils/file" @@ -165,7 +166,12 @@ func (options *Options) configureOutput() { // ConfigureHostDiscovery enables default probes if none is specified // but host discovery option was requested -func (options *Options) configureHostDiscovery() { +func (options *Options) configureHostDiscovery(ports []*port.Port) { + // if less than two ports are specified as input, reduce time and scan directly + if len(ports) <= 2 { + gologger.Info().Msgf("Host discovery disabled: less than two ports were specified") + options.SkipHostDiscovery = true + } if options.shouldDiscoverHosts() && !options.hasProbes() { // if no options were defined enable // - ICMP Echo Request