码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-26 20:54:39      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

过滤劫持和函数回调(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 劫持成功\n" , _Command );
       return 0;
}

//开始拦截
void Hook()
{

       DetourRestoreAfterWith(); //恢复原来状态,
       DetourTransactionBegin(); //拦截开始
       DetourUpdateThread(GetCurrentThread()); //刷新当前线程
        //这里可以连续多次调用DetourAttach,表明HOOK多个函数
        //printf("%p %p\n", &oldsystem, &newsystem);
       DetourAttach(( void **)&oldsystem, newsystem); //实现函数拦截
        //printf("%p %p\n", &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;
}


  




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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!