Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifyCmdFunc to Scanner and WithModifyCmdFunc option #103

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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