驱动开发:内核运用LoadImage屏蔽驱动

在笔者上一篇文章《驱动开发:内核监视LoadImage映像回调》LyShark简单介绍了如何通过PsSetLoadImageNotifyRoutine函数注册回调来监视驱动模块的加载,注意我这里用的是监视而不是监控之所以是监视而不是监控那是因为PsSetLoadImageNotifyRoutine无法实现参数控制,而如果我们想要控制特定驱动的加载则需要自己做一些事情来实现,如下LyShark将解密如何实现屏蔽特定驱动的加载 。
要想实现驱动屏蔽其原理很简单,通过ImageInfo->ImageBase得到镜像基地址,然后调用GetDriverEntryByImageBase函数来得到程序的入口地址,找NT头的OptionalHeader节点,该节点里面就是被加载驱动入口,通过汇编在驱动头部写入ret返回指令,即可实现屏蔽加载特定驱动文件 。
原理其实很容易理解,如果我们需要实现则只需要在《驱动开发:内核监视LoadImage映像回调》这篇文章的代码上稍加改进即可,当检测到lyshark.sys驱动加载时,直接跳转到入口处快速写入一个Ret让驱动返回即可,至于如何写出指令的问题如果不懂建议回头看看《驱动开发:内核CR3切换读写内存》文章中是如何读写内存的,这段代码实现如下所示 。
// 署名权// right to sign one's name on a piece of work// PowerBy: LyShark// Email: me@lyshark.com#include <ntddk.h>#include <intrin.h>#include <ntimage.h>PVOID GetDriverEntryByImageBase(PVOID ImageBase){ PIMAGE_DOS_HEADER pDOSHeader; PIMAGE_NT_HEADERS64 pNTHeader; PVOID pEntryPoint; pDOSHeader = (PIMAGE_DOS_HEADER)ImageBase; pNTHeader = (PIMAGE_NT_HEADERS64)((ULONG64)ImageBase + pDOSHeader->e_lfanew); pEntryPoint = (PVOID)((ULONG64)ImageBase + pNTHeader->OptionalHeader.AddressOfEntryPoint); return pEntryPoint;}VOID UnicodeToChar(PUNICODE_STRING dst, char *src){ ANSI_STRING string; RtlUnicodeStringToAnsiString(&string, dst, TRUE); strcpy(src, string.Buffer); RtlFreeAnsiString(&string);}// 使用开关写保护需要在[C/C++]->[优化]->启用内部函数// 关闭写保护KIRQLWPOFFx64(){ KIRQLirql = KeRaiseIrqlToDpcLevel(); UINT64cr0 = __readcr0(); cr0 &= 0xfffffffffffeffff; _disable(); __writecr0(cr0); returnirql;}// 开启写保护voidWPONx64(KIRQLirql){ UINT64cr0 = __readcr0(); cr0 |= 0x10000; _enable(); __writecr0(cr0); KeLowerIrql(irql);}BOOLEAN DenyLoadDriver(PVOID DriverEntry){ UCHAR fuck[] = "\xB8\x22\x00\x00\xC0\xC3"; KIRQL kirql; /* 在模块开头写入以下汇编指令 Mov eax,c0000022h ret */ if (DriverEntry == NULL) return FALSE; kirql = WPOFFx64(); memcpy(DriverEntry, fuck, sizeof(fuck) / sizeof(fuck[0])); WPONx64(kirql); return TRUE;}VOID MyLySharkComLoadImageNotifyRoutine(PUNICODE_STRING FullImageName, HANDLE ModuleStyle, PIMAGE_INFO ImageInfo){ PVOID pDrvEntry; char szFullImageName[256] = { 0 }; // MmIsAddress 验证地址可用性 if (FullImageName != NULL && MmIsAddressValid(FullImageName)) {// ModuleStyle为零表示加载sysif (ModuleStyle == 0){pDrvEntry = GetDriverEntryByImageBase(ImageInfo->ImageBase);UnicodeToChar(FullImageName, szFullImageName);if (strstr(_strlwr(szFullImageName), "lyshark.sys")){DbgPrint("[LyShark] 拦截SYS内核模块:%s", szFullImageName);DenyLoadDriver(pDrvEntry);}} }}VOID UnDriver(PDRIVER_OBJECT driver){ PsRemoveLoadImageNotifyRoutine((PLOAD_IMAGE_NOTIFY_ROUTINE)MyLySharkComLoadImageNotifyRoutine); DbgPrint("驱动卸载完成...");}NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath){ DbgPrint("hello lyshark.com \n"); PsSetLoadImageNotifyRoutine((PLOAD_IMAGE_NOTIFY_ROUTINE)MyLySharkComLoadImageNotifyRoutine); DbgPrint("驱动加载完成..."); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS;}首先运行我们的驱动,然后我们接着加载

经验总结扩展阅读