寒江独钓这本书第4章讲的是键盘的过滤,我跟着书上面写得代码, 然后跟踪调试, 发现在驱动卸载函数return了之后, 马上就蓝屏了。
这个驱动的功能是绑定kbdclass驱动的所有设备, 然后为所有的IRP_MJ_READ请求设置一个完成函数并使gC2pKeyCount变量++, 然后交给下层驱动处理, 处理完毕之后, 把从键盘扫描到的字符打印出来, 然后gC2pKeyCount --。
驱动卸载函数如下:
void DriverUnload(PDRIVER_OBJECT pDriverObject){
__debugbreak();
PDEVICE_OBJECT pDeviceObject;
PDEVICE_OBJECT oldDeviceObject;
PC2P_DEV_EXT pC2pDevExt;
LARGE_INTEGER lDelay;
PRKTHREAD CurrentThread;
lDelay = RtlConvertLongToLargeInteger(100 * DELAY_ONE_MILLISECOND);
CurrentThread = KeGetCurrentThread();
KeSetPriorityThread(CurrentThread, LOW_REALTIME_PRIORITY);
UNREFERENCED_PARAMETER(pDriverObject);
KdPrint(("Driver is unlaoding......"));
pDeviceObject = pDriverObject->DeviceObject;
while (pDeviceObject)
{
//c2pDetach(pDeviceObject);
pC2pDevExt = (PC2P_DEV_EXT)pDeviceObject->DeviceExtension;
IoDetachDevice(pC2pDevExt->pTargetDeviceObject);
IoDeleteDevice(pDeviceObject);
pC2pDevExt->pFilterDeviceObject = NULL;
pC2pDevExt->pTargetDeviceObject = NULL;
pDeviceObject = pDeviceObject->NextDevice;
}
ASSERT(NULL == pDriverObject->DeviceObject);
while (gC2pKeyCount/*全局变量, 用来记录有多少个请求到来。每到来一个请求就++, 每结束一个请求就--*/)
{
KeDelayExecutionThread(KernelMode, FALSE, &lDelay);
}
KdPrint(("Driver unload OK!"));
return;
//在return了之后, 很快就蓝屏了
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)