Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
process_images.h File Reference
#include <Windows.h>
#include <string>

Go to the source code of this file.

Functions

void RecordProcessStart (DWORD processId, const std::string &image, const FILETIME &time)
 Records a process start (ProcessCreated).
void RecordProcessExit (DWORD processId)
 Records a process exit (ProcessTerminated). Its entry is forgotten a minute later.
std::string LookupProcessImage (DWORD processId, const FILETIME &time)
 The image of the process that had processId at the given time, from the recorded starts. Empty if the service did not see that process start (e.g. it started before the service).
std::string ResolveProcessImage (DWORD processId, const FILETIME &time)
 LookupProcessImage, falling back to asking Windows (QueryFullProcessImageName) for processes the service did not see start. Empty if both fail.

Function Documentation

◆ LookupProcessImage()

std::string LookupProcessImage ( DWORD processId,
const FILETIME & time )

The image of the process that had processId at the given time, from the recorded starts. Empty if the service did not see that process start (e.g. it started before the service).

Definition at line 113 of file process_images.cpp.

114{
115 ULONGLONG at = ToNumber(time);
116 std::lock_guard<std::mutex> guard(g_Lock);
117 auto found = g_Images.find(processId);
118 if (found == g_Images.end()) {
119 return "";
120 }
121 // Newest first: the latest process with this ID that had started by then
122 const auto& entries = found->second;
123 for (auto entry = entries.rbegin(); entry != entries.rend(); ++entry) {
124 if (entry->Start <= at + PANO_IMAGE_TIME_SLACK) {
125 return entry->Image;
126 }
127 }
128 return "";
129}
#define PANO_IMAGE_TIME_SLACK

References ProcessImageEntry::Image, PANO_IMAGE_TIME_SLACK, and ProcessImageEntry::Start.

Referenced by ResolveProcessImage(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ RecordProcessExit()

void RecordProcessExit ( DWORD processId)

Records a process exit (ProcessTerminated). Its entry is forgotten a minute later.

Definition at line 95 of file process_images.cpp.

96{
97 ULONGLONG now = GetTickCount64();
98 std::lock_guard<std::mutex> guard(g_Lock);
99 Prune(now);
100
101 auto found = g_Images.find(processId);
102 if (found == g_Images.end()) {
103 return; // started before the service
104 }
105 // The newest entry is the process that exited; an older one has already exited
106 auto& newest = found->second.back();
107 if (newest.ExitTick == 0) {
108 newest.ExitTick = now;
109 g_Exits.emplace_back(now, processId);
110 }
111}

Referenced by TEST(), and TEST().

◆ RecordProcessStart()

void RecordProcessStart ( DWORD processId,
const std::string & image,
const FILETIME & time )

Records a process start (ProcessCreated).

Parameters
imageThe image path as the driver reports it. "\??\C:\..." and "\SystemRoot\..." are converted to "C:\..." paths
timeWhen the process was created

Definition at line 80 of file process_images.cpp.

81{
82 std::lock_guard<std::mutex> guard(g_Lock);
83 Prune(GetTickCount64());
84
85 auto& entries = g_Images[processId];
87 entry.Image = ToWin32Path(image);
88 entry.Start = ToNumber(time);
89 entries.push_back(std::move(entry));
90 if (entries.size() > PANO_IMAGES_PER_PID) {
91 entries.erase(entries.begin());
92 }
93}
#define PANO_IMAGES_PER_PID

References ProcessImageEntry::Image, PANO_IMAGES_PER_PID, and ProcessImageEntry::Start.

Referenced by TEST(), TEST(), TEST(), and TEST().

◆ ResolveProcessImage()

std::string ResolveProcessImage ( DWORD processId,
const FILETIME & time )

LookupProcessImage, falling back to asking Windows (QueryFullProcessImageName) for processes the service did not see start. Empty if both fail.

Definition at line 173 of file process_images.cpp.

174{
175 std::string image = LookupProcessImage(processId, time);
176 if (!image.empty()) {
177 return image;
178 }
179
180 HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
181 if (process != NULL) {
182 WCHAR path[MAX_PATH * 2];
183 DWORD length = ARRAYSIZE(path);
184 if (QueryFullProcessImageNameW(process, 0, path, &length)) {
185 image = ToString(std::wstring(path, length));
186 }
187 CloseHandle(process);
188 }
189 if (image.empty()) {
190 image = KernelProcessImage(processId);
191 }
192 return image;
193}
#define MAX_PATH
Definition callbacks.h:7
std::string LookupProcessImage(DWORD processId, const FILETIME &time)
The image of the process that had processId at the given time, from the recorded starts....
std::string ToString(const std::wstring &wstr)
Definition utils.cpp:51

References LookupProcessImage(), MAX_PATH, and ToString().

Referenced by TEST(), and TEST().