微软库 Detour
提供API劫持的一套开源库
Detour必须运行在发布模式下
劫持简单实例:
反劫持简单实例:
提供API劫持的一套开源库
Detour必须运行在发布模式下
劫持简单实例:
#include <windows.h>
#include "detours.h"
#pregma comment(lib, "detours")
int (*poldsystem)(const char * _Command) = system; //存放system函数地址
//新的SYSTEM函数,把字符串输出
int newsystem(const char * _Command)
{
char str[1024] = {0};
sprintf(str, "echo %s", _Command);
system(str);
}
void hook()
{
DetourRestoreAfterWith(); //恢复之前状态,避免反复拦截
DetourTransactionBegin(); //开始劫持
DetourUpdateThread(GetCurrentThread()); //刷新当前进程
DetourAttach((void **)&poldsystem), newsystem); //劫持
DetourTransactionConmit(); //立即执行
}
反劫持简单实例:
void unhook()
{
//DetourRestoreAfterWith();
DetourTransactionBegin();开始操作
DetourUpdateThread(GetCurrentThread());//刷新当前进程
DetourDetach((void **)&poldsystem), newsystem); //解除劫持
DetourTransactionCommit(); //立刻生效
}