enumshares.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #include <Lm.h>
  4. #include "enumshares.h"
  5. #include "beacon.h"
  6. #pragma comment(lib, "Netapi32.lib")
  7. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  8. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  9. HRESULT hr = S_FALSE;
  10. va_list argList;
  11. DWORD dwWritten = 0;
  12. if (g_lpStream <= (LPSTREAM)1) {
  13. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  14. if (FAILED(hr)) {
  15. return hr;
  16. }
  17. }
  18. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  19. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  20. if (g_lpwPrintBuffer == NULL) {
  21. hr = E_FAIL;
  22. goto CleanUp;
  23. }
  24. }
  25. va_start(argList, lpwFormat);
  26. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  27. hr = E_FAIL;
  28. goto CleanUp;
  29. }
  30. if (g_lpStream != NULL) {
  31. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  32. goto CleanUp;
  33. }
  34. }
  35. hr = S_OK;
  36. CleanUp:
  37. if (g_lpwPrintBuffer != NULL) {
  38. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  39. }
  40. va_end(argList);
  41. return hr;
  42. }
  43. //https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  44. VOID BeaconOutputStreamW() {
  45. STATSTG ssStreamData = { 0 };
  46. SIZE_T cbSize = 0;
  47. ULONG cbRead = 0;
  48. LARGE_INTEGER pos;
  49. LPWSTR lpwOutput = NULL;
  50. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  51. return;
  52. }
  53. cbSize = ssStreamData.cbSize.LowPart;
  54. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  55. if (lpwOutput != NULL) {
  56. pos.QuadPart = 0;
  57. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  58. goto CleanUp;
  59. }
  60. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  61. goto CleanUp;
  62. }
  63. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  64. }
  65. CleanUp:
  66. if (g_lpStream != NULL) {
  67. g_lpStream->lpVtbl->Release(g_lpStream);
  68. g_lpStream = NULL;
  69. }
  70. if (g_lpwPrintBuffer != NULL) {
  71. MSVCRT$free(g_lpwPrintBuffer);
  72. g_lpwPrintBuffer = NULL;
  73. }
  74. if (lpwOutput != NULL) {
  75. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  76. }
  77. return;
  78. }
  79. PSHARE_INFO_1 listShares(wchar_t *servername) {
  80. PSHARE_INFO_1 pShareInfo = NULL;
  81. DWORD dwEntriesRead = 0, dwTotalEntries = 0, dwResumeHandle = 0;
  82. NET_API_STATUS nStatus;
  83. BeaconPrintToStreamW(L"\nListing shares for: %ls\n", servername);
  84. BeaconPrintToStreamW(L"=====================================================\n");
  85. do {
  86. nStatus = NETAPI32$NetShareEnum(servername, 1, (LPBYTE*)&pShareInfo, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
  87. if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) {
  88. for (DWORD i = 0; i < dwEntriesRead; i++) {
  89. BeaconPrintToStreamW(L"Share Name: %-10ls <- ", pShareInfo[i].shi1_netname);
  90. if (KERNEL32$lstrcmpW(pShareInfo[i].shi1_netname, L"IPC$") == 0) {
  91. BeaconPrintToStreamW(L"[!] No file system access\n");
  92. continue;
  93. }
  94. USE_INFO_2 useInfo = { 0 };
  95. wchar_t fullPath[260];
  96. MSVCRT$_snwprintf(fullPath, sizeof(fullPath) / sizeof(wchar_t) - 1, L"\\\\%s\\%s", servername ? servername : L"localhost", pShareInfo[i].shi1_netname);
  97. useInfo.ui2_remote = fullPath;
  98. useInfo.ui2_asg_type = USE_DISKDEV;
  99. useInfo.ui2_username = NULL; // Use current user's credentials
  100. useInfo.ui2_password = L"";
  101. nStatus = NETAPI32$NetUseAdd(NULL, 2, (LPBYTE)&useInfo, NULL);
  102. if (nStatus == NERR_Success) {
  103. BeaconPrintToStreamW(L"[+] Accessible\n");
  104. NETAPI32$NetUseDel(NULL, fullPath, USE_LOTS_OF_FORCE);
  105. } else {
  106. BeaconPrintToStreamW(L"[-] Error access denied\n");
  107. }
  108. }
  109. NETAPI32$NetApiBufferFree(pShareInfo);
  110. pShareInfo = NULL;
  111. } else {
  112. if (nStatus == ERROR_BAD_NETPATH) {
  113. BeaconPrintToStreamW(L"Connection error: ERROR_BAD_NETPATH\n");
  114. } else if (nStatus == ERROR_ACCESS_DENIED) {
  115. BeaconPrintToStreamW(L"Connection error: ERROR_ACCESS_DENIED\n");
  116. } else {
  117. BeaconPrintToStreamW(L"Connection error code: %d\n", nStatus);
  118. }
  119. break;
  120. }
  121. } while (nStatus == ERROR_MORE_DATA);
  122. return pShareInfo;
  123. }
  124. int go(char *args, int len) {
  125. char* hostname;
  126. char* nextHostname;
  127. int iBytesLen = 0;
  128. CHAR *hostFileBytes;
  129. WCHAR wHostname[MAX_PATH];
  130. datap parser;
  131. BeaconDataParse(&parser, args, len);
  132. hostFileBytes = BeaconDataExtract(&parser, &iBytesLen);
  133. if(iBytesLen != 0) {
  134. BeaconPrintf(CALLBACK_OUTPUT, "[+] Loaded hostname file in memory with a size of %d bytes\n[*] Start share enumeration..\n", iBytesLen);
  135. hostname = MSVCRT$strtok(hostFileBytes, "\r\n");
  136. while (hostname != NULL) {
  137. nextHostname = MSVCRT$strtok(NULL, "\r\n");
  138. if (nextHostname == NULL) {
  139. break;
  140. }
  141. KERNEL32$MultiByteToWideChar(CP_ACP, 0, hostname, -1, wHostname, MAX_PATH);
  142. PSHARE_INFO_1 pShareInfo = listShares(wHostname);
  143. hostname = nextHostname;
  144. BeaconOutputStreamW();
  145. NETAPI32$NetApiBufferFree(pShareInfo);
  146. }
  147. BeaconPrintf(CALLBACK_OUTPUT, "[+] Done!\n");
  148. } else {
  149. BeaconPrintf(CALLBACK_ERROR, "Couldn't load the host file from disk.\n");
  150. }
  151. return 0;
  152. }