Advertisement
Guest User

DLL injection

a guest
Nov 29th, 2012
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main( int argc,char* argv[ ] ) {
  5.     if( argc < 3 ) {
  6.         printf( "Usage: Inject <executable> <injected DLL>\n" );
  7.         return 1;
  8.     }
  9.  
  10.     STARTUPINFO si;
  11.     PROCESS_INFORMATION pi;
  12.     char szLibPath[ MAX_PATH + 1 ];
  13.    
  14.     // Start our process in suspended mode
  15.     ZeroMemory( &si,sizeof( si ) );
  16.     ZeroMemory( &pi,sizeof( pi ) );
  17.     CreateProcess( argv[ 1 ],NULL,NULL,NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi );
  18.  
  19.     strcpy( szLibPath,argv[ 2 ] );
  20.     // Allocate memory in the remote process for szLibPath
  21.     void* pLibRemote = VirtualAllocEx( pi.hProcess,NULL,sizeof(szLibPath),MEM_COMMIT,PAGE_EXECUTE_READWRITE );
  22.     // Write szLibPath to the allocated memory
  23.     WriteProcessMemory( pi.hProcess,pLibRemote,( void* ) szLibPath,sizeof( szLibPath ),NULL );
  24.     // Load our dll into the remote process (via CreateRemoteThread & LoadLibrary)
  25.     HMODULE hKernel32 = GetModuleHandle( "kernel32.dll" );
  26.     HANDLE hThread = CreateRemoteThread( pi.hProcess,NULL,0,( LPTHREAD_START_ROUTINE) GetProcAddress( hKernel32,"LoadLibraryA" ),pLibRemote,0,NULL );
  27.     // Wait for hotpatching to complete
  28.     WaitForSingleObject( hThread,INFINITE );
  29.     // Resume the primary thread of our process
  30.     ResumeThread( pi.hThread );
  31.    
  32.     // Clean up
  33.     CloseHandle( hThread );
  34.     VirtualFreeEx( pi.hProcess, pLibRemote, sizeof(szLibPath), MEM_RELEASE );
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement