|
作者:Tesla.Angela
虽然说WIN64系统不让搞内核INLINE HOOK了,不过这些技术在“内核越狱”后还是能大显神通的。
但是公开教程的14字节INLINE HOOK太长了,严重限制了它的应用。
比如要实现内核级变速齿轮,要HOOK的一个关键函数是HAL!KeQueryPerformanceCounter。
但问题是,HAL!KeQueryPerformanceCounter在小于14字节的地方,有一个偏移地址。
换句话说,直接使用公开教程的INLINE HOOK引擎挂钩,唯一的结果就是BSOD:
- lkd> u hal!kequeryperformancecounter
- hal!KeQueryPerformanceCounter:
- fffff800`01e0a6f4 48895c2410 mov qword ptr [rsp+10h],rbx
- fffff800`01e0a6f9 57 push rdi
- fffff800`01e0a6fa 4883ec20 sub rsp,20h
- fffff800`01e0a6fe 8b1590ae0100 mov edx,dword ptr [hal!HalpQueryPerformanceCounterSource (fffff800`01e25594)] <--这是偏移地址
- fffff800`01e0a704 488bf9 mov rdi,rcx
- fffff800`01e0a707 83ea01 sub edx,1
- fffff800`01e0a70a 0f8482000000 je hal!KeQueryPerformanceCounter+0x9e (fffff800`01e0a792)
- fffff800`01e0a710 83ea01 sub edx,1
复制代码
为什么一定会BSOD呢?因为8b1590ae0100中的90ae0100是动态变化的,不是一个“死的”机器码(需要重定位)。
如果需要重新计算机器码,这就非常麻烦了。但幸好微软没有把事情做绝。我们看一下KeQueryPerformanceCounter的前8个字节是什么:
- lkd> u hal!kequeryperformancecounter-8
- hal!HalpScaleQueryPerformanceCounter+0x40:
- fffff800`01e0a6ec 90 nop
- fffff800`01e0a6ed 90 nop
- fffff800`01e0a6ee 90 nop
- fffff800`01e0a6ef 90 nop
- fffff800`01e0a6f0 90 nop
- fffff800`01e0a6f1 90 nop
- fffff800`01e0a6f2 90 nop
- fffff800`01e0a6f3 90 nop
复制代码
正好是8个NOP。这8个字节的填充码,正好可以填入一个64位的地址。
于是我们可以把那个公开的INLINE HOOK引擎改一下,把代理函数的地址写到目标函数地址-8的位置,然后把FF2500000000改为FF25F2FFFFFF。
HOOK完的效果如下(只变了前6个字节):
- lkd> u hal!kequeryperformancecounter
- hal!KeQueryPerformanceCounter:
- fffff800`01e0a6f4 ff25f2ffffff jmp qword ptr [hal!HalpScaleQueryPerformanceCounter+0x40 (fffff800`01e0a6ec)]
- fffff800`01e0a6fa 4883ec20 sub rsp,20h
- fffff800`01e0a6fe 8b1590ae0100 mov edx,dword ptr [hal!HalpQueryPerformanceCounterSource (fffff800`01e25594)]
- fffff800`01e0a704 488bf9 mov rdi,rcx
- fffff800`01e0a707 83ea01 sub edx,1
- fffff800`01e0a70a 0f8482000000 je hal!KeQueryPerformanceCounter+0x9e (fffff800`01e0a792)
- fffff800`01e0a710 83ea01 sub edx,1
- fffff800`01e0a713 7455 je hal!KeQueryPerformanceCounter+0x76 (fffff800`01e0a76a)
- lkd> dq hal!kequeryperformancecounter-8
- fffff800`01e0a6ec fffff880`0d29928c 8348ffff`fff225ff
- ...省略无关数据...
复制代码 |
|