#include "stdafx.h" #include "Hook.h" #include #include #include "Detours/detours.h" #include "Detours/detver.h" #include #define WM_HOOK_MESSAGE 12222 extern HWND g_hWnd; static HANDLE (WINAPI * Real_CreateFileW)( __in LPCWSTR lpFileName, __in DWORD dwDesiredAccess, __in DWORD dwShareMode, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __in DWORD dwCreationDisposition, __in DWORD dwFlagsAndAttributes, __in_opt HANDLE hTemplateFile ) = CreateFileW; static HANDLE(WINAPI * Real_CreateFileA)( __in LPCSTR lpFileName, __in DWORD dwDesiredAccess, __in DWORD dwShareMode, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __in DWORD dwCreationDisposition, __in DWORD dwFlagsAndAttributes, __in_opt HANDLE hTemplateFile ) = CreateFileA; static BOOL(WINAPI * Real_WriteFile)( __in HANDLE hFile, __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer, __in DWORD nNumberOfBytesToWrite, __out_opt LPDWORD lpNumberOfBytesWritten, __inout_opt LPOVERLAPPED lpOverlapped ) = WriteFile; HANDLE WINAPI Mine_CreateFileW( __in LPCWSTR lpFileName, __in DWORD dwDesiredAccess, __in DWORD dwShareMode, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __in DWORD dwCreationDisposition, __in DWORD dwFlagsAndAttributes, __in_opt HANDLE hTemplateFile ) { std::wstring wsFileName = lpFileName; if (wsFileName.find(L"COM1") != wsFileName.npos || wsFileName.find(L"com1") != wsFileName.npos) { MessageBox(NULL, L"1111", L"1111", MB_OK); //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0); } return Real_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); } HANDLE WINAPI Mine_CreateFileA( __in LPCSTR lpFileName, __in DWORD dwDesiredAccess, __in DWORD dwShareMode, __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes, __in DWORD dwCreationDisposition, __in DWORD dwFlagsAndAttributes, __in_opt HANDLE hTemplateFile ) { std::string FileName = lpFileName; if (FileName.find("COM1") != FileName.npos || FileName.find("com1") != FileName.npos) { MessageBox(NULL, L"1111", L"1111", MB_OK); //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0); } return Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); } BOOL WINAPI Mine_WriteFile( __in HANDLE hFile, __in_bcount_opt(nNumberOfBytesToWrite) LPCVOID lpBuffer, __in DWORD nNumberOfBytesToWrite, __out_opt LPDWORD lpNumberOfBytesWritten, __inout_opt LPOVERLAPPED lpOverlapped ) { //::PostMessage(g_hWnd, WM_HOOK_MESSAGE, 0, 0); return Real_WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped); } void Hook() { LONG error; DetourRestoreAfterWith(); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW); DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA); DetourAttach(&(PVOID&)Real_WriteFile, Mine_WriteFile); error = DetourTransactionCommit(); if (error == NO_ERROR) { printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" " Detoured Echo().\n"); } else { printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" " Error detouring Echo(): %d\n", error); } } void UnHook() { LONG error; DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourDetach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW); DetourDetach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA); DetourDetach(&(PVOID&)Real_WriteFile, Mine_WriteFile); error = DetourTransactionCommit(); printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" " Removed Echo() (result=%d)\n", error); fflush(stdout); }