通过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内核编程也需要包含 ...
随机推荐
- 关于微信H5页面开发中音乐不自动播放的解决方法
我想应该有很多人在做H5场景应用.H5微刊.H5微杂志的时候加入背景音乐吧(客户需求),相信很多人一定碰过不能自动播放的时候,即使是相同的iPhone 5s也有不播放的时候,很蛋疼吧!? 之前我的解决 ...
- mysql简单命令
库: 增 create database db1:新建一个默认编码的库 create database db1 charset uet8 ;建一个编码为 utf8 的库 删 drop database ...
- django基础知识之认识MVT MVC??
MVT Django是一款python的web开发框架 与MVC有所不同,属于MVT框架 m表示model,负责与数据库交互 v表示view,是核心,负责接收请求.获取数据.返回结果(相当于mvc的c ...
- 数组和datatable间的相互转换[C#]
byte[] LogMsgByte = null; DataTable dtMessageInfo = new DataTable(); //将datatable转换为数组 dtMessageInfo ...
- mongodb连接警告修复
问题 Node.js中mongoose模块连接MongoDB数据库时提示(node:12580) DeprecationWarning: current URL string parser is de ...
- html常用标签、包含关系、常用术语,以及网页设计中的字体分类
编程比较舒适的等宽字体:DejaVu Sans Mono 字体的分类: serif (衬线字体){在笔画上面有些特殊的修饰效果} sans-serif (非衬线字体){横平竖直.横就是横,点就是点} ...
- switch使用--查询水果价格案例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JAVA中自定义properties文件介绍
Gradle中的使用 1. 使用gradle.properties buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同 ...
- $strobe$monitor$display
$strobe:当该时刻的所有事件处理完后,在这个时间步的结尾打印一行格式化的文本,语法$strobe( Argument,...);$fstrobe( Mcd, Argument,...);Mc ...
- bzoj1367 [Baltic2004]sequence 左偏树+贪心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1367 题解 先考虑条件为要求不下降序列(不是递增)的情况. 那么考虑一段数值相同的子段,这一段 ...