Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,621 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,914 people online right now. Registration is fast and FREE... Join Now!





Dll Injection

Inject a dynamic link library into any process.

Submitted By: msg555
Actions:
Rating:
Views: 16,857

Language: C++

Last Modified: May 4, 2006
Instructions: The below is a cool snippet of code that can insert a dll into a process. Just provide the Process ID and the path of the dll. What's even cooler about this code is how it works.

Snippet


  1. #include <string>
  2. #include <windows.h>
  3.  
  4. #define MAXWAIT 10000
  5.  
  6. bool insertDll(DWORD procID, std::string dll)
  7. {
  8.     //Find the address of the LoadLibrary api, luckily for us, it is loaded in the same address for every process
  9.     HMODULE hLocKernel32 = GetModuleHandle("Kernel32");
  10.     FARPROC hLocLoadLibrary = GetProcAddress(hLocKernel32, "LoadLibraryA");
  11.    
  12.     //Adjust token privileges to open system processes
  13.     HANDLE hToken;
  14.     TOKEN_PRIVILEGES tkp;
  15.     if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  16.     {
  17.         LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
  18.         tkp.PrivilegeCount = 1;
  19.         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  20.         AdjustTokenPrivileges(hToken, 0, &tkp, sizeof(tkp), NULL, NULL);
  21.     }
  22.  
  23.     //Open the process with all access
  24.     HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
  25.  
  26.     //Allocate memory to hold the path to the Dll File in the process's memory
  27.     dll += '\0';
  28.     LPVOID hRemoteMem = VirtualAllocEx(hProc, NULL, dll.size(), MEM_COMMIT, PAGE_READWRITE);
  29.  
  30.     //Write the path to the Dll File in the location just created
  31.     DWORD numBytesWritten;
  32.     WriteProcessMemory(hProc, hRemoteMem, dll.c_str(), dll.size(), &numBytesWritten);
  33.  
  34.     //Create a remote thread that starts begins at the LoadLibrary function and is passed are memory pointer
  35.     HANDLE hRemoteThread = CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)hLocLoadLibrary, hRemoteMem, 0, NULL);
  36.  
  37.     cout << hRemoteThread << endl;
  38.  
  39.     //Wait for the thread to finish
  40.     bool res = false;
  41.     if (hRemoteThread)
  42.         res = (bool)WaitForSingleObject(hRemoteThread, MAXWAIT) != WAIT_TIMEOUT;
  43.  
  44.     //Free the memory created on the other process
  45.     VirtualFreeEx(hProc, hRemoteMem, dll.size(), MEM_RELEASE);
  46.  
  47.     //Release the handle to the other process
  48.     CloseHandle(hProc);
  49.  
  50.     return res;
  51. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month