Hook.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "stdafx.h"
  2. #include "Hook.h"
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include "Detours/detours.h"
  6. #include "Detours/detver.h"
  7. #include <string>
  8. #define WM_HOOK_MESSAGE 12222
  9. extern HWND g_hWnd;
  10. static HANDLE (WINAPI * Real_CreateFileW)(
  11. __in LPCWSTR lpFileName,
  12. __in DWORD dwDesiredAccess,
  13. __in DWORD dwShareMode,
  14. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  15. __in DWORD dwCreationDisposition,
  16. __in DWORD dwFlagsAndAttributes,
  17. __in_opt HANDLE hTemplateFile
  18. ) = CreateFileW;
  19. static HANDLE(WINAPI * Real_CreateFileA)(
  20. __in LPCSTR 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. ) = CreateFileA;
  28. static BOOL(WINAPI * Real_WriteFile)(
  29. __in HANDLE hFile,
  30. __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer,
  31. __in DWORD nNumberOfBytesToWrite,
  32. __out_opt LPDWORD lpNumberOfBytesWritten,
  33. __inout_opt LPOVERLAPPED lpOverlapped
  34. ) = WriteFile;
  35. HANDLE WINAPI Mine_CreateFileW(
  36. __in LPCWSTR lpFileName,
  37. __in DWORD dwDesiredAccess,
  38. __in DWORD dwShareMode,
  39. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  40. __in DWORD dwCreationDisposition,
  41. __in DWORD dwFlagsAndAttributes,
  42. __in_opt HANDLE hTemplateFile
  43. )
  44. {
  45. std::wstring wsFileName = lpFileName;
  46. if (wsFileName.find(L"COM1") != wsFileName.npos || wsFileName.find(L"com1") != wsFileName.npos)
  47. {
  48. MessageBox(NULL, L"1111", L"1111", MB_OK);
  49. //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0);
  50. }
  51. return Real_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  52. }
  53. HANDLE WINAPI Mine_CreateFileA(
  54. __in LPCSTR lpFileName,
  55. __in DWORD dwDesiredAccess,
  56. __in DWORD dwShareMode,
  57. __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  58. __in DWORD dwCreationDisposition,
  59. __in DWORD dwFlagsAndAttributes,
  60. __in_opt HANDLE hTemplateFile
  61. )
  62. {
  63. std::string FileName = lpFileName;
  64. if (FileName.find("COM1") != FileName.npos || FileName.find("com1") != FileName.npos)
  65. {
  66. MessageBox(NULL, L"1111", L"1111", MB_OK);
  67. //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0);
  68. }
  69. return Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  70. }
  71. BOOL WINAPI Mine_WriteFile(
  72. __in HANDLE hFile,
  73. __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer,
  74. __in DWORD nNumberOfBytesToWrite,
  75. __out_opt LPDWORD lpNumberOfBytesWritten,
  76. __inout_opt LPOVERLAPPED lpOverlapped
  77. )
  78. {
  79. //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0);
  80. return Real_WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped);
  81. }
  82. void Hook()
  83. {
  84. LONG error;
  85. DetourRestoreAfterWith();
  86. DetourTransactionBegin();
  87. DetourUpdateThread(GetCurrentThread());
  88. DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  89. DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  90. DetourAttach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
  91. error = DetourTransactionCommit();
  92. if (error == NO_ERROR) {
  93. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  94. " Detoured Echo().\n");
  95. }
  96. else {
  97. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  98. " Error detouring Echo(): %d\n", error);
  99. }
  100. }
  101. void UnHook()
  102. {
  103. LONG error;
  104. DetourTransactionBegin();
  105. DetourUpdateThread(GetCurrentThread());
  106. DetourDetach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW);
  107. DetourDetach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA);
  108. DetourDetach(&(PVOID&)Real_WriteFile, Mine_WriteFile);
  109. error = DetourTransactionCommit();
  110. printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:"
  111. " Removed Echo() (result=%d)\n", error);
  112. fflush(stdout);
  113. }