BehaviorMon.h
上传用户:xuemeng126
上传日期:2022-07-05
资源大小:454k
文件大小:3k
源码类别:

系统编程

开发平台:

Visual C++

  1. #if defined(_M_IA64) 
  2. //
  3. // IA64 SYSTEM CALL HOOK/UNHOOK
  4. //
  5. //
  6. // On the IA64 the Zw function has an embedded immediate that is the system call number
  7. //
  8. #define SYSCALL_INDEX(_Function) (((*(PULONG)((PUCHAR)(*(PULONG_PTR)_Function+4)) & 0x3) << 7) + (*(PULONG)((PUCHAR)*(PULONG_PTR)_Function) >> 18))
  9. #define HOOK_SYSCALL(_Function, _Hook, _Orig )  
  10.     if( !HookDescriptors[ SYSCALL_INDEX(_Function) ].Hooked ) { 
  11.             ULONG syscallIndex = SYSCALL_INDEX(_Function ); 
  12.             if( !stubsPatched ) PatchStub( gpReg, (PVOID) *(PULONG_PTR *) Stub##_Hook ); 
  13.             HookDescriptors[ syscallIndex ].FuncDesc.EntryPoint = 
  14.                      (ULONGLONG) InterlockedExchangePointer( (PVOID) &KeServiceTablePointers[ syscallIndex ], *(PULONG_PTR *) Stub##_Hook ); 
  15.             HookDescriptors[ syscallIndex ].FuncDesc.GlobalPointer = ((PLABEL_DESCRIPTOR *)&_Function)->GlobalPointer; 
  16.             _Orig = (PVOID) &HookDescriptors[ syscallIndex ].FuncDesc.EntryPoint; 
  17.             HookDescriptors[ syscallIndex ].Hooked = TRUE; 
  18.     }
  19. //
  20. // NOTE: We can't unhook if someone else has hooked on top of us. Note that the
  21. // unhook code below still has a window of vulnerability where someone can hook between
  22. // our test and unhook.
  23. //
  24. #define UNHOOK_SYSCALL(_Function, _Hook, _Orig )  
  25.     if( HookDescriptors[ SYSCALL_INDEX(_Function)].Hooked && KeServiceTablePointers[ SYSCALL_INDEX(_Function) ] == (PVOID) _Hook ) { 
  26.             InterlockedExchangePointer( (PVOID) &KeServiceTablePointers[ SYSCALL_INDEX(_Function) ], (PVOID) _Orig ); 
  27.             HookDescriptors[ SYSCALL_INDEX(_Function) ].Hooked = FALSE; 
  28.     }
  29. #else
  30. //
  31. // X86 SYSTEM CALL HOOK/UNHOOK
  32. //
  33. //
  34. // Define this because we build with the NT4DDK for 32-bit systems, where
  35. // ULONG_PTR isn't defined and is a ULONG anyway
  36. //
  37. typedef ULONG   ULONG_PTR;
  38. //
  39. // On X86 implementations of Zw* functions, the DWORD
  40. // following the first byte is the system call number, so we reach into the Zw function
  41. // passed as a parameter, and pull the number out. This makes system call hooking
  42. //
  43. #define SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)_Function+1)
  44. #define HOOK_SYSCALL(_Function, _Hook, _Orig )  
  45.     if( !HookDescriptors[ SYSCALL_INDEX(_Function) ].Hooked ) { 
  46.             _Orig = (PVOID) InterlockedExchange( (PLONG) &KeServiceTablePointers[ SYSCALL_INDEX(_Function) ], (LONG) _Hook ); 
  47.             HookDescriptors[ SYSCALL_INDEX(_Function) ].Hooked = TRUE; 
  48.     }
  49. //
  50. // NOTE: We can't unhook if someone else has hooked on top of us. Note that the
  51. // unhook code below still has a window of vulnerability where someone can hook between
  52. // our test and unhook.
  53. //
  54. #define UNHOOK_SYSCALL(_Function, _Hook, _Orig )  
  55.     if( HookDescriptors[ SYSCALL_INDEX(_Function)].Hooked && KeServiceTablePointers[ SYSCALL_INDEX(_Function) ] == (PVOID) _Hook ) { 
  56.             InterlockedExchange( (PLONG) &KeServiceTablePointers[ SYSCALL_INDEX(_Function) ], (LONG) _Orig ); 
  57.             HookDescriptors[ SYSCALL_INDEX(_Function) ].Hooked = FALSE; 
  58.     }
  59. #endif