首页
社区
课程
招聘
[求助]关于inline hook sstd表的请教!
发表于: 2014-5-26 16:01 3373

[求助]关于inline hook sstd表的请教!

2014-5-26 16:01
3373
最近刚研究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");
}
收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 1453
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
帮楼主顶一下
2014-5-26 18:27
0
雪    币: 195
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
/计算跳转距离
    *(ULONG*)&jmp_temp[1] = (ULONG)NewOpenKey - g_ntopenkey_old - 5;
  //修改函数的前5个字节
  PageProtectOff();
  RtlCopyMemory((PVOID)g_ntopenkey_old,jmp_code,5); //这里应该写入jmp_temp[]的内容吧,你这样等于没改写。等于把原函数的前5个字节又写回去了,应该用  jmp_temp的内容覆盖原函数前5个字节才对       。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
  PageProtectOn();
2014-5-27 10:20
0
游客
登录 | 注册 方可回帖
返回
//