Skip to content

Instantly share code, notes, and snippets.

@kumatti1
Last active September 23, 2015 21:46
Show Gist options
  • Select an option

  • Save kumatti1/3dd953e6272f176469bb to your computer and use it in GitHub Desktop.

Select an option

Save kumatti1/3dd953e6272f176469bb to your computer and use it in GitHub Desktop.
APIフック
#define UNICODE
#include <windows.h>
#include <stdio.h>
DWORD OldProtect;
BYTE tmp[6];
BYTE ByteAsm[6];
DWORD * pFunc;
int WINAPI Hook_MessageBoxA(
_In_opt_ HWND hWnd,
_In_opt_ LPCTSTR lpText,
_In_opt_ LPCTSTR lpCaption,
_In_ UINT uType
)
{
MessageBoxW(0, L"", L"foo", MB_OK);
return 0;
}
void StarteHook()
{
HMODULE hDll = GetModuleHandleW(L"user32.dll");
pFunc = (DWORD*)GetProcAddress(hDll, "MessageBoxA");
if (VirtualProtect(pFunc, 6, PAGE_EXECUTE_READWRITE, &OldProtect))
{
memcpy(tmp, pFunc, 6);
ByteAsm[0] = 0x68; //push dword ptr
DWORD* dwProc = (DWORD*)Hook_MessageBoxA;
memcpy(&ByteAsm[1], &dwProc, 4);
ByteAsm[5] = 0xC3; //ret
memcpy(pFunc, ByteAsm, 6);
}
}
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
StarteHook();
MessageBoxA(0, "", "hoge", MB_OK);
memcpy(pFunc, tmp, 6);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment