Skip to content

Commit

Permalink
Merge pull request #103 from process0/add-modifycmdfunc
Browse files Browse the repository at this point in the history
Add modifyCmdFunc to Scanner and WithModifyCmdFunc option
  • Loading branch information
elivlo committed Oct 11, 2022
2 parents f32c1a8 + c1dfcfc commit b2a0803
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion nmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"os/exec"
"strings"
"syscall"
"time"

"github.com/pkg/errors"
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b2a0803

Please sign in to comment.