1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <Windows.h>
using namespace std;
FARPROC pfOldMsgBoxW; BYTE NewCode[5] = {0xe9}, OldCode[5];
void HookOn() { DWORD dwTemp = 0; DWORD dwOldProtect; DWORD dwPid = GetCurrentProcessId(); HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwPid);
VirtualProtectEx(hProcess, pfOldMsgBoxW, 5, PAGE_READWRITE, &dwOldProtect); WriteProcessMemory(hProcess, pfOldMsgBoxW, NewCode, 5, 0); VirtualProtectEx(hProcess, pfOldMsgBoxW, 5, dwOldProtect, &dwTemp); }
void HookOff() { DWORD dwTemp = 0; DWORD dwOldProtect; DWORD dwPid = GetCurrentProcessId(); HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwPid);
VirtualProtectEx(hProcess, pfOldMsgBoxW, 5, PAGE_READWRITE, &dwOldProtect); WriteProcessMemory(hProcess, pfOldMsgBoxW, OldCode, 5, 0); VirtualProtectEx(hProcess, pfOldMsgBoxW, 5, dwOldProtect, &dwTemp); }
int WINAPI MyMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) { printf("FUCK YOU VIRTUALPROTECT!\n"); return 0; }
int main() { HMODULE hModule = LoadLibrary(L"User32.dll"); pfOldMsgBoxW = GetProcAddress(hModule, "MessageBoxW");
_asm { lea edi, OldCode mov esi, pfOldMsgBoxW cld movsd movsb }
_asm { lea eax, MyMessageBoxW mov ebx, pfOldMsgBoxW sub eax, ebx sub eax, 5 mov dword ptr[NewCode + 1], eax }
MessageBoxW(0, L"test", L"test", 0); HookOn(); MessageBoxW(0, L"test", L"test", 0); HookOff(); MessageBoxW(0, L"test", L"test", 0); return 0; }
|