 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
kinghamza How do I cheat?
Reputation: 0
Joined: 14 Aug 2009 Posts: 5
|
Posted: Sat Aug 22, 2009 6:20 am Post subject: [SOLVED] My Recv Hook Crashes |
|
|
| Code: | typedef int (WINAPI *HOOKSEND)( SOCKET sendSocket, const char *szBuffer, int iLength, int iFlag );
typedef int (WINAPI *HOOKRECV)( SOCKET sendSocket, char FAR* szBuffer, int iLength, int iFlag );
int WINAPI HookSend( SOCKET sendSocket, const char *szBuffer, int iLength, int iFlag );
int WINAPI HookRecv( SOCKET sendSocket, char FAR* szBuffer, int iLength, int iFlag );
HOOKSEND HookSend_o;
HOOKRECV HookRecv_o;
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
{
static HANDLE hMainThread = NULL;
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
{
hMainThread = CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)ThreadMain, (void*)hinstDLL, 0, 0 );
HookSend_o = (HOOKSEND)HookFunction( hinstDLL, "Ws2_32.dll", "send", (DWORD)HookSend, 5 );
HookRecv_o = (HOOKRECV)HookFunction( hinstDLL, "Ws2_32.dll", "recv", (DWORD)HookRecv, 5 );
}break;
case DLL_PROCESS_DETACH:
{
if( hMainThread )
CloseHandle( hMainThread );
}break;
}
return TRUE;
}
DWORD HookFunction( HMODULE hModule, LPCWSTR DllName, LPCSTR ProcName, DWORD dwReplaced, DWORD dwReturn )
{
DWORD dwOldProtect;
DWORD dwAddressToHook = (DWORD)GetProcAddress(GetModuleHandle(DllName), ProcName);
BYTE *pbTargetCode = (BYTE *)dwAddressToHook;
BYTE *pbReplaced = (BYTE *)dwReplaced;
VirtualProtect((LPVOID)dwAddressToHook, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect);
*pbTargetCode++ = 0xE9;
*((signed int*)(pbTargetCode)) = pbReplaced - (pbTargetCode + 4);
VirtualProtect( (LPVOID)dwAddressToHook, 5, PAGE_EXECUTE, &dwOldProtect );
FlushInstructionCache( GetCurrentProcess(), NULL, NULL );
return dwAddressToHook+dwReturn;
}
int WINAPI HookSend( SOCKET sendSocket, const char *szBuffer, int iLength, int iFlag )
{
g_Socket = sendSocket;
// works nice
return HookSend_o( sendSocket, szBuffer, iLength, iFlag );
}
int WINAPI HookRecv( SOCKET sendSocket, char FAR* szBuffer, int iLength, int iFlag )
{
// on return, the program crashes
return HookRecv_o( sendSocket, szBuffer, iLength, iFlag );
}
//I made it work by modifying it like this
int WINAPI HookRecv( SOCKET sendSocket, char FAR* szBuffer, int iLength, int iFlag )
{
int iResult = HookRecv_o( sendSocket, szBuffer, iLength, iFlag );
//although it is working, the problem is that
//the HookRecv_o never returns, this means
//the code below never gets executed
return iResult;
} |
Last edited by kinghamza on Sat Aug 22, 2009 8:08 pm; edited 1 time in total |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Aug 22, 2009 7:02 am Post subject: |
|
|
| looks like you have corrupted the stack because you never replaced the instructions you overwrote when placing your hook. those instructions were more than likely setting up the stack frame. but because you didn't replace them in your hook function, the return address changes to something random
|
|
| Back to top |
|
 |
kinghamza How do I cheat?
Reputation: 0
Joined: 14 Aug 2009 Posts: 5
|
Posted: Sat Aug 22, 2009 11:15 am Post subject: |
|
|
this solved it all. placed right before the end of the hook.
| Code: |
__asm( "mov edi, edi\n" );
__asm( "push ebp\n" );
__asm( "mov ebp, esp\n" );
|
aww, i thought i didn't have to use inlines
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Aug 22, 2009 3:28 pm Post subject: |
|
|
first line can be omitted, second and third can be put into the same line, eg :
| Code: | __asm
{
push ebp
mov ebp, esp
} |
or even
| Code: | | __asm push ebp __asm mov ebp, esp |
|
|
| Back to top |
|
 |
kinghamza How do I cheat?
Reputation: 0
Joined: 14 Aug 2009 Posts: 5
|
Posted: Sat Aug 22, 2009 8:08 pm Post subject: |
|
|
thanks
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|