Standard DLL Injector

On viernes, 28 de enero de 2011 0 comentarios

Description // Info




Source Code

  1. #define DEFAULT_DLL_NAME                \"gamereversal.dll\"
  2. #define WIN32_LEAN_AND_MEAN
  3. #include
  4.  
  5. // struct with data needed for remote thread.
  6. typedef struct i_data
  7. {
  8.         HINSTANCE       (__stdcall *LoadLibrary)( LPCTSTR lpLibFileName );      
  9.         VOID            (__stdcall *ExitThread)( DWORD dwExitCode );
  10.         VOID            (__stdcall *ExitProcess)( UINT uExitCode );
  11.         int                     (__stdcall *MessageBox)( HWND hWnd, LPCTSTR lpText,     LPCTSTR lpCaption, UINT uType  );
  12.         char            Error[128];
  13.         char            DllName[MAX_PATH];
  14. }i_data;
  15.  
  16.  
  17. __inline DWORD __stdcall InjectDll( i_data *i_data )
  18. {
  19.         if( !i_data->LoadLibrary( i_data->DllName ) )
  20.         {
  21.                 i_data->MessageBox( NULL, i_data->Error, i_data->Error, NULL );
  22.                 i_data->ExitProcess(0);
  23.         }
  24.         i_data->ExitThread(0);
  25.         return 0;
  26. }
  27. __inline void EndInjectDll( void ){ return; }
  28.  
  29.  
  30. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow   )
  31. {
  32.         static PROCESS_INFORMATION      ProcessInformation;
  33.         static STARTUPINFO                      StartupInfo;
  34.         HANDLE                                          hProcess = 0;
  35.         HANDLE                                          hThread = 0;
  36.         i_data                                          idata;
  37.         LPVOID                                          ridata;
  38.         LPVOID                                          rInjectDll;
  39.         DWORD                                           tid;
  40.         char                                            szDll[MAX_PATH];
  41.  
  42.         if( !CreateProcess(    
  43.                                         NULL,
  44.                                         \"<>\",
  45.                                         NULL,
  46.                                         NULL,
  47.                                         NULL,
  48.                                         CREATE_SUSPENDED,
  49.                                         NULL,
  50.                                         NULL,
  51.                                         &StartupInfo,
  52.                                         &ProcessInformation ) )
  53.         {
  54.                 MessageBox( NULL, \"Can\'t kick start the application\", \"www.gamereversal.com\", NULL );
  55.                 return 0;
  56.         }
  57.  
  58.         hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessInformation.dwProcessId );
  59.  
  60.         GetCurrentDirectory( sizeof szDll, szDll );
  61.         wsprintf( szDll, \"%s%s\", szDll, DEFAULT_DLL_NAME );
  62.  
  63.         // fill structure with the needed data we gonna pass to remote thread.
  64.         lstrlen( lpCmdLine ) ? lstrcpy( idata.DllName, lpCmdLine ) : lstrcpy( idata.DllName, szDll );
  65.         lstrcpy( idata.Error, \"Can\'t find dll. You can specify the dll name as command line (with no quote marks and full path).\" );
  66.         idata.ExitThread = ExitThread;
  67.         idata.ExitProcess = ExitProcess;
  68.         idata.LoadLibrary = LoadLibraryA;
  69.         idata.MessageBox = MessageBoxA;
  70.  
  71.         // allocate memory on remote process for the thread and the structure.
  72.         ridata  = VirtualAllocEx( hProcess, NULL, sizeof idata, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
  73.         rInjectDll      = VirtualAllocEx( hProcess, NULL, (unsigned int)((unsigned int)EndInjectDll-(unsigned int)InjectDll), MEM_COMMIT, PAGE_EXECUTE_READWRITE );
  74.  
  75.         if( ridata && rInjectDll )
  76.         {
  77.                 // copy data to remote process.
  78.                 if( WriteProcessMemory( hProcess, ridata, &idata, sizeof idata, NULL ) &&
  79.                     WriteProcessMemory( hProcess, rInjectDll, InjectDll, (unsigned int)((unsigned int)EndInjectDll-(unsigned int)InjectDll), NULL ) )
  80.                 {
  81.                         // create thread on remote process.
  82.                         hThread = CreateRemoteThread( hProcess,
  83.                                                                                   NULL,
  84.                                                                                   0,
  85.                                                                                   (LPTHREAD_START_ROUTINE)rInjectDll,
  86.                                                                                   ridata,
  87.                                                                                   0,
  88.                                                                                   &tid );
  89.                 }
  90.                 if( hThread )
  91.                 {
  92.                         // wait for remote thread to finish.
  93.                         WaitForSingleObject( hThread, INFINITE );
  94.                         // resume main process thread.
  95.                         ResumeThread( ProcessInformation.hThread );
  96.                         CloseHandle( hThread );
  97.                 }
  98.                 // free memory allocated on remote process.
  99.                 VirtualFreeEx( hProcess, ridata, 0, MEM_RELEASE  );
  100.                 VirtualFreeEx( hProcess, rInjectDll, 0, MEM_RELEASE      );
  101.         }
  102.         CloseHandle( hProcess );
  103.         return 0;
  104. }

0 comentarios:

Publicar un comentario