Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[SOLVED] My Recv Hook Crashes

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kinghamza
How do I cheat?
Reputation: 0

Joined: 14 Aug 2009
Posts: 5

PostPosted: Sat Aug 22, 2009 6:20 am    Post subject: [SOLVED] My Recv Hook Crashes Reply with quote

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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Aug 22, 2009 7:02 am    Post subject: Reply with quote

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
View user's profile Send private message
kinghamza
How do I cheat?
Reputation: 0

Joined: 14 Aug 2009
Posts: 5

PostPosted: Sat Aug 22, 2009 11:15 am    Post subject: Reply with quote

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 Crying or Very sad
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Aug 22, 2009 3:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
kinghamza
How do I cheat?
Reputation: 0

Joined: 14 Aug 2009
Posts: 5

PostPosted: Sat Aug 22, 2009 8:08 pm    Post subject: Reply with quote

thanks Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites