过滤劫持和函数回调(2)

过滤劫持和函数回调(2)
注意release模式:




这里实现对tasklist指令的劫持,其它不劫持:


#include<stdio.h>
#include<windows.h>
#include<string.h>
#include"detours.h"
#pragma comment (lib ,"detours.lib" )

static int (*oldsystem)(const char * _Command) = system;

//全部劫持
int  newsystemA(const char * _Command ){
        //puts(_Command);
        return 0;
}

//劫持过滤
int  newsystem(const char * _Command ){
       char*p = strstr( _Command, "mspaint");
       if(!p){
              oldsystem( _Command);          //函数回调
              return 1;
       }
       printf( "%s 劫持成功 " , _Command );
       return 0;
}

//开始拦截
void Hook()
{

       DetourRestoreAfterWith(); //恢复原来状态,
       DetourTransactionBegin(); //拦截开始
       DetourUpdateThread(GetCurrentThread()); //刷新当前线程
        //这里可以连续多次调用DetourAttach,表明HOOK多个函数
        //printf("%p %p ", &oldsystem, &newsystem);
       DetourAttach(( void **)&oldsystem, newsystem); //实现函数拦截
        //printf("%p %p ", &oldsystem, &newsystem);
        //
       DetourTransactionCommit(); //拦截生效

}
//取消拦截
void UnHook()
{
       DetourTransactionBegin(); //拦截开始
       DetourUpdateThread(GetCurrentThread()); //刷新当前线程
        //这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
       DetourDetach(( void **)&oldsystem, newsystem); //撤销拦截函数
       DetourTransactionCommit(); //拦截生效
}

int main(){
       Hook();
       getchar();
       system( "calc");
       getchar();
       system( "notepad");
       getchar();
       system( "mspaint");
       getchar();
       return 0;
}


  




原文地址:https://www.cnblogs.com/ZhangJinkun/p/4531508.html