Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
containers.cpp File Reference
#include "containers.h"
#include "error_message.h"
#include "utils.h"
#include "Configuration.hpp"
#include <iostream>

Go to the source code of this file.

Functions

std::string BuildContainerCommandLine (Configuration::ContainerType ext)
ERRORCODE StartContainers ()
std::wstring DetectorExecutablePath ()
 The path the service starts the detector from: PanoptesDetector.exe in the service's folder. Only a process running this file may use the detector RPCs.
ERRORCODE StartDetector ()
 Starts PanoptesDetector.exe from the service's folder with the rules in its rules subfolder, in the same job as the containers so it ends with the service. Does nothing (and succeeds) when the detector is not installed.
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.

Function Documentation

◆ BuildContainerCommandLine()

std::string BuildContainerCommandLine ( Configuration::ContainerType ext)

Definition at line 7 of file containers.cpp.

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}
#define EXT_ML
#define EXT_YARA
std::string GetExecutableDirectory()
Definition utils.cpp:71

References EXT_ML, EXT_YARA, and GetExecutableDirectory().

Referenced by StartContainers().

◆ DetectorExecutablePath()

std::wstring DetectorExecutablePath ( )

The path the service starts the detector from: PanoptesDetector.exe in the service's folder. Only a process running this file may use the detector RPCs.

Definition at line 116 of file containers.cpp.

116 {
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}
#define MAX_PATH
Definition callbacks.h:7

References MAX_PATH.

Referenced by IsDetectorPeer(), and StartDetector().

◆ IsServiceChildProcess()

bool IsServiceChildProcess ( HANDLE process)

Whether a process is one the service started (a container or the detector).

Parameters
processA handle with PROCESS_QUERY_LIMITED_INFORMATION access

Definition at line 171 of file containers.cpp.

171 {
172 BOOL inJob = FALSE;
173 return g_ContainerJob != NULL && IsProcessInJob(process, g_ContainerJob, &inJob) && inJob;
174}

◆ StartContainers()

ERRORCODE StartContainers ( )

Definition at line 51 of file containers.cpp.

51 {
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}
#define PANO_SUCCESS
#define CONTAINER_START
std::string BuildContainerCommandLine(Configuration::ContainerType ext)
Definition containers.cpp:7

References BuildContainerCommandLine(), CONTAINER_START, Configuration::CONTAINER_TYPE_ML, Configuration::CONTAINER_TYPE_YARA, ERRORCODE, EXT_ML, GetExecutableDirectory(), and PANO_SUCCESS.

Referenced by WinMain().

◆ StartDetector()

ERRORCODE StartDetector ( )

Starts PanoptesDetector.exe from the service's folder with the rules in its rules subfolder, in the same job as the containers so it ends with the service. Does nothing (and succeeds) when the detector is not installed.

Definition at line 127 of file containers.cpp.

127 {
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}
std::wstring DetectorExecutablePath()
The path the service starts the detector from: PanoptesDetector.exe in the service's folder....

References CONTAINER_START, DetectorExecutablePath(), ERRORCODE, and PANO_SUCCESS.

Referenced by WinMain().

◆ StopContainers()

void StopContainers ( )

Ends every container started by StartContainers. Safe to call if none were started.

Definition at line 176 of file containers.cpp.

176 {
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}