最近刚研究INLINE HOOK,可能写的比较小白,代码运行测试加载运行卸载都无蓝屏,但是就是没有跳转到自己的函数里,希望给予代码中错误的指出
#include "ntifs.h"
#include <ntstrsafe.h>
#include <ntddk.h>
#include <string.h>
typedef struct _SERVICE_DESCRIPTOR_TABLE
{
PVOID ServiceTableBase;
PULONG ServiceCounterTableBase;
ULONG NumberOfService;
ULONG ParamTableBase;
}SERVICE_DESCRIPTOR_TABLE,*PSERVICE_DESCRIPTOR_TABLE;
extern PSERVICE_DESCRIPTOR_TABLE KeServiceDescriptorTable;
NTSTATUS NtOpenKey(
__out PHANDLE KeyHandle,
__in ACCESS_MASK DesiredAccess,
__in POBJECT_ATTRIBUTES ObjectAttributes );
VOID UnloadDriver(PDRIVER_OBJECT DriverObject);
//全局
ULONG g_ntopenkey_old;
ULONG g_ntopenkey_save; //保存NT原函数的地址
ULONG g_ntopenkey_jmp; //跳转地址的源地址
UCHAR jmp_code[5]; //函数入口代码
UCHAR jmp_temp[5]; //函数inline代码
void PageProtectOn()
{
__asm{//恢复内存保护
mov eax,cr0
or eax,10000h
mov cr0,eax
sti
}
}
void PageProtectOff()
{
__asm{//去掉内存保护
cli
mov eax,cr0
and eax,not 10000h
mov cr0,eax
}
}
VOID MyFunction()
{
KdPrint(("有程序正在调用NtOpenKey"));
}
__declspec(naked)
NTSTATUS NewOpenKey(
PHANDLE KeyHandle,
ACCESS_MASK DesiredAccess,
POBJECT_ATTRIBUTES ObjectAttributes
){
DbgPrint("Enter my hook NtOpenKey!");
__asm{
pop eax
mov edi,edi
push ebp
mov ebp,esp
jmp [g_ntopenkey_old]
}
}
void HookNtOpenKey(){
//获取SSDT表中的NtOpenProcess函数地址
g_ntopenkey_save = (ULONG)KeServiceDescriptorTable->ServiceTableBase;
g_ntopenkey_save = g_ntopenkey_save + 0xB6 *4;
//保存原函数地址
g_ntopenkey_old = g_ntopenkey_save;
//HOOK处函数地址
g_ntopenkey_jmp = (ULONG)g_ntopenkey_old + 5;
//保存原函数前5个字节
RtlCopyMemory((PVOID)jmp_code,(PVOID)g_ntopenkey_old,5);
//构造跳转指令
jmp_temp[0] = 0xE9;
//计算跳转距离
*(ULONG*)&jmp_temp[1] = (ULONG)NewOpenKey - g_ntopenkey_old - 5;
//修改函数的前5个字节
PageProtectOff();
RtlCopyMemory((PVOID)g_ntopenkey_old,jmp_code,5);
PageProtectOn();
}
VOID Unhook(){
PageProtectOff();
RtlCopyMemory((PVOID)g_ntopenkey_old,(PVOID)g_ntopenkey_old,5);
PageProtectOn();
}
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING RegistryPath)
{
DbgPrint("[RegRoutine]Loading!\n");
pDriverObject->DriverUnload = UnloadDriver;
HookNtOpenKey();
DbgPrint("[RegRoutine]Loading Again!\n");
return STATUS_SUCCESS;
}
VOID UnloadDriver(PDRIVER_OBJECT pDriverObject)
{
Unhook();
DbgPrint("[RegRoutine]UnLoading!\n");
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课