|
| 1 | +#include "MkDir.hpp" |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +#include "Common.hpp" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | +namespace fs = std::filesystem; |
| 10 | + |
| 11 | +constexpr std::string_view moduleName = "mkDir"; |
| 12 | +constexpr unsigned long long moduleHash = djb2(moduleName); |
| 13 | + |
| 14 | +#ifdef _WIN32 |
| 15 | +__declspec(dllexport) MkDir* MkDirConstructor() |
| 16 | +{ |
| 17 | + return new MkDir(); |
| 18 | +} |
| 19 | +#else |
| 20 | +__attribute__((visibility("default"))) MkDir* MkDirConstructor() |
| 21 | +{ |
| 22 | + return new MkDir(); |
| 23 | +} |
| 24 | +#endif |
| 25 | + |
| 26 | +MkDir::MkDir() |
| 27 | +#ifdef BUILD_TEAMSERVER |
| 28 | + : ModuleCmd(std::string(moduleName), moduleHash) |
| 29 | +#else |
| 30 | + : ModuleCmd("", moduleHash) |
| 31 | +#endif |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +MkDir::~MkDir() |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +std::string MkDir::getInfo() |
| 40 | +{ |
| 41 | + std::string info; |
| 42 | +#ifdef BUILD_TEAMSERVER |
| 43 | + info += "mkDir Module:\n"; |
| 44 | + info += "Create a new directory on the target system.\n"; |
| 45 | + info += "\nUsage:\n"; |
| 46 | + info += " - mkDir <path>\n"; |
| 47 | +#endif |
| 48 | + return info; |
| 49 | +} |
| 50 | + |
| 51 | +int MkDir::init(std::vector<std::string> &splitedCmd, C2Message &c2Message) |
| 52 | +{ |
| 53 | +#if defined(BUILD_TEAMSERVER) || defined(BUILD_TESTS) |
| 54 | + if (splitedCmd.size() >= 2) |
| 55 | + { |
| 56 | + std::string path; |
| 57 | + for (size_t i = 1; i < splitedCmd.size(); ++i) |
| 58 | + { |
| 59 | + if (!path.empty()) |
| 60 | + path += " "; |
| 61 | + path += splitedCmd[i]; |
| 62 | + } |
| 63 | + c2Message.set_instruction(splitedCmd[0]); |
| 64 | + c2Message.set_cmd(path); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + c2Message.set_returnvalue(getInfo()); |
| 69 | + return -1; |
| 70 | + } |
| 71 | +#endif |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +#define ERROR_CREATE_DIRECTORY 1 |
| 76 | + |
| 77 | +int MkDir::process(C2Message &c2Message, C2Message &c2RetMessage) |
| 78 | +{ |
| 79 | + std::string path = c2Message.cmd(); |
| 80 | + c2RetMessage.set_instruction(c2RetMessage.instruction()); |
| 81 | + c2RetMessage.set_cmd(path); |
| 82 | + std::error_code ec; |
| 83 | + bool created = fs::create_directories(path, ec); |
| 84 | + if (!ec) |
| 85 | + { |
| 86 | + c2RetMessage.set_returnvalue(created ? "Directory created." : "Already exists."); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + c2RetMessage.set_errorCode(ERROR_CREATE_DIRECTORY); |
| 91 | + } |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +int MkDir::errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg) |
| 96 | +{ |
| 97 | +#ifdef BUILD_TEAMSERVER |
| 98 | + int errorCode = c2RetMessage.errorCode(); |
| 99 | + if (errorCode > 0) |
| 100 | + { |
| 101 | + if (errorCode == ERROR_CREATE_DIRECTORY) |
| 102 | + errorMsg = "Failed: Could not create directory"; |
| 103 | + } |
| 104 | +#endif |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
0 commit comments