gpt4 book ai didi

c++ - Hook MS Detours 并注入(inject) Withdll.exe 时应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:43 25 4
gpt4 key购买 nike

我正在使用 MS Detours 连接 FindNextFile()。我已经成功配置了 Detours 库并编写了一个名为“Detuors.dll”的 dll 和一个名为“FNFSend.exe”的应用程序。以下是代码:

动态链接库:

#include <cstdio>
#include <stdio.h>
#include <windows.h>
#include "detours.h"
#pragma comment (lib,"detours.lib")

//Prototypes
extern "C" __declspec(dllexport) BOOL (WINAPI *pFNF)(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData) = FindNextFile;
extern "C" __declspec(dllexport) BOOL WINAPI MyFNF(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData);

//Log File
FILE* pFNFLogFile;
int counter = 0;

INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
switch(Reason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hDLL);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pFNF, MyFNF);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString("FNF() detoured successfully");
else
OutputDebugString("FNF() not detoured");
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin(); //Detach
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)pFNF, MyFNF);
DetourTransactionCommit();
break;
case DLL_THREAD_ATTACH:
DisableThreadLibraryCalls(hDLL);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pFNF, MyFNF);
if(DetourTransactionCommit() == NO_ERROR)
OutputDebugString("FNF() detoured successfully");
else
OutputDebugString("FNF() not detoured");
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}

//Open file, write contents, close it
extern "C" __declspec(dllexport) int WINAPI MyFNF(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData)
{
counter ++;
fopen_s(&pFNFLogFile, "C:\\FNFLog.txt", "a+");
fprintf(pFNFLogFile, "%s\n", counter);
fclose(pFNFLogFile);
return pFNF(hFindFile, lpFindFileData);
}

两个代码都编译成功,没有错误。应用程序递归调用 FindNextFile(),dll Hook 它并将计数器写入文件。

然后我使用 detours 库本身提供的名为“withdll.exe”的工具来创建一个进程,其中注入(inject)了一个 dll。所以我使用命令将我的 dll 注入(inject)到应用程序中:

withdll/d:Detuors.dll "C:\FNFSend.exe"

注入(inject)后,该函数成功 Hook ,即文件在目录中,但应用程序突然崩溃。在visual studio中调试后,在“output.c”中看到异常如下:

Unhandled exception at 0x6265984f (msvcr90d.dll) in FNFSend.exe: 0xC0000005:
Access violation reading location 0x00000001.

请帮助解决问题。

最佳答案

%s 不是有效的 format string打印出一个数字。请改用 %d

通过指定 %s,您告诉 fprintf 以字符串形式读取地址 counter 处的内存。您尝试调用 fprintf 的第一个值是 1,这就是地址 0x00000001 存在访问冲突的原因。

关于c++ - Hook MS Detours 并注入(inject) Withdll.exe 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com