// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
void test() {
printf_s("测试\n");
}
void test1() {
printf_s("hook\n");
}
char* toChar(DWORD dword) {
char buff[5] = { 0,0,0,0,0 };
memcpy(buff, &dword, sizeof(DWORD));
string cmd = string(buff);
reverse(cmd.begin(), cmd.end());
return buff;
}
int getLength(char * data) {
return sizeof(data) / sizeof(data[0]);
}
char * addChar(char* cru, char* newChar,int crulength ,int newCharLength) {
int size = crulength + newCharLength;
char * re = new char[size];
for (int i = 0;i < crulength;i++) {
re[i] = cru[i];
}
for (int i = 0;i < newCharLength;i++) {
re[i + crulength] = newChar[i];
}
return re;
}
char* hook(void* fun, void* newFun) {
char top[5];
DWORD addr;
__asm {
mov eax, newFun
mov addr, eax
}
char *newCode = toChar(addr);
char jmp[1] = { 233 };
newCode = addChar(newCode, jmp, 4, 1);
DWORD dwFlag;
VirtualProtect(fun, 5, PAGE_EXECUTE_READWRITE, &dwFlag);
memcpy(top, fun, 5);
memcpy(fun, newCode, 5);
printf("%p\n",addr);
printf("%p\n", fun);
VirtualProtect(fun, 5, dwFlag, &dwFlag);
return top;
}
int main()
{
hook(test, test1);
test();
system("pause");
return 0;
}