forked from BestBurning/kmclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmclass.cpp
690 lines (607 loc) · 21.7 KB
/
kmclass.cpp
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
#include "stdafx.h"
#include "kmclass.h"
#include "common.h"
typedef struct _DEVICE_EXTENSION {
PDEVICE_OBJECT kbdDeviceObject; //键盘类设备对象
PDEVICE_OBJECT mouDeviceObject; //鼠标类设备对象
MY_KEYBOARDCALLBACK My_KbdCallback; //KeyboardClassServiceCallback函数
MY_MOUSECALLBACK My_MouCallback; //MouseClassServiceCallback函数
}DEVICE_EXTENSION, * PDEVICE_EXTENSION;
struct
{
PDEVICE_OBJECT KdbDeviceObject;
MY_KEYBOARDCALLBACK KeyboardClassServiceCallback;
PDEVICE_OBJECT MouDeviceObject;
MY_MOUSECALLBACK MouseClassServiceCallback;
}g_KoMCallBack;
void kmclassUnload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS kmclassCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS kmclassDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS kmclassDispatchDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS GetKmclassInfo(PDEVICE_OBJECT DeviceObject, USHORT Index);
NTSTATUS SearchMouServiceCallBack(IN PDRIVER_OBJECT DriverObject);
NTSTATUS SearchServiceFromMouExt(PDRIVER_OBJECT MouDriverObject, PDEVICE_OBJECT pPortDev);
NTSTATUS SearchKdbServiceCallBack(IN PDRIVER_OBJECT DriverObject);
NTSTATUS SearchServiceFromKdbExt(PDRIVER_OBJECT KbdDriverObject, PDEVICE_OBJECT pPortDev);
//////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
extern "C"
#endif
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
UNICODE_STRING DeviceName, Win32Device;
PDEVICE_OBJECT DeviceObject = NULL;
NTSTATUS status;
RtlInitUnicodeString(&DeviceName, KEYMOUSE_DEVICE_NAME);
RtlInitUnicodeString(&Win32Device, KEYMOUSE_DOS_DEVICE_NAME);
for (ULONG i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
DriverObject->MajorFunction[i] = kmclassDefaultHandler;
DriverObject->MajorFunction[IRP_MJ_CREATE] = kmclassCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = kmclassCreateClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = kmclassDispatchDeviceControl;
DriverObject->DriverUnload = kmclassUnload;
status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &DeviceName, FILE_DEVICE_KEYMOUSE, 0, TRUE, &DeviceObject);
if (!NT_SUCCESS(status))
return status;
if (!DeviceObject)
return STATUS_UNEXPECTED_IO_ERROR;
DeviceObject->Flags |= DO_DIRECT_IO;
DeviceObject->AlignmentRequirement = FILE_WORD_ALIGNMENT;
status = IoCreateSymbolicLink(&Win32Device, &DeviceName);
// status = GetKmclassInfo(DeviceObject, KEYBOARD_DEVICE);
status = SearchKdbServiceCallBack(DriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("KEYBOARD_DEVICE ERROR, error = 0x%08lx\n", status));
return status;
}
// status = GetKmclassInfo(DeviceObject, MOUSE_DEVICE);
status = SearchMouServiceCallBack(DriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("MOUSE_DEVICE ERROR, error = 0x%08lx\n", status));
return status;
}
DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
return STATUS_SUCCESS;
}
void kmclassUnload(IN PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING Win32Device;
RtlInitUnicodeString(&Win32Device, KEYMOUSE_DOS_DEVICE_NAME);
IoDeleteSymbolicLink(&Win32Device);
IoDeleteDevice(DriverObject->DeviceObject);
}
NTSTATUS kmclassCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS kmclassDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return Irp->IoStatus.Status;
}
NTSTATUS kmclassDispatchDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION irpStack;
PDEVICE_EXTENSION deviceExtension;
ULONG ioControlCode;
PKEYBOARD_INPUT_DATA KbdInputDataStart, KbdInputDataEnd;
PMOUSE_INPUT_DATA MouseInputDataStart, MouseInputDataEnd;
ULONG InputDataConsumed;
PVOID ioBuffer;
irpStack = IoGetCurrentIrpStackLocation(Irp);
deviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
ioBuffer = Irp->AssociatedIrp.SystemBuffer;
ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
Irp->IoStatus.Information = 0;
switch (ioControlCode)
{
case IOCTL_KEYBOARD:
if (ioBuffer)
{
KEYBOARD_INPUT_DATA kid = *(PKEYBOARD_INPUT_DATA)ioBuffer;
KbdInputDataStart = &kid;
KbdInputDataEnd = KbdInputDataStart + 1;
//deviceExtension->My_KbdCallback(deviceExtension->kbdDeviceObject,
g_KoMCallBack.KeyboardClassServiceCallback(g_KoMCallBack.KdbDeviceObject,
KbdInputDataStart,
KbdInputDataEnd,
&InputDataConsumed);
status = STATUS_SUCCESS;
}
break;
case IOCTL_MOUSE:
if (ioBuffer)
{
MOUSE_INPUT_DATA mid = *(PMOUSE_INPUT_DATA)ioBuffer;
MouseInputDataStart = ∣
MouseInputDataEnd = MouseInputDataStart + 1;
//deviceExtension->My_MouCallback(deviceExtension->mouDeviceObject,
g_KoMCallBack.MouseClassServiceCallback(g_KoMCallBack.MouDeviceObject,
MouseInputDataStart,
MouseInputDataEnd,
&InputDataConsumed);
status = STATUS_SUCCESS;
}
break;
default:
status = STATUS_INVALID_PARAMETER;
KdPrint(("\nunknown IRP_MJ_DEVICE_CONTROL\n"));
break;
}
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
NTSTATUS GetKmclassInfo(PDEVICE_OBJECT DeviceObject, USHORT Index)
{
NTSTATUS status;
UNICODE_STRING ObjectName;
PCWSTR kmhidName, kmclassName, kmName;
PVOID kmDriverStart;
ULONG kmDriverSize;
PVOID* TargetDeviceObject;
PVOID* TargetclassCallback;
PDEVICE_EXTENSION deviceExtension;
PDRIVER_OBJECT kmDriverObject = NULL;
PDRIVER_OBJECT kmclassDriverObject = NULL;
deviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
switch (Index)
{
case KEYBOARD_DEVICE:
kmName = L"kbd";
kmhidName = L"\\Driver\\kbdhid";
kmclassName = L"\\Driver\\kbdclass";
TargetDeviceObject = (PVOID*)&(deviceExtension->kbdDeviceObject);
TargetclassCallback = (PVOID*)&(deviceExtension->My_KbdCallback);
break;
case MOUSE_DEVICE:
kmName = L"mou";
kmhidName = L"\\Driver\\mouhid";
kmclassName = L"\\Driver\\mouclass";
TargetDeviceObject = (PVOID*)&(deviceExtension->mouDeviceObject);
TargetclassCallback = (PVOID*)&(deviceExtension->My_MouCallback);
break;
default:
return STATUS_INVALID_PARAMETER;
}
// 通过USB类设备获取驱动对象
RtlInitUnicodeString(&ObjectName, kmhidName);
status = ObReferenceObjectByName(&ObjectName,
OBJ_CASE_INSENSITIVE,
NULL,
FILE_READ_ACCESS,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&kmDriverObject);
if (!NT_SUCCESS(status))
{
// 通过i8042prt获取驱动对象
RtlInitUnicodeString(&ObjectName, L"\\Driver\\i8042prt");
status = ObReferenceObjectByName(&ObjectName,
OBJ_CASE_INSENSITIVE,
NULL,
FILE_READ_ACCESS,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&kmDriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't Get the i8042prt Driver Object\n"));
return status;
}
}
// 通过kmclass获取键盘鼠标类驱动对象
RtlInitUnicodeString(&ObjectName, kmclassName);
status = ObReferenceObjectByName(&ObjectName,
OBJ_CASE_INSENSITIVE,
NULL,
FILE_READ_ACCESS,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&kmclassDriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't Get the kmclass Driver Object\n"));
return status;
}
else
{
kmDriverStart = kmclassDriverObject->DriverStart;
kmDriverSize = kmclassDriverObject->DriverSize;
}
ULONG DeviceExtensionSize;
PULONG kmDeviceExtension;
PDEVICE_OBJECT kmTempDeviceObject;
PDEVICE_OBJECT kmclassDeviceObject;
PDEVICE_OBJECT kmDeviceObject = kmDriverObject->DeviceObject;
while (kmDeviceObject)
{
kmTempDeviceObject = kmDeviceObject;
while (kmTempDeviceObject)
{
kmDeviceExtension = (PULONG)kmTempDeviceObject->DeviceExtension;
kmclassDeviceObject = kmclassDriverObject->DeviceObject;
DeviceExtensionSize = ((ULONG)kmTempDeviceObject->DeviceObjectExtension - (ULONG)kmTempDeviceObject->DeviceExtension) / 4;
while (kmclassDeviceObject)
{
for (ULONG i = 0; i < DeviceExtensionSize; i++)
{
if (kmDeviceExtension[i] == (ULONG)kmclassDeviceObject &&
kmDeviceExtension[i + 1] > (ULONG)kmDriverStart &&
kmDeviceExtension[i + 1] < (ULONG)kmDriverStart + kmDriverSize)
{
// 将获取到的设备对象保存到自定义扩展设备结构
*TargetDeviceObject = (PVOID)kmDeviceExtension[i];
*TargetclassCallback = (PVOID)kmDeviceExtension[i + 1];
KdPrint(("%SDeviceObject == 0x%x\n", kmName, kmDeviceExtension[i]));
KdPrint(("%SClassServiceCallback == 0x%x\n", kmName, kmDeviceExtension[i + 1]));
return STATUS_SUCCESS;
}
}
kmclassDeviceObject = kmclassDeviceObject->NextDevice;
}
kmTempDeviceObject = kmTempDeviceObject->AttachedDevice;
}
kmDeviceObject = kmDeviceObject->NextDevice;
}
return STATUS_UNSUCCESSFUL;
}
NTSTATUS SearchMouServiceCallBack(IN PDRIVER_OBJECT DriverObject)
{
//定义用到的一组全局变量,这些变量大多数是顾名思义的
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING uniNtNameString;
PDEVICE_OBJECT pTargetDeviceObject = NULL;
PDRIVER_OBJECT KbdDriverObject = NULL;
PDRIVER_OBJECT KbdhidDriverObject = NULL;
PDRIVER_OBJECT Kbd8042DriverObject = NULL;
PDRIVER_OBJECT UsingDriverObject = NULL;
PDEVICE_OBJECT UsingDeviceObject = NULL;
PVOID UsingDeviceExt = NULL;
//这里的代码用来打开USB键盘端口驱动的驱动对象
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\mouhid");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE, NULL, 0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&KbdhidDriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't get the USB Mouse Object\n"));
}
else
{
ObDereferenceObject(KbdhidDriverObject);
KdPrint(("get the USB Mouse Object\n"));
}
//打开PS/2键盘的驱动对象
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\i8042prt");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE,
NULL, 0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&Kbd8042DriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't get the PS/2 Mouse Object %08x\n", status));
}
else
{
ObDereferenceObject(Kbd8042DriverObject);
KdPrint(("get the PS/2 Mouse Object\n"));
}
//如果两个设备都没有找到
if (!Kbd8042DriverObject && !KbdhidDriverObject)
{
return STATUS_SUCCESS;
}
//如果USB键盘和PS/2键盘同时存在,使用USB鼠标
if (KbdhidDriverObject)
{
UsingDriverObject = KbdhidDriverObject;
}
else
{
UsingDriverObject = Kbd8042DriverObject;
}
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\mouclass");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE, NULL,
0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&KbdDriverObject);
if (!NT_SUCCESS(status))
{
//如果没有成功,直接返回即可
KdPrint(("MyAttach: Coundn't get the Mouse driver Object\n"));
return STATUS_UNSUCCESSFUL;
}
else
{
ObDereferenceObject(KbdDriverObject);
}
//遍历KbdDriverObject下的设备对象
UsingDeviceObject = UsingDriverObject->DeviceObject;
while (UsingDeviceObject)
{
status = SearchServiceFromMouExt(KbdDriverObject, UsingDeviceObject);
if (status == STATUS_SUCCESS)
{
break;
}
UsingDeviceObject = UsingDeviceObject->NextDevice;
}
if (g_KoMCallBack.MouDeviceObject && g_KoMCallBack.MouseClassServiceCallback)
{
KdPrint(("Find MouseClassServiceCallback\n"));
}
return status;
}
NTSTATUS SearchServiceFromMouExt(PDRIVER_OBJECT MouDriverObject, PDEVICE_OBJECT pPortDev)
{
PDEVICE_OBJECT pTargetDeviceObject = NULL;
UCHAR* DeviceExt;
int i = 0;
NTSTATUS status;
PVOID KbdDriverStart;
ULONG KbdDriverSize = 0;
PDEVICE_OBJECT pTmpDev;
UNICODE_STRING kbdDriName;
KbdDriverStart = MouDriverObject->DriverStart;
KbdDriverSize = MouDriverObject->DriverSize;
status = STATUS_UNSUCCESSFUL;
RtlInitUnicodeString(&kbdDriName, L"\\Driver\\mouclass");
pTmpDev = pPortDev;
while (pTmpDev->AttachedDevice != NULL)
{
KdPrint(("Att: 0x%x", pTmpDev->AttachedDevice));
KdPrint(("Dri Name : %wZ", &pTmpDev->AttachedDevice->DriverObject->DriverName));
if (RtlCompareUnicodeString(&pTmpDev->AttachedDevice->DriverObject->DriverName,
&kbdDriName, TRUE) == 0)
{
KdPrint(("Find Object Device: "));
break;
}
pTmpDev = pTmpDev->AttachedDevice;
}
if (pTmpDev->AttachedDevice == NULL)
{
return status;
}
pTargetDeviceObject = MouDriverObject->DeviceObject;
while (pTargetDeviceObject)
{
if (pTmpDev->AttachedDevice != pTargetDeviceObject)
{
pTargetDeviceObject = pTargetDeviceObject->NextDevice;
continue;
}
DeviceExt = (UCHAR*)pTmpDev->DeviceExtension;
g_KoMCallBack.MouDeviceObject = NULL;
//遍历我们先找到的端口驱动的设备扩展的每一个指针
for (i = 0; i < 4096; i++, DeviceExt++)
{
PVOID tmp;
if (!MmIsAddressValid(DeviceExt))
{
break;
}
//找到后会填写到这个全局变量中,这里检查是否已经填好了
//如果已经填好了就不用继续找了,可以直接退出
if (g_KoMCallBack.MouDeviceObject && g_KoMCallBack.MouseClassServiceCallback)
{
status = STATUS_SUCCESS;
break;
}
//在端口驱动的设备扩展里,找到了类驱动设备对象,填好类驱动设备对象后继续
tmp = *(PVOID*)DeviceExt;
if (tmp == pTargetDeviceObject)
{
g_KoMCallBack.MouDeviceObject = pTargetDeviceObject;
continue;
}
//如果在设备扩展中找到一个地址位于KbdClass这个驱动中,就可以认为,这就是我们要找的回调函数
if ((tmp > KbdDriverStart) && (tmp < (UCHAR*)KbdDriverStart + KbdDriverSize) &&
(MmIsAddressValid(tmp)))
{
//将这个回调函数记录下来
g_KoMCallBack.MouseClassServiceCallback = (MY_MOUSECALLBACK)tmp;
//g_KoMCallBack.MouSerCallAddr = (PVOID *)DeviceExt;
status = STATUS_SUCCESS;
}
}
if (status == STATUS_SUCCESS)
{
break;
}
//换成下一个设备,继续遍历
pTargetDeviceObject = pTargetDeviceObject->NextDevice;
}
return status;
}
NTSTATUS SearchServiceFromKdbExt(PDRIVER_OBJECT KbdDriverObject, PDEVICE_OBJECT pPortDev)
{
PDEVICE_OBJECT pTargetDeviceObject = NULL;
UCHAR* DeviceExt;
int i = 0;
NTSTATUS status;
PVOID KbdDriverStart;
ULONG KbdDriverSize = 0;
PDEVICE_OBJECT pTmpDev;
UNICODE_STRING kbdDriName;
KbdDriverStart = KbdDriverObject->DriverStart;
KbdDriverSize = KbdDriverObject->DriverSize;
status = STATUS_UNSUCCESSFUL;
RtlInitUnicodeString(&kbdDriName, L"\\Driver\\kbdclass");
pTmpDev = pPortDev;
while (pTmpDev->AttachedDevice != NULL)
{
KdPrint(("Att: 0x%x", pTmpDev->AttachedDevice));
KdPrint(("Dri Name : %wZ", &pTmpDev->AttachedDevice->DriverObject->DriverName));
if (RtlCompareUnicodeString(&pTmpDev->AttachedDevice->DriverObject->DriverName,
&kbdDriName, TRUE) == 0)
{
break;
}
pTmpDev = pTmpDev->AttachedDevice;
}
if (pTmpDev->AttachedDevice == NULL)
{
return status;
}
pTargetDeviceObject = KbdDriverObject->DeviceObject;
while (pTargetDeviceObject)
{
if (pTmpDev->AttachedDevice != pTargetDeviceObject)
{
pTargetDeviceObject = pTargetDeviceObject->NextDevice;
continue;
}
DeviceExt = (UCHAR*)pTmpDev->DeviceExtension;
g_KoMCallBack.KdbDeviceObject = NULL;
//遍历我们先找到的端口驱动的设备扩展的每一个指针
for (i = 0; i < 4096; i++, DeviceExt++)
{
PVOID tmp;
if (!MmIsAddressValid(DeviceExt))
{
break;
}
//找到后会填写到这个全局变量中,这里检查是否已经填好了
//如果已经填好了就不用继续找了,可以直接退出
if (g_KoMCallBack.KdbDeviceObject && g_KoMCallBack.KeyboardClassServiceCallback)
{
status = STATUS_SUCCESS;
break;
}
//在端口驱动的设备扩展里,找到了类驱动设备对象,填好类驱动设备对象后继续
tmp = *(PVOID*)DeviceExt;
if (tmp == pTargetDeviceObject)
{
g_KoMCallBack.KdbDeviceObject = pTargetDeviceObject;
continue;
}
//如果在设备扩展中找到一个地址位于KbdClass这个驱动中,就可以认为,这就是我们要找的回调函数
if ((tmp > KbdDriverStart) && (tmp < (UCHAR*)KbdDriverStart + KbdDriverSize) &&
(MmIsAddressValid(tmp)))
{
//将这个回调函数记录下来
g_KoMCallBack.KeyboardClassServiceCallback = (MY_KEYBOARDCALLBACK)tmp;
}
}
if (status == STATUS_SUCCESS)
{
break;
}
//换成下一个设备,继续遍历
pTargetDeviceObject = pTargetDeviceObject->NextDevice;
}
return status;
}
NTSTATUS SearchKdbServiceCallBack(IN PDRIVER_OBJECT DriverObject)
{
//定义用到的一组全局变量,这些变量大多数是顾名思义的
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING uniNtNameString;
PDEVICE_OBJECT pTargetDeviceObject = NULL;
PDRIVER_OBJECT KbdDriverObject = NULL;
PDRIVER_OBJECT KbdhidDriverObject = NULL;
PDRIVER_OBJECT Kbd8042DriverObject = NULL;
PDRIVER_OBJECT UsingDriverObject = NULL;
PDEVICE_OBJECT UsingDeviceObject = NULL;
PVOID UsingDeviceExt = NULL;
//这里的代码用来打开USB键盘端口驱动的驱动对象
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\kbdhid");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE, NULL, 0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&KbdhidDriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't get the USB driver Object\n"));
}
else
{
ObDereferenceObject(KbdhidDriverObject);
KdPrint(("get the USB driver Object\n"));
}
//打开PS/2键盘的驱动对象
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\i8042prt");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE,
NULL, 0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&Kbd8042DriverObject);
if (!NT_SUCCESS(status))
{
KdPrint(("Couldn't get the PS/2 driver Object %08x\n", status));
}
else
{
ObDereferenceObject(Kbd8042DriverObject);
KdPrint(("get the PS/2 driver Object\n"));
}
//这段代码考虑有一个键盘起作用的情况。如果USB键盘和PS/2键盘同时存在,用PS/2键盘
//如果两个设备都没有找到
if (!Kbd8042DriverObject && !KbdhidDriverObject)
{
return STATUS_SUCCESS;
}
//找到合适的驱动对象,不管是USB还是PS/2,反正一定要找到一个
UsingDriverObject = Kbd8042DriverObject ? Kbd8042DriverObject : KbdhidDriverObject;
RtlInitUnicodeString(&uniNtNameString, L"\\Driver\\kbdclass");
status = ObReferenceObjectByName(&uniNtNameString,
OBJ_CASE_INSENSITIVE, NULL,
0,
*IoDriverObjectType,
KernelMode,
NULL,
(PVOID*)&KbdDriverObject);
if (!NT_SUCCESS(status))
{
//如果没有成功,直接返回即可
KdPrint(("MyAttach: Coundn't get the kbd driver Object\n"));
return STATUS_UNSUCCESSFUL;
}
else
{
ObDereferenceObject(KbdDriverObject);
}
//遍历KbdDriverObject下的设备对象
UsingDeviceObject = UsingDriverObject->DeviceObject;
while (UsingDeviceObject)
{
status = SearchServiceFromKdbExt(KbdDriverObject, UsingDeviceObject);
if (status == STATUS_SUCCESS)
{
break;
}
UsingDeviceObject = UsingDeviceObject->NextDevice;
}
//如果成功找到了,就把这个函数替换成我们自己的回调函数
if (g_KoMCallBack.KdbDeviceObject && g_KoMCallBack.KeyboardClassServiceCallback)
{
KdPrint(("Find keyboradClassServiceCallback\n"));
}
return status;
}