forked from microsoft/SysmonCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocesscache.cpp
199 lines (171 loc) · 6.33 KB
/
processcache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// UnitTests for process cache testing.
#include "test.h"
#include "structs.h"
PROCESSCACHE_FETCH_TEST fetches[] = {
{
"Emtpy cache means no results",
{},
{},
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
},
},
{
"One entry simple match",
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
},
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
},
{},
},
{
"One entry does not match for different process objects",
{
ProcessCacheEntry( 1, 1, 1, (PVOID)1 ),
},
{},
{
ProcessCacheEntry( 1, 1, 1, (PVOID)2 ),
},
},
{
"No time input provide latest",
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
ProcessCacheEntry( 1, 1, 2, nullptr ),
},
{
ProcessCacheEntry( 1, 1, 0, nullptr ),
},
{},
},
{
"Multi entry check",
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
ProcessCacheEntry( 1, 2, 1, nullptr ),
ProcessCacheEntry( 1, 3, 1, nullptr ),
},
{
ProcessCacheEntry( 1, 1, 1, nullptr ),
ProcessCacheEntry( 1, 2, 1, nullptr ),
ProcessCacheEntry( 1, 3, 1, nullptr ),
},
{
ProcessCacheEntry( 1, 4, 1, nullptr ),
},
},
};
// Fetch tests adding and getting entries from the process cache.
TEST( ProcessCacheTests, Fetch )
{
for( auto& fetch : fetches ) {
SCOPED_TRACE( fetch.Description );
ProcessCache::Instance().RemoveEntries();
ASSERT_TRUE( ProcessCache::Instance().Empty() );
std::map<ULONG64, ULONG64> pkeyTimeMap;
// Setup the process cache cache.
for( auto& input : fetch.Input ) {
input.AddToCache();
// Key the latest process created per key in a map.
auto& proc = input.EventHeader->m_EventBody.m_ProcessCreateEvent;
ULONG64 key = proc.m_ProcessKey;
ULONG64 time = proc.m_CreateTime.QuadPart;
auto it = pkeyTimeMap.find( key );
if( it == pkeyTimeMap.end() ) {
pkeyTimeMap.insert( std::pair<ULONG64, ULONG64>( key, time ) );
} else if( ( *it ).second < time ) {
( *it ).second = time;
}
}
// Check expect entries in the process cache.
for( auto& exist : fetch.Exists ) {
PPROCESS_CACHE_INFORMATION cacheEntry = exist.GetFromCache();
ASSERT_NE( cacheEntry, nullptr );
// If the time is unset, replace by latest.
auto& proc = exist.EventHeader->m_EventBody.m_ProcessCreateEvent;
if (proc.m_CreateTime.QuadPart == 0) {
proc.m_CreateTime.QuadPart = pkeyTimeMap[proc.m_ProcessKey];
}
ASSERT_EQ( 0, memcmp( cacheEntry->data, &proc, sizeof( *cacheEntry->data ) ) );
}
// Check non-expected entries.
for( const auto& miss : fetch.Miss ) {
ASSERT_EQ( nullptr, miss.GetFromCache() );
}
}
}
// Remove checks behaviour while adding and removing entries.
TEST( ProcessCacheTests, Remove )
{
ProcessCacheEntry first( 1, 1, 1, nullptr );
ProcessCacheEntry firstAfter( 1, 1, 2, nullptr );
ULONG64 expired = PROCESS_CACHE_FREE_DELAY + first.EventHeader->m_EventBody.m_ProcessCreateEvent.m_CreateTime.QuadPart + 1;
ProcessCacheEntry late( 2, 2, expired, nullptr );
ProcessCache::Instance().RemoveEntries();
ASSERT_TRUE( ProcessCache::Instance().Empty() );
// Add to cache, check cache is not empty and entry is here.
first.AddToCache();
ASSERT_FALSE( ProcessCache::Instance().Empty() );
ASSERT_NE( nullptr, first.GetFromCache() );
// Remove from cache, just mark as removed but stay for delay.
first.RemoveFromCache();
ASSERT_FALSE( ProcessCache::Instance().Empty() );
ASSERT_NE( nullptr, first.GetFromCache() );
// Remove an entry later, clean the cache.
late.RemoveFromCache();
ASSERT_TRUE( ProcessCache::Instance().Empty() );
// Add two entries, one is automatically marked as removed.
first.AddToCache();
ASSERT_NE( nullptr, first.GetFromCache() );
ASSERT_EQ( 0, first.GetFromCache()->removedTime.QuadPart );
firstAfter.AddToCache();
ASSERT_NE( nullptr, first.GetFromCache() );
ASSERT_NE( 0, first.GetFromCache()->removedTime.QuadPart );
ASSERT_FALSE( ProcessCache::Instance().Empty() );
// Removing the late entry does nothing because removedTime is aligned with current time.
late.RemoveFromCache();
auto cacheEntry = first.GetFromCache();
ASSERT_NE( nullptr, cacheEntry );
// Update late to work and test it removed everything.
firstAfter.RemoveFromCache();
auto& lateCreateTime = late.EventHeader->m_EventBody.m_ProcessCreateEvent.m_CreateTime;
lateCreateTime.QuadPart = cacheEntry->removedTime.QuadPart;
lateCreateTime.QuadPart += PROCESS_CACHE_FREE_DELAY + 1;
late.RemoveFromCache();
ASSERT_TRUE( ProcessCache::Instance().Empty() );
}
// ParentProcess checks parent user information can be resolved from the cache.
TEST( ProcessCacheTests, ParentProcessUser) {
SYSMON_DATA_DESCRIPTOR eventBuffer[SYSMON_MAX_EVENT_Fields] = {};
EVENT_DATA_DESCRIPTOR outputBuffer[SYSMON_MAX_EVENT_Fields] = {};
SYSMON_EVENT_HEADER eventHeader = {};
// Prepare a parent entry for SYSTEM user.
ProcessCacheEntry parent( 1, 1, 1, nullptr );
auto& proc = eventHeader.m_EventBody.m_ProcessCreateEvent;
auto& parentProc = parent.EventHeader->m_EventBody.m_ProcessCreateEvent;
proc.m_ParentProcessId = parentProc.m_ProcessId;
proc.m_CreateTime.QuadPart = parentProc.m_CreateTime.QuadPart + 1;
PSID parentUserSid = nullptr;
ASSERT_TRUE( ConvertStringSidToSid( _T("S-1-5-18"), &parentUserSid ) );
ProcessCache::Instance().RemoveEntries();
ASSERT_TRUE( ProcessCache::Instance().Empty() );
PTCHAR* parentUser = reinterpret_cast<PTCHAR*>(&outputBuffer[F_CP_ParentUser].Ptr);
// Process cache is empty, result is empty.
ASSERT_EQ( EventResolveField( &proc.m_CreateTime, &SYSMONEVENT_CREATE_PROCESS_Type, eventBuffer, &eventHeader, F_CP_ParentUser, outputBuffer, false ), 0ul );
ASSERT_STREQ( *parentUser, _T("-") );
// Process cache set without PC_Sid (user) defined.
memset( outputBuffer, 0, sizeof( outputBuffer ) );
parent.AddToCache();
ASSERT_EQ( EventResolveField( &proc.m_CreateTime, &SYSMONEVENT_CREATE_PROCESS_Type, eventBuffer, &eventHeader, F_CP_ParentUser, outputBuffer, false ), 0ul );
ASSERT_STREQ( *parentUser, _T("-") );
parent.RemoveFromCache();
// Process cache is correctly resolved with a SYSTEM user.
memset( outputBuffer, 0, sizeof( outputBuffer ) );
parent.AddExtension( PC_Sid, parentUserSid, GetLengthSid( parentUserSid ) );
parent.AddToCache();
ASSERT_EQ( EventResolveField( &proc.m_CreateTime, &SYSMONEVENT_CREATE_PROCESS_Type, eventBuffer, &eventHeader, F_CP_ParentUser, outputBuffer, false ), 0ul );
ASSERT_STREQ( *parentUser, _T("NT AUTHORITY\\SYSTEM") );
}