Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <windows.h>
  4. #include <cstdint>
  5. #include <vector>
  6. #include <functional>
  7.  
  8. namespace n_vmt {
  9. class c_vmt {
  10. friend class c_vmt_manager; // lol this is probably a bad idea but i dont want all the class members being public
  11. public:
  12. // used for copying the virtual function table ( vft )
  13. bool setup( void* base_class );
  14.  
  15. // used for replacing an existing function pointer with or own
  16. bool hook( const std::uintptr_t index, void* function );
  17.  
  18. // used to revert all virtual functions back to there originals - restores the original vft
  19. bool unload( void );
  20.  
  21. private:
  22. // used for storing the number of virtual functions in the vft
  23. std::uintptr_t m_function_count{ 0 };
  24.  
  25. // anonymouse struct to store information about the vft
  26. struct {
  27. // used for storing the all of the virtual functions in the vft ( doesnt get modified )
  28. std::uintptr_t* m_original{ nullptr };
  29.  
  30. // used for overriding the address's of the virtual functions in the vft to our own function pointer ( gets modified )
  31. std::uintptr_t* m_normal{ nullptr };
  32. } m_vft;
  33.  
  34. };
  35.  
  36. class c_vmt_manager {
  37. public:
  38. // hooks vft and uses lamda in conjunction with std::function to hook virtual functions
  39. bool hook_vft( void* base_class, std::function< bool( c_vmt* ) > function );
  40.  
  41. // restores all of the virtual functions in the vft
  42. bool unload( void );
  43.  
  44. // return the original function of the virtual function
  45. template< typename t > t get_original_fn( void* function ) {
  46. // iterate over hooked vfts
  47. for ( auto vft : this->m_vfts )
  48. // iterate through all the virtual functions in the vtf
  49. for ( std::uintptr_t i = 0; i < vft.m_function_count; i++ )
  50. // if the hooked function matches
  51. if ( vft.m_vft.m_normal[ i ] == reinterpret_cast< std::uintptr_t >( function ) )
  52. // return the original
  53. return ( t )vft.m_vft.m_original[ i ];
  54.  
  55. // if we didnt find any match
  56. return nullptr;
  57. }
  58.  
  59. // unhook SPECIFIC vtable
  60. template< typename t > bool unhook( t base_class ) {
  61. // getting the class pointer from the base class
  62. // cast the base class pointer to a two level pointer because the pointer behind the class pointer is the vft pointer
  63. auto class_ptr = reinterpret_cast< std::uintptr_t** >( base_class );
  64.  
  65. // create a variable on the stack to store the vft pointer
  66. auto vft_ptr = *class_ptr;
  67.  
  68. const auto iterator = std::find_if( this->m_vfts.begin( ), this->m_vfts.end( ), [ & ]( const c_vmt& vmt ) {
  69. return vmt.m_vft.m_normal == vft_ptr;
  70. } );
  71.  
  72. // if the iterator is valid
  73. if ( this->m_vfts.end( ) != iterator ) {
  74. // unload the vtable
  75. this->m_vfts.at( std::uintptr_t( iterator - this->m_vfts.begin( ) ) ).unload( );
  76.  
  77. // remove it from the list
  78. this->m_vfts.erase( iterator );
  79.  
  80. // function succeeded
  81. return true;
  82. }
  83. return false;
  84. }
  85.  
  86. private:
  87. // used for storing all of the vfts hooked
  88. std::vector< c_vmt > m_vfts{ };
  89. };
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement