【问题标题】:Application Crashes when hooked with MS Detours and Injected with Withdll.exe与 MS Detours 挂钩并使用 Withdll.exe 注入时应用程序崩溃
【发布时间】:2013-05-13 09:05:40
【问题描述】:

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

DLL:

#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 将其挂钩并将计数器写入文件。

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

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

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

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

请帮助解决问题。

【问题讨论】:

    标签: c++ windows detours api-hook


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多