ObReferenceObjectByName
一、由于ObReferenceObjectByName没有文档化,故在使用前先做声明:
#ifdef __cplusplus
extern "C"
{
#endif
#include <NTDDK.h>
NTKERNELAPI
NTSTATUS
ObReferenceObjectByName(
IN PUNICODE_STRING ObjectName,
IN ULONG Attributes,
IN PACCESS_STATE PassedAccessState OPTIONAL,
IN ACCESS_MASK DesiredAccess OPTIONAL,
IN POBJECT_TYPE ObjectType,
IN KPROCESSOR_MODE AccessMode,
IN OUT PVOID ParseContext OPTIONAL,
OUT PVOID *Object
);
extern POBJECT_TYPE IoDeviceObjectType;
#ifdef __cplusplus
}
#endif
二、使用
调试ObReferenceObjectByName发现,
(1)可以根据驱动名称可以得到PDRIVER_OBJECT,进而得到该驱动的PDEVICE_OBJECT。
(2)根据设备名称取到PDEVICE_OJBECT为0。
(3)根据符号链接名称取到PDEVICE_OJBECT的地址并不是设备对象地址。
调试代码如下:
UNICODE_STRING DeviceName;
// RtlInitUnicodeString( &DeviceName, L"\\??\\HelloDDKA" );
// RtlInitUnicodeString( &DeviceName, L"\\??\\DriverA" );
RtlInitUnicodeString( &DeviceName, L"\\Driver\\DriverA" );
PDEVICE_OBJECT DeviceObject = NULL;
PFILE_OBJECT FileObject = NULL;
PDRIVER_OBJECT pDriver = NULL;
// ntStatus = ObReferenceObjectByName(&DeviceName,OBJ_CASE_INSENSITIVE,NULL,FILE_ALL_ACCESS,IoDeviceObjectType,KernelMode,NULL,(PVOID*)&DeviceObject);
ntStatus = ObReferenceObjectByName(&DeviceName,OBJ_CASE_INSENSITIVE,NULL,FILE_ALL_ACCESS,IoDeviceObjectType,KernelMode,NULL,(PVOID*)&pDriver);
DeviceObject = pDriver->DeviceObject;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
三、关闭引用
调用上面的ObReferenceObjectByName对象指针,当得到的对象不再使用后,记得调用ObDereferenceObject关闭引用。
(1)如果得到的是驱动对象,则关闭驱动对象引用,如ObDereferenceObject(pDriver)
(2)如果得到是设备对象,则关闭设备对象引用,如ObDereferenceObject(deviceObject)
(3)如果得到的是文件对象,则关闭文件对象引用,如ObDereferenceObject(fileObject)