From 3b1ad87099bba53e85daa30f4e46171ebc30964f Mon Sep 17 00:00:00 2001 From: Tony Metzidis Date: Sun, 5 Jan 2025 08:15:23 -0800 Subject: [PATCH] check for root privileges warning (#137) --- errors.go | 3 +++ nmap.go | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/errors.go b/errors.go index 70fc439..197aac0 100644 --- a/errors.go +++ b/errors.go @@ -19,6 +19,9 @@ var ( // ErrParseOutput means that nmap's output was not parsed successfully. ErrParseOutput = errors.New("unable to parse nmap output, see warnings for details") + // ErrRequiresRoot means this feature requires root privileges (e.g. OS detection) + ErrRequiresRoot = errors.New("this feature requires root privileges") + // ErrResolveName means that Nmap could not resolve a name. ErrResolveName = errors.New("nmap could not resolve a name") ) diff --git a/nmap.go b/nmap.go index d95604c..51cc5e5 100644 --- a/nmap.go +++ b/nmap.go @@ -250,16 +250,18 @@ func choosePorts(result *Run, filter func(Port) bool) { func (s *Scanner) processNmapResult(result *Run, warnings *[]string, stdout, stderr *bytes.Buffer, done chan error, doneProgress chan bool) error { // Wait for nmap to finish. - var err = <-done + var ( + errStatus = <-done + err error + ) close(doneProgress) - if err != nil { - return err - } - // Check stderr output. if err := checkStdErr(stderr, warnings); err != nil { return err } + if errStatus != nil { + return errStatus + } // Parse nmap xml output. Usually nmap always returns valid XML, even if there is a scan error. // Potentially available warnings are returned too, but probably not the reason for a broken XML. @@ -310,6 +312,10 @@ func checkStdErr(stderr *bytes.Buffer, warnings *[]string) error { switch { case strings.Contains(warning, "Malloc Failed!"): return ErrMallocFailed + case strings.Contains(warning, "requires root privileges."): + return ErrRequiresRoot + // TODO: Add cases for other known errors we might want to guard. + default: } } return nil