|
| 1 | +#include "EnumerateShares.hpp" |
| 2 | +#include "Common.hpp" |
| 3 | + |
| 4 | +#include <cstring> |
| 5 | +#ifdef _WIN32 |
| 6 | +#include <windows.h> |
| 7 | +#include <lm.h> |
| 8 | +#pragma comment(lib, "netapi32.lib") |
| 9 | +#else |
| 10 | +#include <samba-4.0/libsmbclient.h> |
| 11 | +#endif |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +constexpr std::string_view moduleName = "enumerateShares"; |
| 16 | +constexpr unsigned long long moduleHash = djb2(moduleName); |
| 17 | + |
| 18 | +#ifdef _WIN32 |
| 19 | +__declspec(dllexport) EnumerateShares* EnumerateSharesConstructor() |
| 20 | +{ |
| 21 | + return new EnumerateShares(); |
| 22 | +} |
| 23 | +#else |
| 24 | +__attribute__((visibility("default"))) EnumerateShares* EnumerateSharesConstructor() |
| 25 | +{ |
| 26 | + return new EnumerateShares(); |
| 27 | +} |
| 28 | +#endif |
| 29 | + |
| 30 | +EnumerateShares::EnumerateShares() |
| 31 | +#ifdef BUILD_TEAMSERVER |
| 32 | + : ModuleCmd(std::string(moduleName), moduleHash) |
| 33 | +#else |
| 34 | + : ModuleCmd("", moduleHash) |
| 35 | +#endif |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +EnumerateShares::~EnumerateShares() |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +std::string EnumerateShares::getInfo() |
| 44 | +{ |
| 45 | + std::string info; |
| 46 | +#ifdef BUILD_TEAMSERVER |
| 47 | + info += "enumerateShares:\n"; |
| 48 | + info += "List available SMB shares.\n"; |
| 49 | +#endif |
| 50 | + return info; |
| 51 | +} |
| 52 | + |
| 53 | +int EnumerateShares::init(std::vector<std::string>& splitedCmd, C2Message& c2Message) |
| 54 | +{ |
| 55 | + std::string host; |
| 56 | + if(splitedCmd.size() > 1) |
| 57 | + host = splitedCmd[1]; |
| 58 | + c2Message.set_instruction(splitedCmd[0]); |
| 59 | + c2Message.set_cmd(host); |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +int EnumerateShares::process(C2Message& c2Message, C2Message& c2RetMessage) |
| 64 | +{ |
| 65 | + std::string host = c2Message.cmd(); |
| 66 | + std::string out = runEnum(host); |
| 67 | + c2RetMessage.set_instruction(c2Message.instruction()); |
| 68 | + c2RetMessage.set_cmd(host); |
| 69 | + c2RetMessage.set_returnvalue(out); |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +std::string EnumerateShares::runEnum(const std::string& host) |
| 74 | +{ |
| 75 | +#ifdef _WIN32 |
| 76 | + std::string result; |
| 77 | + std::wstring wserver; |
| 78 | + if(!host.empty()) |
| 79 | + wserver = L"\\\\" + std::wstring(host.begin(), host.end()); |
| 80 | + LPBYTE buf = nullptr; |
| 81 | + DWORD read = 0, total = 0, resume = 0; |
| 82 | + NET_API_STATUS status = NetShareEnum(host.empty()? NULL : (LPWSTR)wserver.c_str(), 1, &buf, MAX_PREFERRED_LENGTH, &read, &total, &resume); |
| 83 | + if(status == NERR_Success || status == ERROR_MORE_DATA) |
| 84 | + { |
| 85 | + PSHARE_INFO_1 info = (PSHARE_INFO_1)buf; |
| 86 | + for(DWORD i=0; i<read; ++i) |
| 87 | + { |
| 88 | + char name[256] = {0}; |
| 89 | + WideCharToMultiByte(CP_UTF8, 0, info[i].shi1_netname, -1, name, sizeof(name), NULL, NULL); |
| 90 | + result += name; |
| 91 | + if(info[i].shi1_remark) |
| 92 | + { |
| 93 | + char rem[256] = {0}; |
| 94 | + WideCharToMultiByte(CP_UTF8, 0, info[i].shi1_remark, -1, rem, sizeof(rem), NULL, NULL); |
| 95 | + result += " - "; |
| 96 | + result += rem; |
| 97 | + } |
| 98 | + result += "\n"; |
| 99 | + } |
| 100 | + NetApiBufferFree(buf); |
| 101 | + } |
| 102 | + if(result.empty()) |
| 103 | + result = "Enumeration failed or no shares"; |
| 104 | + return result; |
| 105 | +#else |
| 106 | + std::string result; |
| 107 | + auto auth_fn = [](SMBCCTX*, const char*, const char*, char*, int, char* u, int ulen, char* p, int plen){ if(ulen>0) u[0]='\0'; if(plen>0) p[0]='\0'; }; |
| 108 | + SMBCCTX* ctx = smbc_new_context(); |
| 109 | + if(!ctx) return result; |
| 110 | + smbc_setOptionUseKerberos(ctx, 0); |
| 111 | + smbc_setOptionFallbackAfterKerberos(ctx, 1); |
| 112 | + smbc_setFunctionAuthDataWithContext(ctx, auth_fn); |
| 113 | + if(!smbc_init_context(ctx)) |
| 114 | + { |
| 115 | + smbc_free_context(ctx, 1); |
| 116 | + return result; |
| 117 | + } |
| 118 | + smbc_set_context(ctx); |
| 119 | + std::string url = "smb://" + (host.empty()? std::string("") : host); |
| 120 | + int dir = smbc_opendir(url.c_str()); |
| 121 | + if(dir >= 0) |
| 122 | + { |
| 123 | + struct smbc_dirent* ent; |
| 124 | + while((ent = smbc_readdir(dir)) != nullptr) |
| 125 | + { |
| 126 | + if(ent->smbc_type == SMBC_FILE_SHARE) |
| 127 | + { |
| 128 | + result += ent->name; |
| 129 | + result += '\n'; |
| 130 | + } |
| 131 | + } |
| 132 | + smbc_closedir(dir); |
| 133 | + } |
| 134 | + smbc_free_context(ctx, 1); |
| 135 | + if(result.empty()) |
| 136 | + result = "Enumeration failed or no shares"; |
| 137 | + return result; |
| 138 | +#endif |
| 139 | +} |
| 140 | + |
0 commit comments