Hook.cpp 5.2 KB

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