Snorflake

my shitty vmt class

May 27th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. Example usage
  2. CVMTHookManager* PanelHook = new CVMTHookManager(Interfaces.Panel);
  3. orgPaintTraverse = (PaintTraverseFn)PanelHook->HookFunction(41, hkPaintTraverse);
  4. PanelHook->HookTable(true);
  5. #pragma once
  6. #include "sdk.h"
  7. #define k_page_writeable (PAGE_READWRITE | PAGE_EXECUTE_READWRITE)
  8. #define k_page_readable (k_page_writeable|PAGE_READONLY|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_WRITECOPY)
  9. #define k_page_offlimits (PAGE_GUARD|PAGE_NOACCESS)
  10. /*Credits to null for helping me with this. -Snorflake*/
  11. inline void* GetFunction(void* Instance, int Index)
  12. {
  13. DWORD VirtualFunction = (*(DWORD*)Instance) + sizeof(DWORD)* Index;
  14. return (void*)*((DWORD*)VirtualFunction);
  15. }
  16. class CVMTHookManager
  17. {
  18. public:
  19. CVMTHookManager(){}
  20. /*Deconstructor*/
  21. ~CVMTHookManager(){
  22. this->HookTable(false);
  23. }
  24. CVMTHookManager(void* Interface)
  25. {
  26. this->Init(Interface);
  27. }
  28. /*Inits the function*/
  29. bool Init(void* Interface)
  30. {
  31. pOrgTable = *(void**)Interface;
  32. this->count = this->GetCount();
  33. pCopyTable = malloc(sizeof(void*)* count);
  34. memcpy(pCopyTable, pOrgTable, sizeof(void*)* count);
  35. pObject = (DWORD*)Interface;
  36. return true;
  37. }
  38. /*Hook/Unhook*/
  39. void HookTable(bool hooked)
  40. {
  41. if (hooked)
  42. {
  43. *pObject = (DWORD)pCopyTable;
  44. }
  45. else
  46. {
  47. *pObject = (DWORD)pOrgTable;
  48. }
  49. }
  50. /*Hooks function*/
  51. void* HookFunction(int Index, void* hkFunction)
  52. {
  53. if (Index < this->count && Index >= 0)
  54. {
  55. ((DWORD*)pCopyTable)[Index] = (DWORD)hkFunction;
  56. return (void*)((DWORD*)pOrgTable)[Index];
  57. }
  58. return NULL;
  59. }
  60.  
  61. private:
  62. /*Returns if you can read the pointer*/
  63. bool CanReadPointer(void* table)
  64. {
  65. MEMORY_BASIC_INFORMATION mbi;
  66. if (table == nullptr) return false;
  67. if (!VirtualQuery(table, &mbi, sizeof(mbi))) return false;
  68. if (mbi.Protect & k_page_offlimits) return false;
  69. return (mbi.Protect & k_page_readable);
  70. }
  71. /*Gets VMT count*/
  72. int GetCount()
  73. {
  74. int index = 0;
  75. void** table = ((void**)pOrgTable);
  76. for (void* fn; (fn = table[index]) != nullptr; index++)
  77. {
  78. if (!this->CanReadPointer(fn)) break;
  79. }
  80. return index;
  81. }
  82. int count;
  83. void* pCopyTable;
  84. DWORD* pObject;
  85. void* pOrgTable;
  86. };
Advertisement
Add Comment
Please, Sign In to add comment