forked from JKornev/hidden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegFilter.c
774 lines (626 loc) · 20.7 KB
/
RegFilter.c
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// =========================================================================================
// Registry filter
// =========================================================================================
#include "RegFilter.h"
#include "ExcludeList.h"
#include "PsMonitor.h"
#include "Configs.h"
#include "Driver.h"
#include "Helper.h"
#include <Ntstrsafe.h>
#define FILTER_ALLOC_TAG 'FRlF'
BOOLEAN g_regFilterInited = FALSE;
ExcludeContext g_excludeRegKeyContext;
ExcludeContext g_excludeRegValueContext;
// Use this variable for hard code path to registry keys that you would like to hide
// For instance: L"\\REGISTRY\\MACHINE\\SOFTWARE\\test_key",
// Notice: this array should be NULL terminated
CONST PWCHAR g_excludeRegKeys[] = {
NULL
};
// Use this variable for hard code path to registry keys that you would like to hide
// For instance: L"\\REGISTRY\\MACHINE\\SOFTWARE\\test_key\\test_value",
// Notice: this array should be NULL terminated
CONST PWCHAR g_excludeRegValues[] = {
NULL
};
LARGE_INTEGER g_regCookie = { 0 };
BOOLEAN CheckRegistryKeyInExcludeList(PVOID RootObject, PUNICODE_STRING keyPath)
{
PCUNICODE_STRING regPath;
NTSTATUS status;
BOOLEAN found = FALSE;
// Check is the registry path matched to exclude list
if (keyPath->Length > sizeof(WCHAR) && keyPath->Buffer[0] == L'\\')
{
// Check absolute path
found = CheckExcludeListRegKey(g_excludeRegKeyContext, keyPath);
}
else
{
// Check relative path
enum { LOCAL_BUF_SIZE = 256 };
WCHAR localBuffer[LOCAL_BUF_SIZE];
LPWSTR dynBuffer = NULL;
UNICODE_STRING fullRegPath;
USHORT totalSize;
// Obtain root key path
status = CmCallbackGetKeyObjectID(&g_regCookie, RootObject, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x\n", status);
return FALSE;
}
// Concatenate root path + sub key path
totalSize = regPath->Length + keyPath->Length + sizeof(WCHAR);
if (totalSize / sizeof(WCHAR) > LOCAL_BUF_SIZE)
{
// local buffer too small, we should allocate memory
dynBuffer = (LPWSTR)ExAllocatePoolWithTag(NonPagedPool, totalSize, FILTER_ALLOC_TAG);
if (!dynBuffer)
{
LogWarning("Error, memory allocation failed with code:%08x\n", status);
return FALSE;
}
memcpy(dynBuffer, regPath->Buffer, regPath->Length);
fullRegPath.Buffer = dynBuffer;
}
else
{
// use local buffer
fullRegPath.Buffer = localBuffer;
}
// copy root path + sub key path to new buffer
memcpy(fullRegPath.Buffer, regPath->Buffer, regPath->Length);
fullRegPath.Buffer[regPath->Length / sizeof(WCHAR)] = L'\\';
memcpy(
(PCHAR)fullRegPath.Buffer + regPath->Length + sizeof(WCHAR),
keyPath->Buffer,
keyPath->Length);
fullRegPath.Length = totalSize;
fullRegPath.MaximumLength = fullRegPath.Length;
// Compare to exclude list
found = CheckExcludeListRegKey(g_excludeRegKeyContext, &fullRegPath);
if (dynBuffer)
ExFreePoolWithTag(dynBuffer, FILTER_ALLOC_TAG);
}
return found;
}
NTSTATUS RegPreCreateKey(PVOID context, PREG_PRE_CREATE_KEY_INFORMATION info)
{
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
if (CheckExcludeListRegKey(g_excludeRegKeyContext, info->CompleteName))
{
LogTrace("Registry key is hidden: %wZ", info->CompleteName);
return STATUS_ACCESS_DENIED;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreCreateKeyEx(PVOID context, PREG_CREATE_KEY_INFORMATION info)
{
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
if (CheckRegistryKeyInExcludeList(info->RootObject, info->CompleteName))
{
LogTrace("Registry key is hidden: %wZ", info->CompleteName);
return STATUS_ACCESS_DENIED;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreOpenKey(PVOID context, PREG_PRE_OPEN_KEY_INFORMATION info)
{//TODO: isn't used?
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
if (CheckExcludeListRegKey(g_excludeRegKeyContext, info->CompleteName))
{
LogTrace("Registry key is hidden: %wZ", info->CompleteName);
return STATUS_NOT_FOUND;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreOpenKeyEx(PVOID context, PREG_OPEN_KEY_INFORMATION info)
{
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
if (CheckRegistryKeyInExcludeList(info->RootObject, info->CompleteName))
{
LogTrace("Registry key is hidden: %wZ", info->CompleteName);
return STATUS_NOT_FOUND;
}
return STATUS_SUCCESS;
}
BOOLEAN GetNameFromEnumKeyPreInfo(KEY_INFORMATION_CLASS infoClass, PVOID infoBuffer, PUNICODE_STRING keyName)
{
switch (infoClass)
{
case KeyBasicInformation:
{
PKEY_BASIC_INFORMATION keyInfo = (PKEY_BASIC_INFORMATION)infoBuffer;
keyName->Buffer = keyInfo->Name;
keyName->Length = keyName->MaximumLength = (USHORT)keyInfo->NameLength;
}
break;
case KeyNameInformation:
{
PKEY_NAME_INFORMATION keyInfo = (PKEY_NAME_INFORMATION)infoBuffer;
keyName->Buffer = keyInfo->Name;
keyName->Length = keyName->MaximumLength = (USHORT)keyInfo->NameLength;
}
break;
default:
return FALSE;
}
return TRUE;
}
NTSTATUS RegPostEnumKey(PVOID context, PREG_POST_OPERATION_INFORMATION info)
{
PREG_ENUMERATE_KEY_INFORMATION preInfo;
PCUNICODE_STRING regPath;
UNICODE_STRING keyName;
UINT32 incIndex;
NTSTATUS status;
UNREFERENCED_PARAMETER(context);
if (!NT_SUCCESS(info->Status))
return STATUS_SUCCESS;
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
preInfo = (PREG_ENUMERATE_KEY_INFORMATION)info->PreInformation;
if (!GetNameFromEnumKeyPreInfo(preInfo->KeyInformationClass, preInfo->KeyInformation, &keyName))
return STATUS_SUCCESS;
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegKeyContext, (PUNICODE_STRING)regPath, &keyName, &incIndex))
LogTrace("Registry key is going to be hidden in: %wZ (inc: %d)", regPath, incIndex);
if (incIndex > 0)
{
HANDLE Key;
ULONG resLen, i;
BOOLEAN infinite = TRUE;
PVOID tempBuffer;
status = ObOpenObjectByPointer(info->Object, OBJ_KERNEL_HANDLE, NULL, KEY_ALL_ACCESS, *CmKeyObjectType, KernelMode, &Key);
if (!NT_SUCCESS(status))
{
LogWarning("Error, ObOpenObjectByPointer() failed with code:%08x", status);
return STATUS_SUCCESS;
}
tempBuffer = (LPWSTR)ExAllocatePoolWithTag(PagedPool, preInfo->Length, FILTER_ALLOC_TAG);
if (tempBuffer)
{
for (i = 0; infinite; i++)
{
status = ZwEnumerateKey(Key, preInfo->Index + incIndex, preInfo->KeyInformationClass, tempBuffer, preInfo->Length, &resLen);
if (!NT_SUCCESS(status))
break;
if (!GetNameFromEnumKeyPreInfo(preInfo->KeyInformationClass, tempBuffer, &keyName))
break;
if (!CheckExcludeListRegKeyValueName(g_excludeRegKeyContext, (PUNICODE_STRING)regPath, &keyName, &incIndex))
{
*preInfo->ResultLength = resLen;
__try
{
RtlCopyMemory(preInfo->KeyInformation, tempBuffer, resLen);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
LogWarning("Warning, can't copy new key information");
}
break;
}
}
ExFreePoolWithTag(tempBuffer, FILTER_ALLOC_TAG);
}
else
{
status = STATUS_SUCCESS;
}
info->ReturnStatus = status;
ZwClose(Key);
}
return STATUS_SUCCESS;
}
BOOLEAN GetNameFromEnumValuePreInfo(KEY_VALUE_INFORMATION_CLASS infoClass, PVOID infoBuffer, PUNICODE_STRING keyName)
{
switch (infoClass)
{
case KeyValueBasicInformation:
{
PKEY_VALUE_BASIC_INFORMATION keyInfo = (PKEY_VALUE_BASIC_INFORMATION)infoBuffer;
keyName->Buffer = keyInfo->Name;
keyName->Length = keyName->MaximumLength = (USHORT)keyInfo->NameLength;
}
break;
case KeyValueFullInformation:
case KeyValueFullInformationAlign64:
{
PKEY_VALUE_FULL_INFORMATION keyInfo = (PKEY_VALUE_FULL_INFORMATION)infoBuffer;
keyName->Buffer = keyInfo->Name;
keyName->Length = keyName->MaximumLength = (USHORT)keyInfo->NameLength;
}
break;
default:
return FALSE;
}
return TRUE;
}
NTSTATUS RegPostEnumValue(PVOID context, PREG_POST_OPERATION_INFORMATION info)
{
PREG_ENUMERATE_VALUE_KEY_INFORMATION preInfo;
PCUNICODE_STRING regPath;
UNICODE_STRING keyName;
UINT32 incIndex;
NTSTATUS status;
UNREFERENCED_PARAMETER(context);
if (!NT_SUCCESS(info->Status))
return STATUS_SUCCESS;
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
preInfo = (PREG_ENUMERATE_VALUE_KEY_INFORMATION)info->PreInformation;
if (!GetNameFromEnumValuePreInfo(preInfo->KeyValueInformationClass, preInfo->KeyValueInformation, &keyName))
return STATUS_SUCCESS;
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, &keyName, &incIndex))
LogTrace("Registry value is going to be hidden in: %wZ (inc: %d)", regPath, incIndex);
if (incIndex > 0)
{
HANDLE Key;
ULONG resLen, i;
BOOLEAN infinite = TRUE;
PVOID tempBuffer;
status = ObOpenObjectByPointer(info->Object, OBJ_KERNEL_HANDLE, NULL, KEY_ALL_ACCESS, *CmKeyObjectType, KernelMode, &Key);
if (!NT_SUCCESS(status))
{
LogWarning("Error, ObOpenObjectByPointer() failed with code:%08x", status);
return STATUS_SUCCESS;
}
tempBuffer = (LPWSTR)ExAllocatePoolWithTag(PagedPool, preInfo->Length, FILTER_ALLOC_TAG);
if (tempBuffer)
{
for (i = 0; infinite; i++)
{
status = ZwEnumerateValueKey(Key, preInfo->Index + incIndex, preInfo->KeyValueInformationClass, tempBuffer, preInfo->Length, &resLen);
if (!NT_SUCCESS(status))
break;
if (!GetNameFromEnumValuePreInfo(preInfo->KeyValueInformationClass, tempBuffer, &keyName))
break;
if (!CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, &keyName, &incIndex))
{
*preInfo->ResultLength = resLen;
__try
{
RtlCopyMemory(preInfo->KeyValueInformation, tempBuffer, resLen);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
LogWarning("Warning, can't copy new key information");
}
break;
}
}
ExFreePoolWithTag(tempBuffer, FILTER_ALLOC_TAG);
}
else
{
status = STATUS_SUCCESS;
}
info->ReturnStatus = status;
ZwClose(Key);
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreSetValue(PVOID context, PREG_SET_VALUE_KEY_INFORMATION info)
{
NTSTATUS status;
PCUNICODE_STRING regPath;
UINT32 incIndex;
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, info->ValueName, &incIndex))
{
LogTrace("Registry value has been hidden: %wZ\\%wZ (inc: %d)", regPath, info->ValueName, incIndex);
return STATUS_NOT_FOUND;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreDeleteValue(PVOID context, PREG_DELETE_VALUE_KEY_INFORMATION info)
{
NTSTATUS status;
PCUNICODE_STRING regPath;
UINT32 incIndex;
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, info->ValueName, &incIndex))
{
LogTrace("Registry value has been hidden: %wZ\\%wZ (inc: %d)", regPath, info->ValueName, incIndex);
return STATUS_NOT_FOUND;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreQueryValue(PVOID context, PREG_QUERY_VALUE_KEY_INFORMATION info)
{
NTSTATUS status;
PCUNICODE_STRING regPath;
UINT32 incIndex;
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, info->ValueName, &incIndex))
{
LogTrace("Registry value has been hidden: %wZ\\%wZ (inc: %d)", regPath, info->ValueName, incIndex);
return STATUS_NOT_FOUND;
}
return STATUS_SUCCESS;
}
NTSTATUS RegPreQueryMultipleValue(PVOID context, PREG_QUERY_MULTIPLE_VALUE_KEY_INFORMATION info)
{
NTSTATUS status;
PCUNICODE_STRING regPath;
UINT32 incIndex, i;
UNREFERENCED_PARAMETER(context);
if (IsProcessExcluded(PsGetCurrentProcessId()))
return STATUS_SUCCESS;
status = CmCallbackGetKeyObjectID(&g_regCookie, info->Object, NULL, ®Path);
if (!NT_SUCCESS(status))
{
LogWarning("Error, registry name query failed with code:%08x", status);
return STATUS_SUCCESS;
}
for (i = 0; i < info->EntryCount; i++)
{
incIndex = 0;
if (CheckExcludeListRegKeyValueName(g_excludeRegValueContext, (PUNICODE_STRING)regPath, info->ValueEntries[i].ValueName, &incIndex))
{
LogTrace("Registry value has been hidden: %wZ\\%wZ (inc: %d)", regPath, info->ValueEntries[i].ValueName, incIndex);
return STATUS_NOT_FOUND;
}
}
return STATUS_SUCCESS;
}
_Function_class_(EX_CALLBACK_FUNCTION)
NTSTATUS RegistryFilterCallback(PVOID CallbackContext, PVOID Argument1, PVOID Argument2)
{
REG_NOTIFY_CLASS notifyClass = (REG_NOTIFY_CLASS)(ULONG_PTR)Argument1;
NTSTATUS status;
if (!IsDriverEnabled())
return STATUS_SUCCESS;
switch (notifyClass)
{
case RegNtPreCreateKey:
status = RegPreCreateKey(CallbackContext, (PREG_PRE_CREATE_KEY_INFORMATION)Argument2);
break;
case RegNtPreCreateKeyEx:
status = RegPreCreateKeyEx(CallbackContext, (PREG_CREATE_KEY_INFORMATION)Argument2);
break;
case RegNtPreOpenKey:
status = RegPreCreateKey(CallbackContext, (PREG_PRE_OPEN_KEY_INFORMATION)Argument2);
break;
case RegNtPreOpenKeyEx:
status = RegPreOpenKeyEx(CallbackContext, (PREG_OPEN_KEY_INFORMATION)Argument2);
break;
case RegNtPostEnumerateKey:
status = RegPostEnumKey(CallbackContext, (PREG_POST_OPERATION_INFORMATION)Argument2);
break;
case RegNtPostEnumerateValueKey:
status = RegPostEnumValue(CallbackContext, (PREG_POST_OPERATION_INFORMATION)Argument2);
break;
case RegNtSetValueKey:
status = RegPreSetValue(CallbackContext, (PREG_SET_VALUE_KEY_INFORMATION)Argument2);
break;
case RegNtPreDeleteValueKey:
status = RegPreDeleteValue(CallbackContext, (PREG_DELETE_VALUE_KEY_INFORMATION)Argument2);
break;
case RegNtPreQueryValueKey:
status = RegPreQueryValue(CallbackContext, (PREG_QUERY_VALUE_KEY_INFORMATION)Argument2);
break;
case RegNtPreQueryMultipleValueKey:
status = RegPreQueryMultipleValue(CallbackContext, (PREG_QUERY_MULTIPLE_VALUE_KEY_INFORMATION)Argument2);
break;
default:
status = STATUS_SUCCESS;
break;
}
return status;
}
NTSTATUS AddCurrentControlSetVariants(PUNICODE_STRING KeyPath, ExcludeContext Context, ULONGLONG ObjId,
NTSTATUS(*AddRoutine)(ExcludeContext, PUNICODE_STRING, PExcludeEntryId, ExcludeEntryId))
{
UNICODE_STRING currVersion, currVersionXXX, tail;
ULONGLONG objIds[2];
NTSTATUS status;
BOOLEAN tailed;
RtlInitUnicodeString(&currVersion, L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet");
if (!RtlPrefixUnicodeString(&currVersion, KeyPath, TRUE))
return STATUS_SUCCESS;
tailed = (KeyPath->Length >= currVersion.Length + sizeof(WCHAR));
// Does the path contain a valid CurrentControlSet\\ and not CurrentControlXYZ... ?
if (tailed && KeyPath->Buffer[currVersion.Length / sizeof(WCHAR)] != L'\\')
return STATUS_SUCCESS;
currVersionXXX.Buffer = (LPWSTR)ExAllocatePoolWithTag(NonPagedPool, KeyPath->Length, FILTER_ALLOC_TAG);
currVersionXXX.Length = 0;
currVersionXXX.MaximumLength = KeyPath->Length;
if (!currVersionXXX.Buffer)
return STATUS_NO_MEMORY;
__try
{
// ControlSet001
if (tailed)
{
tail.Buffer = KeyPath->Buffer + (currVersion.Length / sizeof(WCHAR)) + 1;
tail.MaximumLength = tail.Length = KeyPath->Length - currVersion.Length - sizeof(WCHAR);
RtlUnicodeStringPrintf(&currVersionXXX, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001\\%wZ", &tail);
}
else
{
RtlInitUnicodeString(&currVersionXXX, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet001");
}
status = AddRoutine(Context, &currVersionXXX, &objIds[0], ObjId);
if (!NT_SUCCESS(status))
return status;
// ControlSet002
if (tailed)
RtlUnicodeStringPrintf(&currVersionXXX, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet002\\%wZ", &tail);
else
RtlInitUnicodeString(&currVersionXXX, L"\\REGISTRY\\MACHINE\\SYSTEM\\ControlSet002");
status = AddRoutine(Context, &currVersionXXX, &objIds[1], ObjId);
if (!NT_SUCCESS(status))
{
RemoveExcludeListEntry(Context, objIds[0]);
return status;
}
}
__finally
{
ExFreePoolWithTag(currVersionXXX.Buffer, FILTER_ALLOC_TAG);
}
return STATUS_SUCCESS;
}
VOID LoadConfigRegKeysCallback(PUNICODE_STRING Str, PVOID Params)
{
ExcludeEntryId id;
UNREFERENCED_PARAMETER(Params);
AddHiddenRegKey(Str, &id);
}
VOID LoadConfigRegValuesCallback(PUNICODE_STRING Str, PVOID Params)
{
ExcludeEntryId id;
UNREFERENCED_PARAMETER(Params);
AddHiddenRegValue(Str, &id);
}
NTSTATUS InitializeRegistryFilter(PDRIVER_OBJECT DriverObject)
{
NTSTATUS status;
UNICODE_STRING altitude, str;
ExcludeEntryId id;
UINT32 i;
// Fill exclude lists
status = InitializeExcludeListContext(&g_excludeRegKeyContext, ExcludeRegKey);
if (!NT_SUCCESS(status))
{
LogError("Error, exclude registry key list initialization failed with code:%08x", status);
return status;
}
for (i = 0; g_excludeRegKeys[i]; i++)
{
RtlInitUnicodeString(&str, g_excludeRegKeys[i]);
AddHiddenRegKey(&str, &id);
}
CfgEnumConfigsTable(HideRegKeysTable, &LoadConfigRegKeysCallback, NULL);
status = InitializeExcludeListContext(&g_excludeRegValueContext, ExcludeRegValue);
if (!NT_SUCCESS(status))
{
LogError("Error, exclude registry value list initialization failed with code:%08x", status);
DestroyExcludeListContext(g_excludeRegKeyContext);
return status;
}
for (i = 0; g_excludeRegValues[i]; i++)
{
RtlInitUnicodeString(&str, g_excludeRegValues[i]);
AddHiddenRegValue(&str, &id);
}
CfgEnumConfigsTable(HideRegValuesTable, &LoadConfigRegValuesCallback, NULL);
// Register registry filter
RtlInitUnicodeString(&altitude, L"320000");
status = CmRegisterCallbackEx(&RegistryFilterCallback, &altitude, DriverObject, NULL, &g_regCookie, NULL);
if (!NT_SUCCESS(status))
{
LogError("Error, registry filter registration failed with code:%08x", status);
DestroyExcludeListContext(g_excludeRegKeyContext);
DestroyExcludeListContext(g_excludeRegValueContext);
return status;
}
g_regFilterInited = TRUE;
LogTrace("Initialization is completed");
return status;
}
NTSTATUS DestroyRegistryFilter()
{
NTSTATUS status;
if (!g_regFilterInited)
return STATUS_NOT_FOUND;
status = CmUnRegisterCallback(g_regCookie);
if (!NT_SUCCESS(status))
LogWarning("Warning, registry filter unregistration failed with code:%08x", status);
DestroyExcludeListContext(g_excludeRegKeyContext);
DestroyExcludeListContext(g_excludeRegValueContext);
g_regFilterInited = FALSE;
LogTrace("Deinitialization is completed");
return status;
}
NTSTATUS AddHiddenRegKey(PUNICODE_STRING KeyPath, PULONGLONG ObjId)
{
NTSTATUS status;
status = AddExcludeListRegistryKey(g_excludeRegKeyContext, KeyPath, ObjId, 0);
if (NT_SUCCESS(status))
{
status = AddCurrentControlSetVariants(KeyPath, g_excludeRegKeyContext, *ObjId, &AddExcludeListRegistryKey);
if (!NT_SUCCESS(status))
RemoveHiddenRegKey(*ObjId);
}
return status;
}
NTSTATUS RemoveHiddenRegKey(ULONGLONG ObjId)
{
return RemoveExcludeListEntry(g_excludeRegKeyContext, ObjId);
}
NTSTATUS RemoveAllHiddenRegKeys()
{
return RemoveAllExcludeListEntries(g_excludeRegKeyContext);
}
NTSTATUS AddHiddenRegValue(PUNICODE_STRING ValuePath, PULONGLONG ObjId)
{
NTSTATUS status;
status = AddExcludeListRegistryValue(g_excludeRegValueContext, ValuePath, ObjId, 0);
if (NT_SUCCESS(status))
{
status = AddCurrentControlSetVariants(ValuePath, g_excludeRegValueContext, *ObjId, &AddExcludeListRegistryValue);
if (!NT_SUCCESS(status))
RemoveHiddenRegValue(*ObjId);
}
return status;
}
NTSTATUS RemoveHiddenRegValue(ULONGLONG ObjId)
{
return RemoveExcludeListEntry(g_excludeRegValueContext, ObjId);
}
NTSTATUS RemoveAllHiddenRegValues()
{
return RemoveAllExcludeListEntries(g_excludeRegValueContext);
}