免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

恢复hp的object hook

模拟HP的object hook 保护 :

driver.h
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include <ntddk.h>
  6. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. #define PAGEDCODE code_seg("PAGE")
  11. #define LOCKEDCODE code_seg()
  12. #define INITCODE code_seg("INIT")

  13. #define PAGEDDATA data_seg("PAGE")
  14. #define LOCKEDDATA data_seg()
  15. #define INITDATA data_seg("INIT")

  16. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))

  17. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);

  18. typedef NTSTATUS (*OB_SECURITY_METHOD)(
  19.     IN PVOID Object,
  20.     IN SECURITY_OPERATION_CODE OperationCode,
  21.     IN PSECURITY_INFORMATION SecurityInformation,
  22.     IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
  23.     IN OUT PULONG CapturedLength,
  24.     IN OUT PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor,
  25.     IN POOL_TYPE PoolType,
  26.     IN PGENERIC_MAPPING GenericMapping,
  27.     IN PVOID unknown
  28.     );

  29. EXTERN_C NTKERNELAPI UCHAR * PsGetProcessImageFileName(__in PEPROCESS Process);
复制代码
driver.cpp
  1. #include "Driver.h"

  2. OB_SECURITY_METHOD g_pfnSecurityProcedure;



  3. NTSTATUS MySecurityProcedure(
  4.                                                          IN PVOID Object,
  5.                                                          IN SECURITY_OPERATION_CODE OperationCode,
  6.                                                          IN PSECURITY_INFORMATION SecurityInformation,
  7.                                                          IN OUT PSECURITY_DESCRIPTOR SecurityDescriptor,
  8.                                                          IN OUT PULONG CapturedLength,
  9.                                                          IN OUT PSECURITY_DESCRIPTOR *ObjectsSecurityDescriptor,
  10.                                                          IN POOL_TYPE PoolType,
  11.                                                          IN PGENERIC_MAPPING GenericMapping,
  12.                                                          IN PVOID unknown
  13.                                                          )
  14. {

  15.        

  16.         PUCHAR pTargetName = PsGetProcessImageFileName(IoGetCurrentProcess());

  17.         if (strstr((const char *)pTargetName, "ollydbg") != 0)
  18.         {
  19.                 __asm int 3;
  20.         }

  21.         return g_pfnSecurityProcedure(
  22.                 Object,
  23.                 OperationCode,
  24.                 SecurityInformation,
  25.                 SecurityDescriptor,
  26.                 CapturedLength,
  27.                 ObjectsSecurityDescriptor,
  28.                 PoolType,
  29.                 GenericMapping,
  30.                 unknown);
  31. }

  32. void HandleObjectHook(BOOLEAN bHook)
  33. {

  34.         PVOID pTypeInfo = (PVOID)((ULONG)*PsThreadType + 0x60);

  35.         if (bHook)
  36.         {
  37.                 g_pfnSecurityProcedure = (OB_SECURITY_METHOD)*(PULONG)((ULONG)pTypeInfo + 0x40);
  38.                 *(PULONG)((ULONG)pTypeInfo + 0x40) = (ULONG)MySecurityProcedure;
  39.         }
  40.         else
  41.         {
  42.                 *(PULONG)((ULONG)pTypeInfo + 0x40) = (ULONG)g_pfnSecurityProcedure;
  43.         }
  44. }


  45. #pragma INITCODE
  46. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  47. {
  48.     NTSTATUS status = STATUS_SUCCESS;

  49.         HandleObjectHook(TRUE);

  50.     DriverObject->DriverUnload = DriverUnload;
  51.     return status;
  52. }

  53. #pragma PAGEDCODE
  54. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
  55. {
  56.   
  57.         HandleObjectHook(FALSE);

  58.     KdPrint(("DriverEntry unLoading...\n"));
  59.   
  60. }
复制代码
恢复hp的object hook:

Driver.h
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include <ntddk.h>
  6. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. #define PAGEDCODE code_seg("PAGE")
  11. #define LOCKEDCODE code_seg()
  12. #define INITCODE code_seg("INIT")

  13. #define PAGEDDATA data_seg("PAGE")
  14. #define LOCKEDDATA data_seg()
  15. #define INITDATA data_seg("INIT")

  16. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))

  17. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);

  18. EXTERN_C NTKERNELAPI NTSTATUS ObAssignSecurity(
  19.                                  __in PACCESS_STATE AccessState,
  20.                                  __in_opt PSECURITY_DESCRIPTOR ParentDescriptor,
  21.                                  __in PVOID Object,
  22.                                  __in POBJECT_TYPE ObjectType);
复制代码
Driver.cpp
  1. #include "Driver.h"

  2. //global
  3. ULONG g_uSeDefaultObjectMethod;

  4. ULONG g_ObpGetObjectSecurity_hookpointer;
  5. ULONG g_retn_ObpGetObjectSecurity;
  6. UCHAR g_szBackupObpGetObjectSecurity[5];

  7. ULONG g_ObAssignSecurity_hookpointer;
  8. ULONG g_retn_ObAssignSecurity;
  9. UCHAR g_szBackupObAssignSecurity[5];

  10. void PageProtectOn()
  11. {
  12.         //恢复内存保护
  13.         __asm
  14.         {
  15.                 mov eax, cr0
  16.                 or eax, 10000h
  17.                 mov cr0, eax
  18.                 sti
  19.         }
  20. }

  21. void PageProtectOff()
  22. {
  23.         //去掉内存保护
  24.         __asm
  25.         {
  26.                 cli
  27.                 mov eax, cr0
  28.                 and eax, not 10000h
  29.                 mov cr0, eax
  30.         }
  31. }

  32. void Jmp_HookFunction(ULONG HookAddr, ULONG Source, UCHAR *Ori_Code)
  33. {
  34.         ULONG jmp_offset;
  35.         UCHAR jmp_code[5] = {0xe9};

  36.         KSPIN_LOCK lock;
  37.         KIRQL irql;

  38.         if (HookAddr == 0 || Source == 0)
  39.         {
  40.                 KdPrint(("Params error!\n"));
  41.                 return;
  42.         }

  43.         RtlCopyMemory(Ori_Code, (PVOID)HookAddr, 5);
  44.         jmp_offset = Source - HookAddr - 5;
  45.         *(PULONG)&jmp_code[1] = jmp_offset;

  46.         KeInitializeSpinLock(&lock);
  47.         KeAcquireSpinLock(&lock, &irql);

  48.         PageProtectOff();
  49.         RtlCopyMemory((PVOID)HookAddr, jmp_code, 5);
  50.         PageProtectOn();

  51.         KeReleaseSpinLock(&lock, irql);

  52.         return;
  53. }

  54. void Res_HookFunction(ULONG HookAddr, UCHAR *Ori_Code, ULONG Length)
  55. {
  56.         KSPIN_LOCK lock;
  57.         KIRQL irql;

  58.         if (HookAddr == 0 || Ori_Code == 0)
  59.         {
  60.                 KdPrint(("Params error!\n"));
  61.                 return;
  62.         }

  63.         KeInitializeSpinLock(&lock);
  64.         KeAcquireSpinLock(&lock, &irql);

  65.         PageProtectOff();
  66.         RtlCopyMemory((PVOID)HookAddr, Ori_Code, Length);
  67.         PageProtectOn();

  68.         KeReleaseSpinLock(&lock, irql);

  69.         return;
  70. }

  71. ULONG GetObpGetObjectSecurityAddr()
  72. {
  73.         ULONG uAddr = (ULONG)ObGetObjectSecurity;

  74.         PUCHAR p = (PUCHAR)uAddr;
  75.         ULONG i = 0;
  76.         BOOLEAN bFind = FALSE;

  77.         while(i < 0x50)
  78.         {
  79.                 if (*p == 0xe8
  80.                         && *(p + 5) == 0x5d
  81.                         && *(p + 6) == 0xc2
  82.                         && *(p + 7) == 0x0c
  83.                         && *(p - 3) == 0xff
  84.                         && *(p - 2) == 0x75)
  85.                 {
  86.                         bFind = TRUE;
  87.                         break;
  88.                 }
  89.                 i++;
  90.                 p++;
  91.         }

  92.         if (!bFind)
  93.         {
  94.                 KdPrint(("Find GetObpGetObjectSecurityAddr faile!\n"));
  95.                 return 0;
  96.         }


  97.         ULONG uFindAddr = (ULONG)( *(PULONG)(p + 1) + p + 5);

  98.         return uFindAddr;

  99. }



  100. ULONG SearchObpGetObjectSecurityHookPointer(ULONG StartAddress)
  101. {

  102.         PUCHAR p = (PUCHAR)StartAddress;
  103.         ULONG i = 0;
  104.         BOOLEAN bFind = FALSE;

  105.         while(i < 0x200)
  106.         {
  107.                 if (*p == 0x81
  108.                         && *(p + 1) == 0xbe
  109.                         && *(p + 2) == 0xa0
  110.                         && *(p - 1) == 0xfc
  111.                         && *(p - 2) == 0x4d)
  112.                 {
  113.                         bFind = TRUE;
  114.                         break;
  115.                 }
  116.                 i++;
  117.                 p++;
  118.         }

  119.         if (!bFind)
  120.         {
  121.                 KdPrint(("Find SearchHookPointer faile!\n"));
  122.                 return 0;
  123.         }


  124.         ULONG uFindAddr = (ULONG)p;

  125.         return uFindAddr;
  126. }

  127. ULONG SearchObAssignSecurityHookPointer(ULONG StartAddress)
  128. {

  129.         PUCHAR p = (PUCHAR)StartAddress;
  130.         ULONG i = 0;
  131.         BOOLEAN bFind = FALSE;

  132.         while(i < 0x200)
  133.         {
  134.                 if (*p == 0xff
  135.                         && *(p + 1) == 0x96
  136.                         && *(p + 2) == 0xa0
  137.                         && *(p - 1) == 0x10
  138.                         && *(p - 2) == 0x75)
  139.                 {
  140.                         bFind = TRUE;
  141.                         break;
  142.                 }
  143.                 i++;
  144.                 p++;
  145.         }

  146.         if (!bFind)
  147.         {
  148.                 KdPrint(("Find SearchHookPointer faile!\n"));
  149.                 return 0;
  150.         }


  151.         ULONG uFindAddr = (ULONG)p;

  152.         return uFindAddr;
  153. }

  154. ULONG FilterObjectType(ULONG ObjectType)
  155. {
  156.         if (ObjectType == (ULONG)*PsThreadType || ObjectType == (ULONG)*PsProcessType)
  157.         {
  158.                 return 1;
  159.         }

  160.         return 0;
  161. }

  162. __declspec(naked) void NewGetObpGetObjectSecurity()
  163. {
  164.         __asm
  165.         {
  166.                 pushad
  167.                 pushfd

  168.                 push esi
  169.                 call FilterObjectType
  170.                 test eax, eax
  171.                 je __exit
  172.                 popfd
  173.                 popad
  174.                 cmp eax,eax
  175.                 jmp g_retn_ObpGetObjectSecurity

  176. __exit:
  177.                 popfd
  178.                 popad
  179.                
  180.                 push eax
  181.                 mov eax, g_uSeDefaultObjectMethod
  182.                 cmp dword ptr [esi + 0A0h], eax
  183.                 pop eax
  184.                 jmp g_retn_ObpGetObjectSecurity
  185.         }
  186. }



  187. __declspec(naked) void NewObAssignSecurity()
  188. {
  189.         __asm
  190.         {
  191.                 pushad
  192.                 pushfd

  193.                 push esi
  194.                 call FilterObjectType
  195.                 test eax, eax
  196.                 je __exit
  197.                 popfd
  198.                 popad
  199.                 mov ebx, g_uSeDefaultObjectMethod
  200.                 call ebx
  201.                 xor ebx,ebx
  202.                 //mov dword ptr [esi + 0A0h], ebx
  203.                 //call dword ptr [esi + 0A0h]
  204.                 jmp g_retn_ObAssignSecurity

  205. __exit:
  206.                 popfd
  207.                 popad
  208.                
  209.                 call dword ptr [esi + 0A0h]
  210.                 jmp g_retn_ObAssignSecurity
  211. }
  212. }
  213. void PassObjectHook()
  214. {
  215.         ULONG uObpGetObjectSecurity = GetObpGetObjectSecurityAddr();
  216.        
  217.         g_ObpGetObjectSecurity_hookpointer = SearchObpGetObjectSecurityHookPointer(uObpGetObjectSecurity);
  218.         g_retn_ObpGetObjectSecurity = g_ObpGetObjectSecurity_hookpointer + 10;

  219.         g_uSeDefaultObjectMethod = *(PULONG)(g_ObpGetObjectSecurity_hookpointer + 6);

  220.         Jmp_HookFunction(g_ObpGetObjectSecurity_hookpointer, (ULONG)NewGetObpGetObjectSecurity, g_szBackupObpGetObjectSecurity);

  221.         //////////////////////////////////////////

  222.         ULONG uObAssignSecurity = (ULONG)ObAssignSecurity;
  223.         g_ObAssignSecurity_hookpointer = SearchObAssignSecurityHookPointer(uObAssignSecurity);
  224.         g_retn_ObAssignSecurity = g_ObAssignSecurity_hookpointer + 6;

  225.         Jmp_HookFunction(g_ObAssignSecurity_hookpointer, (ULONG)NewObAssignSecurity, g_szBackupObAssignSecurity);

  226. }


  227. void UnPassObjectHook()
  228. {
  229.         Res_HookFunction(g_ObpGetObjectSecurity_hookpointer, g_szBackupObpGetObjectSecurity, 5);

  230.         Res_HookFunction(g_ObAssignSecurity_hookpointer, g_szBackupObAssignSecurity, 5);
  231. }


  232. #pragma INITCODE
  233. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  234. {
  235.     NTSTATUS status = STATUS_SUCCESS;

  236.         PassObjectHook();
  237.     DriverObject->DriverUnload = DriverUnload;
  238.     return status;
  239. }

  240. #pragma PAGEDCODE
  241. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
  242. {
  243.   
  244.         UnPassObjectHook();
  245.     KdPrint(("DriverEntry unLoading...\n"));
  246.   
  247. }
复制代码

返回列表