Last active
July 28, 2016 08:45
-
-
Save RElesgoe/80fdcb420f915179892cc82ac70cdfa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MinHook.h" | |
#if defined _M_X64 | |
#pragma comment(lib, "libMinHook.x64.lib") | |
#elif defined _M_IX86 | |
#pragma comment(lib, "libMinHook.x86.lib") | |
#endif | |
#include <cstdio> | |
#include <cstring> | |
#include <fstream> | |
#include <functional> | |
#include <iostream> | |
#include <string> | |
#include <Windows.h> | |
using FreeLibraryProc = BOOL (WINAPI *)(HMODULE); | |
FreeLibraryProc fpFreeLibrary = nullptr; | |
static std::ofstream logger; | |
BOOL WINAPI _FreeLibrary(HMODULE hModule) | |
{ | |
logger << "Intercepting call to FreeLibrary()" << std::endl; | |
return fpFreeLibrary(hModule); | |
} | |
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID) | |
{ | |
switch (fdwReason) | |
{ | |
case DLL_PROCESS_ATTACH: | |
if (fpFreeLibrary != nullptr) | |
break; | |
logger.open("bwhook.log", std::ios::out | std::ios::app); | |
logger << "Logging started" << std::endl; | |
if (MH_Initialize() != MH_OK) | |
{ | |
logger << "Failed to initialize MinHook" << std::endl; | |
logger.close(); | |
return FALSE; | |
} | |
if (MH_CreateHook(&FreeLibrary, &_FreeLibrary, reinterpret_cast<LPVOID *>(&fpFreeLibrary)) != MH_OK) | |
{ | |
logger << "Failed to create a FreeLibrary hook" << std::endl; | |
logger.close(); | |
return FALSE; | |
} | |
if (MH_EnableHook(&FreeLibrary) != MH_OK) | |
{ | |
logger << "Failed to enable FreeLibrary hook" << std::endl; | |
logger.close(); | |
return FALSE; | |
} | |
break; | |
case DLL_PROCESS_DETACH: | |
MH_DisableHook(&FreeLibrary); | |
MH_Uninitialize(); | |
logger << "Logging ended\n" << std::endl; | |
logger.close(); | |
break; | |
default: | |
break; | |
} | |
return TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment