第一步:下載detours3.0,安裝detours
第二步:構建庫文件,nmake編譯
第三步:包含庫文件和頭文件
#include “detours.h” //載入頭文件
#pragma comment(lib,”detours.lib”) //表明要使用靜態庫
第四步:定義舊函數指針指向原來的函數
static int (oldsystem)(const char _Command)=system;
第五步:聲明一個和原函數參數相同的新函數
int newsystemA( char * _Command)
{
char *p=strstr(_Command,”tasklist”);
if(p==NULL)
{
oldsystem(_Command);
}
else
{
printf(“%s”,_Command); //找到了,禁止執行
return 0;
}
return 0;
}
第六步:開始攔截
//開始攔截
void Hook()
{
DetourRestoreAfterWith();//恢復原來狀態,
DetourTransactionBegin();//攔截開始
DetourUpdateThread(GetCurrentThread());//刷新當前線程
//這里可以連續多次調用DetourAttach,表明HOOK多個函數
DetourAttach((void **)&oldsystem, newsystemA);//實現函數攔截
DetourTransactionCommit();//攔截生效
}
第七步:取消攔截
//取消攔截
void UnHook()
{
DetourTransactionBegin();//攔截開始
DetourUpdateThread(GetCurrentThread());//刷新當前線程
//這里可以連續多次調用DetourDetach,表明撤銷多個函數HOOK
DetourDetach((void **)&oldsystem, newsystemA); //撤銷攔截函數
DetourTransactionCommit();//攔截生效
}
第八步:main函數運行,大功告成
void main()
{
system(“calc”);
Hook();
system(“calc”);
system(“tasklist”);
//UnHook();
getchar();
}
注意:一定要在realse模式,而不是在debug模式下運行,不然得不到想要的結果。