|
| 1 | +#include "KillProcess.hpp" |
| 2 | + |
| 3 | +#include "Common.hpp" |
| 4 | + |
| 5 | +#include <cstring> |
| 6 | +#ifdef __linux__ |
| 7 | +#include <signal.h> |
| 8 | +#include <unistd.h> |
| 9 | +#elif _WIN32 |
| 10 | +#include <windows.h> |
| 11 | +#endif |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +constexpr std::string_view moduleName = "killProcess"; |
| 16 | +constexpr unsigned long long moduleHash = djb2(moduleName); |
| 17 | + |
| 18 | +#ifdef _WIN32 |
| 19 | +__declspec(dllexport) KillProcess* KillProcessConstructor() |
| 20 | +{ |
| 21 | + return new KillProcess(); |
| 22 | +} |
| 23 | +#else |
| 24 | +__attribute__((visibility("default"))) KillProcess* KillProcessConstructor() |
| 25 | +{ |
| 26 | + return new KillProcess(); |
| 27 | +} |
| 28 | +#endif |
| 29 | + |
| 30 | +KillProcess::KillProcess() |
| 31 | +#ifdef BUILD_TEAMSERVER |
| 32 | + : ModuleCmd(std::string(moduleName), moduleHash) |
| 33 | +#else |
| 34 | + : ModuleCmd("", moduleHash) |
| 35 | +#endif |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +KillProcess::~KillProcess() |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +std::string KillProcess::getInfo() |
| 44 | +{ |
| 45 | + std::string info; |
| 46 | +#ifdef BUILD_TEAMSERVER |
| 47 | + info += "killProcess Module:\n"; |
| 48 | + info += "Terminate a process by PID.\n"; |
| 49 | + info += "\nUsage:\n"; |
| 50 | + info += " - killProcess <pid>\n"; |
| 51 | +#endif |
| 52 | + return info; |
| 53 | +} |
| 54 | + |
| 55 | +int KillProcess::init(std::vector<std::string> &splitedCmd, C2Message &c2Message) |
| 56 | +{ |
| 57 | +#if defined(BUILD_TEAMSERVER) || defined(BUILD_TESTS) |
| 58 | + if (splitedCmd.size() >= 2) |
| 59 | + { |
| 60 | + c2Message.set_instruction(splitedCmd[0]); |
| 61 | + c2Message.set_pid(std::stoi(splitedCmd[1])); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + c2Message.set_returnvalue(getInfo()); |
| 66 | + return -1; |
| 67 | + } |
| 68 | +#endif |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +#define ERROR_KILL_PROCESS 1 |
| 73 | +int KillProcess::process(C2Message &c2Message, C2Message &c2RetMessage) |
| 74 | +{ |
| 75 | + int pid = c2Message.pid(); |
| 76 | + c2RetMessage.set_instruction(c2RetMessage.instruction()); |
| 77 | + c2RetMessage.set_pid(pid); |
| 78 | +#ifdef __linux__ |
| 79 | + if (kill(pid, SIGKILL) == 0) |
| 80 | + { |
| 81 | + c2RetMessage.set_returnvalue("Killed."); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + c2RetMessage.set_errorCode(ERROR_KILL_PROCESS); |
| 86 | + } |
| 87 | +#elif _WIN32 |
| 88 | + HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); |
| 89 | + if (hProcess) |
| 90 | + { |
| 91 | + BOOL ok = TerminateProcess(hProcess, 1); |
| 92 | + CloseHandle(hProcess); |
| 93 | + if (ok) |
| 94 | + c2RetMessage.set_returnvalue("Killed."); |
| 95 | + else |
| 96 | + c2RetMessage.set_errorCode(ERROR_KILL_PROCESS); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + c2RetMessage.set_errorCode(ERROR_KILL_PROCESS); |
| 101 | + } |
| 102 | +#endif |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +int KillProcess::errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg) |
| 107 | +{ |
| 108 | +#ifdef BUILD_TEAMSERVER |
| 109 | + if (c2RetMessage.errorCode() == ERROR_KILL_PROCESS) |
| 110 | + errorMsg = "Failed: Could not kill process"; |
| 111 | +#endif |
| 112 | + return 0; |
| 113 | +} |
0 commit comments