.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
CreateRemoteThread - Strange behaviour
Old
  (#1)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default CreateRemoteThread - Strange behaviour - 18-09-2004

Note: This has nothing to do with Half-Life at all.
BUGBUGBUG: Code sections are not accepting newlines? All my code appeared on a single line.

For some reason using CreateRemoteThread to inject a dll into a remote process causes it to either not load or behave strangely.

Notepad: Dll injected, DllMain Callled and exits after DllMain returns
Solitaire: Dll injected, DllMain Called, loads but about menu doesn't work aswell as the options menu refreshes the deck instead.

This is really strange, does anyone have an idea as to why this is happening?

HMODULE RemoteLoadModule( HANDLE hProcess, LPSTR lpModulepath ) {
HANDLE hThread = INVALID_HANDLE_VALUE;
DWORD dwLoadLibraryA = 0;
LPVOID lpMemory = NULL;
DWORD dwResult = 0;
DWORD dwBytes = 0;
int nPathlen = 0;

nPathlen = strlen( lpModulepath );

lpMemory = VirtualAllocEx( hProcess, 0, nPathlen, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
dwLoadLibraryA = ( DWORD ) GetProcAddress( GetModuleHandle( "kernel32.dll" ), "LoadLibraryA" );

if ( lpMemory ) {
if ( WriteProcessMemory( hProcess, lpMemory, lpModulepath, nPathlen, &dwBytes ) ) {
hThread = CreateRemoteThread( hProcess, NULL, 0, ( LPTHREAD_START_ROUTINE ) dwLoadLibraryA, lpMemory, 0, NULL );

if ( hThread != INVALID_HANDLE_VALUE ) {
WaitForSingleObject( hThread, INFINITE );
GetExitCodeThread( hThread, &dwResult );

CloseHandle( hThread );
}
}

VirtualFreeEx( hProcess, lpMemory, nPathlen, MEM_RELEASE );
}

return ( HMODULE ) dwResult;
}

BOOL CreateWithInject( LPCTSTR lpApplicationpath, LPCTSTR lpCurrentdirectory, LPTSTR lpCommandline, LPCTSTR lpInjectmodulepath, HMODULE* pRemoteModule ) {
PROCESS_INFORMATION procInfo;
STARTUPINFO startupInfo;
HMODULE hRemote = NULL;
DWORD dwEntrypoint = 0;
BOOL bResult = FALSE;

ZeroMemory( &procInfo, sizeof( PROCESS_INFORMATION ) );
ZeroMemory( &startupInfo, sizeof( STARTUPINFO ) );

if ( CreateProcess( lpApplicationpath, lpCommandline, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, lpCurrentdirectory, &startupInfo, &procInfo ) ) {
hRemote = RemoteLoadModule( procInfo.hProcess, lpInjectmodulepath );

if ( hRemote != NULL )
bResult = TRUE;

// TODO:
// The function exists, free the remote ya lazy bastard

ResumeThread( procInfo.hThread );
WaitForSingleObject( procInfo.hThread, INFINITE );

CloseHandle( procInfo.hProcess );
CloseHandle( procInfo.hThread );
}

return bResult;
}

Last edited by Lazy; 18-09-2004 at 21:36..
  
Reply With Quote
Re: CreateRemoteThread - Strange behaviour
Old
  (#2)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: CreateRemoteThread - Strange behaviour - 19-09-2004

a 'bit' more readable :

PHP Code:
HMODULE RemoteLoadModuleHANDLE hProcessLPSTR lpModulepath )
{
    
HANDLE hThread INVALID_HANDLE_VALUE;
    
DWORD dwLoadLibraryA 0;
    
LPVOID lpMemory NULL;
    
DWORD dwResult 0;
    
DWORD dwBytes 0;
    
int nPathlen 0;

    
nPathlen strlenlpModulepath );

    
lpMemory VirtualAllocExhProcess0nPathlenMEM_COMMITPAGE_EXECUTE_READWRITE );
    
dwLoadLibraryA = ( DWORD GetProcAddressGetModuleHandle"kernel32.dll" ), "LoadLibraryA" );

    if ( 
lpMemory ) {
        if ( 
WriteProcessMemoryhProcesslpMemorylpModulepathnPathlen, &dwBytes ) ) {
            
hThread CreateRemoteThreadhProcessNULL0, ( LPTHREAD_START_ROUTINE dwLoadLibraryAlpMemory0NULL );

            if ( 
hThread != INVALID_HANDLE_VALUE ) {
                
WaitForSingleObjecthThreadINFINITE );
                
GetExitCodeThreadhThread, &dwResult );

                
CloseHandlehThread );
            }
        }

        
VirtualFreeExhProcesslpMemorynPathlenMEM_RELEASE );
    }

    return ( 
HMODULE dwResult;
}



BOOL CreateWithInjectLPCTSTR lpApplicationpathLPCTSTR lpCurrentdirectoryLPTSTR lpCommandlineLPCTSTR lpInjectmodulepathHMODULEpRemoteModule ) {
    
PROCESS_INFORMATION procInfo;
    
STARTUPINFO startupInfo;
    
HMODULE hRemote NULL;
    
DWORD dwEntrypoint 0;
    
BOOL bResult FALSE;

    
ZeroMemory( &procInfosizeofPROCESS_INFORMATION ) );
    
ZeroMemory( &startupInfosizeofSTARTUPINFO ) );

    if ( 
CreateProcesslpApplicationpathlpCommandlineNULLNULLTRUECREATE_SUSPENDEDNULLlpCurrentdirectory, &startupInfo, &procInfo ) ) {
        
hRemote RemoteLoadModuleprocInfo.hProcesslpInjectmodulepath );

        if ( 
hRemote != NULL )
            
bResult TRUE;

        
// TODO:
        // The function exists, free the remote ya lazy bastard :)

        
ResumeThreadprocInfo.hThread );
        
WaitForSingleObjectprocInfo.hThreadINFINITE );

        
CloseHandleprocInfo.hProcess );
        
CloseHandleprocInfo.hThread );
    }

    return 
bResult;



kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)

Last edited by koraX; 19-09-2004 at 22:29..
  
Reply With Quote
Re: CreateRemoteThread - Strange behaviour
Old
  (#3)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: CreateRemoteThread - Strange behaviour - 19-09-2004

Just a few notes, maybe it will help :

- perform only simple tasks in dllmain(). DO NOT load/free other libraries in dllmain(). (I know in most bots they call freelibrary to free game DLL file from dllmain(), but it is not good ) See here for more info.
Quote:
The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary), because this can result in a DLL being used after the system has executed its termination code.
- http://weblogs.asp.net/oldnewthing/a.../27/63401.aspx
- http://weblogs.asp.net/oldnewthing/a.../28/63880.aspx


kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)

Last edited by koraX; 19-09-2004 at 22:41..
  
Reply With Quote
Re: CreateRemoteThread - Strange behaviour
Old
  (#4)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: CreateRemoteThread - Strange behaviour - 20-09-2004

It had the same result both times even when DllMain did nothing and just returned TRUE.
This also only happens when the process is created with the CREATE_SUSPENDED flag. But since that is the only time where IAT patching is useful, injection afterwards is basically useless.

I have had success with proxy dlls but they take a loong time to make and does not work with kernel32.dll functions. There was also another method where you overwrote the entrypoint with your code, let it execute and restored the original. The problem with that was I tried writing 0xCD03 ( equal to __asm int 3 ) and the application would refuse to start.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com