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
|
/*++
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:
evntdrv.c
Abstract:
Sample kernel mode trace provider/driver.
--*/
#include <stdio.h>
#include <ntddk.h>
#include "drvioctl.h"
//
// evntdrvEvents.h is generated by MC.exe with the -km option,
// using the manifest evntdrv.htm.
// The file contains a macro per event, and the required code to raise the
// event.
#include "evntdrvEvents.h"
DRIVER_UNLOAD EventDrvDriverUnload;
_Dispatch_type_(IRP_MJ_CREATE)
_Dispatch_type_(IRP_MJ_CLOSE)
DRIVER_DISPATCH EventDrvDispatchOpenClose;
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
DRIVER_DISPATCH EventDrvDispatchDeviceControl;
#define EventDrv_NT_DEVICE_NAME L"\\Device\\EventEtw"
#define EventDrv_WIN32_DEVICE_NAME L"\\DosDevices\\EVENTETW"
DRIVER_INITIALIZE DriverEntry;
NTSTATUS
EventDrvDispatchOpenClose(
IN PDEVICE_OBJECT pDO,
IN PIRP Irp
);
NTSTATUS
EventDrvDispatchDeviceControl(
IN PDEVICE_OBJECT pDO,
IN PIRP Irp
);
VOID
EventDrvDriverUnload(
IN PDRIVER_OBJECT DriverObject
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text( INIT, DriverEntry )
#pragma alloc_text( PAGE, EventDrvDispatchOpenClose )
#pragma alloc_text( PAGE, EventDrvDispatchDeviceControl )
#pragma alloc_text( PAGE, EventDrvDriverUnload )
#endif // ALLOC_PRAGMA
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This entry point is called directly by the I/O system.
Arguments:
DriverObject - pointer to the driver object
RegistryPath - pointer to a unicode string representing the path
to driver-specific key in the registry
Return Value:
STATUS_SUCCESS if successful
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
UNICODE_STRING DeviceName;
UNICODE_STRING LinkName;
PDEVICE_OBJECT EventDrvDeviceObject;
WCHAR DeviceNameString[128];
ULONG LengthToCopy = 128 * sizeof(WCHAR);
UNREFERENCED_PARAMETER (RegistryPath);
KdPrint(("EventDrv: DriverEntry\n"));
//
// Create Dispatch Entry Points.
//
DriverObject->DriverUnload = EventDrvDriverUnload;
DriverObject->MajorFunction[ IRP_MJ_CREATE ] = EventDrvDispatchOpenClose;
DriverObject->MajorFunction[ IRP_MJ_CLOSE ] = EventDrvDispatchOpenClose;
DriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL ] = EventDrvDispatchDeviceControl;
RtlInitUnicodeString( &DeviceName, EventDrv_NT_DEVICE_NAME );
//
// Create the Device object
//
Status = IoCreateDevice(
DriverObject,
0,
&DeviceName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&EventDrvDeviceObject);
if (!NT_SUCCESS(Status)) {
return Status;
}
RtlInitUnicodeString( &LinkName, EventDrv_WIN32_DEVICE_NAME );
Status = IoCreateSymbolicLink( &LinkName, &DeviceName );
if ( !NT_SUCCESS( Status )) {
IoDeleteDevice( EventDrvDeviceObject );
return Status;
}
//
// Choose a buffering mechanism
//
EventDrvDeviceObject->Flags |= DO_BUFFERED_IO;
//
// Register with ETW
//
EventRegisterSample_Driver();
//
// Log an Event with : DeviceNameLength
// DeviceName
// Status
//
// Copy the device name into the WCHAR local buffer in order
// to place a NULL character at the end, since this field is
// defined in the manifest as a NULL-terminated string
if (DeviceName.Length <= 128 * sizeof(WCHAR)) {
LengthToCopy = DeviceName.Length;
}
RtlCopyMemory(DeviceNameString,
DeviceName.Buffer,
LengthToCopy);
DeviceNameString[LengthToCopy/sizeof(WCHAR)] = L'\0';
EventWriteStartEvent(NULL, DeviceName.Length, DeviceNameString, Status);
return STATUS_SUCCESS;
}
NTSTATUS
EventDrvDispatchOpenClose(
IN PDEVICE_OBJECT pDO,
IN PIRP Irp
)
/*++
Routine Description:
Dispatch routine to handle Create/Close IRPs.
Arguments:
DeviceObject - pointer to a device object.
Irp - pointer to an I/O Request Packet.
Return Value:
NT status code
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER (pDO);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
NTSTATUS
EventDrvDispatchDeviceControl(
IN PDEVICE_OBJECT pDO,
IN PIRP Irp
)
/*++
Routine Description:
Dispatch routine to handle IOCTL IRPs.
Arguments:
DeviceObject - pointer to a device object.
Irp - pointer to an I/O Request Packet.
Return Value:
NT Status code
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation( Irp );
ULONG ControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
PAGED_CODE();
UNREFERENCED_PARAMETER (pDO);
Irp->IoStatus.Information =
irpStack->Parameters.DeviceIoControl.OutputBufferLength;
switch ( ControlCode ) {
case IOCTL_EVNTKMP_TRACE_EVENT_A:
{
EventWriteSampleEventA(NULL);
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
break;
}
default:
//
// Not one we recognize. Error.
//
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
Irp->IoStatus.Information = 0;
break;
}
//
// Get rid of this request
//
IoCompleteRequest( Irp, IO_NO_INCREMENT );
return Status;
}
VOID
EventDrvDriverUnload(
IN PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the resources allocated in DriverEntry.
Arguments:
DriverObject - pointer to a driver object.
Return Value:
VOID.
--*/
{
PDEVICE_OBJECT DevObj;
UNICODE_STRING LinkName;
PAGED_CODE();
KdPrint(("EventDrv: Unloading \n"));
//
// Get pointer to Device object
//
DevObj = DriverObject->DeviceObject;
EventWriteUnloadEvent(NULL, DevObj);
//
// Unregister the driver as an ETW provider
//
EventUnregisterSample_Driver();
//
// Form the Win32 symbolic link name.
//
RtlInitUnicodeString( &LinkName, EventDrv_WIN32_DEVICE_NAME );
//
// Remove symbolic link from Object
// namespace...
//
IoDeleteSymbolicLink( &LinkName );
//
// Unload the callbacks from the kernel to this driver
//
IoDeleteDevice( DevObj );
}
|