pg3 bypass源码阅读 —— 学习x64内核hook跳板技术

时间:2023-03-08 21:23:25

如之前描述的 pg3复杂了许多

先来看看都要hook哪些点

1、hook dpc和定时器分发器,防止seh路线触发pg

KiTimerListExpire,KiRetireDpcList

看一下hook点

pg3 bypass源码阅读 —— 学习x64内核hook跳板技术

hook的就是call的位置。

这里有两种方案:一种是直接jmp + 64bit addr,显然有同步问题,需要暂停所有cpu然后把irql提升到HIGH_LEVEL去操作。

另一种是 call 32bit 跳板 addr,如下图,操作8byte符合原子操作

pg3 bypass源码阅读 —— 学习x64内核hook跳板技术

e8 xxxxxxxx是32位转移,我们需要一个nt范围内的跳板,作者是这样处理的。把KiCustomAccessRoutine4跳转到KiCustomAccessRoutine0,那么KiCustomAccessRoutine4后面的代码就可以随便改了,不需要原子操作,这是一个技巧。

pg3 bypass源码阅读 —— 学习x64内核hook跳板技术

  1. void VistaAll_DpcInterceptor(
  2. PKDPC InDpc,
  3. PVOID InDeferredContext,
  4. PVOID InSystemArgument1,
  5. PVOID InSystemArgument2)
  6. {
  7. ULONGLONG Routine = (ULONGLONG)InDpc->DeferredRoutine;
  8. __try
  9. {
  10. if((Routine >= 0xFFFFFA8000000000) && (Routine <= 0xFFFFFAA000000000))
  11. {
  12. }
  13. else
  14. if(KeContainsSymbol((void*)Routine))
  15. {
  16. if(!PgIsPatchGuardContext(InDeferredContext))
  17. InDpc->DeferredRoutine(InDpc, InDeferredContext, InSystemArgument1, InSystemArgument2);
  18. }
  19. else
  20. InDpc->DeferredRoutine(InDpc, InDeferredContext, InSystemArgument1, InSystemArgument2);
  21. }
  22. __except(EXCEPTION_EXECUTE_HANDLER)
  23. {
  24. }
  25. }

fake dpc的处理非常简单,判断dpc context即可

2.hook ExpWorkerThread 工作线程也有可能触发pg,hook方法同上,fake函数如下

  1. VOID VistaAll_ExpWorkerThreadInterceptor(PWORKER_THREAD_ROUTINE InRoutine, VOID* InContext, VOID* InRSP)
  2. {
  3. ULONGLONG Val = (ULONGLONG)InRoutine;
  4. if((Val >= 0xfffffa8000000000) && (Val <= 0xfffffaa000000000))
  5. return;
  6. __try
  7. {
  8. InRoutine(InContext);
  9. }
  10. __except(EXCEPTION_EXECUTE_HANDLER)
  11. {
  12. }
  13. }

过滤了所有内核的work thread,工作线程是non-seh mode,无法过滤非传统地址,所以过滤了所有的nt工作线程。。总是系统跑起来之后也不会再排新的工作线程就是了。

3.这样还不够,hook KeBugcheckEx作为补充,KeBugcheckEx是被PG循环恢复的,但是分析代码KeBugcheckEx一开始就调用到RtlCaptureContext,所以转去hook RtlCaptureContext,还是用跳板函数,用到了栈回溯

  1. RtlCaptureContext_Hook PROC
  2. ; call high level handler without messing up the context structure...
  3. push rcx
  4. push rdx
  5. push r8
  6. push r9
  7. push r10
  8. push r11
  9. mov rcx, qword ptr[rsp + 128]
  10. mov rdx, qword ptr[rsp + 7 * 8]
  11. sub rsp, 32
  12. call KeBugCheck_Hook
  13. mov qword ptr [rsp], rax
  14. add rsp, 32
  15. pop r11
  16. pop r10
  17. pop r9
  18. pop r8
  19. pop rdx
  20. pop rcx
  21. pop rax
  22. ; recover destroyed bytes of RtlCaptureContext
  23. pushfq
  24. mov word ptr [rcx+38h],cs
  25. mov word ptr [rcx+3Ah],ds
  26. mov word ptr [rcx+3Ch],es
  27. mov word ptr [rcx+42h],ss
  28. ; jump behind destroyed bytes... (RetVal of RtlCaptureContext_HookEx)
  29. jmp qword ptr[rsp - 32 - 8 * 7 + 8]
  30. RtlCaptureContext_Hook ENDP

fake函数将pg进入死循环

  1. ULONGLONG KeBugCheck_Hook(ULONGLONG InBugCode, ULONGLONG InCaller)
  2. {
  3. FAST_MUTEX WaitAlways;
  4. //判断调用者
  5. if((InCaller >= KeBugCheckEx_Sym) && (InCaller <= KeBugCheckEx_Sym + 100))
  6. {
  7. if(InBugCode == CRITICAL_STRUCTURE_CORRUPTION)
  8. {
  9. // KeBugCheckEx disables interrupts before calling RtlCaptureContext()
  10. EnableInterrupts();
  11. //进入死循环
  12. ExInitializeFastMutex(&WaitAlways);
  13. ExAcquireFastMutex(&WaitAlways);
  14. ExAcquireFastMutex(&WaitAlways);
  15. }
  16. }
  17. //返回跳转地址
  18. return RtlCaptureContext_Sym + 14;
  19. }

jpg 改 rar pg3 bypass源码阅读 —— 学习x64内核hook跳板技术