-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathcallback.c
More file actions
479 lines (373 loc) · 14 KB
/
callback.c
File metadata and controls
479 lines (373 loc) · 14 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Callback functions for Ob sample code tests.
//
// Notice:
//
// Use this sample code at your own risk; there is no support from Microsoft for the sample code.
// In addition, this sample code is licensed to you under the terms of the Microsoft Public License
// (http://www.microsoft.com/opensource/licenses.mspx)
//
//
#include "pch.h"
#include "tdriver.h"
//
// Globals
//
KGUARDED_MUTEX TdCallbacksMutex;
BOOLEAN bCallbacksInstalled = FALSE;
#define CB_PROCESS_TERMINATE 0x0001
#define CB_THREAD_TERMINATE 0x0001
// The following are for setting up callbacks for Process and Thread filtering
PVOID pCBRegistrationHandle = NULL;
OB_CALLBACK_REGISTRATION CBObRegistration = { 0 };
OB_OPERATION_REGISTRATION CBOperationRegistrations[2] = { { 0 }, { 0 } };
UNICODE_STRING CBAltitude = {0};
TD_CALLBACK_REGISTRATION CBCallbackRegistration = {0};
// Here is the protected process
WCHAR TdwProtectName[NAME_SIZE+1] = {0};
PVOID TdProtectedTargetProcess = NULL;
HANDLE TdProtectedTargetProcessId = {0};
//
// TdDeleteProtectNameCallback
//
NTSTATUS TdDeleteProtectNameCallback ()
{
NTSTATUS Status = STATUS_SUCCESS;
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdDeleteProtectNameCallback entering\n");
KeAcquireGuardedMutex (&TdCallbacksMutex);
// if the callbacks are active - remove them
if (bCallbacksInstalled == TRUE) {
ObUnRegisterCallbacks(pCBRegistrationHandle);
pCBRegistrationHandle = NULL;
bCallbacksInstalled = FALSE;
}
KeReleaseGuardedMutex (&TdCallbacksMutex);
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdDeleteProtectNameCallback exiting - status 0x%x\n", Status
);
return Status;
}
//
// TdProtectNameCallback
//
NTSTATUS TdProtectNameCallback (
_In_ PTD_PROTECTNAME_INPUT pProtectName
)
{
NTSTATUS Status = STATUS_SUCCESS;
if (!pProtectName) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
"ObCallbackTest: TdProtectNameCallback: name to protect/filter NULL pointer\n"
);
}
else {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdProtectNameCallback: entering name to protect/filter %ls\n", pProtectName->Name
);
}
KeAcquireGuardedMutex (&TdCallbacksMutex);
// Need to copy out the name and then set the flag to filter
// This will allow process creation to watch for the process to be created and get the PID
// and then prevent any other process from opening up that PID to terminate
memcpy(TdwProtectName, pProtectName->Name, sizeof(TdwProtectName));
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: name copied %ls\n", TdwProtectName
);
// Need to enable the OB callbacks
// once the process is matched to a newly created process, the callbacks will protect the process
if (bCallbacksInstalled == FALSE) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdProtectNameCallback: installing callbacks\n"
);
// Setup the Ob Registration calls
CBOperationRegistrations[0].ObjectType = PsProcessType;
CBOperationRegistrations[0].Operations |= OB_OPERATION_HANDLE_CREATE;
CBOperationRegistrations[0].Operations |= OB_OPERATION_HANDLE_DUPLICATE;
CBOperationRegistrations[0].PreOperation = CBTdPreOperationCallback;
CBOperationRegistrations[0].PostOperation = CBTdPostOperationCallback;
CBOperationRegistrations[1].ObjectType = PsThreadType;
CBOperationRegistrations[1].Operations |= OB_OPERATION_HANDLE_CREATE;
CBOperationRegistrations[1].Operations |= OB_OPERATION_HANDLE_DUPLICATE;
CBOperationRegistrations[1].PreOperation = CBTdPreOperationCallback;
CBOperationRegistrations[1].PostOperation = CBTdPostOperationCallback;
RtlInitUnicodeString (&CBAltitude, L"1000");
CBObRegistration.Version = OB_FLT_REGISTRATION_VERSION;
CBObRegistration.OperationRegistrationCount = 2;
CBObRegistration.Altitude = CBAltitude;
CBObRegistration.RegistrationContext = &CBCallbackRegistration;
CBObRegistration.OperationRegistration = CBOperationRegistrations;
Status = ObRegisterCallbacks (
&CBObRegistration,
&pCBRegistrationHandle // save the registration handle to remove callbacks later
);
if (!NT_SUCCESS (Status)) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
"ObCallbackTest: installing OB callbacks failed status 0x%x\n", Status
);
KeReleaseGuardedMutex (&TdCallbacksMutex); // Release the lock before exit
goto Exit;
}
bCallbacksInstalled = TRUE;
}
KeReleaseGuardedMutex (&TdCallbacksMutex);
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdProtectNameCallback: name to protect/filter %ls\n", TdwProtectName
);
Exit:
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdProtectNameCallback: exiting status 0x%x\n", Status
);
return Status;
}
//
// TdCheckProcessMatch - function to test a command line to see if the process is to be protected
//
NTSTATUS TdCheckProcessMatch (
_In_ PCUNICODE_STRING pustrCommand,
_In_ PEPROCESS Process,
_In_ HANDLE ProcessId
)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
WCHAR CommandLineBuffer[NAME_SIZE + 1] = {0}; // force a NULL termination
USHORT CommandLineBytes = 0;
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: entering\n");
if (!pustrCommand || !pustrCommand->Buffer) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: no Command line provided\n"
);
Status = FALSE;
goto Exit;
}
else {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: checking for %ls\n", TdwProtectName
);
}
KeAcquireGuardedMutex (&TdCallbacksMutex);
// Make sure that the CommandLineBuffer is NULL terminated
if (pustrCommand->Length < (NAME_SIZE * sizeof(WCHAR)))
CommandLineBytes = pustrCommand->Length;
else
CommandLineBytes = NAME_SIZE * sizeof(WCHAR);
if (CommandLineBytes) {
memcpy(CommandLineBuffer, pustrCommand->Buffer, CommandLineBytes);
// now check if the process to protect is in the command line
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: command line %ls\n", CommandLineBuffer
);
if (NULL != wcsstr (CommandLineBuffer, TdwProtectName)) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: match FOUND\n"
);
// Set the process to watch
TdProtectedTargetProcess = Process;
TdProtectedTargetProcessId = ProcessId;
Status = STATUS_SUCCESS;
}
}
else {
Status = FALSE; // no command line buffer provided
}
KeReleaseGuardedMutex (&TdCallbacksMutex);
Exit:
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: TdCheckProcessMatch: leaving status 0x%x\n", Status
);
return Status;
}
//
// CBTdPreOperationCallback
//
OB_PREOP_CALLBACK_STATUS
CBTdPreOperationCallback (
_In_ PVOID RegistrationContext,
_Inout_ POB_PRE_OPERATION_INFORMATION PreInfo
)
{
PTD_CALLBACK_REGISTRATION CallbackRegistration;
ACCESS_MASK AccessBitsToClear = 0;
ACCESS_MASK AccessBitsToSet = 0;
ACCESS_MASK InitialDesiredAccess = 0;
ACCESS_MASK OriginalDesiredAccess = 0;
PACCESS_MASK DesiredAccess = NULL;
LPCWSTR ObjectTypeName = NULL;
LPCWSTR OperationName = NULL;
// Not using driver specific values at this time
CallbackRegistration = (PTD_CALLBACK_REGISTRATION)RegistrationContext;
TD_ASSERT (PreInfo->CallContext == NULL);
// Only want to filter attempts to access protected process
// all other processes are left untouched
if (PreInfo->ObjectType == *PsProcessType) {
//
// Ignore requests for processes other than our target process.
//
// if (TdProtectedTargetProcess != NULL &&
// TdProtectedTargetProcess != PreInfo->Object)
if (TdProtectedTargetProcess != PreInfo->Object)
{
goto Exit;
}
//
// Also ignore requests that are trying to open/duplicate the current
// process.
//
if (PreInfo->Object == PsGetCurrentProcess()) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: CBTdPreOperationCallback: ignore process open/duplicate from the protected process itself\n");
goto Exit;
}
ObjectTypeName = L"PsProcessType";
AccessBitsToClear = CB_PROCESS_TERMINATE;
AccessBitsToSet = 0;
}
else if (PreInfo->ObjectType == *PsThreadType) {
HANDLE ProcessIdOfTargetThread = PsGetThreadProcessId ((PETHREAD)PreInfo->Object);
//
// Ignore requests for threads belonging to processes other than our
// target process.
//
// if (CallbackRegistration->TargetProcess != NULL &&
// CallbackRegistration->TargetProcessId != ProcessIdOfTargetThread)
if (TdProtectedTargetProcessId != ProcessIdOfTargetThread) {
goto Exit;
}
//
// Also ignore requests for threads belonging to the current processes.
//
if (ProcessIdOfTargetThread == PsGetCurrentProcessId()) {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: CBTdPreOperationCallback: ignore thread open/duplicate from the protected process itself\n");
goto Exit;
}
ObjectTypeName = L"PsThreadType";
AccessBitsToClear = CB_THREAD_TERMINATE;
AccessBitsToSet = 0;
}
else {
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
"ObCallbackTest: CBTdPreOperationCallback: unexpected object type\n");
goto Exit;
}
switch (PreInfo->Operation) {
case OB_OPERATION_HANDLE_CREATE:
DesiredAccess = &PreInfo->Parameters->CreateHandleInformation.DesiredAccess;
OriginalDesiredAccess = PreInfo->Parameters->CreateHandleInformation.OriginalDesiredAccess;
OperationName = L"OB_OPERATION_HANDLE_CREATE";
break;
case OB_OPERATION_HANDLE_DUPLICATE:
DesiredAccess = &PreInfo->Parameters->DuplicateHandleInformation.DesiredAccess;
OriginalDesiredAccess = PreInfo->Parameters->DuplicateHandleInformation.OriginalDesiredAccess;
OperationName = L"OB_OPERATION_HANDLE_DUPLICATE";
break;
default:
TD_ASSERT (FALSE);
break;
}
InitialDesiredAccess = *DesiredAccess;
// Filter only if request made outside of the kernel
if (PreInfo->KernelHandle != 1) {
*DesiredAccess &= ~AccessBitsToClear;
*DesiredAccess |= AccessBitsToSet;
}
//
// Set call context.
//
TdSetCallContext (PreInfo, CallbackRegistration);
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL, "ObCallbackTest: CBTdPreOperationCallback: PROTECTED process %p (ID 0x%p)\n",
TdProtectedTargetProcess,
(PVOID)TdProtectedTargetProcessId
);
DbgPrintEx (
DPFLTR_IHVDRIVER_ID, DPFLTR_TRACE_LEVEL,
"ObCallbackTest: CBTdPreOperationCallback\n"
" Client Id: %p:%p\n"
" Object: %p\n"
" Type: %ls\n"
" Operation: %ls (KernelHandle=%d)\n"
" OriginalDesiredAccess: 0x%x\n"
" DesiredAccess (in): 0x%x\n"
" DesiredAccess (out): 0x%x\n",
PsGetCurrentProcessId(),
PsGetCurrentThreadId(),
PreInfo->Object,
ObjectTypeName,
OperationName,
PreInfo->KernelHandle,
OriginalDesiredAccess,
InitialDesiredAccess,
*DesiredAccess
);
Exit:
return OB_PREOP_SUCCESS;
}
//
// TdPostOperationCallback
//
VOID
CBTdPostOperationCallback (
_In_ PVOID RegistrationContext,
_In_ POB_POST_OPERATION_INFORMATION PostInfo
)
{
PTD_CALLBACK_REGISTRATION CallbackRegistration = (PTD_CALLBACK_REGISTRATION)RegistrationContext;
TdCheckAndFreeCallContext (PostInfo, CallbackRegistration);
if (PostInfo->ObjectType == *PsProcessType) {
//
// Ignore requests for processes other than our target process.
//
if (CallbackRegistration->TargetProcess != NULL &&
CallbackRegistration->TargetProcess != PostInfo->Object
) {
return;
}
//
// Also ignore requests that are trying to open/duplicate the current
// process.
//
if (PostInfo->Object == PsGetCurrentProcess()) {
return;
}
}
else if (PostInfo->ObjectType == *PsThreadType) {
HANDLE ProcessIdOfTargetThread = PsGetThreadProcessId ((PETHREAD)PostInfo->Object);
//
// Ignore requests for threads belonging to processes other than our
// target process.
//
if (CallbackRegistration->TargetProcess != NULL &&
CallbackRegistration->TargetProcessId != ProcessIdOfTargetThread
) {
return;
}
//
// Also ignore requests for threads belonging to the current processes.
//
if (ProcessIdOfTargetThread == PsGetCurrentProcessId()) {
return;
}
}
else {
TD_ASSERT (FALSE);
}
}