Advertisement
Guest User

Advanced DLL Injector

a guest
Jul 30th, 2011
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #define _WIN32_WINNT _WIN32_WINNT_WINXP
  2.  
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <tlhelp32.h>
  6.  
  7. int gethd()
  8. {
  9.     PROCESSENTRY32 pe32;
  10.     pe32.dwSize = sizeof(pe32);
  11.     HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  12.     if(hProcessSnap == INVALID_HANDLE_VALUE)
  13.         return -1;
  14.     else
  15.     {
  16.         if(!Process32First(hProcessSnap, &pe32))
  17.             return -2;
  18.         do
  19.         {
  20.             std::cout << pe32.szExeFile << "[" << pe32.th32ProcessID << "]\n";
  21.         }while(Process32Next(hProcessSnap, &pe32));
  22.     }
  23.     return 0;
  24. }
  25.  
  26. int main()
  27. {
  28.     std::cout << "\n\n+-+-+-+-+-+-+-+-+\n";
  29.     std::cout << "|1|n|j|3|k|t|0|r|\n";
  30.     std::cout << "+-+-+-+-+-+-+-+-+\n\n\n\n";
  31.     switch(gethd())
  32.     {
  33.         case -1:
  34.             std::cout << "CreateToolhelp32Snapshot()";
  35.         break;
  36.         case -2:
  37.             std::cout << "Process32First()";
  38.         break;
  39.         case 0:
  40.         {
  41.             int pid = 0;
  42.             HANDLE hTarget, hThread;
  43.             LPVOID pAddr, vAlloc;
  44.             OPENFILENAME ofn;
  45.             char dllName[256];
  46.  
  47.             ZeroMemory(&ofn, sizeof(ofn));
  48.             memset(dllName, 0, 256);
  49.            
  50.             std::cout << "\n\n\nEnter the process PID: ";
  51.             std::cin >> pid;
  52.            
  53.             hTarget = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  54.             if(hTarget == INVALID_HANDLE_VALUE)
  55.                 std::cout << "hTarget returned " << GetLastError();
  56.             else
  57.             {
  58.                 ofn.lStructSize = sizeof(ofn);
  59.                 ofn.hwndOwner = FindWindow("ConsoleWindowClass", NULL);
  60.                 ofn.lpstrFile = dllName;
  61.                 ofn.lpstrFile[0] = '\0';
  62.                 ofn.nMaxFile = sizeof(dllName);
  63.                 ofn.lpstrFilter = "";
  64.                 ofn.nFilterIndex = 1;
  65.                 ofn.lpstrInitialDir = "C:\\";
  66.                 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  67.                 if(GetOpenFileName(&ofn))
  68.                 {
  69.                     pAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  70.                     vAlloc = VirtualAllocEx(hTarget, NULL, strlen(dllName), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
  71.                
  72.                     if(pAddr == NULL)
  73.                         std::cout << "pAddr returned " << GetLastError();
  74.                     else if(vAlloc == NULL)
  75.                         std::cout << "vAlloc returned " << GetLastError();
  76.                     else
  77.                     {
  78.                         if(!WriteProcessMemory(hTarget, vAlloc, dllName, strlen(dllName), NULL))
  79.                             std::cout << "WriteProcessMemory returned " << GetLastError();
  80.  
  81.                         if((hThread = CreateRemoteThread(hTarget, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryA, vAlloc, NULL, NULL)) == NULL)
  82.                             std::cout << "CreateRemoteThread returned " << GetLastError();
  83.                         else
  84.                         {
  85.                             std::cout << "Injected " << dllName;
  86.                             WaitForSingleObject(hThread, WAIT_TIMEOUT);
  87.                            
  88.                         }
  89.                     }
  90.                 }
  91.                 else
  92.                     std::cout << "Please choose a DLL.";
  93.             }
  94.         }
  95.     }
  96.     VirtualFreeEx(hTarget, vAlloc, NULL, MEM_RELEASE);
  97.     CloseHandle(hTarget);
  98.     std::cin.ignore();
  99.     std::cin.get();
  100.     return 0;  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement