首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有无限循环的情况下使用c++中的Detours扩展程序中的函数?

如何在没有无限循环的情况下使用c++中的Detours扩展程序中的函数?
EN

Stack Overflow用户
提问于 2013-06-08 10:15:00
回答 1查看 1.3K关注 0票数 1

我正在努力学习如何使用绕行来修改和扩展程序中的函数。在这种情况下,我试图修改Windows 32位中的InsertDateTime函数。

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

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

不知怎么的,我想我需要分离新函数,以便调用旧函数。但当我这么做的时候我会犯错吗?然而,这可能是一些根本性的错误。我该怎么做才好呢?

代码可以在下面看到:

代码语言:javascript
复制
#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(),在这里我试图分离绕道。也许当分离时,我做错了什么

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-08 10:35:14

只有当DLL正在卸载时,才应该删除DllMain中的命令。

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

见这里:

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

代码语言:javascript
复制
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.
}

您可能有问题,因为:

代码语言:javascript
复制
InsertDateTime = (int(__stdcall*)(int))(reinterpret_cast<DWORD>(GetModuleHandleA("notepad.exe")) + 0x978A);

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16998435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档