-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRegistryMonitor.cpp
443 lines (413 loc) · 13.2 KB
/
RegistryMonitor.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "RegistryMonitor.h"
RegistryMonitor::RegistryMonitor(void)
{
wchar_t exListDriverPath[1024];
wchar_t kernelDriverPath[1024];
driverInstalled = false;
monitorRunning = false;
hMonitorStoppedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
GetFullPathName(L"RegistryMonitor.exl", 1024, exListDriverPath, NULL);
Monitor::loadExclusionList(exListDriverPath);
onRegistryExclusionReceivedConnection = EventController::getInstance()->connect_onServerEvent(L"registry-exclusion", boost::bind(&RegistryMonitor::onRegistryExclusionReceived, this, _1));
// Load registry monitor kernel driver
GetFullPathName(L"CaptureRegistryMonitor.sys", 1024, kernelDriverPath, NULL);
if(Monitor::installKernelDriver(kernelDriverPath, L"CaptureRegistryMonitor", L"Capture Registry Monitor"))
{
hDriver = CreateFile(
L"\\\\.\\CaptureRegistryMonitor",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0, // Default security
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, // Perform asynchronous I/O
0); // No template
if(INVALID_HANDLE_VALUE == hDriver) {
printf("RegistryMonitor: ERROR - CreateFile Failed: %i\n", GetLastError());
} else {
driverInstalled = true;
}
}
initialiseObjectNameMap();
}
RegistryMonitor::~RegistryMonitor(void)
{
stop();
if(isDriverInstalled())
{
driverInstalled = false;
CloseHandle(hDriver);
}
CloseHandle(hMonitorStoppedEvent);
}
void
RegistryMonitor::initialiseObjectNameMap()
{
HKEY hTestKey;
wchar_t szTemp[256];
DWORD dwError = RegOpenCurrentUser(KEY_READ , &hTestKey);
if (dwError == ERROR_SUCCESS )
{
NTSTATUS status;
DWORD RequiredLength;
PPUBLIC_OBJECT_TYPE_INFORMATION t;
typedef DWORD (WINAPI *pNtQueryObject)(HANDLE,DWORD,VOID*,DWORD,VOID*);
pNtQueryObject NtQueryObject = (pNtQueryObject)GetProcAddress(GetModuleHandle(L"ntdll.dll"), (LPCSTR)"NtQueryObject");
status = NtQueryObject(hTestKey, 1, NULL,0,&RequiredLength);
if(status == STATUS_INFO_LENGTH_MISMATCH)
{
t = (PPUBLIC_OBJECT_TYPE_INFORMATION)VirtualAlloc(NULL, RequiredLength, MEM_COMMIT, PAGE_READWRITE);
if(status != NtQueryObject(hTestKey, 1,t,RequiredLength,&RequiredLength))
{
ZeroMemory(szTemp, 256);
CopyMemory(&szTemp, t->TypeName.Buffer, RequiredLength);
// Dont change the order of these ... _Classes should be inserted first
// Small bug but who cares
wstring temp2 = szTemp;
temp2 += L"_CLASSES";
wstring name2 = L"HKCR";
objectNameMap.push_back(ObjectPair(temp2, name2));
wstring temp1 = szTemp;
wstring name1 = L"HKCU";
objectNameMap.push_back(ObjectPair(temp1, name1));
wstring temp3 = L"\\REGISTRY\\MACHINE";
wstring name3 = L"HKLM";
objectNameMap.push_back(ObjectPair(temp3, name3));
wstring temp4 = L"\\REGISTRY\\USER";
wstring name4 = L"HKU";
objectNameMap.push_back(ObjectPair(temp4, name4));
wstring temp5 = L"\\Registry\\Machine";
wstring name5 = L"HKLM";
objectNameMap.push_back(ObjectPair(temp5, name5));
}
VirtualFree(t, 0, MEM_RELEASE);
}
}
}
boost::signals::connection
RegistryMonitor::connect_onRegistryEvent(const signal_registryEvent::slot_type& s)
{
return signal_onRegistryEvent.connect(s);
}
void
RegistryMonitor::onRegistryExclusionReceived(Element* pElement)
{
wstring excluded = L"";
wstring registryEventType = L"";
wstring processPath = L"";
wstring registryPath = L"";
vector<Attribute>::iterator it;
for(it = pElement->attributes.begin(); it != pElement->attributes.end(); it++)
{
if(it->name == L"action") {
registryEventType = it->value;
} else if(it->name == L"object") {
registryPath = it->value;
} else if(it->name == L"subject") {
processPath = it->value;
} else if(it->name == L"excluded") {
excluded = it->value;
}
}
Monitor::addExclusion(excluded, registryEventType, processPath, registryPath);
}
void
RegistryMonitor::start()
{
if(!isMonitorRunning() && isDriverInstalled())
{
registryEventsBuffer = (BYTE*)malloc(REGISTRY_EVENTS_BUFFER_SIZE);
registryMonitorThread = new Thread(this);
registryMonitorThread->start("RegistryMonitor");
}
}
void
RegistryMonitor::stop()
{
if(isMonitorRunning() && isDriverInstalled())
{
monitorRunning = false;
WaitForSingleObject(hMonitorStoppedEvent, 1000);
registryMonitorThread->stop();
delete registryMonitorThread;
free(registryEventsBuffer);
}
}
wstring
RegistryMonitor::getRegistryEventName(int registryEventType)
{
wstring eventName = L"<UNKNOWN>";
switch(registryEventType)
{
case RegNtPostCreateKey:
eventName = L"CreateKey";
break;
case RegNtPostOpenKey:
eventName = L"OpenKey";
break;
case RegNtPreDeleteKey:
eventName = L"DeleteKey";
break;
case RegNtDeleteValueKey:
eventName = L"DeleteValueKey";
break;
case RegNtPreSetValueKey:
eventName = L"SetValueKey";
break;
case RegNtEnumerateKey:
eventName = L"EnumerateKey";
break;
case RegNtEnumerateValueKey:
eventName = L"EnumerateValueKey";
break;
case RegNtQueryKey:
eventName = L"QueryKey";
break;
case RegNtQueryValueKey:
eventName = L"QueryValueKey";
break;
case RegNtKeyHandleClose:
eventName = L"CloseKey";
break;
default:
break;
}
return eventName;
}
wstring
RegistryMonitor::convertRegistryObjectNameToHiveName(wstring registryObjectName)
{
/* Convert /Registry/Machine etc to the actual hive name like HKLM */
std::list<ObjectPair>::iterator it;
for(it = objectNameMap.begin(); it != objectNameMap.end(); it++)
{
size_t position = registryObjectName.rfind(it->first,0);
if(position != wstring::npos)
{
return registryObjectName.replace(position, it->first.length(), it->second, 0, it->second.length());
}
}
return registryObjectName;
}
void
RegistryMonitor::run()
{
DWORD dwReturn;
monitorRunning = true;
int waitTime = REGISTRY_DEFAULT_WAIT_TIME;
while(isMonitorRunning())
{
ZeroMemory(registryEventsBuffer, REGISTRY_EVENTS_BUFFER_SIZE);
DeviceIoControl(hDriver,
IOCTL_GET_REGISTRY_EVENTS,
0,
0,
registryEventsBuffer,
REGISTRY_EVENTS_BUFFER_SIZE,
&dwReturn,
NULL);
/* Go through all the registry events received. Events are variable sized
so the starts of them are calculated by adding the lengths of the various
data stored in it */
if(dwReturn >= sizeof(REGISTRY_EVENT))
{
UINT offset = 0;
do {
/* e->registryData contains the registry path first and then optionally
some data */
PREGISTRY_EVENT e = (PREGISTRY_EVENT)(registryEventsBuffer + offset);
BYTE* registryData = NULL;
wchar_t* szRegistryPath = NULL;
wstring registryEventName = getRegistryEventName(e->eventType);
/* Get the registry string */
szRegistryPath = (wchar_t*)malloc(e->registryPathLengthB);
CopyMemory(szRegistryPath, e->registryData, e->registryPathLengthB);
wstring registryPath = convertRegistryObjectNameToHiveName(szRegistryPath);
wstring processPath = ProcessManager::getInstance()->getProcessPath((DWORD)e->processId);
wchar_t processIdString[11];
swprintf_s(processIdString, 11, L"%ld", e->processId);
/* If there is data stored retrieve it */
if(e->dataLengthB > 0)
{
registryData = (BYTE*)malloc(e->dataLengthB);
CopyMemory(registryData, e->registryData+e->registryPathLengthB, e->dataLengthB);
}
/* Is the event excluded */
if(!Monitor::isEventAllowed(registryEventName,processPath,registryPath))
{
wchar_t szTempTime[256];
convertTimefieldsToString(e->time, szTempTime, 256);
wstring time = szTempTime;
//Handle all the post-processing to format the data
wchar_t szTemp[256];
wstring other;
size_t tmp_len;
vector<wstring> extraData;
//registry event extra.at(0) == PID
extraData.push_back(processIdString);
//registry event extra.at(1) == name of registry value
if(e->valueNameLength > 0){
extraData.push_back(e->valueName);
}
else{
extraData.push_back(L"");
}
//registry event extra.at(2) == registry value type
//registry event extra.at(3) == registry value data (if any)
//MS description of data types:
//http://support.microsoft.com/kb/256986
switch(e->dataType){
case REG_NONE:
extraData.push_back(L"REG_NONE");
extraData.push_back(L""); //This is so that the logger adds an extra blank value
break;
case REG_SZ:
extraData.push_back(L"REG_SZ");
if(registryData != NULL){
extraData.push_back((wchar_t *)registryData);
}
else{
extraData.push_back(L"");
}
break;
case REG_EXPAND_SZ:
extraData.push_back(L"REG_EXPAND_SZ");
if(registryData != NULL){
extraData.push_back((wchar_t *)registryData);
}
else{
extraData.push_back(L"");
}
break;
case REG_BINARY:
extraData.push_back(L"REG_BINARY");
if(registryData != NULL){
for(DWORD n = 0; n < e->dataLengthB; n++){
swprintf(szTemp, L"%x", registryData[n]);
other.append(szTemp);
}
extraData.push_back(other);
}
else{
extraData.push_back(L"");
}
break;
case REG_DWORD:
extraData.push_back(L"REG_DWORD");
if(registryData != NULL){
swprintf_s(szTemp, 256, L"%lx", ((DWORD *)registryData)[0]);
extraData.push_back(szTemp);
}
else{
extraData.push_back(L"");
}
break;
//TODO: Untested
case REG_DWORD_BIG_ENDIAN:
extraData.push_back(L"REG_DWORD_BIG_ENDIAN");
if(registryData != NULL){
swprintf_s(szTemp, 256, L"%x%x%x%x", registryData[0],registryData[1],registryData[2],registryData[3]);
extraData.push_back(szTemp);
}
else{
extraData.push_back(L"");
}
break;
//From MS: "A Unicode string naming a symbolic link."
//TODO: Untested
case REG_LINK:
extraData.push_back(L"REG_LINK");
if(registryData != NULL){
extraData.push_back((wchar_t *)registryData);
}
else{
extraData.push_back(L"");
}
break;
//TODO: regedit won't let me make a string,empty string, string, but that
// doesn't mean something else might not be able to do it. Look into it as it would
// break the while condition into ending early.
case REG_MULTI_SZ:
extraData.push_back(L"REG_MULTI_SZ");
if(registryData != NULL){
while(((wchar_t *)registryData)[0] != '\0' ){
other.append((wchar_t *)registryData);
other.append(L"-|-");
tmp_len = wcsnlen((wchar_t *)registryData, 512); //This doesn't count the null char in the length
registryData = (BYTE *)((wchar_t *)registryData + (tmp_len + 1));
}
extraData.push_back(other);
}
else{
extraData.push_back(L"");
}
break;
//TODO: Untested, "A series of nested arrays..."
case REG_RESOURCE_LIST:
extraData.push_back(L"REG_RESOURCE_LIST");
if(registryData != NULL){
extraData.push_back(L"FILL IN");
}
else{
extraData.push_back(L"");
}
break;
//TODO: Untested, "A series of nested arrays..."
case REG_FULL_RESOURCE_DESCRIPTOR:
extraData.push_back(L"REG_FULL_RESOURCE_DESCRIPTOR");
if(registryData != NULL){
extraData.push_back(L"FILL IN");
}
else{
extraData.push_back(L"");
}
break;
//TODO: Untested, "A series of nested arrays..."
case REG_RESOURCE_REQUIREMENTS_LIST:
extraData.push_back(L"REG_RESOURCE_REQUIREMENTS_LIST");
if(registryData != NULL){
extraData.push_back(L"FILL IN");
}
else{
extraData.push_back(L"");
}
break;
case REG_QWORD_LITTLE_ENDIAN:
extraData.push_back(L"REG_QWORD");
if(registryData != NULL){
swprintf_s(szTemp, 256, L"%lx%lx", ((DWORD *)registryData)[0],((DWORD *)registryData)[1]);
extraData.push_back(szTemp);
}
else{
extraData.push_back(L"");
}
break;
default:
extraData.push_back(L"UNKNOWN TYPE!");
if(registryData != NULL){
swprintf_s(szTemp, 256, L"%ld", e->dataType);
extraData.push_back(szTemp);
}
else{
extraData.push_back(L"");
}
break;
}
signal_onRegistryEvent(registryEventName, time, processPath, registryPath, extraData);
}
if(registryData != NULL)
free(registryData);
if(szRegistryPath != NULL)
free(szRegistryPath);
offset += sizeof(REGISTRY_EVENT) + e->registryPathLengthB + e->dataLengthB;
}while(offset < dwReturn);
}
if(dwReturn == (REGISTRY_EVENTS_BUFFER_SIZE))
{
waitTime = REGISTRY_BUFFER_FULL_WAIT_TIME;
} else {
waitTime = REGISTRY_DEFAULT_WAIT_TIME;
}
Sleep(waitTime);
}
SetEvent(hMonitorStoppedEvent);
}