/*++
Module Name:
MiniFilter.c
Abstract:
This is the main module of the MiniFilter miniFilter driver.
Environment:
Kernel mode
--*/
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#pragma prefast(disable:__WARNING_ENCODE_MEMBER_FUNCTION_POINTER, "Not valid for kernel mode drivers")
#define MINISPY_PORT_NAME L"\\MiniPort"
PFLT_FILTER gFilterHandle;
PFLT_PORT gServerPort;
PFLT_PORT gClientPort;
ULONG_PTR OperationStatusCtx = 1;
#define PTDBG_TRACE_ROUTINES 0x00000001
#define PTDBG_TRACE_OPERATION_STATUS 0x00000002
ULONG gTraceFlags = 0;
// Defines the commands between the utility and the filter
typedef enum _MINI_COMMAND {
ENUM_PASS = 0,
ENUM_BLOCK
} MINI_COMMAND;
// Defines the command structure between the utility and the filter.
typedef struct _COMMAND_MESSAGE {
MINI_COMMAND Command;
} COMMAND_MESSAGE, *PCOMMAND_MESSAGE;
MINI_COMMAND gCommand = ENUM_PASS;
#define PT_DBG_PRINT( _dbgLevel, _string ) \
(FlagOn(gTraceFlags,(_dbgLevel)) ? \
DbgPrint _string : \
((int)0))
/*************************************************************************
Prototypes
*************************************************************************/
BOOLEAN NPUnicodeStringToChar(PUNICODE_STRING UniName, char Name[])
{
ANSI_STRING AnsiName;
NTSTATUS ntstatus;
char* nameptr;
__try {
ntstatus = RtlUnicodeStringToAnsiString(&AnsiName, UniName, TRUE);
if (AnsiName.Length < 260) {
nameptr = (PCHAR)AnsiName.Buffer;
//Convert into upper case and copy to buffer
//strcpy(Name, _strupr(nameptr)); //将字符串转换成大写形式
strcpy(Name,_strlwr(nameptr));//讲字符串转换成小写形式
DbgPrint("NPUnicodeStringToChar : %s\n", Name);
}
RtlFreeAnsiString(&AnsiName);
}
__except(EXCEPTION_EXECUTE_HANDLER) {
DbgPrint("NPUnicodeStringToChar EXCEPTION_EXECUTE_HANDLER\n");
return FALSE;
}
return TRUE;
}
//获取进程全路径
PUNICODE_STRING GetSeLocateProcessImageName(PEPROCESS Process,PUNICODE_STRING *pImageFileName)
{
POBJECT_NAME_INFORMATION pProcessImageName = NULL;
PUNICODE_STRING pTempUS = NULL;
ULONG NameLength = 0;
//Process->SeAuditProcessCreationInfo.ImageFileName->Name
//win7 x86 offset = 0x1ec
//if (NULL == Process->SeAuditProcessCreationInfo.ImageFileName)
pProcessImageName = (POBJECT_NAME_INFORMATION)(*(ULONG*)((ULONG)Process + 0x1ec));
if(pProcessImageName == NULL)
{
DbgPrint("Process->SeAuditProcessCreationInfo.ImageFileName == NULL \n");
return NULL;
}
else
{
NameLength = sizeof(UNICODE_STRING) + pProcessImageName->Name.MaximumLength;
pTempUS = ExAllocatePoolWithTag( NonPagedPool, NameLength, 'aPeS' );
if (NULL != pTempUS) {
RtlCopyMemory(
pTempUS,
&pProcessImageName->Name,
NameLength
);
pTempUS->Buffer = (PWSTR)(((PUCHAR) pTempUS) + sizeof(UNICODE_STRING));
*pImageFileName = pTempUS;
DbgPrint("Path:%wZ\n",&pProcessImageName->Name);
return *pImageFileName;
}
return NULL;
}
}
PFILE_OBJECT __declspec(naked) __stdcall _MmGetFileObjectForSection(PVOID Section)
{
__asm
{
push ebp;
mov ebp, esp;
mov eax, dword ptr ss:[ebp + 0x08];
mov eax, dword ptr ds:[eax + 0x14];
mov eax, dword ptr ds:[eax];
mov eax, dword ptr ds:[eax + 0x24];
mov esp, ebp;
pop ebp;
ret 0x04;
}
}
NTSTATUS PsReferenceProcessFilePointer(IN PEPROCESS Process, OUT PVOID *OutFileObject)
{
PVOID SectionObject;
if (SectionObject = *(PVOID*)((PCHAR)Process + 0x138))
{
PFILE_OBJECT FileObject;
FileObject = _MmGetFileObjectForSection(SectionObject);
*OutFileObject = FileObject;
ObReferenceObject (FileObject);
return STATUS_SUCCESS;
}
return STATUS_UNSUCCESSFUL;
}
PUNICODE_STRING PsGetProcessFullName(PEPROCESS pTargetProcess)
{
PFILE_OBJECT pFileObject=NULL;
POBJECT_NAME_INFORMATION pObjectNameInfo=NULL;
if(!NT_SUCCESS(PsReferenceProcessFilePointer(pTargetProcess,&pFileObject)))
return NULL;
if(!NT_SUCCESS(IoQueryFileDosDeviceName(pFileObject,&pObjectNameInfo)))
return NULL;
ObDereferenceObject(pFileObject);
return &(pObjectNameInfo->Name);//尚未释放内存 以及 ObDereferenceObject
}
DRIVER_INITIALIZE DriverEntry;
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
NTSTATUS
MiniFilterInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
);
VOID
MiniFilterInstanceTeardownStart (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
);
VOID
MiniFilterInstanceTeardownComplete (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_TEARDOWN_FLAGS Flags
);
NTSTATUS
MiniFilterUnload (
_In_ FLT_FILTER_UNLOAD_FLAGS Flags
);
NTSTATUS
MiniFilterInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
);
FLT_PREOP_CALLBACK_STATUS
MiniFilterPreOperation (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
VOID
MiniFilterOperationStatusCallback (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ PFLT_IO_PARAMETER_BLOCK ParameterSnapshot,
_In_ NTSTATUS OperationStatus,
_In_ PVOID RequesterContext
);
FLT_POSTOP_CALLBACK_STATUS
MiniFilterPostOperation (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_opt_ PVOID CompletionContext,
_In_ FLT_POST_OPERATION_FLAGS Flags
);
FLT_PREOP_CALLBACK_STATUS
MiniFilterPreOperationNoPostOperation (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
BOOLEAN
MiniFilterDoRequestOperationStatus(
_In_ PFLT_CALLBACK_DATA Data
);
//
// Assign text sections for each routine.
//用户态和内核态建立连接
NTSTATUS
MiniConnect(
__in PFLT_PORT ClientPort,
__in PVOID ServerPortCookie,
__in_bcount(SizeOfContext) PVOID ConnectionContext,
__in ULONG SizeOfContext,
__deref_out_opt PVOID *ConnectionCookie
);
//用户态和内核断开连接
VOID
MiniDisconnect(
__in_opt PVOID ConnectionCookie
);
//用户态和内核态传送数据
NTSTATUS
MiniMessage (
__in PVOID ConnectionCookie,
__in_bcount_opt(InputBufferSize) PVOID InputBuffer,
__in ULONG InputBufferSize,
__out_bcount_part_opt(OutputBufferSize,*ReturnOutputBufferLength) PVOID OutputBuffer,
__in ULONG OutputBufferSize,
__out PULONG ReturnOutputBufferLength
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, MiniFilterUnload)
#pragma alloc_text(PAGE, MiniFilterInstanceQueryTeardown)
#pragma alloc_text(PAGE, MiniFilterInstanceSetup)
#pragma alloc_text(PAGE, MiniFilterInstanceTeardownStart)
#pragma alloc_text(PAGE, MiniFilterInstanceTeardownComplete)
#endif
//
// operation registration
//
CONST FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE,
0,
MiniFilterPreOperation,
MiniFilterPostOperation },
#if 0 // TODO - List all of the requests to filter.
{ IRP_MJ_CREATE_NAMED_PIPE,
0,
MiniFilterPreOperation,
MiniFilterPostOperation },
{ IRP_MJ_CLOSE,
0,
MiniFilterPreOperation,
MiniFilterPostOperation },
{ IRP_MJ_READ,
0,
MiniFilterPreOperation,
MiniFilterPostOperation },
{ IRP_MJ_WRITE,
0,
MiniFilterPreOperation,
MiniFilterPostOperation },
{ IRP_M
nanj
- 粉丝: 31
- 资源: 177
最新资源
- 计算机二级考试全面备考指南与学习心得
- 树木的信息数据集(德国罗斯托克地区树木的信息)
- Python爬虫基础知识与实践指南
- 连接ESP32手表来做验证20241223-140953.pcapng
- 有源电力滤波器,APF,有源电力滤波器仿真,电力电子仿真,无差拿控制,谐波补偿 提供参考文献
- 某平台广告投入分析与销售预测
- 国际象棋桌子检测6-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord数据集合集.rar
- 永磁同步电机参数辨识模型,在线辨识,离线辨识,电参数机械参数均可辨识,基于最小二乘法,滑模观测,电压注入,模型参考自适应等 机械参数在线 离线 ,电气参数在线 (三种方法,最小二乘和mras以及卡尔
- dbeaver-ce-24.3.1-x86-64-setup.exe
- 基于粒子群的ieee30节点优化、配电网有功-无功优化 软件:Matlab+Matpowre 介绍:对配电网中有功-无功协调优化调度展开研究,通过对光伏电源、储能装置、无功电源和变压器分接头等设备协调
- 基于ssm的高校教务管理系统设计与实现
- VirtualGL-2.6.5.x86-64.rpm
- 艾利和iriver Astell&Kern SP3000 V1.30升级固件
- turbovnc-2.2.6.x86-64.rpm
- Labview Modbus-Tcp和西门子全糸列pLC通讯所有数据类型均能读写,速度快,使用在多个项目上,运行稳定,可以扩展到其它品牌PLc,上位机程序一样,只是PLC程序稍微变动一下,上下位机源
- 国际象棋检测10-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0