NoamCohen123

Dll_Injector

Jul 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <Windows.h>
  4.  
  5. int main(int argc, CHAR *argv[])
  6. {
  7.     int pid = 0;
  8.     DWORD number_written;
  9.     pid = atoi(argv[1]);
  10.     CHAR *dll_path;
  11.     dll_path = argv[2];
  12.     LPCWSTR kernel = LPCWSTR("kernel32.dll");
  13.     char *buffer;
  14.  
  15.     // Get handle to kernel32 module
  16.     HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  17.     if (kernel32 == NULL)
  18.     {
  19.         printf("Can't get handle to kernel32 %d", GetLastError());
  20.     }
  21.  
  22.     // Get the address of loadlibrary function
  23.     FARPROC loadLibrary = GetProcAddress(kernel32, "LoadLibraryA");
  24.     //LoadLibraryA(dll_path);
  25.     if (!loadLibrary)
  26.     {
  27.         int f = GetLastError();
  28.         printf("Cant get loadlibrary address %d", f);
  29.     }
  30.  
  31.     HANDLE victim_process = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  32.     if (victim_process == INVALID_HANDLE_VALUE)
  33.     {
  34.         printf("Can't open remote process");
  35.         return 0;
  36.     }
  37.    
  38.     // Because every process has it's own address so we write the name of the dll itself in order for the program to load it
  39.     LPVOID remoteDllName = VirtualAllocEx(victim_process, NULL, strlen(dll_path) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  40.     if (remoteDllName == NULL)
  41.     {
  42.         printf("Can't allocate on the remote process");
  43.     }
  44.  
  45.     // We write the name of the dll to the victim process using the address we got with virtual alloc
  46.     if (!WriteProcessMemory(victim_process, remoteDllName, dll_path, strlen(dll_path) + 1, &number_written))
  47.     {
  48.         printf("Can't write to the remote process");
  49.     }
  50.  
  51.     // Creates the remote thread on the target process.
  52.     HANDLE remoteThread = CreateRemoteThread(victim_process, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibrary, remoteDllName, 0, NULL);
  53.     if (!remoteThread)
  54.     {
  55.         int j = GetLastError();
  56.         printf("Cant open thread on the remote thread ERROR: %d", j);
  57.        
  58.     }
  59.     // Wait for the thread to finish.
  60.     bool finished = WaitForSingleObject(remoteThread, 10000) !=
  61.         WAIT_TIMEOUT;
  62.     VirtualFreeEx(victim_process, remoteDllName, sizeof(dll_path), MEM_RELEASE);
  63.     CloseHandle(victim_process);
  64.  
  65.  
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment