我正在努力学习如何使用绕行来修改和扩展程序中的函数。在这种情况下,我试图修改Windows 32位中的InsertDateTime函数。
我使用Winject注入我创建的dll来修改函数。DLL被正确注入,函数被绕行到我指定的新函数。但是,我绕开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
}发布于 2013-06-08 10:35:14
只有当DLL正在卸载时,才应该删除DllMain中的命令。
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出现在错误位置的“错误”地址。
https://stackoverflow.com/questions/16998435
复制相似问题