对于开启了TypeInfo.SupportsObjectCallbacks
属性的驱动来说自然就支持文件路径转换,当系统中有文件被加载则自动执行LySharkFileObjectpreCall
回调事件,过滤掉无效路径后即可直接输出 , 完整代码如下所示;
// 署名权// right to sign one's name on a piece of work// PowerBy: LyShark// Email: me@lyshark.com#include "lyshark.h"PVOID obHandle;DRIVER_INITIALIZE DriverEntry;// 文件回调OB_PREOP_CALLBACK_STATUS LySharkFileObjectpreCall(PVOID RegistrationContext, POB_PRE_OPERATION_INFORMATION OperationInformation){ UNICODE_STRING DosName; PFILE_OBJECT fileo = OperationInformation->Object; HANDLE CurrentProcessId = PsGetCurrentProcessId(); UNREFERENCED_PARAMETER(RegistrationContext); if (OperationInformation->ObjectType != *IoFileObjectType) {return OB_PREOP_SUCCESS; } // 过滤无效指针 if (fileo->FileName.Buffer == NULL ||!MmIsAddressValid(fileo->FileName.Buffer) ||fileo->DeviceObject == NULL ||!MmIsAddressValid(fileo->DeviceObject)) {return OB_PREOP_SUCCESS; } // 过滤无效路径 if (!_wcsicmp(fileo->FileName.Buffer, L"\\Endpoint") ||!_wcsicmp(fileo->FileName.Buffer, L"?") ||!_wcsicmp(fileo->FileName.Buffer, L"\\.\\.") ||!_wcsicmp(fileo->FileName.Buffer, L"\\")) {return OB_PREOP_SUCCESS; } // 将对象转为DOS路径 RtlVolumeDeviceToDosName(fileo->DeviceObject, &DosName); DbgPrint("[LyShark] 进程PID = %ld | 文件路径 = %wZ%wZ \n", (ULONG64)CurrentProcessId, &DosName, &fileo->FileName); return OB_PREOP_SUCCESS;}VOID EnableObType(POBJECT_TYPE ObjectType){ PMY_OBJECT_TYPE myobtype = (PMY_OBJECT_TYPE)ObjectType; myobtype->TypeInfo.SupportsObjectCallbacks = 1;}VOID UnDriver(PDRIVER_OBJECT driver){ UNREFERENCED_PARAMETER(driver); ObUnRegisterCallbacks(obHandle);}NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath){ NTSTATUS status = STATUS_SUCCESS; PLDR_DATA ldr; DbgPrint("hello lyshark.com \n"); OB_CALLBACK_REGISTRATION obRegFileCallBack; OB_OPERATION_REGISTRATION opRegFileCallBack; // enable IoFileObjectType EnableObType(*IoFileObjectType); // bypass MmVerifyCallbackFunction ldr = (PLDR_DATA)Driver->DriverSection; ldr->Flags |= 0x20; // 初始化回调 memset(&obRegFileCallBack, 0, sizeof(obRegFileCallBack)); obRegFileCallBack.Version = ObGetFilterVersion(); obRegFileCallBack.OperationRegistrationCount = 1; obRegFileCallBack.RegistrationContext = NULL; RtlInitUnicodeString(&obRegFileCallBack.Altitude, L"321000"); obRegFileCallBack.OperationRegistration = &opRegFileCallBack; memset(&opRegFileCallBack, 0, sizeof(opRegFileCallBack)); opRegFileCallBack.ObjectType = IoFileObjectType; opRegFileCallBack.Operations = OB_OPERATION_HANDLE_CREATE | OB_OPERATION_HANDLE_DUPLICATE; opRegFileCallBack.PreOperation = (POB_PRE_OPERATION_CALLBACK)&LySharkFileObjectpreCall; status = ObRegisterCallbacks(&obRegFileCallBack, &obHandle); if (!NT_SUCCESS(status)) {DbgPrint("注册回调错误 \n");status = STATUS_UNSUCCESSFUL; } UNREFERENCED_PARAMETER(RegistryPath); Driver->DriverUnload = &UnDriver; return status;}
运行这个驱动程序 , 当系统中有新文件被加载时则自动输出该文件所属进程PID以及该文件的详细路径 。
文章插图
至于如何阻止打开一个文件其实与
《驱动开发:内核注册并监控对象回调》
文章中使用的方法是一致的 , 首先判断OperationInformation->Operation
是不是OB_OPERATION_HANDLE_CREATE
或OB_OPERATION_HANDLE_DUPLICATE
如果是,则直接设置Parameters->CreateHandleInformation.DesiredAccess
为0直接拒绝加载 。// 文件回调OB_PREOP_CALLBACK_STATUS LySharkFileObjectpreCall(PVOID RegistrationContext, POB_PRE_OPERATION_INFORMATION OperationInformation){ UNICODE_STRING DosName; PFILE_OBJECT fileo = OperationInformation->Object; HANDLE CurrentProcessId = PsGetCurrentProcessId(); UNREFERENCED_PARAMETER(RegistrationContext); if (OperationInformation->ObjectType != *IoFileObjectType) {return OB_PREOP_SUCCESS; } // 过滤无效指针 if (fileo->FileName.Buffer == NULL ||!MmIsAddressValid(fileo->FileName.Buffer) ||fileo->DeviceObject == NULL ||!MmIsAddressValid(fileo->DeviceObject)) {return OB_PREOP_SUCCESS; } // 过滤无效路径 if (!_wcsicmp(fileo->FileName.Buffer, L"\\Endpoint") ||!_wcsicmp(fileo->FileName.Buffer, L"?") ||!_wcsicmp(fileo->FileName.Buffer, L"\\.\\.") ||!_wcsicmp(fileo->FileName.Buffer, L"\\")) {return OB_PREOP_SUCCESS; } // 阻止打开lyshark_com.txt文本 if (wcsstr(_wcslwr(fileo->FileName.Buffer), L"lyshark_com.txt")) {if (OperationInformation->Operation == OB_OPERATION_HANDLE_CREATE){OperationInformation->Parameters->CreateHandleInformation.DesiredAccess = 0;}if (OperationInformation->Operation == OB_OPERATION_HANDLE_DUPLICATE){OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess = 0;}DbgPrint("[LyShark] 已拦截 lyshark_com 文件打开 \n"); } return OB_PREOP_SUCCESS;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 19 基于.NetCore开发博客项目 StarBlog - Markdown渲染方案探索
- 四十七 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-添加Axios并实现登录功能
- 【一】ERNIE:飞桨开源开发套件,入门学习,看看行业顶尖持续学习语义理解框架,如何取得世界多个实战的SOTA效果?
- 驱动开发:内核监控Register注册表回调
- 【pytest官方文档】解读-开发可pip安装的第三方插件
- 驱动开发:内核运用LoadImage屏蔽驱动
- 分享几个关于Camera的坑
- 如何开发简单的游戏(自学开发一个游戏app)
- 个人如何开发手机游戏(如何自己开发一款手机游戏)
- 二 京东云开发者| Redis数据结构-List、Hash、Set及Sorted Set的结构实现