【问题标题】:How to inject DLL making a Detours-enabled hook?如何注入 DLL 制作启用 Detours 的钩子?
【发布时间】:2020-02-05 02:45:28
【问题描述】:

我需要任何建议在获取代码后如何继续 CreateFile() 挂钩:

#include<windows.h>
#include "C:\Detours\Detours-4.0.1\include\detours.h"

static HANDLE(WINAPI* TrueCreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile) = CreateFileW;

__declspec(dllexport) HANDLE WINAPI MyCreateFileW(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD 
dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
if ((LPCTSTR)lpFileName == (LPCTSTR)L"C:\TestHook\file.txt")
{
    return TrueCreateFileW((LPCTSTR)L"C:\TestHook\file.txt", dwDesiredAccess, dwShareMode, lpSecurityAttributes,
        dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}
return TrueCreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
    dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}


BOOL APIENTRY DLLMain(HMODULE hModule, DWORD reason_for_call, LPVOID lpReserved)
{

LONG error;
switch (reason_for_call)
{
case DLL_PROCESS_ATTACH:
    OutputDebugString(L"Attaching HookingDLL.dll");
    //OutputDebugString(strInfo);
    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)TrueCreateFileW, MyCreateFileW);
    error = DetourTransactionCommit();

    if (error == NO_ERROR)
    {
        OutputDebugString(L"Hooking attempt succeeded");
    }
    else
    {
        OutputDebugString(L"Hooking attempt failed");
    }
    break;
case DLL_THREAD_ATTACH:
    break;
case DLL_THREAD_DETACH:
    break;
case DLL_PROCESS_DETACH:
    OutputDebugString(L"Detaching HookingDLL.dll");
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)TrueCreateFileW, MyCreateFileW);
    error = DetourTransactionCommit();

    if (error == NO_ERROR)
    {
        OutputDebugString(L"Successfully detached hook");
    }
    else
    {
        OutputDebugString(L"Hook removal has failed");
    }
    break;
}
return TRUE;
}

我需要的是在 Notepad++ 中创建新的 .txt 文件时调用 MyCreateFileW 挂钩。最有可能的是,我必须添加一个 DLL 注入器来应用该钩子,但在 Internet 上,我没有为初学者找到任何易于理解的分步指南(值得一提的是,我是一名学生)。您能否建议在我的情况下如何继续使用 DLL 注入器?让我注意到我正在使用 Microsoft Detours 来更顺畅、更一致地学习 API 挂钩。

【问题讨论】:

    标签: api winapi hook detours


    【解决方案1】:

    你已经准备好了detour DLL。您需要做的是创建一个新进程并使用DetourCreateProcessWithDlls 将DLL 加载到其中。像这样的:

    DetourCreateProcessWithDll(NULL, "C:\\windows\\notepad.exe", NULL,
            NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
            &si, &pi, DetourPath, DLLPath, NULL);
    

    您可以参考本教程“API Hooking with MS Detours”了解更多详细信息。

    【讨论】:

    • 确实感谢您的建议。我要问另外两件事:我应该通过开发人员命令提示符(我使用 VS 2019)运行 { withdll \d:mydll.dll "path_to_notepad++"} 还是 {setdll} 而不是 {withdll}?我需要两个 cpp 文件(一个带有我之前发布的代码,另一个带有 main() ,如教程中所示),所以无论其他文件在这里都没有关系,对吧?
    • @Max_ReFactor 你说的是官方样本withdllsetdll吗?
    • 不,我的目的是学习如何在命令提示符下启动程序以应用挂钩。我必须在我要注入的 DLL 名称之前输入 withdll 作为 exe 文件名(实际上,这个文件属于 C:\Detours\Detours-4.0.1\bin.X86)吗?选择可执行文件来启动挂钩是否正确,不是吗?
    • @Max_ReFactor 从 withdll.exe 的使用来看,它确实以这种方式工作:withdll /d:mydll.dll "path_to_notepad++"withdll 将插入您的 DLL 并启动 notepad++ 并在此执行完成后恢复更改。如果你想下次插入你的DLL,你需要在前面输入withdll。虽然 setdll 将编辑目标应用程序二进制文件(默认没有恢复更改)但不启动它。我不测试 withdll 和 setdll。你可以试试看有没有帮助。
    • 谢谢。看来我还需要解释。现在我有几个脚本文件:第一个包含我在问题中插入的代码,第二个包含我通过引用找到的 main() 函数,你礼貌地指出。我想这是错误的,因为一个项目同时具有 DLLMain() 和 main() (这是我的错误)。我通过 API Monitor 中的 withdll 和跟踪挂钩设置启动记事本执行。我只看到来自模块 ntdll.dll 的 DLLMain() 调用,而不是我正在寻找的。在执行启动之前,我将 DLL 注入文件粘贴到带有 Detours lib 的文件夹中,也在记事本文件夹中。
    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2010-12-31
    • 2020-04-01
    • 2012-04-21
    相关资源
    最近更新 更多