unsigned char g_OldOpcode[
5
]
=
{};
unsigned char g_NewOpcode[
5
]
=
{
0XE9
};
LPVOID g_OpAddr
=
0
;
HANDLE
WINAPI
MyOpenProcess(
_In_ DWORD dwDesiredAccess,
_In_
BOOL
bInheritHandle,
_In_ DWORD dwProcessId
)
{
OffHook();
MessageBox(
0
, L
"HELLO"
,
0
,
0
);
HANDLE h
=
OpenProcess(PROCESS_ALL_ACCESS, FALSE,
-
1
);
OnHook();
return
h;
}
void Init()
{
HMODULE handle
=
GetModuleHandle(L
"kernel32.dll"
);
g_OpAddr
=
GetProcAddress(handle,
"OpenProcess"
);
memcpy(g_OldOpcode, g_OpAddr,
5
);
DWORD offset
=
(DWORD)MyOpenProcess
-
(DWORD)g_OpAddr
-
5
;
memcpy(g_NewOpcode
+
1
, &offset,
4
);
}
void OnHook()
{
DWORD oldProtect
=
0
;
VirtualProtect(g_OpAddr,
1
, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(g_OpAddr, g_NewOpcode,
5
);
VirtualProtect(g_OpAddr,
1
, oldProtect, &oldProtect);
}
void OffHook()
{
DWORD oldProtect
=
0
;
VirtualProtect(g_OpAddr,
1
, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(g_OpAddr, g_OldOpcode,
5
);
VirtualProtect(g_OpAddr,
1
, oldProtect, &oldProtect);
}