安全矩阵

 找回密码
 立即注册
搜索
查看: 2588|回复: 0

ring0下的Inline hook

[复制链接]

251

主题

270

帖子

1797

积分

金牌会员

Rank: 6Rank: 6

积分
1797
发表于 2022-3-4 20:09:03 | 显示全部楼层 |阅读模式
ring0下的Inline hook

首发于先知社区:
https://xz.aliyun.com/t/10913

前言

Inline hook是直接在以前的函数替里面修改指令,用一个跳转或者其他指令来达到挂钩的目的。这是相对普通的hook来说,因为普通的hook只是修改函数的调用地址,而不是在原来的函数体里面做修改。一般来说,普通的hook比较稳定使用。inline hook 更加高级一点,一般也跟难以被发现。ring3的Inline hook在之前已经实现过了,再看看ring0的Inline hook该如何实现。

探究及实现

这里本来调用链应该是OpenFile -> ZwOpenFile,这里在od里面应该可以看到,这里我就用windbg直接找到这个ring0函数。
首先在windbg里面定位到ZwOpenFile函数,可以看到它的偏移为0x74


通过SSDT表找到所有的内核函数地址,再通过0x74偏移定位到ZwOpenFile函数
  1. kd> dd KeServiceDescriptorTable
  2. kd> dd 80505450 + 74 * 4
  3. kd> u 8057b182
复制代码




我这里因为windbg的原因没有显示函数名称,到pchunter里面确认一下,地址确实是一样的



那么我们要实现Inline hook,无论是使用E8 call,还是E9 jmp,都需要至少5个硬编码才能实现,所以这里我们找5个硬编码进行填入我们代码的操作,这里注意不能够找全局变量和重定位的地址,否则在进行还原的过程中可能地址已经发生改变导致程序不能够正常运行

  1. mov ebp,esp
  2. xor eax,eax
  3. push eax
复制代码




首先我们写一个FilterNtOpenFile函数用来打印我们Inline hook后的一些信息,这里用到PsGetCurrentProcess获取进程的EPROCESS结构,在0x174偏移存放着进程名,我们通过打印进程名来查看一下哪些进程调用了NtOpenFile这个函数


​​
  1. char* p = "r0 InlineHook";
  2. void FilterNtOpenFile(char* p)
  3. {
  4.     KdPrint(("%s\r\n", p));
  5.     KdPrint(("name:%s\r\n", (char*)PsGetCurrentProcess() + 0x174));
  6. }
复制代码

然后提供ServiceDescriptorEntry这个结构体并定义KeServiceDescriptorTable为 ntoskrnl.exe 所导出的全局变量
  1. typedef struct ServiceDescriptorEntry {
  2.     unsigned int* ServiceTableBase;
  3.     unsigned int* ServiceCounterTableBase;
  4.     unsigned int NumberOfServices;
  5.     unsigned char* ParamTableBase;
  6. } ServiceDescriptorTableEntry_t, * PServiceDescriptorTableEntry_t;

  7. __declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;
复制代码

这里我们再利用汇编来执行我们的汇编代码之后再jmp到原覆盖地址+5的地方,先用pushad跟pushfd保存寄存器

  1. void _declspec(naked) NewNtOpenFile()
  2. {
  3.     __asm
  4.     {
  5.         pushad
  6.         pushfd
  7.         push p
  8.         call FilterNtOpenFile
  9.         popfd
  10.         popad
  11.         mov ebp, esp
  12.         xor eax, eax
  13.         push eax
  14.         jmp ReAddress
  15.     }
  16. }
复制代码

首先定义一个数组,用来存放E9jmp跳转的代码
  1. UCHAR jmp_code[5] = "";
复制代码

然后因为我们在8057b185这个地址开始覆盖,函数的起始地址为8057b182,所以偏移为3
  1. ULONG ChangeAddr = 3;
复制代码


然后通过KeServiceDescriptorTable的ServiceTableBase属性定位到NtOpenFile的起始地址,这里在PCHunter里面可以看到NtOpenFile的索引号为116


  1. StartAddr = KeServiceDescriptorTable.ServiceTableBase[116];
复制代码

定义返回地址,用函数的开始地址+偏移+5即可得到返回地址
  1. ReAddress = StartAddr + ChangeAddr + 5;
复制代码

通过E9 jmp的计算公式还需要计算我们自己定义的函数newNtOpenKey相对于NtOpenFile的偏移量
  1. ULONG jmpAddr = (ULONG)NewNtOpenFile - StartAddr - ChangeAddr - 5;
复制代码

将跳转代码写入数组
  1. jmp_code[0] = 0xE9;

  2. *(ULONG*)&jmp_code[1] = jmpAddr;
复制代码

这里就需要写入内存了,这里需要关闭页的只读保护,定义一个ShutPageProtect函数将CR0寄存器的WP位置0
  1. //关闭页只读保护
  2. __asm
  3.     {
  4.         push eax;
  5.         mov eax, cr0;
  6.         and eax, ~0x10000;    // 与0x10000想与后取反
  7.         mov cr0, eax;
  8.         pop eax;
  9.         ret;
  10.     }
复制代码

关闭页保护之后首先将之前的硬编码保存,再进行覆盖
  1. ShutPageProtect();
  2. RtlCopyMemory(Old_code, (PVOID)(StartAddr + ChangeAddr), 5);
  3. RtlCopyMemory((PVOID)(StartAddr + ChangeAddr), jmp_code, 5);
复制代码

写入内存完毕之后再定义一个OpenPageProtect函数将CR0寄存器的WP恢复为1
  1. void _declspec(naked) OpenPageProtect()
  2. {
  3.     __asm
  4.     {
  5.         push eax;
  6.         mov eax, cr0;
  7.         or eax, 0x10000;
  8.         mov cr0, eax;
  9.         pop eax;
  10.         ret;
  11.     }
  12. }
复制代码

那么到这里我们的hook代码就已经完成,因为我们已经将原来的硬编码存入了Old_code这个数组,这里我们编写UnHookNtOpenFile时利用RtlCopyMemory写会到原内存即可
  1. void UnHookNtOpenFile()
  2. {
  3.     ULONG ChangeAddr = 3;

  4.     ShutPageProtect();
  5.     RtlCopyMemory((PVOID)(StartAddr + ChangeAddr), Old_code, 5);
  6.     OpenPageProtect();

  7. }
复制代码

再就是加载驱动和卸载驱动,在加载驱动中调用HookNtOpenFile,在卸载驱动中调用UnHookNtOpenFile即可
  1. //卸载驱动
  2. void DriverUnload(DRIVER_OBJECT* obj)
  3. {
  4.     //卸载钩子
  5.     UnHookNtOpenFile();

  6.     KdPrint(("驱动卸载成功!\n"));
  7. }

  8. /***驱动入口主函数***/
  9. NTSTATUS DriverEntry(DRIVER_OBJECT* driver, UNICODE_STRING* path)
  10. {
  11.     KdPrint(("驱动启动成功!\n"));

  12.     //安装钩子
  13.     HookNtOpenFile();

  14.     driver->DriverUnload = DriverUnload;
  15.     return STATUS_SUCCESS;
  16. }
复制代码

完整代码如下
  1. #include <ntddk.h>

  2. typedef struct ServiceDescriptorEntry {
  3.     unsigned int* ServiceTableBase;
  4.     unsigned int* ServiceCounterTableBase;
  5.     unsigned int NumberOfServices;
  6.     unsigned char* ParamTableBase;
  7. } ServiceDescriptorTableEntry_t, * PServiceDescriptorTableEntry_t;

  8. __declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;

  9. // 关闭页只读保护
  10. void ShutPageProtect();
  11. // 开启页只读保护
  12. void OpenPageProtect();
  13. // 测试函数
  14. void FilterNtOpenFile(char* p);
  15. // 新NtOpenFile
  16. void NewNtOpenFile();
  17. // hook NtOpenFile
  18. void HookNtOpenFile();
  19. // unhook NtOpenFile
  20. void UnHookNtOpenFile();


  21. //关闭页只读保护
  22. void _declspec(naked) ShutPageProtect()
  23. {
  24.     __asm
  25.     {
  26.         push eax;
  27.         mov eax, cr0;
  28.         and eax, ~0x10000;
  29.         mov cr0, eax;
  30.         pop eax;
  31.         ret;
  32.     }
  33. }

  34. //开启页只读保护
  35. void _declspec(naked) OpenPageProtect()
  36. {
  37.     __asm
  38.     {
  39.         push eax;
  40.         mov eax, cr0;
  41.         or eax, 0x10000;
  42.         mov cr0, eax;
  43.         pop eax;
  44.         ret;
  45.     }
  46. }

  47. ULONG    StartAddr;
  48. ULONG    ReAddress;
  49. UCHAR    Old_code[5];

  50. char* p = "r0 InlineHook";
  51. void FilterNtOpenFile(char* p)
  52. {
  53.     KdPrint(("%s\r\n", p));
  54.     KdPrint(("name:%s\r\n", (char*)PsGetCurrentProcess() + 0x174));
  55. }

  56. void _declspec(naked) NewNtOpenFile()
  57. {
  58.     __asm
  59.     {
  60.         pushad
  61.         pushfd
  62.         push p
  63.         call FilterNtOpenFile
  64.         popfd
  65.         popad
  66.         mov ebp, esp
  67.         xor eax, eax
  68.         push eax
  69.         jmp ReAddress
  70.     }
  71. }

  72. void HookNtOpenFile()
  73. {
  74.     // 存放跳转指令的数组
  75.     UCHAR jmp_code[5] = "";
  76.     // 在入口0x3处hook
  77.     ULONG ChangeAddr = 3;
  78.     // NtOpenFile函数的开始地址
  79.     StartAddr = KeServiceDescriptorTable.ServiceTableBase[116];
  80.     // 返回地址
  81.     ReAddress = StartAddr + ChangeAddr + 5;
  82.     // newNtOpenKey相对于NtOpenKey的偏移量
  83.     ULONG jmpAddr = (ULONG)NewNtOpenFile - StartAddr - ChangeAddr - 5;

  84.     // 使用jmp指令跳转,jmp = 0xE9
  85.     jmp_code[0] = 0xE9;
  86.     // 填入偏移地址
  87.     *(ULONG*)&jmp_code[1] = jmpAddr;
  88.    
  89.     ShutPageProtect();

  90.     RtlCopyMemory(Old_code, (PVOID)(StartAddr + ChangeAddr), 5);
  91.     RtlCopyMemory((PVOID)(StartAddr + ChangeAddr), jmp_code, 5);

  92.     OpenPageProtect();

  93. }

  94. void UnHookNtOpenFile()
  95. {
  96.     ULONG ChangeAddr = 3;

  97.     ShutPageProtect();
  98.     RtlCopyMemory((PVOID)(StartAddr + ChangeAddr), Old_code, 5);
  99.     OpenPageProtect();

  100. }

  101. //卸载驱动
  102. void DriverUnload(DRIVER_OBJECT* obj)
  103. {
  104.     //卸载钩子
  105.     UnHookNtOpenFile();

  106.     KdPrint(("驱动卸载成功!\n"));
  107. }

  108. /***驱动入口主函数***/
  109. NTSTATUS DriverEntry(DRIVER_OBJECT* driver, UNICODE_STRING* path)
  110. {
  111.     KdPrint(("驱动启动成功!\n"));

  112.     //安装钩子
  113.     HookNtOpenFile();

  114.     driver->DriverUnload = DriverUnload;
  115.     return STATUS_SUCCESS;
  116. }
复制代码


实现效果如下































回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-24 04:46 , Processed in 0.015143 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表