Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ Detouring Tutorial with MS Detours

c++ detours

  • Please log in to reply
No replies to this topic

#1 DaDopeman

DaDopeman

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 29 April 2016 - 03:49 PM

Decent tutorial, source code works great

 

#include <Windows.h>
 
bool Hook(void * toHook, void * ourFunct, int len) {
if (len < 5) {
return false;
}
 
DWORD curProtection;
VirtualProtect(toHook, len, PAGE_EXECUTE_READWRITE, &curProtection);
 
memset(toHook, 0x90, len);
 
DWORD relativeAddress = ((DWORD)ourFunct - (DWORD)toHook) - 5;
 
*(BYTE*)toHook = 0xE9;
*(DWORD*)((DWORD)toHook + 1) = relativeAddress;
 
DWORD temp;
VirtualProtect(toHook, len, curProtection, &temp);
 
return true;
}
 
DWORD jmpBackAddy;
void __declspec(naked) ourFunct() {
__asm {
add ecx, ecx
mov edx, [ebp-8]
jmp [jmpBackAddy]
}
}
 
DWORD WINAPI MainThread(LPVOID param) {
int hookLength = 6;
DWORD hookAddress = 0x332768;
jmpBackAddy = hookAddress + hookLength;
 
Hook((void*)hookAddress, ourFunct, hookLength);
 
while (true) {
if (GetAsyncKeyState(VK_ESCAPE)) break;
Sleep(50);
}
 
FreeLibraryAndExitThread((HMODULE)param, 0);
 
return 0;
}
 
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
CreateThread(0, 0, MainThread, hModule, 0, 0);
break;
}
 
return TRUE;
}

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download