C++ Windows Hook使用

GitHub - microsoft/Detours: Detours is a software package for monitoring and instrumenting API calls on Windows. It is distributed in source code form.

cpp 复制代码
/*

挂载钩子 setdll /d:C:\Users\g\source\repos\LotTest\Release\lotDll.dll C:\Users\g\source\repos\LotTest\bin\x86\Release\net6.0-windows\LotTest.exe
卸载钩子 setdll /r C:\Users\g\source\repos\LotTest\bin\x86\Release\net6.0-windows\LotTest.exe
*/

#include <Windows.h>
#include "detours/detours.h"

//真实的调用函数,函数原型必须和真实API一致。部分类型如果无法声明可以用void *替代
static int (WINAPI* REALMessageBox) (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) = MessageBox;
//伪造的调用函数,也就是我们的钩子,参数类型和返回值必须和真实的一样,
static int WINAPI MYMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
	//在这里可以任意发挥~~~
	//在函数末尾调用真正的API来返回
	return  REALMessageBox(NULL, "MyHook!! MessageBoxCRACK!!", "Please", MB_OK);
}

void StartHook()
{
	long err;
	DetourRestoreAfterWith();
	//开始事务
	DetourTransactionBegin();
	//更新线程信息  
	DetourUpdateThread(GetCurrentThread());
	//将拦截的函数附加到原函数的地址上
	DetourAttach(&(PVOID&)REALMessageBox, MYMessageBox);
	//结束事务
	err = DetourTransactionCommit();
}

//解除钩子
void EndHook()
{
	//开始事务
	DetourTransactionBegin();
	//更新线程信息 
	DetourUpdateThread(GetCurrentThread());
	//将拦截的函数从原函数的地址上解除
	DetourDetach(&(PVOID&)REALMessageBox, MYMessageBox);
	//结束事务
	DetourTransactionCommit();
}


/*
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "framework.h"

extern void StartHook();//新增

//新增一个导出函数,这个可以随便写,但必须至少有一个导出函数才能使用setdll远程注入
VOID __declspec(dllexport) test()
{
	OutputDebugString(L"__declspec(dllexport) test() \r\n");
}

BOOL APIENTRY DllMain(HMODULE hModule,
	DWORD  ul_reason_for_call,
	LPVOID lpReserved
)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	{ StartHook(); } //新增
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
*/

void injectProcess() {
	HANDLE hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ,
		FALSE, pid);

	if (hProcess != NULL)
	{
		TRACE("InjectHook \n");
		HANDLE hThread;
		char   szLibPath[_MAX_PATH];
		void* pLibRemote = 0;
		DWORD  hLibModule = 0;

		HMODULE hKernel32 = ::GetModuleHandle("Kernel32");

		if (!::GetSystemDirectory(szLibPath, _MAX_PATH))
			return;

		strcat(szLibPath, "C:\\windows\\HookDll.dll");

		pLibRemote = ::VirtualAllocEx(hProcess, NULL, sizeof(szLibPath), MEM_COMMIT, PAGE_READWRITE);

		if (pLibRemote == NULL)
			return;

		::WriteProcessMemory(hProcess, pLibRemote, (void*)szLibPath, sizeof(szLibPath), NULL);

		hThread = ::CreateRemoteThread(hProcess, NULL, 0,
			(LPTHREAD_START_ROUTINE) ::GetProcAddress(hKernel32, "LoadLibraryA"),
			pLibRemote, 0, NULL);

		if (hThread != NULL)
		{
			::WaitForSingleObject(hThread, INFINITE);
			::GetExitCodeThread(hThread, &hLibModule);
			::CloseHandle(hThread);
		}
	}
}

void test() {
	StartHook();
	PVOID g_pOldMessageBoxW = NULL;
	PVOID g_pOldMessageBoxA = NULL;
	g_pOldMessageBoxA = DetourFindFunction("User32.dll", "MessageBoxA");
	MessageBox(0, "test", "test", 0);
	EndHook();
}

创作不易,小小的支持一下吧!

相关推荐
IOT-Power10 小时前
C++ 工厂模式
c++
Huangjin007_10 小时前
【C++ STL篇(十)】深入理解 AVL 树:代码实现、旋转图解与平衡因子详解
开发语言·c++
小明同学0111 小时前
C++后端项目:统一大模型接入 SDK(四)
服务器·开发语言·c++·计算机网络·chatgpt
番茄灭世神11 小时前
PN学堂GD32教程第21篇——WiFiIOT
c语言·stm32·单片机·嵌入式·gd32
不吃土豆的马铃薯11 小时前
Spdlog 入门:日志记录器与日志槽基础详解
服务器·开发语言·c++·c·日志·spdlog
此生决int12 小时前
算法从入门到精通——前缀和
c++·算法·蓝桥杯
Fuyo_111912 小时前
C++中的活字印刷术——模板·初阶
开发语言·c++·笔记
小白|12 小时前
cmake:昇腾CANN构建系统完全指南
java·c++·算法
王老师青少年编程12 小时前
2026年全国青少年信息素养大赛“算法应用主题赛”(初赛)【C++考点大纲】(全场景、组别):文末附备考秘籍!
c++·全国青少年信息素养大赛·初赛·2026年·算法应用主题赛·考点大纲
DogDaoDao12 小时前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch