From 64adad131ff8a1bda9087349e50134995c2e6258 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Sun, 23 Apr 2023 21:37:25 +0200 Subject: [PATCH 1/2] Removing redundant code with utils --- v2/go.mod | 2 +- v2/go.sum | 4 +- v2/pkg/protocols/headless/engine/engine.go | 47 ++-------------------- 3 files changed, 6 insertions(+), 47 deletions(-) diff --git a/v2/go.mod b/v2/go.mod index 88d839ac..d2ff797d 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -79,7 +79,7 @@ require ( github.com/projectdiscovery/sarif v0.0.1 github.com/projectdiscovery/tlsx v1.0.7 github.com/projectdiscovery/uncover v1.0.2 - github.com/projectdiscovery/utils v0.0.21-0.20230419140949-a6527b072e4a + github.com/projectdiscovery/utils v0.0.24 github.com/projectdiscovery/wappalyzergo v0.0.88 github.com/stretchr/testify v1.8.2 gopkg.in/src-d/go-git.v4 v4.13.1 diff --git a/v2/go.sum b/v2/go.sum index feb7aef8..52d8adf7 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -448,8 +448,8 @@ github.com/projectdiscovery/tlsx v1.0.7/go.mod h1:bMz1JMJf1sSBLpk7Y6vLwRZrijxPUv github.com/projectdiscovery/uncover v1.0.2 h1:mRFzflYyvwKkHd3XKufMlDRrb6p1mjFZTSHoNAUpFwo= github.com/projectdiscovery/uncover v1.0.2/go.mod h1:lz4QYfArSA6jJkXyB71kN2/Pc7IW7nJB8c95n7xtwqY= github.com/projectdiscovery/utils v0.0.3/go.mod h1:ne3eSlZlUKuhjHr8FfsfGcGteCzxcbJvFBx4VDBCxK0= -github.com/projectdiscovery/utils v0.0.21-0.20230419140949-a6527b072e4a h1:h9ceITnnFLJ0qucXCrI3WOXtNRK2oAtcCuFsMAiuIc0= -github.com/projectdiscovery/utils v0.0.21-0.20230419140949-a6527b072e4a/go.mod h1:954dxg9AWmNmcNQdc5BpucghibSvC76prWLQFrv14FQ= +github.com/projectdiscovery/utils v0.0.24 h1:3kI8B5/ajKANPM7dfVHyTPFegy0OrVOxU+a3DXvX2a8= +github.com/projectdiscovery/utils v0.0.24/go.mod h1:4ynwLqKugrMQzNjBJbzSDRBtadPwat/lwrXhWA6gdAE= github.com/projectdiscovery/wappalyzergo v0.0.88 h1:N/1vFlKmc3GJco9rANJdHrxg8jdav/xmnICo8rztmH8= github.com/projectdiscovery/wappalyzergo v0.0.88/go.mod h1:HvYuW0Be4JCjVds/+XAEaMSqRG9yrI97UmZq0TPk6A0= github.com/projectdiscovery/yamldoc-go v1.0.4 h1:eZoESapnMw6WAHiVgRwNqvbJEfNHEH148uthhFbG5jE= diff --git a/v2/pkg/protocols/headless/engine/engine.go b/v2/pkg/protocols/headless/engine/engine.go index a547f2a7..6dd02a0c 100644 --- a/v2/pkg/protocols/headless/engine/engine.go +++ b/v2/pkg/protocols/headless/engine/engine.go @@ -9,13 +9,12 @@ import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" "github.com/pkg/errors" - ps "github.com/shirou/gopsutil/v3/process" "github.com/projectdiscovery/nuclei/v2/pkg/types" fileutil "github.com/projectdiscovery/utils/file" osutils "github.com/projectdiscovery/utils/os" + processutil "github.com/projectdiscovery/utils/process" reflectutil "github.com/projectdiscovery/utils/reflect" - stringsutil "github.com/projectdiscovery/utils/strings" ) // Browser is a browser structure for nuclei headless module @@ -34,7 +33,7 @@ func New(options *types.Options) (*Browser, error) { if err != nil { return nil, errors.Wrap(err, "could not create temporary directory") } - previousPIDs := findChromeProcesses() + previousPIDs := processutil.FindProcesses(processutil.IsChromeProcess) chromeLauncher := launcher.New(). Leakless(false). @@ -140,45 +139,5 @@ func (b *Browser) UserAgent() string { 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() - - for _, process := range processes { - // skip non-chrome processes - if !isChromeProcess(process) { - continue - } - // skip chrome processes that were already running - if _, ok := b.previousPIDs[process.Pid]; ok { - continue - } - _ = 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") + processutil.CloseProcesses(processutil.IsChromeProcess, b.previousPIDs) } From bb9ed7bdf0a4b292c7f3fb306564200c2beb1c38 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Sun, 23 Apr 2023 21:45:47 +0200 Subject: [PATCH 2/2] updating go modules --- v2/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v2/go.mod b/v2/go.mod index d2ff797d..7651ca76 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -32,7 +32,7 @@ require ( github.com/remeh/sizedwaitgroup v1.0.0 github.com/rs/xid v1.5.0 github.com/segmentio/ksuid v1.0.4 - github.com/shirou/gopsutil/v3 v3.23.3 + github.com/shirou/gopsutil/v3 v3.23.3 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/cast v1.5.0 github.com/syndtr/goleveldb v1.0.0