Hook.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "stdafx.h"
  2. #include "define.h"
  3. #include "Hook.h"
  4. #include "zpzDll.h"
  5. #include "Detours/detours.h"
  6. #include "Detours/detver.h"
  7. #include <algorithm>
  8. extern bool g_isWork;
  9. extern HWND g_hWnd;
  10. extern char g_sComNum[MAX_PATH];
  11. extern wchar_t g_wsComNum[MAX_PATH];
  12. extern char g_data[MAX_DATA_LENGTH];
  13. extern int data_length;
  14. //这个是打开COM1的hport
  15. HANDLE g_hPort = NULL;
  16. //把这个被hook进程的工作的端口号保存下来
  17. std::string g_sworkComNum;
  18. std::wstring g_wsworkComNum;
  19. static HANDLE (WINAPI * Real_CreateFileW)(
  20. __in LPCWSTR lpFileName,
  21. __in DWORD dwDesiredAccess,
  22. __in DWORD dwShareMode,
  23. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  24. __in DWORD dwCreationDisposition,
  25. __in DWORD dwFlagsAndAttributes,
  26. __in_opt HANDLE hTemplateFile
  27. ) = CreateFileW;
  28. static HANDLE(WINAPI * Real_CreateFileA)(
  29. __in LPCSTR lpFileName,
  30. __in DWORD dwDesiredAccess,
  31. __in DWORD dwShareMode,
  32. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  33. __in DWORD dwCreationDisposition,
  34. __in DWORD dwFlagsAndAttributes,
  35. __in_opt HANDLE hTemplateFile
  36. ) = CreateFileA;
  37. static BOOL(WINAPI * Real_WriteFile)(
  38. __in HANDLE hFile,
  39. __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer,
  40. __in DWORD nNumberOfBytesToWrite,
  41. __out_opt LPDWORD lpNumberOfBytesWritten,
  42. __inout_opt LPOVERLAPPED lpOverlapped
  43. ) = WriteFile;
  44. HANDLE WINAPI Mine_CreateFileW(
  45. __in LPCWSTR lpFileName,
  46. __in DWORD dwDesiredAccess,
  47. __in DWORD dwShareMode,
  48. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  49. __in DWORD dwCreationDisposition,
  50. __in DWORD dwFlagsAndAttributes,
  51. __in_opt HANDLE hTemplateFile
  52. )
  53. {
  54. HANDLE hPort = Real_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  55. std::wstring wsFileName = lpFileName;
  56. std::transform(wsFileName.begin(), wsFileName.end(), wsFileName.begin(), ::toupper);
  57. std::wstring wsComNum = g_wsComNum;
  58. if (wsComNum != L"" && wsFileName.find(g_wsComNum) != wsFileName.npos)
  59. {
  60. //打开的是目标端口
  61. if (hPort != INVALID_HANDLE_VALUE)
  62. {
  63. //成功打开端口,保存端口句柄,开始监听
  64. g_hPort = hPort;
  65. //把这个进程监听的端口号,存起来;
  66. g_wsworkComNum = g_wsComNum;
  67. }
  68. }
  69. return hPort;
  70. }
  71. HANDLE WINAPI Mine_CreateFileA(
  72. __in LPCSTR lpFileName,
  73. __in DWORD dwDesiredAccess,
  74. __in DWORD dwShareMode,
  75. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  76. __in DWORD dwCreationDisposition,
  77. __in DWORD dwFlagsAndAttributes,
  78. __in_opt HANDLE hTemplateFile
  79. )
  80. {
  81. HANDLE hPort = Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  82. std::string FileName = lpFileName;
  83. std::transform(FileName.begin(), FileName.end(), FileName.begin(), ::toupper);
  84. std::string sComNum = g_sComNum;
  85. if (sComNum != "" && FileName.find(g_sComNum) != FileName.npos)
  86. {
  87. //打开的是目标端口
  88. if (hPort != INVALID_HANDLE_VALUE)
  89. {
  90. //成功打开端口,保存端口句柄,开始监听
  91. g_hPort = hPort;
  92. //把这个进程监听的端口号,存起来;
  93. g_sworkComNum = g_sComNum;
  94. }
  95. }
  96. return hPort;
  97. }
  98. BOOL WINAPI Mine_WriteFile(
  99. __in HANDLE hFile,
  100. __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer,
  101. __in DWORD nNumberOfBytesToWrite,
  102. __out_opt LPDWORD lpNumberOfBytesWritten,
  103. __inout_opt LPOVERLAPPED lpOverlapped
  104. )
  105. {
  106. BOOL ret = Real_WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
  107. //同时满足发送数据的句柄和创建目标端口时候的句柄相等,以及工作的端口号等于最新的端口号,才进行监听
  108. if (hFile == g_hPort && g_sworkComNum == g_sComNum)
  109. {
  110. //捕捉到一条客显消息,先把消息内容截取下来
  111. DWORD nWirten = nNumberOfBytesToWrite;
  112. if (nWirten > MAX_DATA_LENGTH)
  113. {
  114. //消息太长了,忽略掉
  115. return ret;
  116. }
  117. memcpy_s(g_data + data_length, nWirten, (char*)lpBuffer, nWirten);
  118. data_length += nWirten;
  119. //再发一个消息通知插件,去读取消息
  120. ::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0);
  121. }
  122. return ret;
  123. }
  124. void Hook()
  125. {
  126. LONG error;
  127. DetourRestoreAfterWith();
  128. DetourTransactionBegin();
  129. DetourUpdateThread(GetCurrentThread());
  130. DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  131. DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  132. DetourAttach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
  133. error = DetourTransactionCommit();
  134. if (error == NO_ERROR) {
  135. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  136. " Detoured Echo().\n");
  137. }
  138. else {
  139. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  140. " Error detouring Echo(): %d\n", error);
  141. }
  142. }
  143. void UnHook()
  144. {
  145. LONG error;
  146. DetourTransactionBegin();
  147. DetourUpdateThread(GetCurrentThread());
  148. DetourDetach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  149. DetourDetach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  150. DetourDetach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
  151. error = DetourTransactionCommit();
  152. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  153. " Removed Echo() (result=%d)\n", error);
  154. fflush(stdout);
  155. }