Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_ProcessImages.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2#include "process_images.h"
3
4// FILETIME a number of seconds after an arbitrary base
5static FILETIME At(double seconds) {
6 ULONGLONG value = 134070000000000000ULL + (ULONGLONG)(seconds * 10000000.0);
7 FILETIME time;
8 time.dwLowDateTime = (DWORD)(value & 0xFFFFFFFF);
9 time.dwHighDateTime = (DWORD)(value >> 32);
10 return time;
11}
12
13// Each test uses its own PIDs: the cache is process-wide
14
15TEST(ProcessImages, ImageIsKnownAfterTheProcessExits) {
16 RecordProcessStart(900001, R"(\??\C:\Tests\Test_NtWriteVirtualMemoryx64.exe)", At(0));
17 RecordProcessExit(900001);
18 // A hook event from just before the exit, delivered after it
19 EXPECT_EQ(LookupProcessImage(900001, At(0.5)), R"(C:\Tests\Test_NtWriteVirtualMemoryx64.exe)");
20}
21
22TEST(ProcessImages, SystemRootPathsBecomeWin32Paths) {
23 RecordProcessStart(900002, R"(\SystemRoot\System32\Conhost.exe)", At(0));
24 char windows[MAX_PATH];
25 UINT length = GetWindowsDirectoryA(windows, MAX_PATH);
26 EXPECT_EQ(LookupProcessImage(900002, At(1)), std::string(windows, length) + R"(\System32\Conhost.exe)");
27}
28
29TEST(ProcessImages, ReusedPidResolvesByEventTime) {
30 RecordProcessStart(900003, R"(C:\old.exe)", At(0));
31 RecordProcessExit(900003);
32 RecordProcessStart(900003, R"(C:\new.exe)", At(10));
33 EXPECT_EQ(LookupProcessImage(900003, At(5)), R"(C:\old.exe)"); // late event from the old process
34 EXPECT_EQ(LookupProcessImage(900003, At(11)), R"(C:\new.exe)");
35}
36
37TEST(ProcessImages, SmallClockDifferenceStillMatches) {
38 // The hook's timestamp can read slightly earlier than the kernel's creation time
39 RecordProcessStart(900004, R"(C:\a.exe)", At(5));
40 EXPECT_EQ(LookupProcessImage(900004, At(4.5)), R"(C:\a.exe)");
41 EXPECT_EQ(LookupProcessImage(900004, At(2)), "");
42}
43
44TEST(ProcessImages, UnknownProcessIsEmpty) {
45 EXPECT_EQ(LookupProcessImage(900005, At(0)), "");
46}
47
48TEST(ProcessImages, ResolveNamesProcessesWithoutAnImageFile) {
49 // QueryFullProcessImageName fails for these; kernel registry and file activity is charged to System
50 EXPECT_EQ(ResolveProcessImage(4, At(0)), "System");
51}
52
53TEST(ProcessImages, ResolveFallsBackToWindowsForRunningProcesses) {
54 // This process started before anything recorded it
55 std::string image = ResolveProcessImage(GetCurrentProcessId(), At(0));
56 EXPECT_NE(image.find("Test_Service.exe"), std::string::npos) << image;
57}
TEST(ProcessImages, ImageIsKnownAfterTheProcessExits)
#define MAX_PATH
Definition callbacks.h:7
void RecordProcessExit(DWORD processId)
Records a process exit (ProcessTerminated). Its entry is forgotten a minute later.
void RecordProcessStart(DWORD processId, const std::string &image, const FILETIME &time)
Records a process start (ProcessCreated).
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 ResolveProcessImage(DWORD processId, const FILETIME &time)
LookupProcessImage, falling back to asking Windows (QueryFullProcessImageName) for processes the serv...