代码如下:
可以保护拓展名.com文件不被删除、重命名、读写、可执行。
#include <ntifs.h>
#include <ntstrsafe.h>
#include <fltKernel.h>
static UNICODE_STRING ProtectedExtention = RTL_CONSTANT_STRING(L"com");
PFLT_FILTER gFileterHandle;
NTSTATUS PtUnload(__in FLT_FILTER_UNLOAD_FLAGS Flags) {
UNREFERENCED_PARAMETER(Flags);
FltUnregisterFilter(gFileterHandle);
return STATUS_SUCCESS;
}
FLT_PREOP_CALLBACK_STATUS NPPreCreate(__inout PFLT_CALLBACK_DATA Data,__in PCFLT_RELATED_OBJECTS FltObjects,__deref_out_opt PVOID *CompletionContext) {
UNREFERENCED_PARAMETER(CompletionContext);
PAGED_CODE();
FLT_PREOP_CALLBACK_STATUS ret = FLT_PREOP_SUCCESS_NO_CALLBACK;
NTSTATUS status;
PFLT_FILE_NAME_INFORMATION FileNameInfo = NULL;
if (Data->Iopb->MajorFunction == IRP_MJ_CREATE) {
if (!FlagOn(Data->Iopb->Parameters.Create.Options, FILE_DISALLOW_EXCLUSIVE)) {
return ret;
}
}
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->Extension, &ProtectedExtention, TRUE) == 0) {
Data->IoStatus.Status = STATUS_ACCESS_DENIED;
Data->IoStatus.Information = 0;
ret = FLT_PREOP_COMPLETE;
}
FltReleaseFileNameInformation(FileNameInfo);
}
}
return ret;
}
CONST FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE, 0, NPPreCreate, NULL },
{ IRP_MJ_SET_INFORMATION, 0, NPPreCreate, NULL },
{ IRP_MJ_OPERATION_END }
};
CONST FLT_REGISTRATION FilterRegistration = {
sizeof(FLT_REGISTRATION),
FLT_REGISTRATION_VERSION,
0,
NULL,
Callbacks,
PtUnload,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
NTSTATUS DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) {
NTSTATUS status;
UNREFERENCED_PARAMETER(RegistryPath);
status = FltRegisterFilter(DriverObject,&FilterRegistration,&gFileterHandle);
if (NT_SUCCESS(status)) {
status = FltStartFiltering(gFileterHandle);
if (!NT_SUCCESS(status)) {
FltUnregisterFilter(gFileterHandle);
}
}
return status;
}
环境说明:
需要在链接器->输入->附加项->添加(fltMgr.lib)