#include "stdafx.h" #include "Hook.h" #include #include #include "Detours/detours.h" #include "Detours/detver.h" #include 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; 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 == L"COM1" || wsFileName == L"com1") { MessageBox(NULL, L"1111111111", L"111111111111111111111", MB_OK); } 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 == "COM1" || FileName == "com1") { MessageBox(NULL, L"1111111111", L"2222", MB_OK); } return Real_CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); } void Hook() { LONG error; DetourRestoreAfterWith(); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&(PVOID&)Real_CreateFileW, Mine_CreateFileW); DetourAttach(&(PVOID&)Real_CreateFileA, Mine_CreateFileA); 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); error = DetourTransactionCommit(); printf("echofx" DETOURS_STRINGIFY(DETOURS_BITS) ".dll:" " Removed Echo() (result=%d)\n", error); fflush(stdout); }