Guest User

Untitled

a guest
Jul 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. // Compile: g++ -o iathook.exe .\iathook.cpp
  2. #include <Windows.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7. #define MH_ALL_HOOKS NULL
  8.  
  9. // Resturn status enums
  10. typedef enum MH_STATUS {
  11. MH_UNKNOWN = -1,
  12. MH_OK = 0
  13. } MH_STATUS;
  14.  
  15. HINSTANCE dllHandle;
  16.  
  17. // Definitions for the MinHook functions
  18. typedef MH_STATUS(WINAPI* _MH_Initialize)(VOID);
  19. _MH_Initialize MH_Initialize;
  20. typedef MH_STATUS(WINAPI* _MH_Uninitialize)(VOID);
  21. _MH_Uninitialize MH_Uninitialize;
  22. typedef MH_STATUS(WINAPI* _MH_RemoveHook)(LPVOID);
  23. _MH_RemoveHook MH_RemoveHook;
  24. typedef MH_STATUS(WINAPI* _MH_EnableHook)(LPVOID);
  25. _MH_EnableHook MH_EnableHook;
  26. typedef MH_STATUS(WINAPI* _MH_DisableHook)(LPVOID);
  27. _MH_DisableHook MH_DisableHook;
  28. typedef MH_STATUS(WINAPI* _MH_CreateHook)(LPVOID, LPVOID, LPVOID);
  29. _MH_CreateHook MH_CreateHook;
  30.  
  31. // Definition for MessageBoxA function
  32. typedef int(WINAPI* _MessageBoxA)(HWND, LPCTSTR, LPCTSTR, UINT);
  33. // fpMessageBoxA - we will use it to store original pointer to the funxtion
  34. _MessageBoxA fpMessageBoxA;
  35.  
  36. bool setup() { // Load functions used by MinHook
  37. dllHandle = LoadLibraryA("MinHook.dll");
  38. if(!dllHandle) {
  39. cout << "[-] Failed to load the dll" << endl;
  40. return false;
  41. }
  42.  
  43. MH_Initialize = (_MH_Initialize)GetProcAddress(dllHandle, "MH_Initialize");
  44. if(!MH_Initialize) {
  45. cout << "[-] MH_Initialize" << endl;
  46. return false;
  47. }
  48.  
  49. MH_Uninitialize = (_MH_Uninitialize)GetProcAddress(dllHandle, "MH_Uninitialize");
  50. if(!MH_Uninitialize) {
  51. cout << "[-] MH_Uninitialize" << endl;
  52. return false;
  53. }
  54.  
  55. MH_RemoveHook = (_MH_RemoveHook)GetProcAddress(dllHandle, "MH_RemoveHook");
  56. if(!MH_RemoveHook) {
  57. cout << "[-] MH_RemoveHook" << endl;
  58. return false;
  59. }
  60.  
  61. MH_EnableHook = (_MH_EnableHook)GetProcAddress(dllHandle, "MH_EnableHook");
  62. if(!MH_EnableHook) {
  63. cout << "[-] MH_EnableHook" << endl;
  64. return false;
  65. }
  66.  
  67. MH_DisableHook = (_MH_DisableHook)GetProcAddress(dllHandle, "MH_DisableHook");
  68. if(!MH_DisableHook) {
  69. cout << "[-] MH_DisableHook" << endl;
  70. return false;
  71. }
  72.  
  73. MH_CreateHook = (_MH_CreateHook)GetProcAddress(dllHandle, "MH_CreateHook");
  74. if(!MH_CreateHook) {
  75. cout << "[-] MH_CreateHook" << endl;
  76. return false;
  77. }
  78.  
  79. return true;
  80. }
  81.  
  82. // Hooked function
  83. int MyMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uInt) {
  84. cout << "MessageBoxA was called" << endl;
  85. cout << "Caption: " << lpText << endl;
  86. cout << "Text: " << lpText << endl;
  87. cout << "[Press Enter to display the message]";
  88. getchar();
  89.  
  90. // Call the original function
  91. fpMessageBoxA(hWnd, "Modified text, just because we can :)", lpCaption, uInt);
  92. }
  93.  
  94. int main() {
  95. if(!setup()) {
  96. cout << "[-] Setup failed" << endl;
  97. } else {
  98. cout << "[+] Setup completed successfully" << endl;
  99. }
  100.  
  101. // Initialize MinHook.
  102. if (MH_Initialize() != MH_OK) {
  103. cout << "[-] Failed to Initialize MinHook" << endl;
  104. return 0;
  105. }
  106.  
  107. // Create hook
  108. if (MH_CreateHook((LPVOID)&MessageBoxA, (LPVOID)&MyMessageBoxA, (LPVOID)&fpMessageBoxA) != MH_OK)
  109. cout << "[-] Failed to create hook for CreateProcessA" << endl;
  110. else
  111. cout << "[+] CreateProcessA hook created successfully" << endl;
  112.  
  113. MessageBoxA(NULL, "MessageBoxA text, unhooked!", "Caption", MB_OK);
  114. // Enable hook
  115. if(MH_EnableHook((LPVOID)&MessageBoxA) != MH_OK) {
  116. cout << "[-] Failed to enable hook" << endl;
  117. }
  118. MessageBoxA(NULL, "MessageBoxA text, unhooked!", "Caption", MB_OK);
  119.  
  120. // Cleanup
  121. MH_DisableHook(MH_ALL_HOOKS);
  122. MH_RemoveHook(MH_ALL_HOOKS);
  123.  
  124. MH_Uninitialize();
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment