Remote functions

On lunes, 22 de noviembre de 2010 0 comentarios

Remote functions

So you want to remotely control an application, get some information about it?

Maybe your GetRemoteProcAddress implementation sucks alot?

Check this thing out, it's just some code i wrote while working on my manual mapping issues, this isn't *the* code i used but rather its own standalone version, the version i use is a little more sophisticated but basically does the same things.

Remote.h
PHP Code:
#ifndef __REMOTE_HEADER__
#define __REMOTE_HEADER__
namespace Remote {
    
namespace Allocate
    
{
        
void*    AllocHANDLE hProcesssize_t Size );
        
void*    CommitHANDLE hProcessvoidDatasize_t Size );
        
void    FreeHANDLE hProcessvoidDatasize_t Size );
    };

    
HANDLE    GetRemoteProcessHandleAchar *pszProcessName );
    
HMODULE GetRemoteModuleHandleAHANDLE hProcess, const char *szModule );
    
HMODULE RemoteLoadLibraryAHANDLE hProcesschar *pszLibraryPath );
    
FARPROC GetRemoteProcAddressHANDLE hProcesschar *pszModuleNamechar *pszProcName );
};

#endif //__REMOTE_HEADER__ 
Remote.cpp
PHP Code:
#include "stdafx.h"
#include "Remote.h"
namespace Remote {
    
namespace Allocate
    
{
        
voidAllocHANDLE hProcesssize_t Size )
        {
            return 
VirtualAllocExhProcessNULLSizeMEM_COMMIT MEM_RESERVEPAGE_EXECUTE_READWRITE );
        }

        
voidCommitHANDLE hProcessvoidDatasize_t Size )
        {
            
voidAllocatedPointer AllochProcessSize );

            if( 
AllocatedPointer )
            {
                if( 
WriteProcessMemoryhProcessAllocatedPointerDataSizeNULL ) == TRUE )
                {
                    return 
AllocatedPointer;
                }
                
                
FreehProcessAllocatedPointerSize );
            }

            return 
NULL;
        }

        
void FreeHANDLE hProcessvoidDatasize_t Size )
        {
            
VirtualFreeExhProcessDataSizeMEM_RELEASE );
        }
    };

    
HANDLE GetRemoteProcessHandleAchar *pszProcessName )
    {
        
HANDLE tlh CreateToolhelp32SnapshotTH32CS_SNAPPROCESSNULL );

        
PROCESSENTRY32 proEntry;
        
        
proEntry.dwSize sizeofPROCESSENTRY32 );

        
Process32Firsttlh, &proEntry );
        do
        {
            if( 
_stricmppszProcessNameproEntry.szExeFile ) == )
            {
                
CloseHandletlh );

                return 
OpenProcessPROCESS_ALL_ACCESSFALSEproEntry.th32ProcessID );
            }
        }
        while( 
Process32Nexttlh, &proEntry ) );

        
CloseHandletlh );

        return 
INVALID_HANDLE_VALUE;
    }

    
HMODULE GetRemoteModuleHandleAHANDLE hProcess, const char *szModule )
    {
        
HANDLE tlh CreateToolhelp32SnapshotTH32CS_SNAPMODULEGetProcessIdhProcess ) );

        
MODULEENTRY32 modEntry;
        
        
modEntry.dwSize sizeofMODULEENTRY32 );

        
Module32Firsttlh, &modEntry );
        do
        {
            if( 
_stricmpszModulemodEntry.szModule ) == )
            {
                
CloseHandletlh );

                return 
modEntry.hModule;
            }
        }
        while( 
Module32Nexttlh, &modEntry ) );

        
CloseHandletlh );

        return 
NULL;
    }

    
HMODULE RemoteLoadLibraryAHANDLE hProcesschar *pszLibraryPath )
    {
        
unsigned long ulReturnValue NULL;

        if( 
pszLibraryPath )
        {
            
FARPROC fpLoadLibraryARemote GetRemoteProcAddresshProcess"Kernel32.dll""LoadLibraryA" );

            if( 
fpLoadLibraryARemote )
            {
                
voidAllocatedResult Allocate::AllochProcesssizeofunsigned long ) );
                
voidCommitedLibName Allocate::CommithProcesspszLibraryPathstrlenpszLibraryPath ) + );

                if( 
CommitedLibName )
                {
                    
unsigned char LoadLibraryAThreadBuffer22 ] =
                    {
                        
0x680x000x000x000x00,         //push lib name
                        
0xB80x000x000x000x00,        //mov eax, LoadLibraryA
                        
0xFF0xD0,                         //call eax
                        
0xA30x000x000x000x00,         //mov result, eax
                        
0x330xC0,                         //xor eax, eax (eax = 0)
                        
0xC20x040x00                     //retn 4
                    
};

                    *( 
unsigned long* )( LoadLibraryAThreadBuffer 0x01 ) = ( unsigned long CommitedLibName;
                    *( 
unsigned long* )( LoadLibraryAThreadBuffer 0x06 ) = ( unsigned long fpLoadLibraryARemote;
                    *( 
unsigned long* )( LoadLibraryAThreadBuffer 0x0D ) = ( unsigned long AllocatedResult;

                    
voidRemoteBufferToWrite Allocate::CommithProcessLoadLibraryAThreadBuffersizeofLoadLibraryAThreadBuffer ) );

                    if( 
RemoteBufferToWrite )
                    {
                        
HANDLE hSpawnedThread CreateRemoteThreadhProcess00, ( LPTHREAD_START_ROUTINE RemoteBufferToWrite00);

                        
WaitForSingleObjecthSpawnedThreadINFINITE ); // Async..

                        
ReadProcessMemoryhProcessAllocatedResult, &ulReturnValuesizeofunsigned long ), NULL );

                        
Allocate::FreehProcessRemoteBufferToWritesizeofLoadLibraryAThreadBuffer ) );
                    }

                    
Allocate::FreehProcessCommitedLibNamestrlenpszLibraryPath ) + );
                }
            }
        }

        return 
reinterpret_castHMODULE >( ulReturnValue );
    }

    
FARPROC GetRemoteProcAddressHANDLE hProcesschar *pszModuleNamechar *pszProcName )
    {
        
FARPROC fpReturnValue NULL;
        
        
HMODULE hLocalKernel GetModuleHandleA"Kernel32.dll" );

        if( 
hLocalKernel )
        {
            
HMODULE hRemoteKernel GetRemoteModuleHandleAhProcess"Kernel32.dll" );

            if( 
hRemoteKernel )
            {
                
unsigned long RemoteGetProcAddress 
                    ( 
unsigned long hRemoteKernel + ( unsigned long )( ( unsigned long GetProcAddress - ( unsigned long hLocalKernel );

                
voidResultOfGetProcAddress Allocate::AllochProcesssizeofunsigned long ) );

                
voidCommitedProcName Allocate::CommithProcesspszProcNamestrlenpszProcName ) + );

                if( 
ResultOfGetProcAddress && CommitedProcName )
                {
                    
unsigned char GetProcAddressThreadBuffer27 ] =
                    {
                        
0x680x000x000x000x00,         //push proc name
                        
0x680x000x000x000x00,         //push module address
                        
0xB80x000x000x000x00,        //mov eax, GetProcAddress
                        
0xFF0xD0,                         //call eax
                        
0xA30x000x000x000x00,         //mov result, eax
                        
0x330xC0,                         //xor eax, eax (eax = 0)
                        
0xC20x040x00                     //retn 4
                    
};

                    *( 
unsigned long* )( GetProcAddressThreadBuffer 0x01 ) = ( unsigned long CommitedProcName;
                    *( 
unsigned long* )( GetProcAddressThreadBuffer 0x06 ) = ( unsigned long hRemoteKernel;
                    *( 
unsigned long* )( GetProcAddressThreadBuffer 0x0B ) = ( unsigned long RemoteGetProcAddress;
                    *( 
unsigned long* )( GetProcAddressThreadBuffer 0x12 ) = ( unsigned long ResultOfGetProcAddress;

                    
voidRemoteBufferToWrite Allocate::CommithProcessGetProcAddressThreadBuffersizeofGetProcAddressThreadBuffer ) );

                    if( 
RemoteBufferToWrite )
                    {
                        
HANDLE hSpawnedThread CreateRemoteThreadhProcess00, ( LPTHREAD_START_ROUTINE RemoteBufferToWrite00);

                        
WaitForSingleObjecthSpawnedThreadINFINITE ); // Async..

                        
ReadProcessMemoryhProcessResultOfGetProcAddress, &fpReturnValuesizeofunsigned long ), NULL );

                        
Allocate::FreehProcessRemoteBufferToWritesizeofGetProcAddressThreadBuffer ) );
                    }
                }

                if( 
ResultOfGetProcAddress )
                {
                    
Allocate::FreehProcessResultOfGetProcAddresssizeofunsigned long ) );
                }

                if( 
CommitedProcName )
                {
                    
Allocate::FreehProcessCommitedProcNamestrlenpszProcName ) + );
                }

            }
        }

        return 
fpReturnValue;
    }
}; 
Usage
PHP Code:
#include "stdafx.h"
#include "Remote.h"
DWORD WINAPI lpThreadForSpaceBarLPVOID lpParam )
{
    while( ( 
GetAsyncKeyStateVK_SPACE ) & ) == )
    {
        
Sleep100 );
    }

    return 
0;
}
int _tmain(int argc_TCHARargv[])
{
    
HANDLE hCalculator Remote::GetRemoteProcessHandleA"calc.exe" );

    if( 
hCalculator != INVALID_HANDLE_VALUE )
    {
        
FARPROC RemoteLoadLibraryA Remote::GetRemoteProcAddresshCalculator"Kernel32.dll""LoadLibraryA" );

        if( 
RemoteLoadLibraryA )
        {
            
printf"LoadLibraryA Address is [0x%X]\n"RemoteLoadLibraryA );
        }
        else
        {
            
printf"LoadLibraryA Address was not found..\n" );
        }

        
HMODULE hRemoteUser32 Remote::RemoteLoadLibraryAhCalculator"User32.dll" );

        if( 
hRemoteUser32 )
        {
            
printf"USER32.DLL Address is [0x%X][0x%X]\n"hRemoteUser32GetModuleHandleA"User32.dll" ) );
        }
        else
        {
            
printf"USER32.DLL was not found..\n" );
        }

        
CloseHandlehCalculator ); // Remember to close the handle from OpenProcess
    
}
    else
    {
        
printf"Error opening process: INVALID_HANDLE_VALUE\n" );
    }

    
printf"Press the space bar to continue...\n" );

    
WaitForSingleObjectCreateThread00lpThreadForSpaceBar00), INFINITE );

    return 
0;
Benefits using this implementation for a loader for example, would be that you don't need to actually load modules into your loader to get the foreign address of them (Except Kernel32.dll, but, COME ON, lol) and that modules impossible to load from your loader (like CSS's tier0.dll or vstdlib.dll) will not be a problem anymore.

0 comentarios:

Publicar un comentario