文件防删除保护(miniifiter)

0x01 思路(原理)

  驱动层文件保护的思路是通过minifilter过滤文件删除相关的IRP,并将目标文件与被保护文件相比较,命中保护规则的话返回STATUS_ACCESS_DENIED拒绝访问;也可以进一步利用回调函数获取的FLT_CALLBACK_DATA结构体解析文件删除操作对应的发起者进程,与白名单进程(允许执行删除操作的进程)相比较。

  需要过滤的IRP类型:IRP_MJ_SET_INFORMATION与IRP_MJ_CREATE(或还有其他类型IRP需要过滤,尚未知)

IRP_MJ_SET_INFORMATION的产生原因一般是请求设置文件属性等等,比如ZwSetInformationFile函数可以发起此IRP.这里顺带说一句的是,许多驱动都用ZwSetInformationFile函数来实现强删文件的,比如腾讯电脑管家中的TSSysKit.sys驱动中文件穿透操作的删除文件就是调用ZwSetInformationFile函数将FileInformationClass设为FileDispositionInformation且((PFILE_DISPOSITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer)->DeleteFile设为TRUE来达到删除文件的目的(如果是强删正在运行的文件,那么还需将文件对象的SectionObjectPointer->ImageSectionObject和 SectionObjectPointer->DataSectionObject 两个字段置零)。因此,对于IRP_MJ_SET_INFORMATION,应当检查FileInformationClass是否是FileRenameInformation和FileShortNameInformation标志。

  IRP_MJ_CREATE的产生原因一般是请求打开文件句柄,文件对象以及其他内核对象等等,比如ZwCreateFile函数可以发起此IRP.如果ZwCreateFile函数指定了FILE_DELETE_ON_CLOSE标志,那么当最后一个文件句柄被NtClose关闭时,文件会被删除,这也就是过滤IRP_MJ_CREATE的原因。因此,对于IRP_MJ_CREATE,应当检查是否有FILE_DELETE_ON_CLOSE 标志指定。

0x01 源码及注释如下

 #include <fltKernel.h>
//#include "FileDeleteProtect.h" #define DEBUG
#ifdef DEBUG
#define DBG_PRINTF(_fmt, ...) DbgPrint(_fmt, __VA_ARGS__)
#else
#define DBG_PRINTF(_fmt, ...) { NOTHING; }
#endif DRIVER_INITIALIZE DriverEntry; NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath);
NTSTATUS Unload(_In_ FLT_FILTER_UNLOAD_FLAGS Flags);
FLT_PREOP_CALLBACK_STATUS PreAntiDelete(_Inout_ PFLT_CALLBACK_DATA Data, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext); //过滤IRP的回调数组
CONST FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE, , PreAntiDelete, NULL }, // DELETE_ON_CLOSE 标志.
{ IRP_MJ_SET_INFORMATION, , PreAntiDelete, NULL }, // FileDispositionInformation(Ex).
{ IRP_MJ_OPERATION_END }
}; CONST FLT_REGISTRATION FilterRegistration = {
sizeof(FLT_REGISTRATION), // Size
FLT_REGISTRATION_VERSION, // Version
, // Flags
NULL, // ContextRegistration
Callbacks, // OperationRegistration
Unload, // FilterUnloadCallback
NULL, // InstanceSetupCallback
NULL, // InstanceQueryTeardownCallback
NULL, // InstanceTeardownStartCallback
NULL, // InstanceTeardownCompleteCallback
NULL, // GenerateFileNameCallback
NULL, // NormalizeNameComponentCallback
NULL // NormalizeContextCleanupCallback
}; PFLT_FILTER Filter;
static UNICODE_STRING Protected = RTL_CONSTANT_STRING(L"EXE"); NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) { UNREFERENCED_PARAMETER(RegistryPath); NTSTATUS Status = FltRegisterFilter(DriverObject, &FilterRegistration, &Filter);
if (!NT_SUCCESS(Status)) {
DBG_PRINTF("Failed to register filter: <0x%08x>.\n", Status);
return Status;
} Status = FltStartFiltering(Filter);
if (!NT_SUCCESS(Status)) {
DBG_PRINTF("Failed to start filter: <0x%08x>.\n", Status);
FltUnregisterFilter(Filter);
} return Status;
} NTSTATUS Unload(_In_ FLT_FILTER_UNLOAD_FLAGS Flags) {
UNREFERENCED_PARAMETER(Flags);
DBG_PRINTF("Unload called.\n");
FltUnregisterFilter(Filter); return STATUS_SUCCESS;
} /*
* PreCallback的调用时机
* IRP_MJ_CREATE——ZwCreateFile
* IRP_MJ_SET_INFORMATION——ZwSetInformation.
*/
FLT_PREOP_CALLBACK_STATUS PreAntiDelete(_Inout_ PFLT_CALLBACK_DATA Data, _In_ PCFLT_RELATED_OBJECTS FltObjects, _Flt_CompletionContext_Outptr_ PVOID *CompletionContext) {
UNREFERENCED_PARAMETER(CompletionContext);
PAGED_CODE();//PreCallback的IRQL应当<= APC_LEVEL FLT_PREOP_CALLBACK_STATUS Status = FLT_PREOP_SUCCESS_NO_CALLBACK;//不再调用postoperation callback routine, BOOLEAN IsDirectory;//目录操作跳过,不管
NTSTATUS status = FltIsDirectory(FltObjects->FileObject, FltObjects->Instance, &IsDirectory);
if (NT_SUCCESS(status)) {
if (IsDirectory == TRUE) {
return Status;
}
} if (Data->Iopb->MajorFunction == IRP_MJ_CREATE) {
if (!FlagOn(Data->Iopb->Parameters.Create.Options, FILE_DELETE_ON_CLOSE)) {
return Status;
}
} if (Data->Iopb->MajorFunction == IRP_MJ_SET_INFORMATION) {
switch (Data->Iopb->Parameters.SetFileInformation.FileInformationClass) {
case FileRenameInformation:
case FileRenameInformationEx:
case FileDispositionInformation:
case FileDispositionInformationEx:
case FileRenameInformationBypassAccessCheck:
case FileRenameInformationExBypassAccessCheck:
case FileShortNameInformation:
break;
default:
return Status;
}
} /*
ZwCreateFile和ZwSetInformation函数都是运行在PASSIVE_LEVEL的,所以当前的线程上下文
应当就是ZwCreateFile或ZwSetInformation函数的调用者进程对应的线程的上下文Context,
所以这里可以判断调用者进程是否在白名单中(如果存在白名单进程的话)
if (IoThreadToProcess(Data->Thread) == WhiteListProcess) {
return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
*/ PFLT_FILE_NAME_INFORMATION FileNameInfo = NULL;
if (FltObjects->FileObject != NULL) {
status = FltGetFileNameInformation(Data, FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &FileNameInfo);
if (NT_SUCCESS(status)) {
FltParseFileNameInformation(FileNameInfo);
//if (RtlCompareUnicodeString(&FileNameInfo->Name, &Protected, TRUE) == 0) {
if (RtlCompareUnicodeString(&FileNameInfo->Extension, &Protected, TRUE) == ) {
DBG_PRINTF("Protecting file deletion/rename!");
Data->IoStatus.Status = STATUS_ACCESS_DENIED;
Data->IoStatus.Information = ;
Status = FLT_PREOP_COMPLETE;
}
// Clean up file name information.
FltReleaseFileNameInformation(FileNameInfo);
}
} return Status;
}
上一篇:在Hadoop分布式文件系统的索引和搜索


下一篇:Elasticsearch分布式搜索