居然没发现还有注入功能,上代码。
#include <iostream>
#include <windows.h>
#include "detours.h"
#pragma comment(lib, "detours32.lib")
int main() {
STARTUPINFOW si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// 第三方软件的路径
LPCWSTR applicationPath = L"D:/test.exe";
// 我们编译好的 DLL 路径
LPCSTR dllPath = "D:/ndk.dll";
BOOL success = DetourCreateProcessWithDllEx(
applicationPath, NULL, NULL, NULL, FALSE,
CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
&si, &pi, dllPath, NULL
);
if (success) {
std::cout << "Successfully launched and hooked!" << std::endl;
ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
}
else {
std::cerr << "Failed to launch. Error code: " << GetLastError() << std::endl;
}
return 0;
}DetourCreateProcessWithDll 找不到序数
Detours 3.0 及以后的版本要求被注入的 DLL 必须导出一个名为 DetourFinishHelperProcess 的函数,或者至少包含一个特定的导出位(序数 1),否则注入机制会失效并报错。
#include <windows.h>
#include "detours.h"
#include <iostream>
// --- 关键:必须导出一个函数以供 Detours 识别 ---
// 即使这个函数什么都不做,也必须存在于导出表中。
extern "C" __declspec(dllexport) void CALLBACK MyDetoursExport() {
// 留空即可
}
// 模拟要 Hook 的目标函数(例如:MessageBoxW)
static int (WINAPI* Real_MessageBoxW)(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) = MessageBoxW;
// 自定义的 Hook 函数
int WINAPI Mine_MessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) {
return Real_MessageBoxW(hWnd, L"Hooked by Detours!", L"Notice", uType);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
// 忽略 Detours 辅助进程
if (DetourIsHelperProcess()) {
return TRUE;
}
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
// 禁止 DLL_THREAD_ATTACH 以提高性能
DisableThreadLibraryCalls(hModule);
// 开始 Hook 事务
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
// 绑定你想要 Hook 的函数
DetourAttach(&(PVOID&)Real_MessageBoxW, Mine_MessageBoxW);
if (DetourTransactionCommit() == NO_ERROR) {
OutputDebugStringA("DLL Attached: Hook Successfully!");
}
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
// 卸载 Hook
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)Real_MessageBoxW, Mine_MessageBoxW);
DetourTransactionCommit();
}
return TRUE;
}2. 导出模块定义文件 (ndk.def) 为了确保序数 1 能够被正确识别,建议在 DLL 项目中添加一个 .def 文件。 在项目中右键 -> 添加新项 -> 模块定义文件 (.def)。 输入以下内容:
LIBRARY ndk
EXPORTS
MyDetoursExport @1
本文链接:https://it72.com/12803.htm