mirror of
https://github.com/matthieu-hackwitharts/Win32_Offensive_Cheatsheet
synced 2026-06-08 15:46:38 +00:00
51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
#include "pch.h"
|
|
#include <iostream>
|
|
#include <Windows.h>
|
|
|
|
FARPROC messageBoxAddress = NULL;
|
|
SIZE_T bytesWritten = 0;
|
|
char messageBoxOriginalBytes[6] = {};
|
|
|
|
int __stdcall HookedMessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) {
|
|
|
|
// print intercepted values from the MessageBoxA function
|
|
std::cout << "Ohai from the hooked function\n";
|
|
std::cout << "Text: " << (LPCSTR)lpText << "\nCaption: " << (LPCSTR)lpCaption << std::endl;
|
|
|
|
// unpatch MessageBoxA
|
|
WriteProcessMemory(GetCurrentProcess(), (LPVOID)messageBoxAddress, messageBoxOriginalBytes, sizeof(messageBoxOriginalBytes), &bytesWritten);
|
|
|
|
// call the original MessageBoxA
|
|
return MessageBoxA(NULL, lpText, lpCaption, uType);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// show messagebox before hooking
|
|
MessageBoxA(NULL, "hi", "hi", MB_OK);
|
|
|
|
HINSTANCE library = LoadLibraryA("user32.dll");
|
|
SIZE_T bytesRead = 0;
|
|
|
|
// get address of the MessageBox function in memory
|
|
messageBoxAddress = GetProcAddress(library, "MessageBoxA");
|
|
|
|
// save the first 6 bytes of the original MessageBoxA function - will need for unhooking
|
|
ReadProcessMemory(GetCurrentProcess(), messageBoxAddress, messageBoxOriginalBytes, 6, &bytesRead);
|
|
|
|
// create a patch "push <address of new MessageBoxA); ret"
|
|
void *hookedMessageBoxAddress = &HookedMessageBox;
|
|
char patch[6] = { 0 };
|
|
memcpy_s(patch, 1, "\x68", 1);
|
|
memcpy_s(patch + 1, 4, &hookedMessageBoxAddress, 4);
|
|
memcpy_s(patch + 5, 1, "\xC3", 1);
|
|
|
|
// patch the MessageBoxA
|
|
WriteProcessMemory(GetCurrentProcess(), (LPVOID)messageBoxAddress, patch, sizeof(patch), &bytesWritten);
|
|
|
|
// show messagebox after hooking
|
|
MessageBoxA(NULL, "hi", "hi", MB_OK);
|
|
|
|
return 0;
|
|
}
|