Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Include.h"
- #define PtrFromRva(base, rva) (((PBYTE)base) + rva)
- void PatchIAT ( HMODULE Module, PSTR ImportedModuleName, PSTR ImportedProcName, PVOID AlternateProc, PVOID* OldProc = 0 )
- {
- if ( !Module )
- return;
- PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)Module;
- PIMAGE_NT_HEADERS NtHeader;
- PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor;
- UINT Index;
- NtHeader = (PIMAGE_NT_HEADERS)PtrFromRva(DosHeader, DosHeader->e_lfanew);
- char msg[8192];
- if ( NtHeader->Signature != IMAGE_NT_SIGNATURE )
- return;
- ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)PtrFromRva(DosHeader, NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
- for ( Index = 0; ImportDescriptor[Index].Characteristics != 0; Index++ )
- {
- PSTR DllName = (PSTR)PtrFromRva(DosHeader, ImportDescriptor[Index].Name);
- if ( !DllName )
- continue;
- if ( strcmp ( DllName, ImportedModuleName ) == 0 )
- {
- PIMAGE_THUNK_DATA Thunk;
- PIMAGE_THUNK_DATA OrigThunk;
- if ( !ImportDescriptor[Index].FirstThunk || !ImportDescriptor[Index].OriginalFirstThunk )
- return;
- Thunk = (PIMAGE_THUNK_DATA)PtrFromRva(DosHeader, ImportDescriptor[Index].FirstThunk);
- OrigThunk = (PIMAGE_THUNK_DATA)PtrFromRva(DosHeader, ImportDescriptor[Index].OriginalFirstThunk);
- for ( ; OrigThunk->u1.Function != 0; OrigThunk++, Thunk++ )
- {
- if ( OrigThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG )
- continue;
- PIMAGE_IMPORT_BY_NAME Import = (PIMAGE_IMPORT_BY_NAME)PtrFromRva(DosHeader, OrigThunk->u1.AddressOfData);
- if ( strcmp ( ImportedProcName, (const char*)Import->Name ) == 0 )
- {
- DWORD Junk;
- MEMORY_BASIC_INFORMATION MemoryInfo;
- VirtualQuery ( Thunk, &MemoryInfo, sizeof(MEMORY_BASIC_INFORMATION) );
- VirtualProtect ( MemoryInfo.BaseAddress, MemoryInfo.RegionSize, PAGE_EXECUTE_READWRITE, &MemoryInfo.Protect );
- if ( OldProc )
- *OldProc = (PVOID)Thunk->u1.Function;
- Thunk->u1.Function = (ULONG)AlternateProc;
- VirtualProtect ( MemoryInfo.BaseAddress, MemoryInfo.RegionSize, MemoryInfo.Protect, &Junk );
- return;
- }
- }
- }
- }
- }
- bool EnableMouseHook = false;
- typedef BOOL (WINAPI* SetCursorPosFn)( int X, int Y );
- typedef BOOL (WINAPI* GetCursorPosFn)( LPPOINT lpPoint );
- SetCursorPosFn oSetCursorPos = 0;
- GetCursorPosFn oGetCursorPos = 0;
- BOOL WINAPI Hooked_SetCursorPos ( int X, int Y )
- {
- if ( EnableMouseHook )
- return TRUE;
- return oSetCursorPos ( X, Y );
- }
- BOOL WINAPI Hooked_GetCursorPos ( LPPOINT lpPoint )
- {
- if ( EnableMouseHook )
- {
- lpPoint->x = 1680 / 2;
- lpPoint->y = 1050 / 2;
- return TRUE;
- }
- return oGetCursorPos ( lpPoint );
- }
- void InstallHook ()
- {
- MODULEENTRY32 me32 = { 0 };
- me32.dwSize = sizeof(MODULEENTRY32);
- HANDLE Snapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPMODULE, GetCurrentProcessId () );
- if ( !Snapshot )
- {
- OutputDebugStringA ( "snapshot failed!\n" );
- return;
- }
- if ( !Module32First ( Snapshot, &me32 ) )
- {
- OutputDebugStringA ( "module32first failed!\n" );
- CloseHandle ( Snapshot );
- return;
- }
- do
- {
- HMODULE Module = me32.hModule;
- PatchIAT ( Module, "USER32.dll", "SetCursorPos", &Hooked_SetCursorPos );
- PatchIAT ( Module, "USER32.dll", "GetCursorPos", &Hooked_GetCursorPos );
- }
- while ( Module32Next ( Snapshot, &me32 ) );
- CloseHandle ( Snapshot );
- }
- void ShutdownHook ()
- {
- MODULEENTRY32 me32 = { 0 };
- me32.dwSize = sizeof(MODULEENTRY32);
- HANDLE Snapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPMODULE, GetCurrentProcessId () );
- if ( !Snapshot )
- {
- OutputDebugStringA ( "snapshot failed!\n" );
- return;
- }
- if ( !Module32First ( Snapshot, &me32 ) )
- {
- OutputDebugStringA ( "module32first failed!\n" );
- CloseHandle ( Snapshot );
- return;
- }
- do
- {
- HMODULE Module = me32.hModule;
- PatchIAT ( Module, "USER32.dll", "SetCursorPos", (PVOID)oSetCursorPos );
- PatchIAT ( Module, "USER32.dll", "GetCursorPos", (PVOID)oGetCursorPos );
- }
- while ( Module32Next ( Snapshot, &me32 ) );
- CloseHandle ( Snapshot );
- }
- void InitThread ( HMODULE hModule )
- {
- oSetCursorPos = (SetCursorPosFn) GetProcAddress ( GetModuleHandleA ( "user32.dll" ), "SetCursorPos" );
- oGetCursorPos = (GetCursorPosFn) GetProcAddress ( GetModuleHandleA ( "user32.dll" ), "GetCursorPos" );
- InstallHook ();
- while ( !GetAsyncKeyState ( VK_F11 ) )
- {
- Sleep ( 100 );
- if ( GetAsyncKeyState ( VK_INSERT ) & 1 )
- EnableMouseHook = !EnableMouseHook;
- }
- ShutdownHook ();
- FreeLibraryAndExitThread ( hModule, 0 );
- }
- BOOL WINAPI DllMain ( HMODULE hModule, DWORD dwReason, LPVOID lpReserved )
- {
- if ( dwReason == DLL_PROCESS_ATTACH )
- {
- DisableThreadLibraryCalls ( hModule );
- CreateThread ( 0, 0, (LPTHREAD_START_ROUTINE)InitThread, hModule, 0, 0 );
- }
- else if ( dwReason == DLL_PROCESS_DETACH )
- {
- ShutdownHook ();
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment