Guest User

Untitled

a guest
May 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. // Test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7.  
  8. #define SEPARATOR printf("====================\n")
  9.  
  10. class SomeClass
  11. {
  12. public:
  13. virtual void Init() {}
  14. virtual void Go() {}
  15. virtual void SomethingElse() {}
  16. virtual void Shutdown() {}
  17. };
  18.  
  19. void DumpVTable(void* baseclass)
  20. {
  21. void**& vtable = *(void***)baseclass;
  22.  
  23. printf("vtable: 0x%p\n", (void*)vtable);
  24.  
  25. for (size_t index = 0; vtable[index]; index++)
  26. {
  27. // Ignore outliers
  28. static uintptr_t last_entry = (uintptr_t)vtable[index];
  29. if (((uintptr_t)vtable[index] - last_entry) > 0xFFF) continue;
  30.  
  31. printf("[%i] 0x%p\n", index, (void*)vtable[index]);
  32. }
  33.  
  34. }
  35.  
  36. template <typename Fn> void*& Get(Fn a)
  37. {
  38. return (void*&)a;
  39. }
  40.  
  41. int main()
  42. {
  43. SEPARATOR;
  44.  
  45. SomeClass* instance = new SomeClass();
  46. printf("SomeClass instance: 0x%p\n", (void*)instance);
  47.  
  48. SEPARATOR;
  49.  
  50. printf("VTable address dump:\n");
  51.  
  52. DumpVTable(instance);
  53.  
  54. SEPARATOR;
  55.  
  56. SomeClass* other_instance = new SomeClass();
  57. printf("SomeClass other_instance: 0x%p\n", (void*)other_instance);
  58.  
  59. SEPARATOR;
  60.  
  61. printf("VTable address dump:\n");
  62.  
  63. DumpVTable(other_instance);
  64.  
  65. SEPARATOR;
  66.  
  67. auto init_address = &SomeClass::Init;
  68. void* init_ptr = Get(init_address);
  69.  
  70. printf("Address of SomeClass::Init 0x%p\n", init_ptr);
  71.  
  72. void**& instance_vtable = *(void***)instance;
  73.  
  74. printf("instance_vtable[0] + 0x%X: 0x%p\n", 0x2D, (uintptr_t)(instance_vtable[0]) + 0x2D);
  75.  
  76. auto go_address = &SomeClass::Go;
  77. void* go_ptr = Get(go_address);
  78.  
  79. printf("Address of SomeClass::Go 0x%p\n", go_ptr);
  80.  
  81. printf("instance_vtable[1] + 0x%X: 0x%p\n", 0x28, (uintptr_t)(instance_vtable[1]) + 0x28);
  82.  
  83. auto something_address = &SomeClass::SomethingElse;
  84. void* something_ptr = Get(go_address);
  85.  
  86. printf("Address of SomeClass::SomethingElse 0x%p\n", something_ptr);
  87.  
  88. printf("instance_vtable[2] + 0x%X: 0x%p\n", 0x1E, (uintptr_t)(instance_vtable[2]) + 0x1E);
  89.  
  90. auto shutdown_address = &SomeClass::Shutdown;
  91. void* shutdown_ptr = Get(shutdown_address);
  92.  
  93. printf("Address of SomeClass::SomethingElse 0x%p\n", shutdown_ptr);
  94.  
  95. printf("instance_vtable[4] + 0x%X: 0x%p\n", 0x2D, (uintptr_t)(instance_vtable[3]) + 0x2D);
  96.  
  97. std::cin.get();
  98.  
  99. return 0;
  100. }
Add Comment
Please, Sign In to add comment