通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载
- /*
- windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过
- */
- #include <ntddk.h>
- #include "nt_help.h"
- DRIVER_INITIALIZE DriverEntry;
- typedef struct _OBJECT_TYPE_INITIALIZER {
- USHORT Length;
- BOOLEAN UseDefaultObject;
- BOOLEAN CaseInsensitive;
- #if WINVER>=0x0600
- ULONG ObjectTypeCode;
- #endif
- ULONG InvalidAttributes;
- GENERIC_MAPPING GenericMapping;
- ULONG ValidAccessMask;
- BOOLEAN SecurityRequired;
- BOOLEAN MaintainHandleCount;
- BOOLEAN MaintainTypeList;
- POOL_TYPE PoolType;
- ULONG DefaultPagedPoolCharge;
- ULONG DefaultNonPagedPoolCharge;
- PVOID DumpProcedure;
- PVOID OpenProcedure;
- PVOID CloseProcedure;
- PVOID DeleteProcedure;
- PVOID ParseProcedure;
- PVOID SecurityProcedure;
- PVOID QueryNameProcedure;
- PVOID OkayToCloseProcedure;
- } OBJECT_TYPE_INITIALIZER, *POBJECT_TYPE_INITIALIZER;
- typedef struct _OBJECT_TYPE {
- #if WINVER<0x0600
- ERESOURCE Mutex;
- #endif
- LIST_ENTRY TypeList;
- UNICODE_STRING Name; // Copy from object header for convenience
- PVOID DefaultObject;
- ULONG Index;
- ULONG TotalNumberOfObjects;
- ULONG TotalNumberOfHandles;
- ULONG HighWaterNumberOfObjects;
- ULONG HighWaterNumberOfHandles;
- OBJECT_TYPE_INITIALIZER TypeInfo;
- } OBJECT_TYPE, *POBJECT_TYPE;
- extern POBJECT_TYPE* MmSectionObjectType;
- PVOID pNtCreateSection = NULL;
- SYSTEM_MODULE_INFORMATION ntModInfo = {0};
- #pragma alloc_text(INIT, DriverEntry)
- NTSTATUS DevicePassthrough(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
- {
- NTSTATUS status = STATUS_SUCCESS;
- PIO_STACK_LOCATION irpSp;
- irpSp = IoGetCurrentIrpStackLocation(Irp);
- Irp->IoStatus.Status = status;
- IoCompleteRequest(Irp, IO_NO_INCREMENT);
- return status;
- }
- VOID DriverUnload (IN PDRIVER_OBJECT DriverObject)
- {
- (*MmSectionObjectType)->TypeInfo.OpenProcedure = NULL;
- KdPrint(("DriverUnload Done!\n"));
- }
- #if WINVER>=0x0600
- NTSTATUS HookSectionOpen(
- IN ULONG OpenReason,
- IN ULONG AccessMode,
- IN PEPROCESS Process OPTIONAL,
- IN PVOID Object,
- IN ACCESS_MASK* GrantedAccess,
- IN ULONG HandleCount
- )
- #else
- NTSTATUS HookSectionOpen(
- IN ULONG OpenReason,
- IN PEPROCESS Process OPTIONAL,
- IN PVOID Object,
- IN ACCESS_MASK GrantedAccess,
- IN ULONG HandleCount
- )
- #endif
- {
- PVOID* esp = (PVOID*)&esp;
- PVOID* esp_end = (PVOID*)((((DWORD64)esp>>12) + 1)<<12); //4k round up
- PVOID* p = esp;
- ULONG SectionPageProtection, AllocationAttributes;
- HANDLE FileHandle;
- NTSTATUS Status;
- /*
- * do stack walk back to NtCreateSection function
- */
- while (p < esp_end &&
- (*p < pNtCreateSection ||
- *p > (PVOID)((PBYTE)pNtCreateSection + 0x300)))
- p++;
- if (p >= esp_end){
- //KdPrint(("no found NtCreateSection %p -> %p\n", esp, esp_end));
- return STATUS_SUCCESS;
- }
- //KdPrint(("%p HookSectionOpen-Object:%p esp:%p %p\n", pNtCreateSection, Object, esp, *p));
- #ifdef _WIN64
- /*
- * esp layout look likes[2003 X64 DUMP]:
- fffff800`0104113d nt!KiSystemServiceCopyEnd+0x3 retaddr <-------call nt!NtCreateSection
- fffffadf`f662ec00 00000000`00000000 param1
- fffffadf`f662ec08 00000000`000f001f param2 DesiredAccess
- fffffadf`f662ec10 00000000`00000000
- fffffadf`f662ec18 00000000`00000000
- fffffadf`f662ec20 00000100`00000010 SectionPageProtection
- fffffadf`f662ec28 00000000`01000000 AllocationAttributes
- fffffadf`f662ec30 00000000`0000054c FileHandle
- * - ...
- */
- p++;
- /*
- * search retaddr -> nt!KiSystemServiceCopyEnd
- */
- while (p < esp_end &&
- (*p < ntModInfo.ImageBase ||
- *p > (PVOID)((PBYTE)ntModInfo.ImageBase + ntModInfo.ImageSize)))
- p++;
- if (p >= esp_end){
- //KdPrint(("no found nt!KiSystemxxxx %p -> %p\n", esp, esp_end));
- return STATUS_SUCCESS;
- }
- #else
- /* stack DUMP from 2003/x86
- * ebp = p - 1
- fa06f4d8 fa06f540
- fa06f4dc 80908715 nt!NtCreateSection+0x15c
- ...
- fa06f540 fa06f564
- fa06f544 808234cb nt!KiFastCallEntry+0xf8
- fa06f548 fa06f668 param1
- */
- p = (PVOID*)*(p - 1);
- p++;
- #endif
- SectionPageProtection = (ULONG)*(p + 5);
- AllocationAttributes = (ULONG)*(p + 6);
- FileHandle = *(p + 7);
- //KdPrint(("%x %x %p\n", SectionPageProtection, AllocationAttributes, FileHandle));
- if (FileHandle
- && SectionPageProtection == PAGE_EXECUTE
- && (AllocationAttributes == SEC_IMAGE || AllocationAttributes == 0x100000)){
- /* windows7 AllocationAttributes = 0x100000 to LoadDriver */
- PFILE_OBJECT File;
- Status = ObReferenceObjectByHandle (FileHandle,
- 0,
- NULL,
- KernelMode,
- (PVOID *)&File,
- NULL);
- if (!NT_SUCCESS(Status)) {
- return STATUS_SUCCESS;
- }
- KdPrint(("FileName:%wZ\n", &File->FileName));
- ObDereferenceObject(File);
- }
- return STATUS_SUCCESS;
- }
- BOOL GetNtImgBase(PSYSTEM_MODULE_INFORMATION modInfo)
- {
- PSYSMODULELIST sysModuleList = NULL;
- ULONG size, i;
- NtQuerySystemInformation(SystemModuleInformation, &size, 0, &size);
- sysModuleList = ExAllocatePoolWithTag(PagedPool, size, 'hlpm');
- if (sysModuleList){
- NtQuerySystemInformation(SystemModuleInformation, sysModuleList, size, NULL);
- /* nt module should be the first one */
- *modInfo = *sysModuleList->Modules;
- ExFreePool(sysModuleList);
- return TRUE;
- }
- return FALSE;
- }
- NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
- {
- DWORD i;
- UNICODE_STRING sFuncName;
- RtlInitUnicodeString(&sFuncName, L"NtCreateSection");
- pNtCreateSection = MmGetSystemRoutineAddress(&sFuncName);
- if (!GetNtImgBase(&ntModInfo)){
- KdPrint(("EnumSysModule nt base failed!\n"));
- return STATUS_UNSUCCESSFUL;
- }
- KdPrint(("nt:%p pNtCreateSection:%p\nMmSectionObjectType:%p %p %p\n",
- ntModInfo.ImageBase,
- pNtCreateSection,
- *MmSectionObjectType,
- (*MmSectionObjectType)->TypeInfo.OpenProcedure,
- (*MmSectionObjectType)->TypeInfo.DeleteProcedure));
- (*MmSectionObjectType)->TypeInfo.OpenProcedure = HookSectionOpen;
- for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
- DriverObject->MajorFunction[i] = DevicePassthrough;
- DriverObject->DriverUnload = DriverUnload;
- return STATUS_SUCCESS;
- }
通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载的更多相关文章
- 动态链接库dll的 静态加载 与 动态加载
dll 两种链接方式 : 动态链接和静态链接(链接亦称加载) 动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统 ...
- [转载] 动态链接库dll的 静态加载 与 动态加载
转载自:http://blog.csdn.net/youxin2012/article/details/11538491 dll 两种链接方式 : 动态链接和静态链接(链接亦称加载) 动态链接是 ...
- 动态监控驱动、dll、exe加载
/* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include <ntddk.h> #include " ...
- DLL中加载其它DLL使用LoadLibrary加载动态库失败的解决办法
方式一 采用LoadLibraryEx 若DLL不在调用方的同一目录下,可以用LoadLibrary(L"DLL绝对路径")加载.但若调用的DLL内部又调用另外一个DLL,此时调用 ...
- Win64 驱动内核编程-13.回调监控模块加载
回调监控模块加载 模块加载包括用户层模块(.DLL)和内核模块(.SYS)的加载.传统方法要监控这两者加在必须 HOOK 好几个函数,比如 NtCreateSection 和 NtLoadDriver ...
- module_init宏解析 linux驱动的入口函数module_init的加载和释放
linux驱动的入口函数module_init的加载和释放 http://blog.csdn.net/zhandoushi1982/article/details/4927579 void free_ ...
- dll显式加载与隐式加载
使用动态DLL有两种方法,一种是隐式链接,一种是显式链接,如果用loadlibrary就是显示链接,用lib就属于隐式链接. 两种方法对于你的程序调用动态库时没有任何区别,只是你在编程时,步骤是不一样 ...
- 动态符号链接的细节 与 linux程序的加载过程
转: http://hi.baidu.com/clivestudio/item/4341015363058d3d32e0a952 值得玩味的一篇分析程序链接.装载.动态链接细节的好文档 导读: by ...
- linux驱动的入口函数module_init的加载和释放【转】
本文转载自:http://blog.csdn.net/zhandoushi1982/article/details/4927579 就像你写C程序需要包含C库的头文件那样,Linux内核编程也需要包含 ...
随机推荐
- tensorflow学习笔记七----------RNN
和神经网络不同的是,RNN中的数据批次之间是有相互联系的.输入的数据需要是要求序列化的. 1.将数据处理成序列化: 2.将一号数据传入到隐藏层进行处理,在传入到RNN中进行处理,RNN产生两个结果,一 ...
- Springboot 打jar包项目无法访问jsp问题解决方案
maven编译插件,请选择1.4.2.RELEASE版本,1.5.x的版本已经不再支持 pom.xml重要部分如下: <build> <resources> <resou ...
- Docker 启动与停止容器
启动已运行过的容器 docker start 容器名称|容器id 如: docker start mycentos 启动所有运行过的容器(注意:反单引号` `), docker ps -a -q 是查 ...
- Java设计模式之——代理设计模式
1.什么是代理设计模式 所谓代理模式是指客户端并不直接调用实际的对象,而是通过调用代理,来间接的调用实际的对象. 这里举一个栗子:就拿我们平时租房子来举例子,好比租客和房主之间的关系,我们租房子往往不 ...
- github配置及使用
安装git 对于linux系统,不同发行版本的安装方法不一样,请参考https://git-scm.com/download/linux.以ubuntu为例: sudo add-apt-reposit ...
- VB中的SSTab控件隐藏选项卡方法
请教下,VB中Sstab控件,如何隐藏选项卡呢? SSTab1.TabVisible(1) = False object.TabVisible(tab) [ = boolean ] tab 数值表达式 ...
- Stylus-富有表现力的、动态的、健壮的CSS
今天总结一下Stylus记一些常用的也是最基本的用法 一. 选择器 Stylus是基于缩进的这让我们可以更快捷的编写css比如 body { margin:; paddind:; font-size ...
- php实现图片相似搜索
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- 【hackerrank】Weather Observation Station 18
题目如下: Consider and to be two points on a 2D plane. happens to equal the minimum value in Northern ...
- 面试题常考&必考之--http访问一个页面的全流程(Tcp/IP协议)
分析:-http访问一个页面的全流程,也就好比我们在地址栏输入地址,然后点击回车进行访问 该面试题的主要考点是:计算机网络的TCP/IP协议栈 描述图片:首先应用层提交http请求,传到传输层后由,T ...