天马阁

 找回密码
 立即注册
                                        →→→→→→→→→→→→ 1点击查看所有VIP教程目录长列表(总教程数269个) 2办理VIP详情进入 ←←←←←←←←←←←←
1 x64CE与x64dbg入门基础教程 7课 已完结 2 x64汇编语言基础教程 16课 已完结 3 x64辅助入门基础教程 9课 已完结 4 C++x64内存辅助实战技术教程 149课 已完结
5 C++x64内存检测与过检测技术教程 10课 已完结 6 C+x64二叉树分析遍历与LUA自动登陆教程 19课已完结 7 C++BT功能原理与x64实战教程 29课 已完结 8 C+FPS框透视与自瞄x64实现原理及防护思路 30课完结
64驱?封? 9 64反驱? 10 64位V? 11 绝? 12 ???课?
13 64透 ? 14 64U ? 15 64Q ? 16 64功 ?
17 64U ? 18 64模 ? 19 64多 ? 20 64网 ?
21 64注 ? 22 64火 ? 23 64棋 ? 24 64自二链L?
25 64破 ? VIP会员办理QQ: 89986068   
【请先加好友,然后到好友列表双击联系客服办理,不然可能无法接受到信息。】
27 加入2000人交流群637034024 3 28 免责声明?
查看: 4389|回复: 0

MHOOK的使用

[复制链接]

12

主题

0

回帖

14

积分

编程入门

Rank: 1

天马币
24
发表于 2024-3-4 09:18:28 | 显示全部楼层 |阅读模式
之前写辅助都用的是网上的帖子HOOK目标程序的send和recv,然后采用WriteProcessMemory替换我们的函数和Windows API函数,不断的来回切换,这个其实数据密集的时候容易出错也不稳定,不提倡!并且代码输写很容易出错。这里我贴出我自己写过的HOOK库,用过攻城略地和大皇帝等等游戏,均通过测试,非常之稳定
  1. 把MHOOK封装成动态链接库,直接调用DLL文件进行HOOK目标函数,也可以实现远程注入。
  2. 并且不会被游戏反辅助发觉。动态链接库代码如下:

  3. #ifdef _M_IX86
  4. #define _M_IX86_X64
  5. #elif defined _M_X64
  6. #define _M_IX86_X64
  7. #endif

  8. BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction);
  9. BOOL Mhook_Unhook(PVOID *ppHookedFunction);

  10. #define MHOOKS_MAX_SUPPORTED_HOOKS 64
  11. 我这里只针对HOOK socket的send和recv

  12. #include "stdafx.h"
  13. #include "mhook-lib/mhook.h"

  14. extern mhook_func _msend;
  15. extern mhook_func _mrecv;
  16. extern mhook_func _mwsend;
  17. extern mhook_func _mwrecv;

  18. //ppSystemFunction为系统API,pHookFunction为自己定义的API
  19. BOOL t001(PVOID *ppSystemFunction, PVOID pHookFunction)
  20. {
  21. return Mhook_SetHook(ppSystemFunction,pHookFunction);
  22. }

  23. //pHookFunction为自己定义的API
  24. BOOL t002(PVOID *ppHookedFunction)
  25. {
  26. return Mhook_Unhook(ppHookedFunction);
  27. }

  28. //设置1.0函数地址
  29. BOOL t003(mhook_func pHookSendFunc,mhook_func pHookRecvFuc)
  30. {
  31. _msend = pHookSendFunc;
  32. _mrecv = pHookRecvFuc;
  33. return TRUE;
  34. }

  35. //设置2.0函数地址
  36. BOOL t004(mhook_func pHookSendFunc,mhook_func pHookRecvFuc)
  37. {
  38. _mwsend = pHookSendFunc;
  39. _mwrecv = pHookRecvFuc;
  40. return TRUE;
  41. }


  42. DLL入口文件

  43. #include "stdafx.h"
  44. #include "mhook-lib/mhook.h"
  45. #include

  46. //////////////封包函数//////////////
  47. static void GT_WriteReleaseLog(char* str,char* path="C:\mk.log")
  48. {
  49. HANDLE hFile = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  50. if(hFile == NULL)
  51. return;
  52. //设置文件中进行读写的当前位置
  53. _llseek((HFILE)hFile,0, SEEK_END);
  54. DWORD dw;
  55. WriteFile(hFile,str,strlen(str),&dw,NULL);
  56. _lclose((HFILE)hFile);
  57. }
  58. HMODULE hMod = LoadLibraryA("Ws2_32");
  59. //1.0
  60. typedef int (WINAPI *_send)(SOCKET s, const char *buf, int len, int flags);
  61. typedef int (WINAPI *_recv)(SOCKET s, char *buf, int len, int flags);
  62. _send g_trueSend = (_send)GetProcAddress(hMod,"send");
  63. _recv g_trueRecv = (_recv)GetProcAddress(hMod,"recv");
  64. //2.0
  65. typedef int (WINAPI *_wsend)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
  66. LPDWORD lpNumberOfBytesSent,DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped,
  67. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
  68. typedef int (WINAPI *_wrecv)(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
  69. LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped,
  70. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
  71. _wsend g_trueWSend = (_wsend)GetProcAddress(hMod,"WSASend");
  72. _wrecv g_trueWRecv = (_wrecv)GetProcAddress(hMod,"WSARecv");

  73. mhook_func _msend = NULL;
  74. mhook_func _mrecv = NULL;
  75. mhook_func _mwsend = NULL;
  76. mhook_func _mwrecv = NULL;

  77. /**
  78. 参数描述:
  79. SOCKET s 发送端套接字描述符
  80. const char *buf 应用程序要发送的数据的缓冲区(想要发送的数据)
  81. int len 实际要发送的字节数
  82. int flags 一般置为0即可
  83. 如果没有错误发生,send将返回的总字节数发送
  84. */
  85. int WINAPI hook_send(SOCKET s, const char *buf, int len, int flags)
  86. {
  87. int ret = g_trueSend(s,buf,len,flags);
  88. if (ret > 0)
  89. {
  90. char *temp = new char[ret];
  91. memcpy_s(temp,ret,buf,ret);
  92. if(_msend != NULL)
  93. _msend(s,temp,ret);
  94. delete temp;
  95. }
  96. return ret;
  97. }

  98. /**
  99. 参数描述:
  100. SOCKET s 发送端套接字描述符
  101. const char *buf 应用程序存放接收的数据的缓冲区
  102. int len buf的长度
  103. int flags 一般置为0即可
  104. 如果没有错误发生,recv返回的字节数的接收
  105. */
  106. int WINAPI hook_recv(SOCKET s, char *buf, int len, int flags)
  107. {
  108. int ret = g_trueRecv(s,buf,len,flags);
  109. if (ret > 0)
  110. {
  111. char *temp = new char[ret];
  112. memcpy_s(temp,ret,buf,ret);
  113. if(_msend != NULL)
  114. _mrecv(s,temp,ret);
  115. delete temp;
  116. }
  117. return ret;
  118. }

  119. /*
  120. s:标识一个已连接套接口的描述字。
  121. lpBuffers:一个指向WSABUF结构数组的指针。每个WSABUF结构包含缓冲区的指针和缓冲区的大小。
  122. dwBufferCount:lpBuffers数组中WSABUF结构的数目。
  123. lpNumberOfBytesSent:如果发送操作立即完成,则为一个指向所发送数据字节数的指针。
  124. dwFlags:标志位。
  125. lpOverlapped:指向WSAOVERLAPPED结构的指针(对于非重叠套接口则忽略)。
  126. lpCompletionRoutine:一个指向发送操作完成后调用的完成例程的指针。(对于非重叠套接口则忽略)。
  127. 若无错误发生且发送操作立即完成,则WSASend()函数返回0
  128. */
  129. int WINAPI hook_wsend(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
  130. LPDWORD lpNumberOfBytesSent,DWORD dwFlags, LPWSAOVERLAPPED lpOverlapped,
  131. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
  132. {
  133. int ret = g_trueWSend(s,lpBuffers,dwBufferCount,lpNumberOfBytesSent
  134. ,dwFlags,lpOverlapped,lpCompletionRoutine);
  135. DWORD len = *lpNumberOfBytesSent;
  136. if (len > 0)
  137. {
  138. char *temp = new char[len];
  139. memcpy_s(temp,len,lpBuffers->buf,len);
  140. if(_mwsend != NULL)
  141. _mwsend(s,temp,len);
  142. delete temp;
  143. }
  144. return ret;
  145. }

  146. int WINAPI hook_wrecv(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
  147. LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped,
  148. LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
  149. {
  150. int ret = g_trueWRecv(s,lpBuffers,dwBufferCount,lpNumberOfBytesRecvd,lpFlags
  151. ,lpOverlapped,lpCompletionRoutine);
  152. DWORD len = *lpNumberOfBytesRecvd;
  153. if (len > 0)
  154. {
  155. char *temp = new char[len];
  156. memcpy_s(temp,len,lpBuffers->buf,len);
  157. if(_mwrecv != NULL)
  158. _mwrecv(s,temp,len);
  159. delete temp;
  160. }
  161. return ret;
  162. }

  163. BOOL APIENTRY DllMain(HMODULE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
  164. {
  165. switch (ul_reason_for_call)
  166. {
  167. case DLL_PROCESS_ATTACH:
  168. //直接在这里HOOK SEND和RECV函数
  169. Mhook_SetHook((LPVOID*)&g_trueSend,hook_send);
  170. Mhook_SetHook((LPVOID*)&g_trueRecv,hook_recv);
  171. Mhook_SetHook((LPVOID*)&g_trueWSend,hook_wsend);
  172. Mhook_SetHook((LPVOID*)&g_trueWRecv,hook_wrecv);
  173. break;
  174. case DLL_THREAD_ATTACH:
  175. break;
  176. case DLL_THREAD_DETACH:
  177. break;
  178. case DLL_PROCESS_DETACH:
  179. //直接在这里UNHOOK SEND和RECV函数)
  180. Mhook_Unhook((LPVOID*)&g_trueSend);
  181. Mhook_Unhook((LPVOID*)&g_trueRecv);
  182. Mhook_Unhook((LPVOID*)&g_trueWSend);
  183. Mhook_Unhook((LPVOID*)&g_trueWRecv);
  184. if(hModule != NULL)
  185. FreeLibrary(hModule);
  186. break;
  187. }
  188. return TRUE;
  189. }
  190. 最后加上DEF文件


  191. LIBRARY

  192. EXPORTS
  193. ; 此处可以是显式导出
  194. t001 @1
  195. t002 @2
  196. t003 @3
  197. t004 @4
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

天马阁|C/C++辅助教程|安卓逆向安全| 论坛导航|免责申明|Archiver||网站地图
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表天马阁立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2010-2021 All Right Reserved.
快速回复 返回顶部 返回列表