Detours 1.5 auf JMP

09/09/2015 20:35 Daifoku#1
Ich habe eine Funktion A.
A hat als erste Anweisung einen JMP.
DetourFunction liefert keinerlei Probleme und mein Hook wird ordenungsgemäß ausgeführt.
DetourRemove liefert false. Ein Blick in den debugger bestätigt dieses Ergebnis. Der Hook wurde nicht entfernt.


Offensichtlich kommt DetourRemove mit dem Berechnen des neuen JMP Offsets nicht zurecht. Gibt es für diesen Fall eine spezielle Funktion die ich nutzen muss ?


PS: Eine Antwort eilt nicht. Ich habe das Problem bereits behoben, indem ich mir einen eigenen DetourHandler geschrieben habe. Aber es wäre trotzdem schön zu wissen, ob die nativen MS Detours 1.5 für diesen Fall eine Lösung bieten.
10/02/2015 23:52 xAxTer#2
It's so easy but it needs some background knowledge about C++ detors.

anyway, I made a full detor hooking for you:

Code:
// globals for hook and retn
DWORD dwHook = 0x00000000; //place your address here

BOOL WINAPI DllMain( HMODULE hMod, DWORD dwReason, LPVOID lpReserved )
{
	if( dwReason == DLL_PROCESS_ATTACH )
	{
		// hook it by a JMP on the dwHook offset
		PlaceJMP( (PBYTE)dwHook, (DWORD)TargetFunc, 0x4);
		MessageBox(0, "Hook placed successfully.", "Info", MB_OK);
	}
 
	return TRUE;
}


// Target Function to be Hooked
__declspec( naked ) void TargetFunc()
{
	__asm PUSH EBX;
	__asm PUSH EBP;
	__asm PUSH ESI;
	__asm MOV ESI, [EDI+0x0A7C]; // jumped
 
	__asm PUSHAD;
 
	Func();
 
	__asm POPAD;
	__asm JMP [dwRet];
}
 

void PlaceJMP(BYTE *pAddress, DWORD dwJumpTo, DWORD dwLen )
{
	DWORD dwOldProtect, dwBkup, dwRelAddr;
	
	VirtualProtect(pAddress, dwLen, PAGE_EXECUTE_READWRITE, &dwOldProtect);
	dwRelAddr = (DWORD) (dwJumpTo - (DWORD) pAddress) - 5;
	*pAddress = 0xE9;
	
	*((DWORD *)(pAddress + 0x1)) = dwRelAddr;
	for(DWORD x = 0x5; x < dwLen; x++) *(pAddress + x) = 0x90;
	VirtualProtect(pAddress, dwLen, dwOldProtect, &dwBkup);
	
	return;
}
10/03/2015 00:10 Daifoku#3
That was not the question ;-)

I made my own detours (which was really easy) but I wanted to know how to do it with MS Detours 1.5. There is no documentation about detours 1.5, 2.0, 3.0, ...

and this topic was about DetourRemove. Detouring worked fine but I couldn't remove the Detour because the trampoline was wrong.