Guest User

Untitled

a guest
Jun 2nd, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cstdint>
  4. #include <memory>
  5.  
  6. template<class entity>
  7. class c_hook
  8. {
  9. public:
  10. explicit c_hook(entity* ent)
  11. {
  12. base = reinterpret_cast<uintptr_t*>(ent);
  13. original = *base;
  14.  
  15. const auto l = length() + 1;
  16. current = std::make_unique<uint32_t[]>(l);
  17. std::memcpy(current.get(), reinterpret_cast<void*>(original - sizeof(uint32_t)), l * sizeof(uint32_t));
  18.  
  19. patch_pointer(base);
  20. }
  21.  
  22. template<typename function, typename original_function>
  23. function apply(const uint32_t index, original_function func)
  24. {
  25. auto old = reinterpret_cast<uintptr_t*>(original)[index];
  26. current.get()[index + 1] = reinterpret_cast<uintptr_t>(func);
  27. return reinterpret_cast<function>(old);
  28. }
  29.  
  30. void patch_pointer(uintptr_t* location) const
  31. {
  32. if (!location)
  33. return;
  34.  
  35. DWORD old;
  36. PVOID address = location;
  37. ULONG size = sizeof(uintptr_t);
  38. syscall(NtProtectVirtualMemory)(current_process, &address, &size, PAGE_READWRITE, &old);
  39. address = location;
  40. size = sizeof(uintptr_t);
  41. *location = reinterpret_cast<uint32_t>(current.get()) + sizeof(uint32_t);
  42. syscall(NtProtectVirtualMemory)(current_process, &address, &size, old, &old);
  43. }
  44.  
  45. private:
  46. uint32_t length() const
  47. {
  48. uint32_t index;
  49. const auto vmt = reinterpret_cast<uint32_t*>(original);
  50.  
  51. for (index = 0; vmt[index]; index++)
  52. if (IS_INTRESOURCE(vmt[index]))
  53. break;
  54.  
  55. return index;
  56. }
  57.  
  58. std::uintptr_t* base;
  59. std::uintptr_t original;
  60. std::unique_ptr<uint32_t[]> current;
  61. };
Advertisement
Add Comment
Please, Sign In to add comment