Advertisement
Wizkid

ENUM WINDOWS HOOKS

Jul 16th, 2012
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.56 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////
  2. // +----------------------------------------------------------------------------+ //
  3. // |                                                                            | //
  4. // | ENUM WINDOWS HOOKS                                                         | //
  5. // |                                                                            | //
  6. // +----------------------------------------------------------------------------+ //
  7. // |                                                                            | //
  8. // | NT Internals - http://www.ntinternals.org/                                 | //
  9. // | alex ntinternals org                                                       | //
  10. // | 06 September 2010                                                          | //
  11. // |                                                                            | //
  12. // | References:                                                                | //
  13. // |                                                                            | //
  14. // | Any application-defined hook procedure on my machine?                      | //
  15. // | Zairon - http://zairon.wordpress.com/2006/12/06/                           | //
  16. // |          any-application-defined-hook-procedure-on-my-machine/             | //
  17. // |                                                                            | //
  18. // | Window hooks: Inside                                                       | //
  19. // | Twister - http://twister.rootkits.su/articles/hooks_inside.php             | //
  20. // |                                                                            | //
  21. // | Inject: climb through the window                                           | //
  22. // | Twister - http://twister.rootkits.su/articles/windowinject.php             | //
  23. // |                                                                            | //
  24. // +----------------------------------------------------------------------------+ //
  25. ////////////////////////////////////////////////////////////////////////////////////
  26.  
  27. #include "EnumWindowsHooks.h"
  28.  
  29.  
  30. int __cdecl main(int argc, char **argv)
  31. {
  32.     NTSTATUS NtStatus = STATUS_UNSUCCESSFUL;
  33.  
  34.     PVOID ImageBase;
  35.     ULONG DllCharacteristics = 0;
  36.     PVOID User32InitializeImmEntryTable = NULL;
  37.  
  38.     UNICODE_STRING DllName;
  39.     ANSI_STRING ProcedureName;
  40.    
  41.     ULONG i;
  42.     ULONG UserDelta = 0;
  43.     ULONG HandleEntries = 0;
  44.  
  45.     SHARED_INFO *SharedInfo = NULL;
  46.     HANDLE_ENTRY *UserHandleTable = NULL;
  47.     HOOK *HookInfo = NULL;
  48.    
  49.    
  50.     __try
  51.     {
  52.         system("cls");
  53.  
  54.  
  55.         RtlInitUnicodeString(
  56.                              &DllName,
  57.                              L"user32");
  58.  
  59.         NtStatus = LdrLoadDll(
  60.                               NULL,                // DllPath
  61.                               &DllCharacteristics, // DllCharacteristics
  62.                               &DllName,            // DllName
  63.                               &ImageBase);         // DllHandle
  64.  
  65.         if(NtStatus == STATUS_SUCCESS)
  66.         {
  67.             RtlInitAnsiString(
  68.                               &ProcedureName,
  69.                               "User32InitializeImmEntryTable");
  70.  
  71.             NtStatus = LdrGetProcedureAddress(
  72.                                               ImageBase,                               // DllHandle
  73.                                               &ProcedureName,                          // ProcedureName
  74.                                               0,                                       // ProcedureNumber OPTIONAL
  75.                                               (PVOID*)&User32InitializeImmEntryTable); // ProcedureAddress
  76.  
  77.             if(NtStatus == STATUS_SUCCESS)
  78.             {
  79.                 __asm
  80.                 {
  81.                     mov esi, User32InitializeImmEntryTable
  82.                     test esi, esi
  83.                     jz __exit2
  84.                     mov ecx, 0x80
  85.                        
  86.                 __loop:
  87.                     dec ecx
  88.                     test ecx, ecx
  89.                     jz __exit1
  90.  
  91.                     lodsb
  92.                     cmp al, 0x50
  93.                     jnz __loop
  94.  
  95.                     lodsb
  96.                     cmp al, 0x68
  97.                     jnz __loop
  98.  
  99.                     lodsd
  100.                     mov SharedInfo, eax
  101.  
  102.                     jmp __exit2
  103.  
  104.                 __exit1:
  105.                     mov SharedInfo, ecx
  106.                    
  107.                 __exit2:
  108.                     sub eax, eax
  109.                     mov eax, fs:[eax+0x18]
  110.                     lea eax, [eax+0x06CC]
  111.                     mov eax, [eax+0x001C]
  112.                     mov UserDelta, eax
  113.                 }
  114.  
  115.                 HandleEntries = *((ULONG *)((ULONG)SharedInfo->ServerInfo + 8));
  116.  
  117.                 printf(
  118.                        " +--------------------------------------------------------------------+\n"
  119.                        " | SHARED_INFO - %.8X                                             |\n"
  120.                        " +--------------------------------------------------------------------+\n"
  121.                        " | ServerInfo - %.8X                                              |\n"
  122.                        " | HandleEntryList - %.8X                                         |\n"
  123.                        " | HandleEntries - %.8X                                           |\n"
  124.                        " | DisplayInfo - %.8X                                             |\n"
  125.                        " | SharedDelta - %.8X                                             |\n"
  126.                        " | UserDelta - %.8X                                               |\n"
  127.                        " +--------------------------------------------------------------------+\n\n",
  128.                        SharedInfo,
  129.                        SharedInfo->ServerInfo,
  130.                        SharedInfo->HandleEntryList,
  131.                        HandleEntries,
  132.                        SharedInfo->DisplayInfo,
  133.                        SharedInfo->SharedDelta,
  134.                        UserDelta);
  135.  
  136.                 UserHandleTable = (HANDLE_ENTRY *)SharedInfo->HandleEntryList;
  137.                
  138.                 for(i=0; i<HandleEntries; i++)
  139.                 {
  140.                     if(UserHandleTable[i].Type == TYPE_HOOK)
  141.                     {
  142.                         __try
  143.                         {
  144.                             HookInfo = (HOOK *)((ULONG)UserHandleTable[i].Head - UserDelta);
  145.  
  146.                             printf(
  147.                                    " +--------------------------------------------------------------------+\n"
  148.                                    " | HOOK - %.8X                                                    |\n"
  149.                                    " +--------------------------------------------------------------------+\n"
  150.                                    " | Handle - %.8X                                                  |\n"
  151.                                    " | LockObj - %.8X                                                 |\n"
  152.                                    " | ThreadInfo- %.8X                                               |\n"
  153.                                    " | Desktop1 - %.8X                                                |\n"
  154.                                    " | Self - %.8X                                                    |\n"
  155.                                    " | NextHook - %.8X                                                |\n"
  156.                                    " | HookType - %.8X                                                |\n"
  157.                                    " | FunctionAddress - %.8X                                         |\n"
  158.                                    " | Flags - %.8X                                                   |\n"
  159.                                    " | ModuleHandle - %.8X                                            |\n"
  160.                                    " | Hooked - %.8X                                                  |\n"
  161.                                    " | Desktop2 - %.8X                                                |\n"
  162.                                    " +--------------------------------------------------------------------+\n\n",
  163.                                    (ULONG)UserHandleTable[i].Head - UserDelta,
  164.                                    HookInfo->Handle,
  165.                                    HookInfo->LockObj,
  166.                                    HookInfo->ThreadInfo,
  167.                                    HookInfo->Desktop1,
  168.                                    HookInfo->Self,
  169.                                    HookInfo->NextHook,
  170.                                    HookInfo->HookType,
  171.                                    HookInfo->FunctionAddress,
  172.                                    HookInfo->Flags,
  173.                                    HookInfo->ModuleHandle,
  174.                                    HookInfo->Hooked,
  175.                                    HookInfo->Desktop2);
  176.                         }
  177.                         __except(EXCEPTION_EXECUTE_HANDLER) {}
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     __except(EXCEPTION_EXECUTE_HANDLER)
  184.     {
  185.         printf(">> main - %.8X\n", GetExceptionCode());
  186.  
  187.         return GetExceptionCode();
  188.     }
  189.  
  190.     return FALSE;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement