From c1dfcfc5083fbb31129f4e3716ced49d22ab9aee Mon Sep 17 00:00:00 2001 From: process0 <29227206+process0@users.noreply.github.com> Date: Fri, 7 Oct 2022 12:08:20 -0400 Subject: [PATCH] Add support for WithCustomSysProcAttr option and Scanner.modifySysProcAttr to modify *cmd.Exec SysProcAttr --- nmap.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/nmap.go b/nmap.go index 3732218..a8b07bc 100644 --- a/nmap.go +++ b/nmap.go @@ -10,6 +10,7 @@ import ( "io" "os/exec" "strings" + "syscall" "time" "github.com/pkg/errors" @@ -29,7 +30,8 @@ type Streamer interface { // Scanner represents an Nmap scanner. type Scanner struct { - cmd *exec.Cmd + cmd *exec.Cmd + modifySysProcAttr func(*syscall.SysProcAttr) args []string binaryPath string @@ -94,6 +96,9 @@ func (s *Scanner) Run() (result *Run, warnings []string, err error) { // Prepare nmap process cmd := exec.Command(s.binaryPath, args...) + if s.modifySysProcAttr != nil { + s.modifySysProcAttr(cmd.SysProcAttr) + } cmd.Stdout = &stdout cmd.Stderr = &stderr @@ -181,6 +186,9 @@ func (s *Scanner) RunWithProgress(liveProgress chan<- float32) (result *Run, war // Prepare nmap process. cmd := exec.Command(s.binaryPath, args...) + if s.modifySysProcAttr != nil { + s.modifySysProcAttr(cmd.SysProcAttr) + } cmd.Stderr = &stderr cmd.Stdout = &stdout @@ -295,6 +303,9 @@ func (s *Scanner) RunWithStreamer(stream Streamer, file string) (warnings []stri // Prepare nmap process. cmd := exec.CommandContext(s.ctx, s.binaryPath, args...) + if s.modifySysProcAttr != nil { + s.modifySysProcAttr(cmd.SysProcAttr) + } // Write stderr to buffer. stderrBuf := bytes.Buffer{} @@ -354,6 +365,10 @@ func (s *Scanner) RunAsync() error { args = append(args, "-") s.cmd = exec.Command(s.binaryPath, args...) + if s.modifySysProcAttr != nil { + s.modifySysProcAttr(s.cmd.SysProcAttr) + } + stderr, err := s.cmd.StderrPipe() if err != nil { return fmt.Errorf("unable to get error output from asynchronous nmap run: %v", err) @@ -1595,6 +1610,13 @@ func WithGrepOutput(outputFileName string) Option { } } +// WithCustomSysProcAttr allows customizing the *syscall.SysProcAttr on the *exec.Cmd instance +func WithCustomSysProcAttr(f func(*syscall.SysProcAttr)) Option { + return func(s *Scanner) { + s.modifySysProcAttr = f + } +} + // ReturnArgs return the list of nmap args func (s *Scanner) Args() []string { return s.args