-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathKMon_KL.c
431 lines (359 loc) · 15.2 KB
/
KMon_KL.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
#include "ntddk.h"
#include "ntifs.h"
#include "declarations.h"
#include <stdlib.h>
#define IOCTL_SETEVENTS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
#define IOCTL_QUERYREQUEST CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
#define IOCTL_DESTROYEVENTS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
// todo: add MDL globals to device extension
// todo: 'formalise' debug output
// todo: fix debug output of zwTerminateProcess hook(currently outputs (null)
PMDL mdlSysCall;
PVOID *MappedSystemCallTable;
typedef struct _DEVICE_EXTENSION {
PKEVENT pUserEvent; // user-mode notification event
PKEVENT pKernelEvent; // kernel-mode notification event
KSPIN_LOCK lockBusy;
UNICODE_STRING usKeyPath;
BOOLEAN bAllowCreateKey;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
typedef NTSTATUS (*ZWCREATEKEY) (OUT PHANDLE KeyHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG TitleIndex, IN PUNICODE_STRING Class OPTIONAL, IN ULONG CreateOptions, OUT PULONG Disposition OPTIONAL);
//typedef NTSTATUS (*ZWTERMINATEPROCESS) (IN HANDLE ProcessHandle, IN NTSTATUS ExitStatus);
ZWCREATEKEY OldZwCreateKey;
//ZWTERMINATEPROCESS OldZwTerminateProcess;
/* --------------------FIX ME-----------------------
NTSTATUS NewZwTerminateProcess(IN HANDLE ProcessHandle, IN NTSTATUS ExitStatus)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PFILE_OBJECT pfHandleObj;
POBJECT_NAME_INFORMATION pobjNameInfo;
UNICODE_STRING usProcessName;
ULONG ulRet;
if(ProcessHandle){
ntStatus = ObReferenceObjectByHandle(ProcessHandle, FILE_ALL_ACCESS, NULL, KernelMode, &pfHandleObj, NULL);
if(NT_SUCCESS(ntStatus)){
pobjNameInfo = NULL;
ntStatus = ObQueryNameString(pfHandleObj, pobjNameInfo, 0, &ulRet);
if(ntStatus == STATUS_INFO_LENGTH_MISMATCH){
pobjNameInfo = ExAllocatePool(NonPagedPool, ulRet);
if(pobjNameInfo)
ntStatus = ObQueryNameString(pfHandleObj, pobjNameInfo, ulRet, &ulRet);
}
if(NT_SUCCESS(ntStatus)){
RtlInitUnicodeString(&usProcessName, L"explorer.exe");
if(!RtlCompareUnicodeString(&usProcessName, &pobjNameInfo->Name, FALSE))
ntStatus = STATUS_ACCESS_DENIED;
else
ntStatus = STATUS_SUCCESS;
}else
DbgPrint("ObQueryNameString failed\n");
if(pobjNameInfo)
ExFreePool(pobjNameInfo);
ObDereferenceObject(pfHandleObj);
}else{
DbgPrint("ObReferenceObjectByHandle failed\n");
ntStatus = ((ZWTERMINATEPROCESS)(OldZwTerminateProcess)) (ProcessHandle, ExitStatus);
}
}
if(ntStatus == STATUS_SUCCESS){
ntStatus = ((ZWTERMINATEPROCESS)(OldZwTerminateProcess)) (ProcessHandle, ExitStatus);
}
return ntStatus;
}*/
NTSTATUS NewZwCreateKey(OUT PHANDLE KeyHandle, IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG TitleIndex, IN PUNICODE_STRING Class OPTIONAL,
IN ULONG CreateOptions, OUT PULONG Disposition OPTIONAL)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
UNICODE_STRING usObjName, usPathName, usPipe;//, usProcessName;
PFILE_OBJECT pFileObj, pfHandleObj;
POBJECT_NAME_INFORMATION pobjNameInfo;
PDEVICE_OBJECT pDeviceObj;
PDEVICE_EXTENSION pDevExtension;
PEPROCESS peProcess;
KIRQL lockIRQL;
ULONG ulRet;
peProcess = IoGetCurrentProcess();
//DbgPrint("\n\nAttempt to create registry key:\n\t%.16s: %wZ\n", peProcess->ImageFileName, ObjectAttributes->ObjectName);
RtlInitUnicodeString(&usObjName, L"\\DosDevices\\ioCtl");
RtlInitUnicodeString(&usPathName, L"New Key #1");
ntStatus = IoGetDeviceObjectPointer(&usObjName, FILE_ALL_ACCESS, &pFileObj, &pDeviceObj);
if(NT_SUCCESS(ntStatus)){
pDevExtension = (PDEVICE_EXTENSION)pDeviceObj->DeviceExtension;
if( !pDevExtension->pKernelEvent || !pDevExtension->pUserEvent ||
RtlCompareUnicodeString(ObjectAttributes->ObjectName, &usPathName, FALSE) ){
ObDereferenceObject(pFileObj);
ntStatus = ((ZWCREATEKEY)(OldZwCreateKey))
(KeyHandle, DesiredAccess, ObjectAttributes, TitleIndex, Class, CreateOptions, Disposition);
DbgPrint("\nNo match... Key creation allowed!\n");
return ntStatus;
}
KeAcquireSpinLock(&pDevExtension->lockBusy, &lockIRQL);
//----refrence handle to get full key path-----
if(ObjectAttributes->RootDirectory){
ntStatus = ObReferenceObjectByHandle(ObjectAttributes->RootDirectory, FILE_ALL_ACCESS, NULL, KernelMode, &pfHandleObj, NULL);
if(NT_SUCCESS(ntStatus)){
pobjNameInfo = NULL;
ntStatus = ObQueryNameString(pfHandleObj, pobjNameInfo, 0, &ulRet);
if(ntStatus == STATUS_INFO_LENGTH_MISMATCH){
pobjNameInfo = ExAllocatePool(NonPagedPool, ulRet);
if(pobjNameInfo)
ntStatus = ObQueryNameString(pfHandleObj, pobjNameInfo, ulRet, &ulRet);
}
if(NT_SUCCESS(ntStatus)){
//mbstowcs(&wcProcessName, peProcess->ImageFileName, (strlen(peProcess->ImageFileName) + 5));
//RtlInitUnicodeString(&usProcessName, &wcProcessName);
RtlInitUnicodeString(&usPipe, L"|");
pDevExtension->usKeyPath.Length = 0;
pDevExtension->usKeyPath.MaximumLength = (pobjNameInfo->Name.Length +
(usPipe.Length * 3) + ObjectAttributes->ObjectName->Length + peProcess->SeAuditProcessCreationInfo.ImageFileName->Name.MaximumLength);
//DbgPrint("%d %d %d %d\n",
// pobjNameInfo->Name.Length, usPipe.Length, ObjectAttributes->ObjectName->Length, usProcessName.Length);
pDevExtension->usKeyPath.Buffer = ExAllocatePool(NonPagedPool, pDevExtension->usKeyPath.MaximumLength);
if(pDevExtension->usKeyPath.Buffer){
RtlCopyUnicodeString(&pDevExtension->usKeyPath, &pobjNameInfo->Name);
RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, &usPipe);
RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, ObjectAttributes->ObjectName);
RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, &usPipe);
//RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, &usProcessName);
RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, &peProcess->SeAuditProcessCreationInfo.ImageFileName->Name);
RtlAppendUnicodeStringToString(&pDevExtension->usKeyPath, &usPipe);
}
}else
DbgPrint("\nObQueryNameString() failed\n");
if(pobjNameInfo)
ExFreePool(pobjNameInfo);
ObDereferenceObject(pfHandleObj);
}else
DbgPrint("\nObReferenceObjectByHandle failed, ret = 0x%08x\n", ntStatus);
}else{
DbgPrint("\nObjectAttributes->RootDirectory = null\n");
pDevExtension->usKeyPath.Length = 0;
pDevExtension->usKeyPath.MaximumLength = ObjectAttributes->ObjectName->MaximumLength;
pDevExtension->usKeyPath.Buffer = ExAllocatePool(NonPagedPool, pDevExtension->usKeyPath.MaximumLength);
if(pDevExtension->usKeyPath.Buffer)
RtlCopyUnicodeString(&pDevExtension->usKeyPath, ObjectAttributes->ObjectName);
}
//-----------
// wait for usermode app to finish reading data
KeSetEvent(pDevExtension->pKernelEvent, 0, TRUE);
KeWaitForSingleObject(pDevExtension->pUserEvent, Executive, KernelMode, FALSE, NULL);
KeResetEvent(pDevExtension->pKernelEvent);
KeResetEvent(pDevExtension->pUserEvent);
// wait for user response(allow, deny)
KeWaitForSingleObject(pDevExtension->pUserEvent, Executive, KernelMode, FALSE, NULL);
ntStatus = (pDevExtension->bAllowCreateKey == TRUE) ? STATUS_SUCCESS : STATUS_ACCESS_DENIED;
//----cleanup----
if(pDevExtension->usKeyPath.Buffer)
ExFreePool(pDevExtension->usKeyPath.Buffer);
KeReleaseSpinLock(&pDevExtension->lockBusy, lockIRQL);
ObDereferenceObject(pFileObj);
}
if(ntStatus == STATUS_SUCCESS){
ntStatus = ((ZWCREATEKEY)(OldZwCreateKey))
(KeyHandle, DesiredAccess, ObjectAttributes, TitleIndex, Class, CreateOptions, Disposition);
DbgPrint("\nKey creation allowed!\n");
}else
DbgPrint("\nKey creation denied!\n");
return ntStatus;
}
NTSTATUS DriverRead(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PIO_STACK_LOCATION pIoStackIrp = NULL;
PDEVICE_EXTENSION pDevExtension;
ANSI_STRING asString;
PVOID pBuffer;
ULONG dwLength = 0;
DbgPrint("IRP_MJ_READ recevied\n");
pDevExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);
ntStatus = RtlUnicodeStringToAnsiString(&asString, &pDevExtension->usKeyPath, TRUE);
if(NT_SUCCESS(ntStatus)){
dwLength = (asString.Length + 1);
if(pIoStackIrp && Irp->MdlAddress && pIoStackIrp->Parameters.Read.Length >= dwLength){
pBuffer = MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority);
if(pBuffer){
RtlZeroMemory(pBuffer, dwLength);
RtlCopyMemory(pBuffer, asString.Buffer, asString.Length);
ntStatus = STATUS_SUCCESS;
}else{
DbgPrint("MmGetSystemAddressForMdlSafe failed!\n");
ntStatus = STATUS_UNSUCCESSFUL;
}
}else{
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
RtlFreeAnsiString(&asString);
}else{
DbgPrint("RtlUnicodeStringToAnsiString failed!\n");
ntStatus = STATUS_UNSUCCESSFUL;
}
Irp->IoStatus.Information = dwLength;
Irp->IoStatus.Status = ntStatus;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return ntStatus;
}
NTSTATUS DriverIrpUnsupported(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
/*
PIO_STACK_LOCATION pIoStackIrp;
pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);
DbgPrint("\nUnsupported IRP recevied: %02X:%02X\n", pIoStackIrp->MajorFunction, pIoStackIrp->MinorFunction);
*/
return STATUS_NOT_SUPPORTED;
}
NTSTATUS DriverIoCtl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PIO_STACK_LOCATION pIoStackIrp;
PDEVICE_EXTENSION pDevExtension;
DbgPrint("IO Control recevied\n");
pDevExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
pIoStackIrp = IoGetCurrentIrpStackLocation(Irp);
if(pIoStackIrp){
switch(pIoStackIrp->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_SETEVENTS: // obtains handle's to created from user-mode
{
DbgPrint("IOCTL_SETEVENTS received\n");
if(pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength == (sizeof(HANDLE) * 2)){
PHANDLE pHandle = (PHANDLE)Irp->AssociatedIrp.SystemBuffer;
if(pHandle){
ntStatus = ObReferenceObjectByHandle(pHandle[0], 0, NULL,
UserMode, &pDevExtension->pUserEvent, NULL);
if(ntStatus != STATUS_SUCCESS){
DbgPrint("Could not obtain handle to pUserEvent\n");
ntStatus = STATUS_UNSUCCESSFUL;
break;
}
ntStatus = ObReferenceObjectByHandle(pHandle[1], 0, NULL,
UserMode, &pDevExtension->pKernelEvent, NULL);
if(ntStatus != STATUS_SUCCESS){
ObDereferenceObject(pDevExtension->pUserEvent);
DbgPrint("Could not obtain handle to pKernelEvent\n");
ntStatus = STATUS_UNSUCCESSFUL;
break;
}
KeResetEvent(pDevExtension->pKernelEvent);
KeResetEvent(pDevExtension->pUserEvent);
DbgPrint("Event objects obtained successfully\n");
}else DbgPrint("Failed pHandle invalid\n");
}
else DbgPrint("Failed BufferLength: %d\n",
pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength);
break;
}
case IOCTL_QUERYREQUEST:
{
DbgPrint("IOCTL_QUERYREQUEST received\n");
if(pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength == sizeof(BOOLEAN)){
BOOLEAN *bAllow = (BOOLEAN *)Irp->AssociatedIrp.SystemBuffer;
if(bAllow){
DbgPrint("nAllow = %d\n", *bAllow);
pDevExtension->bAllowCreateKey = *bAllow;
KeSetEvent(pDevExtension->pUserEvent, 0, FALSE);
}else{
ntStatus = STATUS_UNSUCCESSFUL;
}
}else{
DbgPrint("IOCTL_QUERYREQUEST Failed, BufferLength: %d should be %d\n",
pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength, sizeof(BOOLEAN));
ntStatus = STATUS_BUFFER_TOO_SMALL;
}
break;
}
case IOCTL_DESTROYEVENTS:
{
DbgPrint("IOCTL_DESTROYEVENTS received\n");
if(pDevExtension->pUserEvent){
ObDereferenceObject(pDevExtension->pUserEvent);
pDevExtension->pUserEvent = NULL;
}
if(pDevExtension->pKernelEvent){
ObDereferenceObject(pDevExtension->pKernelEvent);
pDevExtension->pKernelEvent = NULL;
}
break;
}
}
}
Irp->IoStatus.Information = pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength;
Irp->IoStatus.Status = ntStatus;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return ntStatus;
}
VOID OnUnload(IN PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING usDosDriverName;
PDEVICE_EXTENSION pDevExtension;
DbgPrint("Unloading\n");
pDevExtension = (PDEVICE_EXTENSION)DriverObject->DeviceObject->DeviceExtension;
if(pDevExtension->pUserEvent){
ObDereferenceObject(pDevExtension->pUserEvent);
pDevExtension->pUserEvent = NULL;
}
if(pDevExtension->pKernelEvent){
ObDereferenceObject(pDevExtension->pKernelEvent);
pDevExtension->pKernelEvent = NULL;
}
UNHOOK_SYSCALL(ZwCreateKey, OldZwCreateKey, NewZwCreateKey);
DbgPrint("ZwCreateKey unhooked\n");
//UNHOOK_SYSCALL(ZwTerminateProcess, OldZwTerminateProcess, NewZwTerminateProcess);
//DbgPrint("ZwTerminateProcess unhooked\n");
if(mdlSysCall){
MmUnmapLockedPages(MappedSystemCallTable, mdlSysCall);
IoFreeMdl(mdlSysCall);
}
RtlInitUnicodeString(&usDosDriverName, L"\\DosDevices\\ioCtl");
IoDeleteSymbolicLink(&usDosDriverName);
IoDeleteDevice(DriverObject->DeviceObject);
return;
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
PDEVICE_OBJECT pDevice;
UNICODE_STRING usDriverName, usDosDriverName;
unsigned int i = 0;
DriverObject->DriverUnload = OnUnload;
// make ssdt writable...
mdlSysCall = MmCreateMdl(NULL,
KeServiceDescriptorTable->ServiceTable,
KeServiceDescriptorTable->TableSize * 4);
if(!mdlSysCall){
DbgPrint("MDL could not be created\n");
return STATUS_UNSUCCESSFUL;
}
MmBuildMdlForNonPagedPool(mdlSysCall);
mdlSysCall->MdlFlags = mdlSysCall->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
MappedSystemCallTable = MmMapLockedPages(mdlSysCall, KernelMode);
DbgPrint("SSDT writable\n");
RtlInitUnicodeString(&usDriverName, L"\\Device\\ioCtl");
RtlInitUnicodeString(&usDosDriverName, L"\\DosDevices\\ioCtl");
ntStatus = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &usDriverName,
FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN,
FALSE, &pDevice);
if(NT_SUCCESS(ntStatus)){
DbgPrint("Driver handle created successfully\n");
for(i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
DriverObject->MajorFunction[i] = DriverIrpUnsupported;
RtlZeroMemory(pDevice->DeviceExtension, sizeof(DEVICE_EXTENSION));
KeInitializeSpinLock(&((PDEVICE_EXTENSION)pDevice->DeviceExtension)->lockBusy);
DriverObject->MajorFunction[IRP_MJ_READ] = DriverRead;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DriverIoCtl;
ntStatus = IoCreateSymbolicLink(&usDosDriverName, &usDriverName);
if(!NT_SUCCESS(ntStatus)){
DbgPrint("Could not create Symbolic link");
}
pDevice->Flags &= ~DO_DEVICE_INITIALIZING;
pDevice->Flags |= DO_DIRECT_IO;
OldZwCreateKey = (ZWCREATEKEY)( SYSTEMSERVICE(ZwCreateKey) );
//OldZwTerminateProcess = (ZWTERMINATEPROCESS)( SYSTEMSERVICE(ZwTerminateProcess) );
HOOK_SYSCALL(ZwCreateKey, NewZwCreateKey, OldZwCreateKey);
DbgPrint("ZwCreateKey hooked\n");
//HOOK_SYSCALL(ZwTerminateProcess, NewZwTerminateProcess, OldZwTerminateProcess);
//DbgPrint("ZwTerminateProcess hooked\n");
}
return ntStatus;
}