Guest User

Untitled

a guest
Dec 25th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. int InjectLibrary(DWORD   dwProcessId, PCSTR   sciezka,
  2.     DWORD   dwWait) {
  3.     HANDLE  hProcessHandle    = NULL;
  4.     LPVOID  pvRemoteMemory    = NULL;
  5.     SIZE_T  nLibraryPathSize    = 0;
  6.     SIZE_T  nBytesWriteSize  = 0;
  7.     FARPROC pLoadLibrary        = NULL;
  8.     HANDLE  hRemoteThread      = NULL;
  9.  
  10.     //if ( ( dwProcessId <= 0 ) || ( !sciezka ) )//
  11.     //  return 1;
  12. SetDebugPrivilege(TRUE);
  13. //  enableDebugPrivileges();
  14.     hProcessHandle = OpenProcess( PROCESS_THREAD_ACCESS, FALSE, dwProcessId );
  15.    
  16.     nLibraryPathSize = strlen( sciezka ) + 1; //
  17.     if ( !(pvRemoteMemory = VirtualAllocEx( hProcessHandle,
  18.          NULL, nLibraryPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE ) ) ) {
  19.         CloseHandle( hProcessHandle );
  20.     }
  21.     if ( !WriteProcessMemory( hProcessHandle, pvRemoteMemory, sciezka,
  22.          nLibraryPathSize, &nBytesWriteSize ) ){
  23.         VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
  24.         CloseHandle( hProcessHandle );
  25.     }
  26.     if ( !( pLoadLibrary = GetProcAddress( GetModuleHandle( "kernel32" ), "LoadLibraryA" ) ) ) {
  27.         VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
  28.         CloseHandle( hProcessHandle );
  29.     }
  30.     if ( !( hRemoteThread = CreateRemoteThread( hProcessHandle, NULL, 0,
  31.         (LPTHREAD_START_ROUTINE)pLoadLibrary, pvRemoteMemory, 0, NULL ) ) ) {
  32.         VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
  33.         CloseHandle( hProcessHandle );
  34.     }
  35.     if ( WaitForSingleObject( hRemoteThread, dwWait ) == WAIT_FAILED ) {
  36.         VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
  37.         CloseHandle( hRemoteThread );
  38.         CloseHandle( hProcessHandle );
  39.     }
  40.     VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
  41.     CloseHandle( hRemoteThread );
  42.     CloseHandle( hProcessHandle );
  43.     return true;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment