Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int InjectLibrary(DWORD dwProcessId, PCSTR sciezka,
- DWORD dwWait) {
- HANDLE hProcessHandle = NULL;
- LPVOID pvRemoteMemory = NULL;
- SIZE_T nLibraryPathSize = 0;
- SIZE_T nBytesWriteSize = 0;
- FARPROC pLoadLibrary = NULL;
- HANDLE hRemoteThread = NULL;
- //if ( ( dwProcessId <= 0 ) || ( !sciezka ) )//
- // return 1;
- SetDebugPrivilege(TRUE);
- // enableDebugPrivileges();
- hProcessHandle = OpenProcess( PROCESS_THREAD_ACCESS, FALSE, dwProcessId );
- nLibraryPathSize = strlen( sciezka ) + 1; //
- if ( !(pvRemoteMemory = VirtualAllocEx( hProcessHandle,
- NULL, nLibraryPathSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE ) ) ) {
- CloseHandle( hProcessHandle );
- }
- if ( !WriteProcessMemory( hProcessHandle, pvRemoteMemory, sciezka,
- nLibraryPathSize, &nBytesWriteSize ) ){
- VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
- CloseHandle( hProcessHandle );
- }
- if ( !( pLoadLibrary = GetProcAddress( GetModuleHandle( "kernel32" ), "LoadLibraryA" ) ) ) {
- VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
- CloseHandle( hProcessHandle );
- }
- if ( !( hRemoteThread = CreateRemoteThread( hProcessHandle, NULL, 0,
- (LPTHREAD_START_ROUTINE)pLoadLibrary, pvRemoteMemory, 0, NULL ) ) ) {
- VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
- CloseHandle( hProcessHandle );
- }
- if ( WaitForSingleObject( hRemoteThread, dwWait ) == WAIT_FAILED ) {
- VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
- CloseHandle( hRemoteThread );
- CloseHandle( hProcessHandle );
- }
- VirtualFreeEx( hProcessHandle, pvRemoteMemory, nLibraryPathSize, MEM_DECOMMIT );
- CloseHandle( hRemoteThread );
- CloseHandle( hProcessHandle );
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment