Skip to content

Commit

Permalink
fix(ps): Use start time from event timestamp
Browse files Browse the repository at this point in the history
If we fail to obtain the process start time
use the one from the event timestamp.
  • Loading branch information
rabbitstack committed Jan 15, 2025
1 parent ea6f95c commit 5caa7da
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/etw/processors/ps_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (p psProcessor) ProcessEvent(e *kevent.Kevent) (*kevent.Kevent, bool, error
p.regionProber.Remove(evt.Kparams.MustGetPid())
return evt, false, multierror.Wrap(err, p.psnap.Remove(evt))
}

return evt, false, multierror.Wrap(err, p.psnap.Write(evt))
case ktypes.CreateThread, ktypes.TerminateThread, ktypes.ThreadRundown:
pid, err := e.Kparams.GetPid()
Expand All @@ -65,6 +66,7 @@ func (p psProcessor) ProcessEvent(e *kevent.Kevent) (*kevent.Kevent, bool, error
if err != nil {
return e, false, err
}

return e, false, p.psnap.RemoveThread(pid, tid)
case ktypes.OpenProcess, ktypes.OpenThread:
pid, err := e.Kparams.GetPid()
Expand All @@ -76,8 +78,10 @@ func (p psProcessor) ProcessEvent(e *kevent.Kevent) (*kevent.Kevent, bool, error
e.AppendParam(kparams.Exe, kparams.Path, proc.Exe)
e.AppendParam(kparams.ProcessName, kparams.AnsiString, proc.Name)
}

return e, false, nil
}

return e, true, nil
}

Expand Down Expand Up @@ -105,7 +109,7 @@ func (p psProcessor) processEvent(e *kevent.Kevent) (*kevent.Kevent, error) {

// query process start time
pid := e.Kparams.MustGetPid()
started, err := getStartTime(pid)
started, err := getStartTime(pid, e)
if err != nil {
started = e.Timestamp
}
Expand All @@ -117,10 +121,10 @@ func (p psProcessor) processEvent(e *kevent.Kevent) (*kevent.Kevent, error) {
func (psProcessor) Name() ProcessorType { return Ps }
func (p psProcessor) Close() {}

func getStartTime(pid uint32) (time.Time, error) {
func getStartTime(pid uint32, e *kevent.Kevent) (time.Time, error) {
proc, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
return time.Now(), err
return e.Timestamp, err
}
//nolint:errcheck
defer windows.CloseHandle(proc)
Expand All @@ -132,7 +136,7 @@ func getStartTime(pid uint32) (time.Time, error) {
)
err = windows.GetProcessTimes(proc, &ct, &xt, &kt, &ut)
if err != nil {
return time.Now(), err
return e.Timestamp, err
}
return time.Unix(0, ct.Nanoseconds()), nil
}

0 comments on commit 5caa7da

Please sign in to comment.