Facebook
From Social Water Vole, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 262
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <tlhelp32.h> /* PROCESSENTRY32 */
  4. #include <conio.h> /* _getch() */
  5. #include <shlwapi.h> /* StrStrI */
  6. #include <utility> /* FindTopWindow */
  7.  
  8. #pragma comment(lib, "shlwapi.lib") /* unresolved external symbol __imp__StrStrIW@8 */
  9.  
  10. typedef HMODULE(WINAPI *pLoadLibraryA)(LPCSTR);
  11. typedef FARPROC(WINAPI *pGetProcAddress)(HMODULE, LPCSTR);
  12.  
  13. typedef BOOL(WINAPI *PDLL_MAIN)(HMODULE, DWORD, PVOID);
  14.  
  15. typedef struct _MANUAL_INJECT
  16. {
  17.         PVOID ImageBase;
  18.         PIMAGE_NT_HEADERS NtHeaders;
  19.         PIMAGE_BASE_RELOCATION BaseRelocation;
  20.         PIMAGE_IMPORT_DESCRIPTOR ImportDirectory;
  21.         pLoadLibraryA fnLoadLibraryA;
  22.         pGetProcAddress fnGetProcAddress;
  23. }MANUAL_INJECT, *PMANUAL_INJECT;
  24.  
  25. DWORD WINAPI LoadDll(PVOID p)
  26. {
  27.         PMANUAL_INJECT ManualInject;
  28.  
  29.         HMODULE hModule;
  30.         DWORD i, Function, count, delta;
  31.  
  32.         PDWORD ptr;
  33.         PWORD list;
  34.  
  35.         PIMAGE_BASE_RELOCATION pIBR;
  36.         PIMAGE_IMPORT_DESCRIPTOR pIID;
  37.         PIMAGE_IMPORT_BY_NAME pIBN;
  38.         PIMAGE_THUNK_DATA FirstThunk, OrigFirstThunk;
  39.  
  40.         PDLL_MAIN EntryPoint;
  41.  
  42.         ManualInject = (PMANUAL_INJECT)p;
  43.  
  44.         pIBR = ManualInject->BaseRelocation;
  45.         delta = (DWORD)((LPBYTE)ManualInject->ImageBase - ManualInject->NtHeaders->OptionalHeader.ImageBase); // Calculate the delta
  46.  
  47.                                                                                                                                                                                                                   // Relocate the image
  48.  
  49.         while (pIBR->VirtualAddress)
  50.         {
  51.                 if (pIBR->SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION))
  52.                 {
  53.                         count = (pIBR->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
  54.                         list = (PWORD)(pIBR + 1);
  55.  
  56.                         for (i = 0; i<count; i++)
  57.                         {
  58.                                 if (list[i])
  59.                                 {
  60.                                         ptr = (PDWORD)((LPBYTE)ManualInject->ImageBase + (pIBR->VirtualAddress + (list[i] & 0xFFF)));
  61.                                         *ptr += delta;
  62.                                 }
  63.                         }
  64.                 }
  65.  
  66.                 pIBR = (PIMAGE_BASE_RELOCATION)((LPBYTE)pIBR + pIBR->SizeOfBlock);
  67.         }
  68.  
  69.         pIID = ManualInject->ImportDirectory;
  70.  
  71.         // Resolve DLL imports
  72.  
  73.         while (pIID->Characteristics)
  74.         {
  75.                 OrigFirstThunk = (PIMAGE_THUNK_DATA)((LPBYTE)ManualInject->ImageBase + pIID->OriginalFirstThunk);
  76.                 FirstThunk = (PIMAGE_THUNK_DATA)((LPBYTE)ManualInject->ImageBase + pIID->FirstThunk);
  77.  
  78.                 hModule = ManualInject->fnLoadLibraryA((LPCSTR)ManualInject->ImageBase + pIID->Name);
  79.  
  80.                 if (!hModule)
  81.                 {
  82.                         return FALSE;
  83.                 }
  84.  
  85.                 while (OrigFirstThunk->u1.AddressOfData)
  86.                 {
  87.                         if (OrigFirstThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG)
  88.                         {
  89.                                 // Import by ordinal
  90.  
  91.                                 Function = (DWORD)ManualInject->fnGetProcAddress(hModule, (LPCSTR)(OrigFirstThunk->u1.Ordinal & 0xFFFF));
  92.  
  93.                                 if (!Function)
  94.                                 {
  95.                                         return FALSE;
  96.                                 }
  97.  
  98.                                 FirstThunk->u1.Function = Function;
  99.                         }
  100.  
  101.                         else
  102.                         {
  103.                                 // Import by name
  104.  
  105.                                 pIBN = (PIMAGE_IMPORT_BY_NAME)((LPBYTE)ManualInject->ImageBase + OrigFirstThunk->u1.AddressOfData);
  106.                                 Function = (DWORD)ManualInject->fnGetProcAddress(hModule, (LPCSTR)pIBN->Name);
  107.  
  108.                                 if (!Function)
  109.                                 {
  110.                                         return FALSE;
  111.                                 }
  112.  
  113.                                 FirstThunk->u1.Function = Function;
  114.                         }
  115.  
  116.                         OrigFirstThunk++;
  117.                         FirstThunk++;
  118.                 }
  119.  
  120.                 pIID++;
  121.         }
  122.  
  123.         if (ManualInject->NtHeaders->OptionalHeader.AddressOfEntryPoint)
  124.         {
  125.                 EntryPoint = (PDLL_MAIN)((LPBYTE)ManualInject->ImageBase + ManualInject->NtHeaders->OptionalHeader.AddressOfEntryPoint);
  126.                 return EntryPoint((HMODULE)ManualInject->ImageBase, DLL_PROCESS_ATTACH, NULL); // Call the entry point
  127.         }
  128.  
  129.         return TRUE;
  130. }
  131.  
  132. DWORD WINAPI LoadDllEnd()
  133. {
  134.         return 0;
  135. }
  136.  
  137. HWND FindTopWindow(DWORD pid)
  138. {
  139.         std::pair<HWND, DWORD> params = { 0, pid };
  140.  
  141.         // Enumerate the windows using a lambda to process each window
  142.         BOOL bResult = EnumWindows([](HWND hwnd, LPARAM lParam) -> BOOL
  143.         {
  144.                 auto pParams = (std::pair<HWND, DWORD>*)(lParam);
  145.  
  146.                 DWORD processId;
  147.                 if (GetWindowThreadProcessId(hwnd, &processId) && processId == pParams->second)
  148.                 {
  149.                         // Stop enumerating
  150.                         SetLastError(-1);
  151.                         pParams->first = hwnd;
  152.                         return FALSE;
  153.                 }
  154.  
  155.                 // Continue enumerating
  156.                 return TRUE;
  157.         }, (LPARAM)&params);
  158.  
  159.         if (!bResult && GetLastError() == -1 && params.first)
  160.         {
  161.                 return params.first;
  162.         }
  163.  
  164.         return 0;
  165. }
  166.  
  167. DWORD GetTargetThreadIDFromProcName(const char * ProcName)
  168. {
  169.         PROCESSENTRY32 pe;
  170.         HANDLE thSnapShot;
  171.         BOOL retval, ProcFound = false;
  172.  
  173.         thSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  174.         if (thSnapShot == INVALID_HANDLE_VALUE)
  175.         {
  176.                 //MessageBox(NULL, "Error: Unable to create toolhelp snapshot!", "2MLoader", MB_OK);
  177.                 printf("Error: Unable to create toolhelp snapshot!");
  178.                 return false;
  179.         }
  180.  
  181.         pe.dwSize = sizeof(PROCESSENTRY32);
  182.  
  183.         retval = Process32First(thSnapShot, &pe);
  184.         while (retval)
  185.         {
  186.                 if (StrStrI(pe.szExeFile, ProcName))
  187.                 {
  188.                         return pe.th32ProcessID;
  189.                 }
  190.                 retval = Process32Next(thSnapShot, &pe);
  191.         }
  192.         return 0;
  193. }
  194.  
  195. DWORD ProcessId;
  196.  
  197. int wmain(int argc, wchar_t* argv[])
  198. {
  199.         PIMAGE_DOS_HEADER pIDH;
  200.         PIMAGE_NT_HEADERS pINH;
  201.         PIMAGE_SECTION_HEADER pISH;
  202.  
  203.         HANDLE hProcess, hThread, hFile, hToken;
  204.         PVOID buffer, image, mem;
  205.         DWORD i, FileSize, ExitCode, read;
  206.  
  207.         TOKEN_PRIVILEGES tp;
  208.         MANUAL_INJECT ManualInject;
  209.  
  210.         if (OpenProcessToken((HANDLE)-1, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  211.         {
  212.                 tp.PrivilegeCount = 1;
  213.                 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  214.  
  215.                 tp.Privileges[0].Luid.LowPart = 20;
  216.                 tp.Privileges[0].Luid.HighPart = 0;
  217.  
  218.                 AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  219.                 CloseHandle(hToken);
  220.         }
  221.  
  222.         printf("\nOpening the DLL.\n");
  223.         hFile = CreateFile("AO.dll", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); // Open the DLL
  224.  
  225.         if (hFile == INVALID_HANDLE_VALUE)
  226.         {
  227.                 printf("\nError: Unable to open the DLL (%d)\n", GetLastError());
  228.                 return -1;
  229.         }
  230.  
  231.         FileSize = GetFileSize(hFile, NULL);
  232.         buffer = VirtualAlloc(NULL, FileSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  233.  
  234.         if (!buffer)
  235.         {
  236.                 printf("\nError: Unable to allocate memory for DLL data (%d)\n", GetLastError());
  237.  
  238.                 CloseHandle(hFile);
  239.                 return -1;
  240.         }
  241.  
  242.         // Read the DLL
  243.  
  244.         if (!ReadFile(hFile, buffer, FileSize, &read, NULL))
  245.         {
  246.                 printf("\nError: Unable to read the DLL (%d)\n", GetLastError());
  247.  
  248.                 VirtualFree(buffer, 0, MEM_RELEASE);
  249.                 CloseHandle(hFile);
  250.  
  251.                 return -1;
  252.         }
  253.  
  254.         CloseHandle(hFile);
  255.  
  256.         pIDH = (PIMAGE_DOS_HEADER)buffer;
  257.  
  258.         if (pIDH->e_magic != IMAGE_DOS_SIGNATURE)
  259.         {
  260.                 printf("\nError: Invalid executable image.\n");
  261.  
  262.                 VirtualFree(buffer, 0, MEM_RELEASE);
  263.                 return -1;
  264.         }
  265.  
  266.         pINH = (PIMAGE_NT_HEADERS)((LPBYTE)buffer + pIDH->e_lfanew);
  267.  
  268.         if (pINH->Signature != IMAGE_NT_SIGNATURE)
  269.         {
  270.                 printf("\nError: Invalid PE header.\n");
  271.  
  272.                 VirtualFree(buffer, 0, MEM_RELEASE);
  273.                 return -1;
  274.         }
  275.  
  276.         if (!(pINH->FileHeader.Characteristics & IMAGE_FILE_DLL))
  277.         {
  278.                 printf("\nError: The image is not DLL.\n");
  279.  
  280.                 VirtualFree(buffer, 0, MEM_RELEASE);
  281.                 return -1;
  282.         }
  283.  
  284.         // Retrieve process ID
  285.         ProcessId = 0;
  286.         char buf[MAX_PATH] = { 0 };
  287.         do
  288.         {
  289.  
  290.                 PROCESSENTRY32 entry;
  291.                 entry.dwSize = sizeof(PROCESSENTRY32);
  292.  
  293.                 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  294.  
  295.                 if (Process32First(snapshot, &entry))
  296.                         while (Process32Next(snapshot, &entry)) {
  297.                                 if (StrStrI(entry.szExeFile, "Multi Theft Auto.exe")) {
  298.                                         ProcessId = entry.th32ProcessID;
  299.                                 }
  300.                         }
  301.  
  302.                 CloseHandle(snapshot);
  303.  
  304.                 //ProcessId = GetTargetThreadIDFromProcName("Multi Theft Auto.exe");
  305.  
  306.                 Sleep(100);
  307.         } while (!ProcessId);
  308.  
  309.         printf("Multi Theft Auto.exe ProcessId = %d\n", ProcessId);
  310.  
  311.         printf("\nOpening target process.\n");
  312.         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
  313.  
  314.         if (!hProcess)
  315.         {
  316.                 printf("\nError: Unable to open target process (%d)\n", GetLastError());
  317.  
  318.                 VirtualFree(buffer, 0, MEM_RELEASE);
  319.                 CloseHandle(hProcess);
  320.  
  321.                 return -1;
  322.         }
  323.  
  324.         printf("\nAllocating memory for the DLL.\n");
  325.         image = VirtualAllocEx(hProcess, NULL, pINH->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); // Allocate memory for the DLL
  326.  
  327.         if (!image)
  328.         {
  329.                 printf("\nError: Unable to allocate memory for the DLL (%d)\n", GetLastError());
  330.  
  331.                 VirtualFree(buffer, 0, MEM_RELEASE);
  332.                 CloseHandle(hProcess);
  333.  
  334.                 return -1;
  335.         }
  336.  
  337.         // Copy the header to target process
  338.  
  339.         printf("\nCopying headers into target process.\n");
  340.  
  341.         if (!WriteProcessMemory(hProcess, image, buffer, pINH->OptionalHeader.SizeOfHeaders, NULL))
  342.         {
  343.                 printf("\nError: Unable to copy headers to target process (%d)\n", GetLastError());
  344.  
  345.                 VirtualFreeEx(hProcess, image, 0, MEM_RELEASE);
  346.                 CloseHandle(hProcess);
  347.  
  348.                 VirtualFree(buffer, 0, MEM_RELEASE);
  349.                 return -1;
  350.         }
  351.  
  352.         pISH = (PIMAGE_SECTION_HEADER)(pINH + 1);
  353.  
  354.         // Copy the DLL to target process
  355.  
  356.         printf("\nCopying sections to target process.\n");
  357.  
  358.         for (i = 0; i<pINH->FileHeader.NumberOfSections; i++)
  359.         {
  360.                 WriteProcessMemory(hProcess, (PVOID)((LPBYTE)image + pISH[i].VirtualAddress), (PVOID)((LPBYTE)buffer + pISH[i].PointerToRawData), pISH[i].SizeOfRawData, NULL);
  361.         }
  362.  
  363.         printf("\nAllocating memory for the loader code.\n");
  364.         mem = VirtualAllocEx(hProcess, NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); // Allocate memory for the loader code
  365.  
  366.         if (!mem)
  367.         {
  368.                 printf("\nError: Unable to allocate memory for the loader code (%d)\n", GetLastError());
  369.  
  370.                 VirtualFreeEx(hProcess, image, 0, MEM_RELEASE);
  371.                 CloseHandle(hProcess);
  372.  
  373.                 VirtualFree(buffer, 0, MEM_RELEASE);
  374.                 return -1;
  375.         }
  376.  
  377.         printf("\nLoader code allocated at %#x\n", mem);
  378.         memset(&ManualInject, 0, sizeof(MANUAL_INJECT));
  379.  
  380.         ManualInject.ImageBase = image;
  381.         ManualInject.NtHeaders = (PIMAGE_NT_HEADERS)((LPBYTE)image + pIDH->e_lfanew);
  382.         ManualInject.BaseRelocation = (PIMAGE_BASE_RELOCATION)((LPBYTE)image + pINH->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
  383.         ManualInject.ImportDirectory = (PIMAGE_IMPORT_DESCRIPTOR)((LPBYTE)image + pINH->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
  384.         ManualInject.fnLoadLibraryA = LoadLibraryA;
  385.         ManualInject.fnGetProcAddress = GetProcAddress;
  386.  
  387.         printf("\nWriting loader code to target process.\n");
  388.  
  389.         WriteProcessMemory(hProcess, mem, &ManualInject, sizeof(MANUAL_INJECT), NULL); // Write the loader information to target process
  390.         WriteProcessMemory(hProcess, (PVOID)((PMANUAL_INJECT)mem + 1), LoadDll, (DWORD)LoadDllEnd - (DWORD)LoadDll, NULL); // Write the loader code to target process
  391.  
  392.         printf("\nExecuting loader code.\n");
  393.         hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)((PMANUAL_INJECT)mem + 1), mem, 0, NULL); // Create a remote thread to execute the loader code
  394.  
  395.         if (!hThread)
  396.         {
  397.                 printf("\nError: Unable to execute loader code (%d)\n", GetLastError());
  398.  
  399.                 VirtualFreeEx(hProcess, mem, 0, MEM_RELEASE);
  400.                 VirtualFreeEx(hProcess, image, 0, MEM_RELEASE);
  401.  
  402.                 CloseHandle(hProcess);
  403.  
  404.                 VirtualFree(buffer, 0, MEM_RELEASE);
  405.                 return -1;
  406.         }
  407.  
  408.         WaitForSingleObject(hThread, INFINITE);
  409.         GetExitCodeThread(hThread, &ExitCode);
  410.  
  411.         if (!ExitCode)
  412.         {
  413.                 VirtualFreeEx(hProcess, mem, 0, MEM_RELEASE);
  414.                 VirtualFreeEx(hProcess, image, 0, MEM_RELEASE);
  415.  
  416.                 CloseHandle(hThread);
  417.                 CloseHandle(hProcess);
  418.  
  419.                 VirtualFree(buffer, 0, MEM_RELEASE);
  420.                 return -1;
  421.         }
  422.  
  423.         CloseHandle(hThread);
  424.         VirtualFreeEx(hProcess, mem, 0, MEM_RELEASE);
  425.  
  426.         CloseHandle(hProcess);
  427.  
  428.         printf("\nDLL injected at %#x\n", image);
  429.  
  430.         if (pINH->OptionalHeader.AddressOfEntryPoint)
  431.         {
  432.                 printf("\nDLL entry point: %#x\n", (PVOID)((LPBYTE)image + pINH->OptionalHeader.AddressOfEntryPoint));
  433.         }
  434.  
  435.         VirtualFree(buffer, 0, MEM_RELEASE);
  436.         return 0;
  437. }