Skip to content

Commit 73d6242

Browse files
authored
Merge pull request #115 from projectdiscovery/bugfix-privileges-rework
Bugfix privileges rework
2 parents d7ce0c0 + d0bb285 commit 73d6242

File tree

8 files changed

+23
-123
lines changed

8 files changed

+23
-123
lines changed

v2/pkg/runas/runas_all.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

v2/pkg/runas/runas_linux.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

v2/pkg/runner/banners.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/projectdiscovery/gologger"
9-
"github.com/projectdiscovery/naabu/v2/pkg/runas"
109
"github.com/projectdiscovery/naabu/v2/pkg/scan"
1110
)
1211

@@ -33,7 +32,7 @@ func showBanner() {
3332
func showNetworkCapabilities(options *Options) {
3433
accessLevel := "non root"
3534
scanType := "CONNECT"
36-
if isRoot() && !options.Unprivileged {
35+
if isRoot() && options.ScanType == SynScan {
3736
accessLevel = "root"
3837
scanType = "TCP/ICMP/SYN"
3938
}
@@ -68,18 +67,6 @@ func showNetworkInterfaces() error {
6867
return nil
6968
}
7069

71-
func handlePrivileges(options *Options) error {
72-
if options.Privileged {
73-
return runas.Root()
74-
}
75-
76-
if options.Unprivileged {
77-
return runas.Nobody()
78-
}
79-
80-
return nil
81-
}
82-
8370
func (options *Options) writeDefaultConfig() {
8471
dummyconfig := `
8572
# Number of retries

v2/pkg/runner/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ type ConfigFile struct {
1515
Verify bool `yaml:"verify,omitempty"`
1616
// Ping uses ping probes to discover fastest active host and discover dead hosts
1717
Ping bool `yaml:"ping,omitempty"`
18-
// Attempts to run as root
19-
Privileged bool `yaml:"privileged,omitempty"`
20-
// Drop root privileges
21-
Unprivileged bool `yaml:"unprivileged,omitempty"`
2218
// Excludes ip of knows CDN ranges
2319
ExcludeCDN bool `yaml:"exclude-cdn,omitempty"`
2420
// Retries is the number of retries for the port

v2/pkg/runner/default.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ const (
1111
DefaultRetriesConnectScan = 3
1212

1313
ExternalTargetForTune = "8.8.8.8"
14+
15+
SynScan = "s"
16+
ConnectScan = "c"
1417
)

v2/pkg/runner/options.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ type Options struct {
2020
Version bool // Version specifies if we should just show version and exit
2121
Ping bool // Ping uses ping probes to discover fastest active host and discover dead hosts
2222
Debug bool // Prints out debug information
23-
Privileged bool // Attempts to run as root
24-
Unprivileged bool // Drop root privileges
2523
ExcludeCDN bool // Excludes ip of knows CDN ranges for full port scan
2624
Nmap bool // Invoke nmap detailed scan on results
2725
InterfacesList bool // InterfacesList show interfaces list
@@ -46,6 +44,7 @@ type Options struct {
4644
Threads int // Internal worker threads
4745
EnableProgressBar bool // Enable progress bar
4846
ScanAllIPS bool // Scan all the ips
47+
ScanType string // Scan Type
4948
config *ConfigFile
5049
}
5150

@@ -75,8 +74,6 @@ func ParseOptions() *Options {
7574
flag.BoolVar(&options.Debug, "debug", false, "Enable debugging information")
7675
flag.StringVar(&options.SourceIP, "source-ip", "", "Source Ip")
7776
flag.StringVar(&options.Interface, "interface", "", "Network Interface to use for port scan")
78-
flag.BoolVar(&options.Privileged, "privileged", false, "Attempts to run as root - Use sudo if possible")
79-
flag.BoolVar(&options.Unprivileged, "unprivileged", false, "Drop root privileges")
8077
flag.BoolVar(&options.ExcludeCDN, "exclude-cdn", false, "Sikp full port scans for CDNs (only checks for 80,443)")
8178
flag.IntVar(&options.WarmUpTime, "warm-up-time", 2, "Time in seconds between scan phases")
8279
flag.BoolVar(&options.InterfacesList, "interface-list", false, "List available interfaces and public ip")
@@ -86,6 +83,7 @@ func ParseOptions() *Options {
8683
flag.IntVar(&options.Threads, "c", 25, "General internal worker threads")
8784
flag.BoolVar(&options.EnableProgressBar, "stats", false, "Display stats of the running scan")
8885
flag.BoolVar(&options.ScanAllIPS, "scan-all-ips", false, "Scan all the ips")
86+
flag.StringVar(&options.ScanType, "s", SynScan, "Scan Type (s - Syn default, c - Connect)")
8987

9088
flag.Parse()
9189

@@ -135,12 +133,6 @@ func ParseOptions() *Options {
135133

136134
showNetworkCapabilities(options)
137135

138-
// Handle privileges - most probably elevation will fail as the process would need to invoke fork()
139-
err = handlePrivileges(options)
140-
if err != nil {
141-
gologger.Warningf("Could not set privileges:%s\n", err)
142-
}
143-
144136
return options
145137
}
146138

@@ -181,8 +173,7 @@ func (options *Options) MergeFromConfig(configFileName string, ignoreError bool)
181173
if configFile.TopPorts != "" {
182174
options.TopPorts = configFile.TopPorts
183175
}
184-
options.Privileged = configFile.Privileged
185-
options.Unprivileged = configFile.Unprivileged
176+
186177
options.ExcludeCDN = configFile.ExcludeCDN
187178
if configFile.SourceIP != "" {
188179
options.SourceIP = configFile.SourceIP

v2/pkg/runner/runner.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewRunner(options *Options) (*Runner, error) {
7878
runner.dnsclient = dnsclient
7979

8080
// Tune source
81-
if isRoot() {
81+
if isRoot() && options.ScanType == SynScan {
8282
// Set values if those were specified via cli
8383
if err := runner.SetSourceIPAndInterface(); err != nil {
8484
// Otherwise try to obtain them automatically
@@ -104,10 +104,12 @@ func NewRunner(options *Options) (*Runner, error) {
104104
func (r *Runner) SetSourceIPAndInterface() error {
105105
if r.options.SourceIP != "" && r.options.Interface != "" {
106106
r.scanner.SourceIP = net.ParseIP(r.options.SourceIP)
107-
var err error
108-
r.scanner.NetworkInterface, err = net.InterfaceByName(r.options.Interface)
109-
if err != nil {
110-
return err
107+
if r.options.Interface != "" {
108+
var err error
109+
r.scanner.NetworkInterface, err = net.InterfaceByName(r.options.Interface)
110+
if err != nil {
111+
return err
112+
}
111113
}
112114
}
113115

@@ -125,7 +127,7 @@ func (r *Runner) RunEnumeration() error {
125127
r.wgscan = sizedwaitgroup.New(r.options.Rate)
126128
r.limiter = ratelimit.New(r.options.Rate)
127129

128-
if isRoot() && !r.options.Unprivileged {
130+
if isRoot() && r.options.ScanType == SynScan {
129131
err = r.scanner.SetupHandlers()
130132
if err != nil {
131133
return err
@@ -183,11 +185,11 @@ retry:
183185

184186
r.limiter.Take()
185187
// connect scan
186-
if !isRoot() || r.options.Unprivileged {
188+
if isRoot() && r.options.ScanType == SynScan {
189+
r.RawSocketEnumeration(ip, port)
190+
} else {
187191
r.wgscan.Add()
188192
go r.handleHostPort(ip, port)
189-
} else {
190-
r.RawSocketEnumeration(ip, port)
191193
}
192194
if r.options.EnableProgressBar {
193195
r.stats.IncrementCounter("packets", 1)

v2/pkg/scan/scan.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ func (s *Scanner) TuneSource(ip string) error {
560560

561561
// SetupHandlers to listen on all interfaces
562562
func (s *Scanner) SetupHandlers() error {
563+
if s.NetworkInterface != nil {
564+
return s.SetupHandler(s.NetworkInterface.Name)
565+
}
566+
563567
itfs, err := net.Interfaces()
564568
if err != nil {
565569
return err
@@ -568,10 +572,7 @@ func (s *Scanner) SetupHandlers() error {
568572
if itf.Flags&net.FlagUp == 0 {
569573
continue // interface down
570574
}
571-
err := s.SetupHandler(itf.Name)
572-
if err != nil {
573-
return err
574-
}
575+
s.SetupHandler(itf.Name)
575576
}
576577

577578
return nil

0 commit comments

Comments
 (0)