-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --processes flag to scan active process commands (#469)
* Add --processes flag to scan active process commands Signed-off-by: egibs <[email protected]> * Fix Linux ps command Signed-off-by: egibs <[email protected]> * Avoid generating a report for malcontent when running a scan Signed-off-by: egibs <[email protected]> * Use gopsutil instead of parsing ps Signed-off-by: egibs <[email protected]> * Appease the linter Signed-off-by: egibs <[email protected]> * Re-add unique path functionality Signed-off-by: egibs <[email protected]> --------- Signed-off-by: egibs <[email protected]>
- Loading branch information
Showing
8 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/shirou/gopsutil/v4/process" | ||
) | ||
|
||
type Process struct { | ||
PID int32 | ||
Path string | ||
} | ||
|
||
// GetAllProcessPaths is an exported function that returns a slice of Process PIDs and commands (path). | ||
func GetAllProcessPaths(ctx context.Context) ([]Process, error) { | ||
// Retrieve all of the active PIDs | ||
procs, err := process.ProcessesWithContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Store PIDs and their respective commands (paths) in a map of paths and their Process structs | ||
processMap := make(map[string]Process) | ||
for _, p := range procs { | ||
path, err := p.Exe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, exists := processMap[path]; !exists && path != "" && isValidPath(path) { | ||
processMap[path] = Process{ | ||
PID: p.Pid, | ||
Path: path, | ||
} | ||
} | ||
} | ||
|
||
return procMapSlice(processMap), nil | ||
} | ||
|
||
// procMapSlice converts a map of paths and their Process structs to a slice of Processes. | ||
func procMapSlice(m map[string]Process) []Process { | ||
result := make([]Process, 0, len(m)) | ||
for _, v := range m { | ||
result = append(result, v) | ||
} | ||
return result | ||
} | ||
|
||
// isValidPath checks if the given path is valid. | ||
func isValidPath(path string) bool { | ||
_, err := os.Stat(path) | ||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters