Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compile: g++ -o iathook.exe .\iathook.cpp
- #include <Windows.h>
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- #define MH_ALL_HOOKS NULL
- // Resturn status enums
- typedef enum MH_STATUS {
- MH_UNKNOWN = -1,
- MH_OK = 0
- } MH_STATUS;
- HINSTANCE dllHandle;
- // Definitions for the MinHook functions
- typedef MH_STATUS(WINAPI* _MH_Initialize)(VOID);
- _MH_Initialize MH_Initialize;
- typedef MH_STATUS(WINAPI* _MH_Uninitialize)(VOID);
- _MH_Uninitialize MH_Uninitialize;
- typedef MH_STATUS(WINAPI* _MH_RemoveHook)(LPVOID);
- _MH_RemoveHook MH_RemoveHook;
- typedef MH_STATUS(WINAPI* _MH_EnableHook)(LPVOID);
- _MH_EnableHook MH_EnableHook;
- typedef MH_STATUS(WINAPI* _MH_DisableHook)(LPVOID);
- _MH_DisableHook MH_DisableHook;
- typedef MH_STATUS(WINAPI* _MH_CreateHook)(LPVOID, LPVOID, LPVOID);
- _MH_CreateHook MH_CreateHook;
- // Definition for MessageBoxA function
- typedef int(WINAPI* _MessageBoxA)(HWND, LPCTSTR, LPCTSTR, UINT);
- // fpMessageBoxA - we will use it to store original pointer to the funxtion
- _MessageBoxA fpMessageBoxA;
- bool setup() { // Load functions used by MinHook
- dllHandle = LoadLibraryA("MinHook.dll");
- if(!dllHandle) {
- cout << "[-] Failed to load the dll" << endl;
- return false;
- }
- MH_Initialize = (_MH_Initialize)GetProcAddress(dllHandle, "MH_Initialize");
- if(!MH_Initialize) {
- cout << "[-] MH_Initialize" << endl;
- return false;
- }
- MH_Uninitialize = (_MH_Uninitialize)GetProcAddress(dllHandle, "MH_Uninitialize");
- if(!MH_Uninitialize) {
- cout << "[-] MH_Uninitialize" << endl;
- return false;
- }
- MH_RemoveHook = (_MH_RemoveHook)GetProcAddress(dllHandle, "MH_RemoveHook");
- if(!MH_RemoveHook) {
- cout << "[-] MH_RemoveHook" << endl;
- return false;
- }
- MH_EnableHook = (_MH_EnableHook)GetProcAddress(dllHandle, "MH_EnableHook");
- if(!MH_EnableHook) {
- cout << "[-] MH_EnableHook" << endl;
- return false;
- }
- MH_DisableHook = (_MH_DisableHook)GetProcAddress(dllHandle, "MH_DisableHook");
- if(!MH_DisableHook) {
- cout << "[-] MH_DisableHook" << endl;
- return false;
- }
- MH_CreateHook = (_MH_CreateHook)GetProcAddress(dllHandle, "MH_CreateHook");
- if(!MH_CreateHook) {
- cout << "[-] MH_CreateHook" << endl;
- return false;
- }
- return true;
- }
- // Hooked function
- int MyMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uInt) {
- cout << "MessageBoxA was called" << endl;
- cout << "Caption: " << lpText << endl;
- cout << "Text: " << lpText << endl;
- cout << "[Press Enter to display the message]";
- getchar();
- // Call the original function
- fpMessageBoxA(hWnd, "Modified text, just because we can :)", lpCaption, uInt);
- }
- int main() {
- if(!setup()) {
- cout << "[-] Setup failed" << endl;
- } else {
- cout << "[+] Setup completed successfully" << endl;
- }
- // Initialize MinHook.
- if (MH_Initialize() != MH_OK) {
- cout << "[-] Failed to Initialize MinHook" << endl;
- return 0;
- }
- // Create hook
- if (MH_CreateHook((LPVOID)&MessageBoxA, (LPVOID)&MyMessageBoxA, (LPVOID)&fpMessageBoxA) != MH_OK)
- cout << "[-] Failed to create hook for CreateProcessA" << endl;
- else
- cout << "[+] CreateProcessA hook created successfully" << endl;
- MessageBoxA(NULL, "MessageBoxA text, unhooked!", "Caption", MB_OK);
- // Enable hook
- if(MH_EnableHook((LPVOID)&MessageBoxA) != MH_OK) {
- cout << "[-] Failed to enable hook" << endl;
- }
- MessageBoxA(NULL, "MessageBoxA text, unhooked!", "Caption", MB_OK);
- // Cleanup
- MH_DisableHook(MH_ALL_HOOKS);
- MH_RemoveHook(MH_ALL_HOOKS);
- MH_Uninitialize();
- return 0;
- }
Add Comment
Please, Sign In to add comment