keybode

iat SetCursorPos & GetCursorPos hook

Nov 25th, 2014
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. #include "Include.h"
  2.  
  3. #define PtrFromRva(base, rva) (((PBYTE)base) + rva)
  4.  
  5. void PatchIAT ( HMODULE Module, PSTR ImportedModuleName, PSTR ImportedProcName, PVOID AlternateProc, PVOID* OldProc = 0 )
  6. {
  7.     if ( !Module )
  8.         return;
  9.  
  10.     PIMAGE_DOS_HEADER           DosHeader = (PIMAGE_DOS_HEADER)Module;
  11.     PIMAGE_NT_HEADERS           NtHeader;
  12.     PIMAGE_IMPORT_DESCRIPTOR    ImportDescriptor;
  13.     UINT                        Index;
  14.  
  15.     NtHeader = (PIMAGE_NT_HEADERS)PtrFromRva(DosHeader, DosHeader->e_lfanew);
  16.  
  17.     char msg[8192];
  18.  
  19.     if ( NtHeader->Signature != IMAGE_NT_SIGNATURE )
  20.         return;
  21.  
  22.     ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)PtrFromRva(DosHeader, NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
  23.  
  24.     for ( Index = 0; ImportDescriptor[Index].Characteristics != 0; Index++ )
  25.     {
  26.         PSTR DllName = (PSTR)PtrFromRva(DosHeader, ImportDescriptor[Index].Name);
  27.  
  28.         if ( !DllName )
  29.             continue;
  30.  
  31.         if ( strcmp ( DllName, ImportedModuleName ) == 0 )
  32.         {
  33.             PIMAGE_THUNK_DATA Thunk;
  34.             PIMAGE_THUNK_DATA OrigThunk;
  35.  
  36.             if ( !ImportDescriptor[Index].FirstThunk || !ImportDescriptor[Index].OriginalFirstThunk )
  37.                 return;
  38.  
  39.             Thunk = (PIMAGE_THUNK_DATA)PtrFromRva(DosHeader, ImportDescriptor[Index].FirstThunk);
  40.  
  41.             OrigThunk = (PIMAGE_THUNK_DATA)PtrFromRva(DosHeader, ImportDescriptor[Index].OriginalFirstThunk);
  42.  
  43.             for ( ; OrigThunk->u1.Function != 0; OrigThunk++, Thunk++ )
  44.             {
  45.                 if ( OrigThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG )
  46.                     continue;
  47.  
  48.                 PIMAGE_IMPORT_BY_NAME Import = (PIMAGE_IMPORT_BY_NAME)PtrFromRva(DosHeader, OrigThunk->u1.AddressOfData);
  49.  
  50.                 if ( strcmp ( ImportedProcName, (const char*)Import->Name ) == 0 )
  51.                 {
  52.                     DWORD Junk;
  53.  
  54.                     MEMORY_BASIC_INFORMATION MemoryInfo;
  55.  
  56.                     VirtualQuery ( Thunk, &MemoryInfo, sizeof(MEMORY_BASIC_INFORMATION) );
  57.  
  58.                     VirtualProtect ( MemoryInfo.BaseAddress, MemoryInfo.RegionSize, PAGE_EXECUTE_READWRITE, &MemoryInfo.Protect );
  59.  
  60.                     if ( OldProc )
  61.                         *OldProc = (PVOID)Thunk->u1.Function;
  62.  
  63.                     Thunk->u1.Function = (ULONG)AlternateProc;
  64.  
  65.                     VirtualProtect ( MemoryInfo.BaseAddress, MemoryInfo.RegionSize, MemoryInfo.Protect, &Junk );
  66.                    
  67.                     return;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. bool EnableMouseHook = false;
  75.  
  76. typedef BOOL (WINAPI* SetCursorPosFn)( int X, int Y );
  77. typedef BOOL (WINAPI* GetCursorPosFn)( LPPOINT lpPoint );
  78.  
  79. SetCursorPosFn oSetCursorPos = 0;
  80. GetCursorPosFn oGetCursorPos = 0;
  81.  
  82. BOOL WINAPI Hooked_SetCursorPos ( int X, int Y )
  83. {
  84.     if ( EnableMouseHook )
  85.         return TRUE;
  86.    
  87.     return oSetCursorPos ( X, Y );
  88. }
  89.  
  90. BOOL WINAPI Hooked_GetCursorPos ( LPPOINT lpPoint )
  91. {
  92.     if ( EnableMouseHook )
  93.     {
  94.         lpPoint->x = 1680 / 2;
  95.         lpPoint->y = 1050 / 2;
  96.  
  97.         return TRUE;
  98.     }
  99.  
  100.     return oGetCursorPos ( lpPoint );
  101. }
  102.  
  103. void InstallHook ()
  104. {
  105.     MODULEENTRY32 me32 = { 0 };
  106.  
  107.     me32.dwSize = sizeof(MODULEENTRY32);
  108.  
  109.     HANDLE Snapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPMODULE, GetCurrentProcessId () );
  110.  
  111.     if ( !Snapshot )
  112.     {
  113.         OutputDebugStringA ( "snapshot failed!\n" );
  114.         return;
  115.     }
  116.  
  117.     if ( !Module32First ( Snapshot, &me32 ) )
  118.     {
  119.         OutputDebugStringA ( "module32first failed!\n" );
  120.         CloseHandle ( Snapshot );
  121.         return;
  122.     }
  123.  
  124.     do
  125.     {
  126.         HMODULE Module = me32.hModule;
  127.  
  128.         PatchIAT ( Module, "USER32.dll", "SetCursorPos", &Hooked_SetCursorPos );
  129.         PatchIAT ( Module, "USER32.dll", "GetCursorPos", &Hooked_GetCursorPos );
  130.     }
  131.     while ( Module32Next ( Snapshot, &me32 ) );
  132.  
  133.     CloseHandle ( Snapshot );
  134. }
  135.  
  136. void ShutdownHook ()
  137. {
  138.     MODULEENTRY32 me32 = { 0 };
  139.  
  140.     me32.dwSize = sizeof(MODULEENTRY32);
  141.  
  142.     HANDLE Snapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPMODULE, GetCurrentProcessId () );
  143.  
  144.     if ( !Snapshot )
  145.     {
  146.         OutputDebugStringA ( "snapshot failed!\n" );
  147.         return;
  148.     }
  149.  
  150.     if ( !Module32First ( Snapshot, &me32 ) )
  151.     {
  152.         OutputDebugStringA ( "module32first failed!\n" );
  153.         CloseHandle ( Snapshot );
  154.         return;
  155.     }
  156.  
  157.     do
  158.     {
  159.         HMODULE Module = me32.hModule;
  160.  
  161.         PatchIAT ( Module, "USER32.dll", "SetCursorPos", (PVOID)oSetCursorPos );
  162.         PatchIAT ( Module, "USER32.dll", "GetCursorPos", (PVOID)oGetCursorPos );
  163.     }
  164.     while ( Module32Next ( Snapshot, &me32 ) );
  165.  
  166.     CloseHandle ( Snapshot );
  167. }
  168.  
  169. void InitThread ( HMODULE hModule )
  170. {
  171.     oSetCursorPos = (SetCursorPosFn) GetProcAddress ( GetModuleHandleA ( "user32.dll" ), "SetCursorPos" );
  172.     oGetCursorPos = (GetCursorPosFn) GetProcAddress ( GetModuleHandleA ( "user32.dll" ), "GetCursorPos" );
  173.  
  174.     InstallHook ();
  175.  
  176.     while ( !GetAsyncKeyState ( VK_F11 ) )
  177.     {
  178.         Sleep ( 100 );
  179.  
  180.         if ( GetAsyncKeyState ( VK_INSERT ) & 1 )
  181.             EnableMouseHook = !EnableMouseHook;
  182.     }
  183.  
  184.     ShutdownHook ();
  185.     FreeLibraryAndExitThread ( hModule, 0 );
  186. }
  187.  
  188. BOOL WINAPI DllMain ( HMODULE hModule, DWORD dwReason, LPVOID lpReserved )
  189. {
  190.     if ( dwReason == DLL_PROCESS_ATTACH )
  191.     {
  192.         DisableThreadLibraryCalls ( hModule );
  193.  
  194.         CreateThread ( 0, 0, (LPTHREAD_START_ROUTINE)InitThread, hModule, 0, 0 );
  195.     }
  196.     else if ( dwReason == DLL_PROCESS_DETACH )
  197.     {
  198.         ShutdownHook ();
  199.     }
  200.  
  201.     return TRUE;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment