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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
regctrl.c
Abstract:
Invokes the usermode and kernel mode callback samples.
Environment:
User mode Win32 console application
Revision History:
--*/
#include "regctrl.h"
//
// Global variables
//
//
// Handle to the driver
//
HANDLE g_Driver;
//
// Handle to the root test key
//
HKEY g_RootKey;
//
// Version number for the registry callback
//
ULONG g_MajorVersion;
ULONG g_MinorVersion;
BOOL
GetCallbackVersion();
VOID
DoKernelModeSamples();
VOID
DoUserModeSamples();
LPCWSTR
GetKernelModeSampleName (
_In_ KERNELMODE_SAMPLE Sample
);
VOID __cdecl
wmain(
_In_ ULONG argc,
_In_reads_(argc) LPCWSTR argv[]
)
{
BOOL Result;
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
Result = UtilLoadDriver(DRIVER_NAME,
DRIVER_NAME_WITH_EXT,
WIN32_DEVICE_NAME,
&g_Driver);
if (Result != TRUE) {
ErrorPrint("UtilLoadDriver failed, exiting...");
exit(1);
}
printf("\n");
printf("Starting Callback samples...\n");
printf("\n");
printf("To get more detailed output from the sample, do either one of these steps:\n");
printf("\n");
printf("\tA. In kernel debugger: \n");
printf("\tkd> ed nt!Kd_IHVDRIVER_Mask 0x8\n\n");
printf("\tB. Run this script and reboot:\n");
printf("\treg add \"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Debug Print Filter\" /v IHVDRIVER /t REG_DWORD /d 0x8\n\n");
//
// Get the registry callback version to determine what samples can
// run on the system.
//
if (GetCallbackVersion()) {
InfoPrint("Callback version is %u.%u", g_MajorVersion, g_MinorVersion);
}
DoKernelModeSamples();
DoUserModeSamples();
UtilUnloadDriver(g_Driver, NULL, DRIVER_NAME);
}
BOOL
GetCallbackVersion(
)
/*++
Routine Description:
This routine asks the driver for the registry callback version and
stores it in the global variables g_MajorVersion and g_MinorVersion.
--*/
{
DWORD BytesReturned = 0;
BOOL Result;
GET_CALLBACK_VERSION_OUTPUT Output = {0};
Result = DeviceIoControl(g_Driver,
IOCTL_GET_CALLBACK_VERSION,
NULL,
0,
&Output,
sizeof(GET_CALLBACK_VERSION_OUTPUT),
&BytesReturned,
NULL);
if (Result != TRUE) {
ErrorPrint("DeviceIoControl for GET_CALLBACK_VERSION failed, error %d\n", GetLastError());
return FALSE;
}
g_MajorVersion = Output.MajorVersion;
g_MinorVersion = Output.MinorVersion;
return TRUE;
}
VOID
DoUserModeSamples(
)
/*++
Routine Description:
Creates the callback root test key and calls the usermode samples.
--*/
{
LONG Res;
Res = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
ROOT_KEY_REL_PATH,
0,
NULL,
0,
KEY_ALL_ACCESS,
NULL,
&g_RootKey,
NULL);
if (Res != ERROR_SUCCESS) {
ErrorPrint("Creating root key failed. Error %d", Res);
goto Exit;
}
PreNotificationBlockSample();
PreNotificationBypassSample();
PostNotificationOverrideSuccessSample();
PostNotificationOverrideErrorSample();
CaptureSample();
Exit:
if (g_RootKey != NULL) {
RegCloseKey(g_RootKey);
}
RegDeleteKey(HKEY_LOCAL_MACHINE, ROOT_KEY_REL_PATH);
}
VOID
DoKernelModeSamples(
)
/*++
Routine Description:
Tells the driver to run the kernel mode samples and prints out the
results.
--*/
{
UINT Index;
DWORD BytesReturned = 0;
BOOL Result;
DO_KERNELMODE_SAMPLES_OUTPUT Output = {0};
Result = DeviceIoControl (g_Driver,
IOCTL_DO_KERNELMODE_SAMPLES,
NULL,
0,
&Output,
sizeof(DO_KERNELMODE_SAMPLES_OUTPUT),
&BytesReturned,
NULL);
if (Result != TRUE) {
ErrorPrint("DeviceIoControl for DO_KERNELMODE_SAMPLES failed, error %d\n", GetLastError());
return;
}
InfoPrint("");
InfoPrint("=== Results of KernelMode Samples ===");
for (Index = 0; Index < MAX_KERNELMODE_SAMPLES; Index++) {
InfoPrint("\t%S: %s",
GetKernelModeSampleName(Index),
Output.SampleResults[Index]? "Succeeded" : "FAILED");
}
}
LPCWSTR
GetKernelModeSampleName (
_In_ KERNELMODE_SAMPLE Sample
)
/*++
Routine Description:
Converts from a KERNELMODE_SAMPLE value to a string
Arguments:
Sample - value that identifies a kernel mode sample
Return Value:
Returns a string of the name of Sample.
--*/
{
switch (Sample) {
case KERNELMODE_SAMPLE_PRE_NOTIFICATION_BLOCK:
return L"Pre-Notification Block Sample";
case KERNELMODE_SAMPLE_PRE_NOTIFICATION_BYPASS:
return L"Pre-Notification Bypass Sample";
case KERNELMODE_SAMPLE_POST_NOTIFICATION_OVERRIDE_SUCCESS:
return L"Post-Notification Override Success Sample";
case KERNELMODE_SAMPLE_POST_NOTIFICATION_OVERRIDE_ERROR:
return L"Post-Notification Override Error Sample";
case KERNELMODE_SAMPLE_TRANSACTION_REPLAY:
return L"Transaction Replay Sample";
case KERNELMODE_SAMPLE_TRANSACTION_ENLIST:
return L"Transaction Enlist Sample";
case KERNELMODE_SAMPLE_MULTIPLE_ALTITUDE_BLOCK_DURING_PRE:
return L"Multiple Altitude Block During Pre Sample";
case KERNELMODE_SAMPLE_MULTIPLE_ALTITUDE_INTERNAL_INVOCATION:
return L"Multiple Altitude Internal Invocation Sample";
case KERNELMODE_SAMPLE_SET_CALL_CONTEXT:
return L"Set Call Context Sample";
case KERNELMODE_SAMPLE_SET_OBJECT_CONTEXT:
return L"Set Object Context Sample";
case KERNELMODE_SAMPLE_VERSION_CREATE_OPEN_V1:
return L"Create Open V1 Sample";
default:
return L"Unsupported Kernel Mode Sample";
}
}
|