enumshares.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //START TrustedSec BOF print code: https://github.com/trustedsec/CS-Situational-Awareness-BOF/blob/master/src/common/base.c
  8. #ifndef bufsize
  9. #define bufsize 8192
  10. #endif
  11. char *output = 0;
  12. WORD currentoutsize = 0;
  13. HANDLE trash = NULL;
  14. int bofstart();
  15. void internal_printf(const char* format, ...);
  16. void printoutput(BOOL done);
  17. int bofstart() {
  18. output = (char*)MSVCRT$calloc(bufsize, 1);
  19. currentoutsize = 0;
  20. return 1;
  21. }
  22. void internal_printf(const char* format, ...){
  23. int buffersize = 0;
  24. int transfersize = 0;
  25. char * curloc = NULL;
  26. char* intBuffer = NULL;
  27. va_list args;
  28. va_start(args, format);
  29. buffersize = MSVCRT$vsnprintf(NULL, 0, format, args);
  30. va_end(args);
  31. if (buffersize == -1) return;
  32. char* transferBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);
  33. intBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, buffersize);
  34. va_start(args, format);
  35. MSVCRT$vsnprintf(intBuffer, buffersize, format, args);
  36. va_end(args);
  37. if(buffersize + currentoutsize < bufsize)
  38. {
  39. MSVCRT$memcpy(output+currentoutsize, intBuffer, buffersize);
  40. currentoutsize += buffersize;
  41. } else {
  42. curloc = intBuffer;
  43. while(buffersize > 0)
  44. {
  45. transfersize = bufsize - currentoutsize;
  46. if(buffersize < transfersize)
  47. {
  48. transfersize = buffersize;
  49. }
  50. MSVCRT$memcpy(output+currentoutsize, curloc, transfersize);
  51. currentoutsize += transfersize;
  52. if(currentoutsize == bufsize)
  53. {
  54. printoutput(FALSE);
  55. }
  56. MSVCRT$memset(transferBuffer, 0, transfersize);
  57. curloc += transfersize;
  58. buffersize -= transfersize;
  59. }
  60. }
  61. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, intBuffer);
  62. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, transferBuffer);
  63. }
  64. void printoutput(BOOL done) {
  65. char * msg = NULL;
  66. BeaconOutput(CALLBACK_OUTPUT, output, currentoutsize);
  67. currentoutsize = 0;
  68. MSVCRT$memset(output, 0, bufsize);
  69. if(done) {MSVCRT$free(output); output=NULL;}
  70. }
  71. //END TrustedSec BOF print code.
  72. PSHARE_INFO_1 listShares(wchar_t *servername) {
  73. PSHARE_INFO_1 pShareInfo = NULL;
  74. DWORD dwEntriesRead = 0, dwTotalEntries = 0, dwResumeHandle = 0;
  75. NET_API_STATUS nStatus;
  76. internal_printf("\n\nListing shares for: %ls\n", servername);
  77. internal_printf("=====================================================\n");
  78. do {
  79. nStatus = NETAPI32$NetShareEnum(servername, 1, (LPBYTE*)&pShareInfo, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
  80. if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) {
  81. for (DWORD i = 0; i < dwEntriesRead; i++) {
  82. internal_printf("Share Name: %-10ls <- ", pShareInfo[i].shi1_netname);
  83. if (KERNEL32$lstrcmpW(pShareInfo[i].shi1_netname, L"IPC$") == 0) {
  84. internal_printf("[!] No file system access\n");
  85. continue;
  86. }
  87. USE_INFO_2 useInfo = { 0 };
  88. wchar_t fullPath[260];
  89. MSVCRT$_snwprintf(fullPath, sizeof(fullPath) / sizeof(wchar_t) - 1, L"\\\\%s\\%s", servername ? servername : L"localhost", pShareInfo[i].shi1_netname);
  90. useInfo.ui2_remote = fullPath;
  91. useInfo.ui2_asg_type = USE_DISKDEV;
  92. useInfo.ui2_username = NULL; // Use current user's credentials
  93. useInfo.ui2_password = L"";
  94. nStatus = NETAPI32$NetUseAdd(NULL, 2, (LPBYTE)&useInfo, NULL);
  95. if (nStatus == NERR_Success) {
  96. internal_printf("[+] Accessible\n");
  97. NETAPI32$NetUseDel(NULL, fullPath, USE_LOTS_OF_FORCE);
  98. } else {
  99. internal_printf("[-] Error access denied\n");
  100. }
  101. }
  102. NETAPI32$NetApiBufferFree(pShareInfo);
  103. pShareInfo = NULL;
  104. } else {
  105. if (nStatus == ERROR_BAD_NETPATH) {
  106. internal_printf("Connection error: ERROR_BAD_NETPATH\n");
  107. } else if (nStatus == ERROR_ACCESS_DENIED) {
  108. internal_printf("Connection error: ERROR_ACCESS_DENIED\n");
  109. } else {
  110. internal_printf("Connection error code: %d\n", nStatus);
  111. }
  112. break;
  113. }
  114. } while (nStatus == ERROR_MORE_DATA);
  115. return pShareInfo;
  116. }
  117. int go(char *args, int len) {
  118. char* hostname;
  119. char* nextHostname;
  120. int iBytesLen = 0;
  121. CHAR *hostFileBytes;
  122. WCHAR wHostname[MAX_PATH];
  123. datap parser;
  124. BeaconDataParse(&parser, args, len);
  125. hostFileBytes = BeaconDataExtract(&parser, &iBytesLen);
  126. if(!bofstart()) return;
  127. if(iBytesLen != 0) {
  128. BeaconPrintf(CALLBACK_OUTPUT, "[+] Loaded hostname file in memory with a size of %d bytes\n", iBytesLen);
  129. hostname = MSVCRT$strtok(hostFileBytes, "\r\n");
  130. while (hostname != NULL) {
  131. nextHostname = MSVCRT$strtok(NULL, "\r\n");
  132. if (nextHostname == NULL) {
  133. break;
  134. }
  135. KERNEL32$MultiByteToWideChar(CP_ACP, 0, hostname, -1, wHostname, MAX_PATH);
  136. PSHARE_INFO_1 pShareInfo = listShares(wHostname);
  137. hostname = nextHostname;
  138. NETAPI32$NetApiBufferFree(pShareInfo);
  139. }
  140. printoutput(TRUE);
  141. BeaconPrintf(CALLBACK_OUTPUT, "[+] Finished enumerating!\n");
  142. } else {
  143. BeaconPrintf(CALLBACK_ERROR, "Couldn't load the host file from disk.\n");
  144. }
  145. return 0;
  146. }