Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
containers.cpp
Go to the documentation of this file.
1#include "containers.h"
2#include "error_message.h"
3#include "utils.h"
4#include "Configuration.hpp"
5#include <iostream>
6
8{
9 std::string localPath = GetExecutableDirectory();
10 std::string extPath = localPath + "\\ext\\";
11 std::string result = "\"" + localPath + "\\PanoptesContainer.exe" + "\" \"";
12
13 switch (ext) {
14 case CONTAINER_TYPE_YARA:
15 result.append(extPath + EXT_YARA);
16 break;
17 case CONTAINER_TYPE_ML:
18 result.append(extPath + EXT_ML);
19 break;
20 default:
21 break;
22 }
23 result.append("\"");
24
25 return result;
26}
27
28// Every container runs in this job. The job kills its processes when its last handle closes,
29// so containers end with the service even if the service is killed rather than shut down
30static HANDLE g_ContainerJob = NULL;
31
32static bool CreateContainerJob() {
33 if (g_ContainerJob != NULL) {
34 return true; // StartContainers and StartDetector share it
35 }
36 g_ContainerJob = CreateJobObjectW(NULL, NULL);
37 if (g_ContainerJob == NULL) {
38 return false;
39 }
40
41 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits{};
42 limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
43 if (!SetInformationJobObject(g_ContainerJob, JobObjectExtendedLimitInformation, &limits, sizeof(limits))) {
44 CloseHandle(g_ContainerJob);
45 g_ContainerJob = NULL;
46 return false;
47 }
48 return true;
49}
50
52 // YARA is always loaded. PanoptesML is optional (built with BUILD_ML), so its container only
53 // starts when its DLL is in the ext folder
54 std::vector<Configuration::ContainerType> extensibility = { Configuration::CONTAINER_TYPE_YARA };
55 std::string mlPath = GetExecutableDirectory() + "\\ext\\" + EXT_ML;
56 if (GetFileAttributesA(mlPath.c_str()) != INVALID_FILE_ATTRIBUTES) {
57 extensibility.push_back(Configuration::CONTAINER_TYPE_ML);
58 }
59
60 if (!CreateContainerJob()) {
61 return CONTAINER_START;
62 }
63
64 STARTUPINFOA startupInfo;
65 PROCESS_INFORMATION processInformation;
66
67 ZeroMemory(&startupInfo, sizeof(startupInfo));
68 startupInfo.cb = sizeof(startupInfo);
69 ZeroMemory(&processInformation, sizeof(processInformation));
70
71 for (auto containerType : extensibility) {
72 std::string cmdStr = BuildContainerCommandLine(containerType);
73 // Started suspended so the container is in the job before it runs any code
74#ifdef _DEBUG
75 DWORD creationFlags = CREATE_SUSPENDED;
76#else
77 DWORD creationFlags = CREATE_SUSPENDED | CREATE_NO_WINDOW;
78#endif
79
80 if (!CreateProcessA(
81 NULL, // No module name (use command line)
82 (LPSTR)cmdStr.c_str(), // Command line
83 NULL, // Process handle not inheritable
84 NULL, // Thread handle not inheritable
85 FALSE, // Set handle inheritance to FALSE
86 creationFlags, // No creation flags
87 NULL, // Use parent's environment block
88 NULL, // Use parent's starting directory
89 &startupInfo, // Pointer to STARTUPINFO structure
90 &processInformation) // Pointer to PROCESS_INFORMATION structure
91 )
92 {
93 //string message = format("Failed to create process for {} extensibility: {}", m_extensibilityListName[i], GetLastError());
94 //LogErrorMessage(message);
95 //DWORD errMsg = GetLastError();
96 return CONTAINER_START;
97 }
98
99 BOOL assigned = AssignProcessToJobObject(g_ContainerJob, processInformation.hProcess);
100 if (assigned) {
101 ResumeThread(processInformation.hThread);
102 }
103 else {
104 TerminateProcess(processInformation.hProcess, 1);
105 }
106 CloseHandle(processInformation.hThread);
107 CloseHandle(processInformation.hProcess);
108 if (!assigned) {
109 return CONTAINER_START;
110 }
111 }
112
113 return PANO_SUCCESS;
114}
115
117 WCHAR path[MAX_PATH];
118 DWORD length = GetModuleFileNameW(NULL, path, MAX_PATH);
119 if (length == 0 || length == MAX_PATH) {
120 return L"";
121 }
122 std::wstring directory(path, length);
123 directory.resize(directory.find_last_of(L'\\') + 1);
124 return directory + L"PanoptesDetector.exe";
125}
126
128 std::wstring exe = DetectorExecutablePath();
129 if (exe.empty() || GetFileAttributesW(exe.c_str()) == INVALID_FILE_ATTRIBUTES) {
130 // The detector is optional: without it, the service still logs and scans
131 std::cerr << "PanoptesDetector.exe is not next to the service; behavioral rules are off" << std::endl;
132 return PANO_SUCCESS;
133 }
134 if (!CreateContainerJob()) {
135 return CONTAINER_START;
136 }
137
138 std::wstring folder = exe.substr(0, exe.find_last_of(L'\\'));
139 std::wstring commandLine = L"\"" + exe + L"\" -rules \"" + folder + L"\\rules\"";
140#ifdef _DEBUG
141 // Debug builds of the service publish their port under HKCU, and the detector's output
142 // shares the service's console
143 commandLine += L" -debug-service";
144 DWORD creationFlags = CREATE_SUSPENDED;
145#else
146 DWORD creationFlags = CREATE_SUSPENDED | CREATE_NO_WINDOW;
147#endif
148
149 STARTUPINFOW startupInfo{};
150 startupInfo.cb = sizeof(startupInfo);
151 PROCESS_INFORMATION processInformation{};
152 if (!CreateProcessW(exe.c_str(), commandLine.data(), NULL, NULL, FALSE, creationFlags, NULL,
153 folder.c_str(), &startupInfo, &processInformation)) {
154 std::cerr << "Failed to start PanoptesDetector.exe (error " << GetLastError() << ")" << std::endl;
155 return CONTAINER_START;
156 }
157
158 // In the job before it runs, so it ends with the service like the containers do
159 BOOL assigned = AssignProcessToJobObject(g_ContainerJob, processInformation.hProcess);
160 if (assigned) {
161 ResumeThread(processInformation.hThread);
162 }
163 else {
164 TerminateProcess(processInformation.hProcess, 1);
165 }
166 CloseHandle(processInformation.hThread);
167 CloseHandle(processInformation.hProcess);
168 return assigned ? PANO_SUCCESS : CONTAINER_START;
169}
170
171bool IsServiceChildProcess(HANDLE process) {
172 BOOL inJob = FALSE;
173 return g_ContainerJob != NULL && IsProcessInJob(process, g_ContainerJob, &inJob) && inJob;
174}
175
177 if (g_ContainerJob == NULL) {
178 return;
179 }
180
181 // Containers hold no state that needs saving, so they are ended rather than asked to exit
182 TerminateJobObject(g_ContainerJob, 0);
183 CloseHandle(g_ContainerJob);
184 g_ContainerJob = NULL;
185}
#define PANO_SUCCESS
#define CONTAINER_START
#define MAX_PATH
Definition callbacks.h:7
ContainerType
The type of container that the extensibility is running in.
ERRORCODE StartContainers()
std::string BuildContainerCommandLine(Configuration::ContainerType ext)
Definition containers.cpp:7
bool IsServiceChildProcess(HANDLE process)
Whether a process is one the service started (a container or the detector).
void StopContainers()
Ends every container started by StartContainers. Safe to call if none were started.
std::wstring DetectorExecutablePath()
The path the service starts the detector from: PanoptesDetector.exe in the service's folder....
ERRORCODE StartDetector()
Starts PanoptesDetector.exe from the service's folder with the rules in its rules subfolder,...
#define ERRORCODE
#define EXT_ML
#define EXT_YARA
std::string GetExecutableDirectory()
Definition utils.cpp:71