基本思路其实不复杂,sfilter先创建一个control device object,用于接受应用程序的控制,然后创建多个device object,绑定到文件系统驱动的device object上(譬如NTFS,FAT,CDFS,等等),这里要注意不要绑到文件系统识别器(file system recognizer)上,它是用来加载真正的文件系统驱动程序的。
绑定完文件系统驱动的device object后,sfilter就可以收到发给各个文件系统驱动程序的irp了,这样你可以在windows要求文件系统装配各个卷的时候获得消息,然后你就可以得到文件系统为各个卷所创建的device object,然后再创建device object,绑定到这些volumn object上,下面的工作就是用你的FiDO过滤发往各个卷的irp,取得文件名称及其操作,进行判断,如果是对敏感文件的操作,fail掉该irp,这样就可以了。
第一步:创建CDO,这是在DriverEntry里:
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING pRegistryPath)
{
……
// Create the Control Device Object(CDO). This object represents this
// driver. Note that it does not have a device extension.
RtlInitUnicodeString(&wstrName, L"\FileSystem\Filters\SFilter");
ntStatus = IoCreateDevice(pDriverObject,
0, //has no device extension
&wstrName,
FILE_DEVICE_DISK_FILE_SYSTEM,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&gSFilterControlDeviceObject);
……
}
第二步,绑定各个文件系统的DO,这是在SfAttachToFileSystemDevice里完成的:
(这里有一个小tips,就是windows提供了一个函数IoRegisterFsRegistrationChange,用来注册文件系统的通知,这样当新的文件系统驱动程序load进来的时候你可以得到通知,有机会去绑定它,俺们不放过任何一个文件系统哈哈)
NTSTATUS SfAttachToFileSystemDevice(IN PDEVICE_OBJECT DeviceObject, IN PUNICODE_STRING DeviceName)
{
……
// We want to attach to this file system. Create a new device object we can attach with.
ntStatus = IoCreateDevice(gSFilterDriverObject,
sizeof(SFILTER_DEVICE_EXTENSION),
NULL,
DeviceObject->DeviceType,
0,
FALSE,
&newDeviceObject);
if(!NT_SUCCESS(ntStatus))
{
return ntStatus;
}
……
// Do the attachment
devExt = newDeviceObject->DeviceExtension;
ntStatus = SfAttachDeviceToDeviceStack(newDeviceObject,
DeviceObject,
&devExt->AttachedToDeviceObject);
if(!NT_SUCCESS(ntStatus))
{
goto ErrorCleanupDevice;
}
……
}
第三步:绑定完了文件系统,下面就是坐等irp上门报道了,因为是文件系统,所以我们只对IRP_MJ_FILE_SYSTEM_CONTROL消息感兴趣,对它的处理在SfFsControl里进行:
NTSTATUS SfFsControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
PAGED_CODE();
// Sfilter doesn't allow handles to its control device object to be created,
// therefore, no other operation should be able to come through.
ASSERT(!IS_MY_CONTROL_DEVICE_OBJECT(DeviceObject));
ASSERT(IS_MY_DEVICE_OBJECT(DeviceObject));
// Process the minor function code.
switch(irpSp->MinorFunction)
{
case IRP_MN_MOUNT_VOLUME:
return SfFsControlMountVolume(DeviceObject, Irp); //收到装配卷的请求
case IRP_MN_LOAD_FILE_SYSTEM:
return SfFsControlLoadFileSystem(DeviceObject, Irp);
case IRP_MN_USER_FS_REQUEST:
{
switch(irpSp->Parameters.FileSystemControl.FsControlCode)
{
case FSCTL_DISMOUNT_VOLUME:
{
PSFILTER_DEVICE_EXTENSION devExt = DeviceObject->DeviceExtension;
SF_LOG_PRINT(SFDEBUG_DISPLAY_ATTACHMENT_NAMES,
("SFilter!SfFsControl: Dismounting volume %p \"%wZ\"\n",
devExt->AttachedToDeviceObject,
&devExt->DeviceName));
break;
}
}
break;
}
}
// Pass all other file system control requests through.
IoSkipCurrentIrpStackLocation(Irp);
return IoCallDriver(((PSFILTER_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->AttachedToDeviceObject, Irp);
}
最后一步就是挂接文件系统所装配的所有卷了,这里有两种,一是sfilter挂接到文件系统上去的时候,文件系统已经装配好的卷,还有就是之后装配的卷。后者我们可以在SfFsControlMountVolume里轻松搞定,前者就需要主动去枚举了,这个用SfEnumerateFileSystemVolumes来完成。就不贴代码了,sfilter里有,可以自己找来看。