1
0

MakeThunk.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef NULLSOFT_MAKETHUNKH
  2. #define NULLSOFT_MAKETHUNKH
  3. #include <vector>
  4. class ThunkHolder
  5. {
  6. private:
  7. #pragma pack(push,1)
  8. class ThisThunk
  9. {
  10. private:
  11. unsigned __int8 mov_eax_imm32;
  12. unsigned __int32 save_ebx;
  13. unsigned __int16 mov_reax_ebx;
  14. unsigned __int8 pop_ebx;
  15. unsigned __int8 push_imm32;
  16. unsigned __int32 m_this;
  17. unsigned __int8 m_call_rel32;
  18. unsigned __int32 m_rel_proc;
  19. unsigned __int8 m_pop_eax;
  20. unsigned __int8 m_push_ebx;
  21. unsigned __int8 m_mov_ecx_imm32_2;
  22. unsigned __int32 m_restore_ebx;
  23. unsigned __int16 m_mov_ebx_recx;
  24. unsigned __int8 m_ret;
  25. unsigned __int32 m_ebx;
  26. public:
  27. template <class class_t, class proc_t>
  28. ThisThunk(class_t *pThis, proc_t proc)
  29. {
  30. __int32 procAdr = *(__int32 *) & proc;
  31. /* first, save ebx to memory,
  32. effectively: save_ebx = ebx;
  33. */
  34. mov_eax_imm32 = 0xB8;
  35. save_ebx = (__int32) & m_ebx;
  36. mov_reax_ebx = 0x1889;
  37. pop_ebx = 0x5B;
  38. push_imm32 = 0x68;
  39. m_this = (__int32)pThis;
  40. m_call_rel32 = 0xE8;
  41. m_rel_proc = procAdr - (__int32) & m_pop_eax;
  42. m_pop_eax = 0x59;
  43. m_push_ebx = 0x53;
  44. m_mov_ecx_imm32_2 = 0xB9;
  45. m_restore_ebx = (__int32) & m_ebx;
  46. m_mov_ebx_recx = 0x198B;
  47. m_ret = 0xC3;
  48. }
  49. /*
  50. mov eax, &save_ebx
  51. mov [eax], ebx
  52. pop ebx
  53. push pThis
  54. call rel32 m_relproc
  55. pop ecx
  56. push ebx
  57. mov ecx, &save_ebx
  58. mov ebx, [ecx]
  59. ret
  60. */
  61. };
  62. #pragma pack(pop)
  63. public:
  64. template <class class_t, class proc_t, class this_proc_t>
  65. void operator ()(class_t *pThis, proc_t &proc, this_proc_t thisProc)
  66. {
  67. ThisThunk *newThunk = new ThisThunk(pThis, thisProc);
  68. thunks.push_back(newThunk);
  69. proc = (proc_t)newThunk;
  70. }
  71. ~ThunkHolder()
  72. {
  73. std::vector<ThisThunk *>::iterator itr;
  74. for (itr = thunks.begin();itr != thunks.end();itr++)
  75. {
  76. delete (*itr);
  77. *itr = 0;
  78. }
  79. thunks.clear();
  80. }
  81. std::vector<ThisThunk *> thunks;
  82. };
  83. #endif