Advertisement
EddyKrieger

netHost

Jul 7th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Storage.h"
  3. #include "Antidump.h"
  4. #include <metahost.h>
  5. #pragma comment(lib, "MSCorEE.lib")
  6.  
  7. #import "C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb" raw_interfaces_only \
  8.     high_property_prefixes("_get","_put","_putref")     \
  9.     rename("ReportEvent", "InteropServices_ReportEvent")
  10. using namespace mscorlib;
  11.  
  12. #define RAW_ASSEMBLY_LENGTH 982016
  13.  
  14. void runNetAssembly();
  15.  
  16.    
  17. void runNetAssembly()
  18. {
  19.     ICLRMetaHost* pMetaHost = NULL;
  20.  
  21.     HRESULT hr;
  22.  
  23.     /* Get ICLRMetaHost instance */
  24.  
  25.     hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (VOID**)&pMetaHost);
  26.  
  27.     if (FAILED(hr))
  28.     {
  29.  
  30.     }
  31.  
  32.     //printf("[+] CLRCreateInstance(...) succeeded\n");
  33.  
  34.     ICLRRuntimeInfo* pRuntimeInfo = NULL;
  35.  
  36.     /* Get ICLRRuntimeInfo instance */
  37.  
  38.     hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (VOID**)&pRuntimeInfo);
  39.  
  40.     if (FAILED(hr))
  41.     {
  42.  
  43.     }
  44.  
  45.     //printf("[+] pMetaHost->GetRuntime(...) succeeded\n");
  46.  
  47.     BOOL bLoadable;
  48.  
  49.     /* Check if the specified runtime can be loaded */
  50.  
  51.     hr = pRuntimeInfo->IsLoadable(&bLoadable);
  52.  
  53.     if (FAILED(hr) || !bLoadable)
  54.     {
  55.  
  56.     }
  57.  
  58.     //printf("[+] pRuntimeInfo->IsLoadable(...) succeeded\n");
  59.  
  60.     ICorRuntimeHost* pRuntimeHost = NULL;
  61.  
  62.     /* Get ICorRuntimeHost instance */
  63.  
  64.     hr = pRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_ICorRuntimeHost, (VOID**)&pRuntimeHost);
  65.  
  66.     if (FAILED(hr))
  67.     {
  68.  
  69.  
  70.     }
  71.  
  72.     //printf("[+] pRuntimeInfo->GetInterface(...) succeeded\n");
  73.  
  74.     /* Start the CLR */
  75.  
  76.     hr = pRuntimeHost->Start();
  77.  
  78.     if (FAILED(hr))
  79.     {
  80.  
  81.  
  82.     }
  83.  
  84.     //printf("[+] pRuntimeHost->Start() succeeded\n");
  85.  
  86.     IUnknownPtr pAppDomainThunk = NULL;
  87.  
  88.     hr = pRuntimeHost->GetDefaultDomain(&pAppDomainThunk);
  89.  
  90.     if (FAILED(hr))
  91.     {
  92.  
  93.  
  94.     }
  95.  
  96.     //printf("[+] pRuntimeHost->GetDefaultDomain(...) succeeded\n");
  97.  
  98.     _AppDomainPtr pDefaultAppDomain = NULL;
  99.  
  100.     /* Equivalent of System.AppDomain.CurrentDomain in C# */
  101.  
  102.     hr = pAppDomainThunk->QueryInterface(__uuidof(_AppDomain), (VOID**)&pDefaultAppDomain);
  103.  
  104.     if (FAILED(hr))
  105.     {
  106.  
  107.     }
  108.  
  109.     //printf("[+] pAppDomainThunk->QueryInterface(...) succeeded\n");
  110.  
  111.     _AssemblyPtr pAssembly = NULL;
  112.  
  113.     SAFEARRAYBOUND rgsabound[1];
  114.  
  115.     rgsabound[0].cElements = RAW_ASSEMBLY_LENGTH;
  116.  
  117.     rgsabound[0].lLbound = 0;
  118.  
  119.     SAFEARRAY* pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
  120.  
  121.     void* pvData = NULL;
  122.  
  123.     hr = SafeArrayAccessData(pSafeArray, &pvData);
  124.  
  125.     if (FAILED(hr))
  126.     {
  127.  
  128.     }
  129.  
  130.     //printf("[+] SafeArrayAccessData(...) succeeded\n");
  131.  
  132.  
  133.  
  134.    
  135.  
  136.     //memcpy(pvData, bytes, dwSize);
  137.     memcpy(pvData, rawData, RAW_ASSEMBLY_LENGTH);
  138.  
  139.     hr = SafeArrayUnaccessData(pSafeArray);
  140.  
  141.     if (FAILED(hr))
  142.     {
  143.  
  144.  
  145.     }
  146.  
  147.     //printf("[+] SafeArrayUnaccessData(...) succeeded\n");
  148.  
  149.     /* Equivalent of System.AppDomain.CurrentDomain.Load(byte[] rawAssembly) */
  150.  
  151.     hr = pDefaultAppDomain->Load_3(pSafeArray, &pAssembly);
  152.  
  153.     if (FAILED(hr))
  154.     {
  155.  
  156.     }
  157.  
  158.     //printf("[+] pDefaultAppDomain->Load_3(...) succeeded\n");
  159.  
  160.     _MethodInfoPtr pMethodInfo = NULL;
  161.  
  162.     _AssemblyNamePtr pAsmName = NULL;
  163.  
  164.     /* Assembly.EntryPoint Property */
  165.  
  166.     //auto asmPtr = &mscorlib::_Assembly::get_EntryPoint;
  167.  
  168.     hr = pAssembly->GetName(&pAsmName);
  169.  
  170.     hr = pAssembly->get_EntryPoint(&pMethodInfo);
  171.  
  172.     if (FAILED(hr))
  173.     {
  174.  
  175.  
  176.     }
  177.  
  178.     //printf("[+] pAssembly->get_EntryPoint(...) succeeded\n");
  179.  
  180.     VARIANT retVal;
  181.     ZeroMemory(&retVal, sizeof(VARIANT));
  182.  
  183.     VARIANT obj;
  184.     ZeroMemory(&obj, sizeof(VARIANT));
  185.     obj.vt = VT_NULL;
  186.  
  187.     //TODO! Change cElement to the number of Main arguments
  188.     SAFEARRAY *psaStaticMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 0);
  189.  
  190.     /* EntryPoint.Invoke(null, new object[0]) */
  191.  
  192.  
  193.  
  194.     hr = pMethodInfo->Invoke_3(obj, psaStaticMethodArgs, &retVal);
  195.  
  196.     if (FAILED(hr))
  197.     {
  198.         MessageBox(NULL, L"Error!", L"Internal error:  00001", MB_ICONWARNING | MB_OK);
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement