C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)

要实现下面程序,首先我们需要三个文件 detours.h ,detours.lib ,detver.h(可以去网上下载)

1. 首先让我们看看,一个最简单的C程序,如何劫持system函数.

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include"detours.h"
//#include"detver.h"
#pragma comment(lib , "detours.lib") // 存储函数指针地址
//指针分为:一般指针,多级指针。 指针函数,函数指针,指针数组 。数组指针。
//static int(*poldsystem) (const char * _Command)=system;
static int( *poldsystem )(const char * _Command) = system;
//自己实现一个system函数
int mysystem( const char * _Command){
const char * ps = NULL;
ps = strstr(_Command ,"calc");
if (ps != NULL)
printf("%s 已经被劫持啦!\n", _Command);
else
printf("run run run 北鼻 !");
return ;
}; //开始拦截
void Hook(){
DetourRestoreAfterWith(); //恢复初始状态
DetourTransactionBegin(); //拦截开始
DetourUpdateThread(GetCurrentThread()); //刷新当前线程
//此处下面填写自己想要拦截的函数
DetourAttach((void **)&poldsystem, mysystem); //实现函数拦截
DetourTransactionCommit(); //提交事务,拦截生效
} //撤销拦截
void UnHook() {
DetourTransactionBegin(); //拦截开始
DetourUpdateThread(GetCurrentThread()); //刷新当前线程
DetourDetach((void **)&poldsystem, mysystem);
DetourTransactionCommit();
}
int main( void ) { system("calc");
Hook();
system("tasklist");
UnHook();
getchar();
//system("pause");
return ;
}

截图:

结果显示。第一个system实现了,但是第二个system被劫持,无法执行、

C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)

2.   如果相对其他的程序或者软件劫持的话,只需要生成动态库(.dll)形式。注入到该程序或者软件模块中,这样就可以了!!  做到这儿,是不是再加上一点点线程的知识,就有想做一个桌面锁的冲动呀!

上一篇:为什么Java方法里面不能再嵌套方法?


下一篇:play(1) 第一个简单的应用