原理: JMP后的偏移量 =目标地址 – 原地址 - 5
#include "stdafx.h"
#include "MyHook.h"
#include <iostream>
using namespace std;
void MyPint()
{
cout << "test" << endl;
}
void MyPint2()
{
cout << "test2" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
PROC pMyPint1 = (PROC)MyPint;
PROC pMyPint2 = (PROC)MyPint2;
MyPint();
BYTE m_bOldBytes[5];
BYTE m_bNewBytes[5];
ZeroMemory(m_bOldBytes, 5);
ZeroMemory(m_bNewBytes, 5);
if(pMyPint1 != NULL)
{
DWORD dwNum = 0;
ReadProcessMemory(GetCurrentProcess(), pMyPint1, m_bOldBytes, 5, &dwNum);
m_bNewBytes[0] = '\xe9'; //jmp
*(DWORD *)(m_bNewBytes + 1) = (DWORD)pMyPint2 - (DWORD)pMyPint1 - 5;
WriteProcessMemory(GetCurrentProcess(), pMyPint1, m_bNewBytes, 5, &dwNum);
}
MyPint();
if(pMyPint1 != 0)
{
DWORD dwNum = 0;
WriteProcessMemory(GetCurrentProcess(), pMyPint1, m_bOldBytes, 5, &dwNum);
}
MyPint();
system("pause");
return 0;
}
运行结果