[英]How to hook functions that aren't in the Win32 API?
我做了一個非常簡單的程序,創建一個窗口並在左上角顯示“隨機數是:[0到9的隨機數]”。 顯示數字的功能如下所示:
void DisplayThings(HDC hdc, HWND hWnd, int randomNum)
{
std::stringstream text;
text << "Random number is: " << randomNum;
TextOut(hdc, 0, 0, text.str().c_str(), text.str().length());
}
在OllyDBG中,我發現它在地址0x11211A0:
接下來,我創建了一個試圖在Microsoft Detour的幫助下繞過DisplayThings函數的dll,這是dll的樣子:
#pragma comment(lib, "detours.lib")
#include <Windows.h>
#include <detours.h>
#include <tchar.h>
#include <sstream>
typedef void (*pDisplayThingsFunc)(HDC hdc, HWND hWnd, int randomNum);
void DisplayThingsFunc(HDC hdc, HWND hWnd, int randomNum)
{
printf("function is being detoured\n");
TextOut(hdc, 0, 20, L"detoured", 8);
}
pDisplayThingsFunc DisplayThingsFuncToDetour = (pDisplayThingsFunc)(0x11211A0);
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
//DWORD *hiddenValueAdress = (DWORD*)(*(DWORD*)0x020FAB8);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
AllocConsole();
freopen("CONOUT$", "w", stdout);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)DisplayThingsFuncToDetour, DisplayThingsFunc);
DetourTransactionCommit();
break;
case DLL_PROCESS_DETACH:
FreeConsole();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)DisplayThingsFuncToDetour, DisplayThingsFunc);
DetourTransactionCommit();
break;
}
return TRUE;
}
一旦我注入了dll,地址就會被JMP正確地替換為我的新函數,但隨后發生了一些奇怪的事情......控制台繼續打印“函數正在繞行”,但TextOut函數無法顯示任何內容......
任何幫助,將不勝感激!
編譯器正在優化代碼,因為它推斷出它可以安全地為此函數執行此操作。
您可以更改其中一個(例如通過導出符號,將其分配給函數指針等)。
或者你可以嘗試現在掛鈎它。
首先,您需要推斷出您感興趣的參數通過的位置, 這很可能是一個寄存器。 最簡單的方法是分析對TextOut的調用並追溯hdc參數。 如果你很幸運並且只是通過快速調用來優化它,將函數和函數指針的類型更改為fastcall,並完成。
如果你沒那么幸運,你可能不得不從帶有內聯匯編的額外寄存器中獲取它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.