Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1234: Honor the -iv flag when scanning #1235

Merged
merged 10 commits into from
Oct 14, 2024
58 changes: 58 additions & 0 deletions v2/pkg/runner/ips_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"net"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -40,3 +41,60 @@ func TestIsIpOrCidr(t *testing.T) {
require.False(t, isIpOrCidr(invalidItem))
}
}

// Helper function for the following 3 tests
func testIps(testIps []string) func() ([]*net.IPNet, []string) {
ips := []*net.IPNet{}

for _, ip := range testIps {
_, net, _ := net.ParseCIDR(ip)
ips = append(ips, net)
}

return func() ([]*net.IPNet, []string) {
return ips, []string{}
}
}

func TestIpV4Only(t *testing.T) {
ips := []string{"1.1.1.1/32", "2.2.2.2/32", "1.1.1.0/24", "fe80::623e:5fff:fe76:7d82/64", "100.121.237.116/32", "fd7a:115c:a1e0::fb01:ed74/48"}

r, err := NewRunner(&Options{
IPVersion: []string{"4"},
})
require.Nil(t, err)

targets, targetsV4, targetsV6, _, err := r.GetTargetIps(testIps(ips))
require.Nil(t, err)
require.Equal(t, targets, targetsV4)
require.Empty(t, targetsV6)
}

func TestIpV6Only(t *testing.T) {
ips := []string{"1.1.1.1/32", "2.2.2.2/32", "1.1.1.0/24", "fe80::623e:5fff:fe76:7d82/64", "100.121.237.116/32", "fd7a:115c:a1e0::fb01:ed74/48"}

r, err := NewRunner(&Options{
IPVersion: []string{"6"},
})
require.Nil(t, err)

targets, targetsV4, targetsV6, _, err := r.GetTargetIps(testIps(ips))
require.Nil(t, err)
require.Equal(t, targets, targetsV6)
require.Empty(t, targetsV4)
}

func TestIpV4AndV6(t *testing.T) {
ips := []string{"1.1.1.1/32", "2.2.2.2/32", "1.1.1.0/24", "fe80::623e:5fff:fe76:7d82/64", "100.121.237.116/32", "fd7a:115c:a1e0::fb01:ed74/48"}

r, err := NewRunner(&Options{
IPVersion: []string{"4", "6"},
})
require.Nil(t, err)

targets, targetsV4, targetsV6, _, err := r.GetTargetIps(testIps(ips))
expected := append(targetsV4, targetsV6...)

require.Nil(t, err)
require.EqualValues(t, expected, targets)
}
2 changes: 1 addition & 1 deletion v2/pkg/runner/nmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *Runner) handleNmap() error {
err := cmd.Run()
if err != nil {
errMsg := errors.Wrap(err, "Could not run nmap command")
gologger.Error().Msgf("%v", errMsg.Error())
gologger.Error().Msg(errMsg.Error())
return errMsg
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions v2/pkg/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/projectdiscovery/naabu/v2/pkg/result"
"github.com/projectdiscovery/naabu/v2/pkg/scan"
fileutil "github.com/projectdiscovery/utils/file"
sliceutil "github.com/projectdiscovery/utils/slice"

"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
Expand Down Expand Up @@ -283,3 +284,11 @@ func (options *Options) hasProbes() bool {
func (options *Options) shouldUseRawPackets() bool {
return isOSSupported() && privileges.IsPrivileged && options.ScanType == SynScan && scan.PkgRouter != nil
}

func (options *Options) ShouldScanIPv4() bool {
return sliceutil.Contains(options.IPVersion, "4")
}

func (options *Options) ShouldScanIPv6() bool {
return sliceutil.Contains(options.IPVersion, "6")
}
4 changes: 2 additions & 2 deletions v2/pkg/runner/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func writeCSVHeaders(data *Result, writer *csv.Writer) {

if err := writer.Write(headers); err != nil {
errMsg := errors.Wrap(err, "Could not write headers")
gologger.Error().Msgf("%v", errMsg.Error())
gologger.Error().Msg(errMsg.Error())
}
}

Expand All @@ -179,6 +179,6 @@ func writeCSVRow(data *Result, writer *csv.Writer) {
}
if err := writer.Write(rowData); err != nil {
errMsg := errors.Wrap(err, "Could not write row")
gologger.Error().Msgf("%v", errMsg.Error())
gologger.Error().Msg(errMsg.Error())
}
}
22 changes: 18 additions & 4 deletions v2/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,15 +648,29 @@ func (r *Runner) getPreprocessedIps() (cidrs []*net.IPNet, ipsWithPort []string)
return
}

func (r *Runner) GetTargetIps(ipsCallback func() ([]*net.IPNet, []string)) (targets, targetsV4, targetsv6 []*net.IPNet, targetsWithPort []string, err error) {
func (r *Runner) GetTargetIps(ipsCallback func() ([]*net.IPNet, []string)) (targets, targetsV4, targetsV6 []*net.IPNet, targetsWithPort []string, err error) {
targets, targetsWithPort = ipsCallback()

// shrinks the ips to the minimum amount of cidr
targetsV4, targetsv6 = mapcidr.CoalesceCIDRs(targets)
if len(targetsV4) == 0 && len(targetsv6) == 0 && len(targetsWithPort) == 0 {
targetsV4, targetsV6 = mapcidr.CoalesceCIDRs(targets)
if len(targetsV4) == 0 && len(targetsV6) == 0 && len(targetsWithPort) == 0 {
return nil, nil, nil, nil, errors.New("no valid ipv4 or ipv6 targets were found")
}
return targets, targetsV4, targetsv6, targetsWithPort, nil

targets = make([]*net.IPNet, 0, len(targets))
if r.options.ShouldScanIPv4() {
targets = append(targets, targetsV4...)
} else {
targetsV4 = make([]*net.IPNet, 0)
}

if r.options.ShouldScanIPv6() {
targets = append(targets, targetsV6...)
} else {
targetsV6 = make([]*net.IPNet, 0)
}

return targets, targetsV4, targetsV6, targetsWithPort, nil
}

func (r *Runner) ShowScanResultOnExit() {
Expand Down
Loading