gpt4 book ai didi

c++ - 如何在不进入无限循环的情况下使用 C++ 中的 Detours 扩展程序内函数?

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

我正在尝试学习使用弯路修改和扩展程序中的功能。在本例中,我尝试修改 Windows 记事本 32 位中的 InsertDateTime 函数。

我正在使用 Winject 注入(inject)我创建的用于修改函数的 dll。 DLL 被正确注入(inject),函数绕道到我指定的新函数。但是,我绕过 InsertDateTime() 的新函数应该在名为 MyInsertDateTime() 的新函数末尾调用原始函数 InsertDateTime()。

但是,问题是,当新函数被调用时,最后尝试调用旧函数。当然,这一个也会在无限循环中依次被重定向/绕行。所以原来的函数永远不能调用!

不知何故,我想我需要分离新函数才能调用旧函数。但是当我这样做时我得到错误?然而,这可能是一些根本性的错误。我应该如何正确执行此操作?

代码如下:

#include <iostream>

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

using namespace std;

static int(__stdcall* InsertDateTime)(int);

int MyInsertDateTime(int x) //Our function
{
MessageBox(NULL, TEXT("Detoured Function Call"), TEXT("Min funktion"), MB_OK);
return InsertDateTime(x); //Return the origional function
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
InsertDateTime = (int(__stdcall*)(int))(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A);
switch (ul_reason_for_call) //Decide what to do
{
case DLL_PROCESS_ATTACH: //On dll attach
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)(&InsertDateTime), (PVOID)MyInsertDateTime);
DetourTransactionCommit();
break;
case DLL_THREAD_ATTACH: //On thread attach
break;
case DLL_THREAD_DETACH: //On thread detach
break;
case DLL_PROCESS_DETACH: //on process detach
DetourDetach((PVOID*)(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A), InsertDateTime);
break;
}
return TRUE;
}

最后是修改后的 MyInsertDateTime(),我在其中尝试取消绕行。也许我在分离时做错了什么?

int MyInsertDateTime(int x) //Our function
{
//Messagebox
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A), InsertDateTime);
DetourTransactionCommit();
MessageBox(NULL, TEXT("InsertDateTime Just Got Called"), TEXT("Min funktion"), MB_OK);
return InsertDateTime(x); //Return the origional function
}

最佳答案

您应该只在卸载 DLL 时删除 DllMain 中的 Detour。

DetourAttach() 为您提供指向原始函数的函数指针。

看这里:

http://www.codeproject.com/Articles/30140/API-Hooking-with-MS-Detours

DetourAttach(&(PVOID&)pSend, MySend);

int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+");
fprintf(pSendLogFile, "%s\n", buf);
fclose(pSendLogFile);
return pSend(s, buf, len, flags); // Calling original function obtained via the DetourAttach call, this will NOT cause MySend to be called again.
}

您可能遇到问题是因为:

InsertDateTime = (int(__stdcall*)(int))(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A);

可能是导致 jmp 位于错误位置的“错误”地址。

关于c++ - 如何在不进入无限循环的情况下使用 C++ 中的 Detours 扩展程序内函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16998435/

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