178 lines
4.6 KiB
Go
Raw Normal View History

package engine
import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/pkg/errors"
2021-09-07 17:31:46 +03:00
ps "github.com/shirou/gopsutil/v3/process"
2022-04-28 01:50:22 +02:00
"github.com/projectdiscovery/fileutil"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
"github.com/projectdiscovery/stringsutil"
)
// Browser is a browser structure for nuclei headless module
type Browser struct {
customAgent string
tempDir string
previousPIDs map[int32]struct{} // track already running PIDs
engine *rod.Browser
httpclient *http.Client
options *types.Options
}
// New creates a new nuclei headless browser module
func New(options *types.Options) (*Browser, error) {
dataStore, err := os.MkdirTemp("", "nuclei-*")
if err != nil {
return nil, errors.Wrap(err, "could not create temporary directory")
}
previousPIDs := findChromeProcesses()
2021-02-26 13:13:11 +05:30
chromeLauncher := launcher.New().
Leakless(false).
Set("disable-gpu", "true").
Set("ignore-certificate-errors", "true").
Set("ignore-certificate-errors", "1").
Set("disable-crash-reporter", "true").
Set("disable-notifications", "true").
Set("hide-scrollbars", "true").
Set("window-size", fmt.Sprintf("%d,%d", 1080, 1920)).
Set("mute-audio", "true").
Set("incognito", "true").
Delete("use-mock-keychain").
UserDataDir(dataStore)
if MustDisableSandbox() {
chromeLauncher = chromeLauncher.NoSandbox(true)
}
2022-04-28 01:50:22 +02:00
executablePath, err := os.Executable()
if err != nil {
return nil, err
}
// if musl is used, most likely we are on alpine linux which is not supported by go-rod, so we fallback to default chrome
useMusl, _ := fileutil.UseMusl(executablePath)
if options.UseInstalledChrome || useMusl {
if chromePath, hasChrome := launcher.LookPath(); hasChrome {
chromeLauncher.Bin(chromePath)
} else {
return nil, errors.New("the chrome browser is not installed")
}
}
if options.ShowBrowser {
2021-02-26 13:13:11 +05:30
chromeLauncher = chromeLauncher.Headless(false)
} else {
2021-02-26 13:13:11 +05:30
chromeLauncher = chromeLauncher.Headless(true)
}
if types.ProxyURL != "" {
chromeLauncher = chromeLauncher.Proxy(types.ProxyURL)
}
2021-02-26 13:13:11 +05:30
launcherURL, err := chromeLauncher.Launch()
if err != nil {
return nil, err
}
browser := rod.New().ControlURL(launcherURL)
2021-02-26 13:13:11 +05:30
if browserErr := browser.Connect(); browserErr != nil {
return nil, browserErr
}
customAgent := ""
for _, option := range options.CustomHeaders {
parts := strings.SplitN(option, ":", 2)
if len(parts) != 2 {
continue
}
if strings.EqualFold(parts[0], "User-Agent") {
customAgent = parts[1]
}
}
httpclient, err := newHttpClient(options)
if err != nil {
return nil, err
}
engine := &Browser{
tempDir: dataStore,
customAgent: customAgent,
engine: browser,
httpclient: httpclient,
options: options,
}
engine.previousPIDs = previousPIDs
return engine, nil
}
// MustDisableSandbox determines if the current os and user needs sandbox mode disabled
func MustDisableSandbox() bool {
// linux with root user needs "--no-sandbox" option
// https://github.com/chromium/chromium/blob/c4d3c31083a2e1481253ff2d24298a1dfe19c754/chrome/test/chromedriver/client/chromedriver.py#L209
return runtime.GOOS == "linux" && os.Geteuid() == 0
}
// SetUserAgent sets custom user agent to the browser
func (b *Browser) SetUserAgent(customUserAgent string) {
b.customAgent = customUserAgent
}
// UserAgent fetch the currently set custom user agent
func (b *Browser) UserAgent() string {
return b.customAgent
}
// Close closes the browser engine
func (b *Browser) Close() {
b.engine.Close()
os.RemoveAll(b.tempDir)
b.killChromeProcesses()
}
// killChromeProcesses any and all new chrome processes started after
// headless process launch.
func (b *Browser) killChromeProcesses() {
processes, _ := ps.Processes()
2021-06-05 18:01:08 +05:30
for _, process := range processes {
2021-09-07 17:31:46 +03:00
// skip non-chrome processes
if !isChromeProcess(process) {
continue
}
// skip chrome processes that were already running
if _, ok := b.previousPIDs[process.Pid]; ok {
continue
}
2021-07-05 17:29:45 +05:30
_ = process.Kill()
}
}
// findChromeProcesses finds chrome process running on host
func findChromeProcesses() map[int32]struct{} {
processes, _ := ps.Processes()
list := make(map[int32]struct{})
for _, process := range processes {
if isChromeProcess(process) {
list[process.Pid] = struct{}{}
if ppid, err := process.Ppid(); err == nil {
list[ppid] = struct{}{}
}
}
}
return list
}
// isChromeProcess checks if a process is chrome/chromium
func isChromeProcess(process *ps.Process) bool {
name, _ := process.Name()
executable, _ := process.Exe()
return stringsutil.ContainsAny(name, "chrome", "chromium") || stringsutil.ContainsAny(executable, "chrome", "chromium")
}