使用detours實現劫持

第一步:下載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; 
}
markdown

第六步:開始攔截 
//開始攔截 
void Hook() 
{
函數

DetourRestoreAfterWith();//恢復原來狀態,
DetourTransactionBegin();//攔截開始
DetourUpdateThread(GetCurrentThread());//刷新當前線程
//這裏能夠連續屢次調用DetourAttach,代表HOOK多個函數

DetourAttach((void **)&oldsystem, newsystemA);//實現函數攔截

DetourTransactionCommit();//攔截生效

} 
第七步:取消攔截 
//取消攔截 
void UnHook() 
{
spa

DetourTransactionBegin();//攔截開始
DetourUpdateThread(GetCurrentThread());//刷新當前線程
//這裏能夠連續屢次調用DetourDetach,代表撤銷多個函數HOOK
DetourDetach((void **)&oldsystem, newsystemA); //撤銷攔截函數
DetourTransactionCommit();//攔截生效

}線程

第八步:main函數運行,大功告成 
void main() 
{ 
system(「calc」); 
Hook(); 
system(「calc」); 
system(「tasklist」); 
//UnHook(); 
getchar(); 
}
debug

注意:必定要在realse模式,而不是在debug模式下運行,否則得不到想要的結果。指針

相關文章
相關標籤/搜索