findwebclient.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "findwebclient.h"
  4. #include "beacon.h"
  5. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  6. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  7. HRESULT hr = S_FALSE;
  8. va_list argList;
  9. DWORD dwWritten = 0;
  10. if (g_lpStream <= (LPSTREAM)1) {
  11. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  12. if (FAILED(hr)) {
  13. return hr;
  14. }
  15. }
  16. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  17. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  18. if (g_lpwPrintBuffer == NULL) {
  19. hr = E_FAIL;
  20. goto CleanUp;
  21. }
  22. }
  23. va_start(argList, lpwFormat);
  24. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  25. hr = E_FAIL;
  26. goto CleanUp;
  27. }
  28. if (g_lpStream != NULL) {
  29. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  30. goto CleanUp;
  31. }
  32. }
  33. hr = S_OK;
  34. CleanUp:
  35. if (g_lpwPrintBuffer != NULL) {
  36. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  37. }
  38. va_end(argList);
  39. return hr;
  40. }
  41. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  42. VOID BeaconOutputStreamW() {
  43. STATSTG ssStreamData = { 0 };
  44. SIZE_T cbSize = 0;
  45. ULONG cbRead = 0;
  46. LARGE_INTEGER pos;
  47. LPWSTR lpwOutput = NULL;
  48. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  49. return;
  50. }
  51. cbSize = ssStreamData.cbSize.LowPart;
  52. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  53. if (lpwOutput != NULL) {
  54. pos.QuadPart = 0;
  55. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  56. goto CleanUp;
  57. }
  58. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  59. goto CleanUp;
  60. }
  61. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  62. }
  63. CleanUp:
  64. if (g_lpStream != NULL) {
  65. g_lpStream->lpVtbl->Release(g_lpStream);
  66. g_lpStream = NULL;
  67. }
  68. if (g_lpwPrintBuffer != NULL) {
  69. MSVCRT$free(g_lpwPrintBuffer);
  70. g_lpwPrintBuffer = NULL;
  71. }
  72. if (lpwOutput != NULL) {
  73. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  74. }
  75. return;
  76. }
  77. int go(char *args, int len) {
  78. char* pipeNameHead = "\\\\";
  79. char* pipeNameTail = "\\pipe\\DAV RPC SERVICE";
  80. BOOL pipeStatus = 0;
  81. char* hostname;
  82. char* nextHostname;
  83. char* debug;
  84. int iBytesLen = 0;
  85. CHAR *hostFileBytes;
  86. WCHAR wHostname[256];
  87. datap parser;
  88. BeaconDataParse(&parser, args, len);
  89. hostFileBytes = BeaconDataExtract(&parser, &iBytesLen);
  90. debug = BeaconDataExtract(&parser, NULL);
  91. if(iBytesLen != 0) {
  92. BeaconPrintf(CALLBACK_OUTPUT, "[+] Loaded file in memory with a size of %d bytes\n[*] Start WebClient enumeration..\n", iBytesLen);
  93. BeaconPrintToStreamW(L"\nEnumeration results:\n");
  94. BeaconPrintToStreamW(L"==============================================\n");
  95. hostname = MSVCRT$strtok(hostFileBytes, "\r\n");
  96. while (hostname != NULL) {
  97. nextHostname = MSVCRT$strtok(NULL, "\r\n");
  98. if (nextHostname == NULL) {
  99. break;
  100. }
  101. size_t len = MSVCRT$strlen(hostname);
  102. char* fullPipeName = (char*) MSVCRT$malloc(len + MSVCRT$strlen(pipeNameHead) + MSVCRT$strlen(pipeNameTail) + 1);
  103. MSVCRT$strcpy(fullPipeName, pipeNameHead);
  104. MSVCRT$strcat(fullPipeName, hostname);
  105. MSVCRT$strcat(fullPipeName, pipeNameTail);
  106. pipeStatus = KERNEL32$WaitNamedPipeA(fullPipeName, 3000);
  107. if (pipeStatus == 0 && (MSVCRT$strcmp(debug, "debug") == 0)) {
  108. KERNEL32$MultiByteToWideChar(CP_ACP, 0, hostname, -1, wHostname, 256);
  109. BeaconPrintToStreamW(L"[-] WebClient service not found on %s\n", wHostname);
  110. } else if (pipeStatus == 0) {
  111. } else {
  112. KERNEL32$MultiByteToWideChar(CP_ACP, 0, hostname, -1, wHostname, 256);
  113. BeaconPrintToStreamW(L"[+] WebClient running on %s\n", wHostname);
  114. }
  115. MSVCRT$free(fullPipeName);
  116. hostname = nextHostname;
  117. }
  118. BeaconOutputStreamW();
  119. } else {
  120. BeaconPrintf(CALLBACK_ERROR, "Couldn't load the host file from disk.\n");
  121. }
  122. return 0;
  123. }