Advertisement
vovan333

la putain

Nov 3rd, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include <stdint.h>
  3. #include <Windows.h>
  4.  
  5. namespace asmjs
  6. {
  7.     class VMTHook
  8.     {
  9.         static int Unprotect(void* region)
  10.         {
  11.             MEMORY_BASIC_INFORMATION mbi;
  12.             VirtualQuery((LPCVOID)region, &mbi, sizeof(mbi));
  13.             VirtualProtect(mbi.BaseAddress, mbi.RegionSize, PAGE_READWRITE, &mbi.Protect);
  14.             return mbi.Protect;
  15.         }
  16.  
  17.         static void Protect(void* region, int protection)
  18.         {
  19.             MEMORY_BASIC_INFORMATION mbi;
  20.             VirtualQuery((LPCVOID)region, &mbi, sizeof(mbi));
  21.             VirtualProtect(mbi.BaseAddress, mbi.RegionSize, protection, &mbi.Protect);
  22.         }
  23.  
  24.         public:
  25.  
  26.         /*
  27.         * pObjectInstance: pointer to an pObjectInstance of a class
  28.         * hookFn: function to overwrite with
  29.         * methodOffset: 0 = method 1, 1 = method 2 etc...
  30.         * return: original function
  31.         */
  32.  
  33.         static void* Install(void* pObjectInstance, void* hookFn, int methodOffset)
  34.         {
  35.             intptr_t vtable = *((intptr_t*)pObjectInstance);
  36.             intptr_t entry = vtable + sizeof(intptr_t) * methodOffset;
  37.             intptr_t original = *((intptr_t*)entry);
  38.  
  39.             int originalProtection = Unprotect((void*)entry);
  40.             *((intptr_t*)entry) = (intptr_t)hookFn;
  41.             Protect((void*)entry, originalProtection);
  42.  
  43.             return (void*)original;
  44.         }
  45.     };
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement