Skip to content

Commit 31b2b72

Browse files
committed
fix: map top-N port spec to naabu top-ports, not the -p list
The control plane sends ports as "top-100"; passing it straight to naabu's -p made it read "top" as a port and fail. Route "top-<N>"/"full" to TopPorts and only literal lists/ranges to Ports.
1 parent 6546c53 commit 31b2b72

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎pkg/runtools/naabu.go‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"os"
1010
"strconv"
11+
"strings"
1112
"sync"
1213

1314
"github.com/projectdiscovery/goflags"
@@ -19,7 +20,8 @@ import (
1920
type NaabuOptions struct {
2021
// OutputFile receives one "host:port" line per open port. Required.
2122
OutputFile string
22-
// Ports is a comma-separated list (e.g. "80,443,8443"); empty uses naabu's top-1000.
23+
// Ports is the control-plane port spec: "top-<N>" (e.g. "top-100"), "full",
24+
// or a literal list/range ("80,443", "1-1000"). Empty uses naabu's default.
2325
Ports string
2426
SkipHostDiscovery bool
2527
// ServiceVersion enables nmap-service-probes scanning (no external nmap binary).
@@ -47,7 +49,6 @@ func RunNaabu(ctx context.Context, hosts []string, opts NaabuOptions) (string, e
4749
var mu sync.Mutex
4850
naabuOpts := &runner.Options{
4951
Host: goflags.StringSlice(hosts),
50-
Ports: opts.Ports,
5152
SkipHostDiscovery: opts.SkipHostDiscovery,
5253
ServiceVersion: opts.ServiceVersion,
5354
ServiceDiscovery: opts.ServiceDiscovery,
@@ -69,6 +70,7 @@ func RunNaabu(ctx context.Context, hosts []string, opts NaabuOptions) (string, e
6970
}
7071
},
7172
}
73+
setNaabuPorts(naabuOpts, opts.Ports)
7274

7375
r, err := runner.NewRunner(naabuOpts)
7476
if err != nil {
@@ -82,3 +84,21 @@ func RunNaabu(ctx context.Context, hosts []string, opts NaabuOptions) (string, e
8284
}
8385
return opts.OutputFile, nil
8486
}
87+
88+
// setNaabuPorts routes the control-plane port spec to the right naabu field.
89+
// "top-<N>" and "full" select naabu's top-ports list (-top-ports); a literal
90+
// list/range goes to -p. Passing "top-100" to Ports makes naabu read it as a
91+
// range and fail with "invalid port number: 'top'".
92+
func setNaabuPorts(o *runner.Options, spec string) {
93+
spec = strings.TrimSpace(spec)
94+
switch {
95+
case spec == "":
96+
// leave naabu's default
97+
case strings.EqualFold(spec, "full"):
98+
o.TopPorts = "full"
99+
case strings.HasPrefix(spec, "top-"):
100+
o.TopPorts = strings.TrimPrefix(spec, "top-")
101+
default:
102+
o.Ports = spec
103+
}
104+
}

‎pkg/runtools/naabu_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package runtools
33
import (
44
"context"
55
"testing"
6+
7+
"github.com/projectdiscovery/naabu/v2/pkg/runner"
68
)
79

810
func TestRunNaabu_RequiresOutputFile(t *testing.T) {
@@ -11,3 +13,33 @@ func TestRunNaabu_RequiresOutputFile(t *testing.T) {
1113
t.Fatal("expected error when OutputFile is empty")
1214
}
1315
}
16+
17+
// Guards against the "invalid port number: 'top'" failure: the control plane
18+
// sends "top-100", which must map to naabu's TopPorts, not the -p list.
19+
func TestSetNaabuPorts(t *testing.T) {
20+
tests := []struct {
21+
spec string
22+
wantTopPorts string
23+
wantPorts string
24+
}{
25+
{"top-100", "100", ""},
26+
{"top-1000", "1000", ""},
27+
{"full", "full", ""},
28+
{"80,443", "", "80,443"},
29+
{"1-1000", "", "1-1000"},
30+
{"", "", ""},
31+
{" top-100 ", "100", ""},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.spec, func(t *testing.T) {
35+
o := &runner.Options{}
36+
setNaabuPorts(o, tt.spec)
37+
if o.TopPorts != tt.wantTopPorts {
38+
t.Errorf("TopPorts = %q, want %q", o.TopPorts, tt.wantTopPorts)
39+
}
40+
if o.Ports != tt.wantPorts {
41+
t.Errorf("Ports = %q, want %q", o.Ports, tt.wantPorts)
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)