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

[process/windows] - Ignore accessing command line arguments for selected processes (currently, lsass.exe) #198

Merged
merged 12 commits into from
Dec 27, 2024
4 changes: 4 additions & 0 deletions metric/system/process/helpers_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ func isNonFatal(err error) bool {
errors.Is(err, syscall.EINVAL) ||
errors.Is(err, NonFatalErr{}))
}

func processesToIgnore() map[uint64]struct{} {
return map[uint64]struct{}{}
}
24 changes: 24 additions & 0 deletions metric/system/process/helpers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"syscall"

"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"

"github.com/elastic/elastic-agent-libs/logp"
)

func isNonFatal(err error) bool {
Expand All @@ -35,3 +38,24 @@ func isNonFatal(err error) bool {
errors.Is(err, syscall.EINVAL) ||
errors.Is(err, windows.ERROR_INVALID_PARAMETER) || errors.Is(err, NonFatalErr{})
}

func processesToIgnore() map[uint64]struct{} {
m := make(map[uint64]struct{})
// processesToIgnore checks if we should ignore the pid, to avoid elevated permissions

// LSASS.exe is a process which has no useful cmdline arguments, we should ignore acessing such process to avoid triggering Windows ASR rules
// we can query pid for LASASS.exe from registry
key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Lsa", registry.READ)
if err != nil {
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
logp.L().Warnw("Failed to read registry path SYSTEM\\CurrentControlSet\\Control\\Lsa", "error", err)
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
return m
}
defer key.Close()
lsassPid, _, err := key.GetIntegerValue("LasPid")
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logp.L().Warnw("Failed to read pid for lsass.exe", "error", err)
return m
}
m[lsassPid] = struct{}{}
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
return m
}
18 changes: 10 additions & 8 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,17 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {

} // end cgroups processor

status, err = FillMetricsRequiringMoreAccess(pid, status)
if err != nil {
procStats.logger.Debugf("error calling FillMetricsRequiringMoreAccess for pid %d: %w", pid, err)
}
if _, isExcluded := procStats.excludedPIDs[uint64(pid)]; !isExcluded {
status, err = FillMetricsRequiringMoreAccess(pid, status)
if err != nil {
procStats.logger.Debugf("error calling FillMetricsRequiringMoreAccess for pid %d: %w", pid, err)
}

// Generate `status.Cmdline` here for compatibility because on Windows
// `status.Args` is set by `FillMetricsRequiringMoreAccess`.
if len(status.Args) > 0 && status.Cmdline == "" {
status.Cmdline = strings.Join(status.Args, " ")
// Generate `status.Cmdline` here for compatibility because on Windows
// `status.Args` is set by `FillMetricsRequiringMoreAccess`.
if len(status.Args) > 0 && status.Cmdline == "" {
status.Cmdline = strings.Join(status.Args, " ")
}
VihasMakwana marked this conversation as resolved.
Show resolved Hide resolved
}

// network data
Expand Down
2 changes: 2 additions & 0 deletions metric/system/process/process_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Stats struct {
cgroups *cgroup.Reader
logger *logp.Logger
host types.Host
excludedPIDs map[uint64]struct{} // List of PIDs to ignore while calling FillMetricsRequiringMoreAccess
}

// PidState are the constants for various PID states
Expand Down Expand Up @@ -207,6 +208,7 @@ func (procStats *Stats) Init() error {
}
procStats.cgroups = cgReader
}
procStats.excludedPIDs = processesToIgnore()
return nil
}

Expand Down
Loading