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

Fix loading script from file and evaluate as devtools for chromedp #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 18 additions & 15 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,6 @@ flags.`)),
logger := slog.New(log.Logger)

// Configure the driver
switch opts.Scan.Driver {
case "gorod":
scanDriver, err = driver.NewGorod(logger, *opts)
if err != nil {
return err
}
case "chromedp":
scanDriver, err = driver.NewChromedp(logger, *opts)
if err != nil {
return err
}
default:
return errors.New("invalid scan driver chosen")
}

log.Debug("scanning driver started", "driver", opts.Scan.Driver)

Expand Down Expand Up @@ -118,11 +104,28 @@ flags.`)),
}

// Get the runner up. Basically, all of the subcommands will use this.
scanRunner, err = runner.NewRunner(logger, scanDriver, *opts, scanWriters)
scanRunner, err = runner.NewRunner(logger, opts, scanWriters)
if err != nil {
return err
}

switch opts.Scan.Driver {
case "gorod":
scanDriver, err = driver.NewGorod(logger, *opts)
if err != nil {
return err
}
case "chromedp":
scanDriver, err = driver.NewChromedp(logger, *opts)
if err != nil {
return err
}
default:
return errors.New("invalid scan driver chosen")
}

scanRunner.Driver = scanDriver

return nil
// TODO: maybe add https://github.com/projectdiscovery/networkpolicy support?
},
Expand Down
5 changes: 4 additions & 1 deletion pkg/runner/drivers/chromedp.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,12 @@ func (run *Chromedp) Witness(target string, thisRunner *runner.Runner) (*models.

// run any javascript we have
if run.options.Scan.JavaScript != "" {
if err := chromedp.Run(navigationCtx, chromedp.Evaluate(run.options.Scan.JavaScript, nil)); err != nil {
// maybe can be added to outputs?
var jsResult []byte
if err := chromedp.Run(navigationCtx, chromedp.EvaluateAsDevTools(run.options.Scan.JavaScript, &jsResult)); err != nil {
return nil, fmt.Errorf("failed to evaluate user-provided javascript: %w", err)
}
logger.Debug("ran user-provided javascript", "result", string(jsResult))
}

// get cookies
Expand Down
5 changes: 2 additions & 3 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Runner struct {
Wappalyzer *wappalyzer.Wappalyze

// options for the Runner to consider
options Options
options *Options
// writers are the result writers to use
writers []writers.Writer
// log handler
Expand All @@ -37,7 +37,7 @@ type Runner struct {

// New gets a new Runner ready for probing.
// It's up to the caller to call Close() on the runner
func NewRunner(logger *slog.Logger, driver Driver, opts Options, writers []writers.Writer) (*Runner, error) {
func NewRunner(logger *slog.Logger, opts *Options, writers []writers.Writer) (*Runner, error) {
if !opts.Scan.ScreenshotSkipSave {
screenshotPath, err := islazy.CreateDir(opts.Scan.ScreenshotPath)
if err != nil {
Expand Down Expand Up @@ -74,7 +74,6 @@ func NewRunner(logger *slog.Logger, driver Driver, opts Options, writers []write
ctx, cancel := context.WithCancel(context.Background())

return &Runner{
Driver: driver,
Wappalyzer: wap,
options: opts,
writers: writers,
Expand Down
12 changes: 7 additions & 5 deletions web/api/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@ func (h *ApiHandler) SubmitHandler(w http.ResponseWriter, r *http.Request) {

logger := slog.New(log.Logger)

driver, err := driver.NewChromedp(logger, *options)
runner, err := runner.NewRunner(logger, options, []writers.Writer{writer})
if err != nil {
http.Error(w, "Error sarting driver", http.StatusInternalServerError)
log.Error("error starting runner", "err", err)
http.Error(w, "Error starting runner", http.StatusInternalServerError)
return
}

runner, err := runner.NewRunner(logger, driver, *options, []writers.Writer{writer})
driver, err := driver.NewChromedp(logger, *options)
if err != nil {
log.Error("error starting runner", "err", err)
http.Error(w, "Error starting runner", http.StatusInternalServerError)
http.Error(w, "Error sarting driver", http.StatusInternalServerError)
return
}

runner.Driver = driver

// have everything we need! start ther runner goroutine
go dispatchRunner(runner, request.URLs)

Expand Down
12 changes: 7 additions & 5 deletions web/api/submit_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ func (h *ApiHandler) SubmitSingleHandler(w http.ResponseWriter, r *http.Request)

logger := slog.New(log.Logger)

driver, err := driver.NewChromedp(logger, *options)
runner, err := runner.NewRunner(logger, options, []writers.Writer{writer})
if err != nil {
http.Error(w, "Error sarting driver", http.StatusInternalServerError)
log.Error("error starting runner", "err", err)
http.Error(w, "Error starting runner", http.StatusInternalServerError)
return
}

runner, err := runner.NewRunner(logger, driver, *options, []writers.Writer{writer})
driver, err := driver.NewChromedp(logger, *options)
if err != nil {
log.Error("error starting runner", "err", err)
http.Error(w, "Error starting runner", http.StatusInternalServerError)
http.Error(w, "Error sarting driver", http.StatusInternalServerError)
return
}

runner.Driver = driver

go func() {
runner.Targets <- request.URL
close(runner.Targets)
Expand Down