Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
container.cpp
Go to the documentation of this file.
1#include <grpcpp/grpcpp.h>
2#include "panoptes.grpc.pb.h"
3#include "panoptes.pb.h"
4#include "container_ipc.hpp"
5#include <ExtensibilityCore.h>
6#include <iostream>
7#include <atomic>
8
9namespace fs = std::filesystem;
10
14
16std::atomic_bool isConnected = true;
17std::unique_ptr<PanoptesService::Stub> stub;
18int BindPort = 0;
19
20// Debug builds of the service run unelevated and publish the port under HKCU
21#ifdef _DEBUG
22#define PANOPTES_REG_ROOT HKEY_CURRENT_USER
23#else
24#define PANOPTES_REG_ROOT HKEY_LOCAL_MACHINE
25#endif // _DEBUG
26
30bool GetRegistryPortValue(DWORD& portValue)
31{
32 HKEY hKey;
33 DWORD dwType = REG_DWORD;
34 DWORD dwSize = sizeof(DWORD);
35
36 // Open the key
37 LONG lResult = RegOpenKeyExA(
39 "SOFTWARE\\Panoptes",
40 0,
41 KEY_READ,
42 &hKey
43 );
44
45 if (lResult != ERROR_SUCCESS) {
46 std::cerr << "Error opening registry key. Error code: " << lResult << std::endl;
47 return false;
48 }
49
50 // Read the SRV_PORT value
51 lResult = RegQueryValueExA(
52 hKey,
53 "SRV_PORT",
54 NULL,
55 &dwType,
56 reinterpret_cast<LPBYTE>(&portValue),
57 &dwSize
58 );
59
60 RegCloseKey(hKey);
61
62 if (lResult != ERROR_SUCCESS) {
63 std::cerr << "Error reading registry value. Error code: " << lResult << std::endl;
64 return false;
65 }
66
67 if (dwType != REG_DWORD) {
68 std::cerr << "Unexpected value type in registry." << std::endl;
69 return false;
70 }
71
72 return true;
73}
74
79 while (isConnected) {
80 PeScan pe_data = message_queue_pe_->dequeue();
81 if (pe_data.PePath != "")
82 {
83 PanoEntry(&pe_data);
84 }
85 }
86}
87
90 DWORD portValue = 0;
91 if (!GetRegistryPortValue(portValue)) {
92 std::cerr << "Failed to get registry port value." << std::endl;
93 return;
94 }
95 std::string server_url = "localhost:" + std::to_string(portValue);
96
97 while (true)
98 {
99 std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(server_url,
100 grpc::InsecureChannelCredentials());
101
102 stub = PanoptesService::NewStub(channel);
103
104 grpc::ClientContext context;
105 HealthCheckRequest request;
106 HealthCheckResponse response;
107 request.set_ping(true);
108
109
110 grpc::Status status = stub->HealthCheck(&context, request, &response);
111
112 if (!status.ok()) {
113 isConnected.store(false);
114 return;
115 }
116
117 Sleep(2500);
118 }
119}
120
124bool LoadExtensibility(std::string dllFile) {
125 HMODULE extensibility = LoadLibraryA(dllFile.c_str());
126 if (extensibility == NULL) {
127 DWORD error = GetLastError();
128 return false;
129 }
130 else {
131 PanoBind = (ExtensibilityCore::PanoBindPtr)GetProcAddress(extensibility, "PanoBind");
132 if (PanoBind == NULL) {
133 return false;
134 }
135 PanoEntry = (ExtensibilityCore::PanoEntryPtr)GetProcAddress(extensibility, "PanoEntry");
136 if (PanoEntry == NULL) {
137 return false;
138 }
139 PanoUnbind = (ExtensibilityCore::PanoUnbindPtr)GetProcAddress(extensibility, "PanoUnbind");
140 if (PanoUnbind == NULL) {
141 return false;
142 }
143 }
144
145 return true;
146}
147
155bool BindToServiceWithRetry(int containerPort, int maxAttempts, int baseSleep, double multiplier) {
156
157 for (int attempt = 0; attempt < maxAttempts; ++attempt) {
158 PanoBind(containerPort);
159 if (BindPort > 0)
160 break;
161
162 if (attempt < maxAttempts - 1) { // Don't sleep after the last attempt
163 int sleep_duration_ms = baseSleep * std::pow(multiplier, attempt);
164 std::cout << "Sleeping for " << sleep_duration_ms << " ms" << std::endl;
165 std::this_thread::sleep_for(std::chrono::milliseconds(sleep_duration_ms));
166 }
167 }
168
169 return true;
170}
171
178int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
179{
180 if (__argc < 1) {
181 printf("No Args");
182 exit(1);
183 }
184
185 // Extensibilities load their data (e.g. rules.pkg) relative to the working directory,
186 // so run from the container's folder rather than wherever it was launched from
187 char modulePath[MAX_PATH];
188 DWORD length = GetModuleFileNameA(NULL, modulePath, MAX_PATH);
189 if (length != 0 && length != MAX_PATH) {
190 std::string containerDir(modulePath, length);
191 SetCurrentDirectoryA(containerDir.substr(0, containerDir.find_last_of("\\/")).c_str());
192 }
193
194 std::string dllPath(__argv[1]);
195 if (!LoadExtensibility(dllPath)) {
196 printf("Error Loading Extensibility");
197 exit(1);
198 }
199
200 HANDLE grpcServerThread = CreateThread(
201 NULL, // default security attributes
202 0, // default stack size
203 (LPTHREAD_START_ROUTINE)RunContainerServer, // thread function
204 &BindPort, // no thread function arguments
205 0, // default creation flags
206 NULL // receive thread identifier
207 );
208 Sleep(2000);
209 if (grpcServerThread == NULL || BindPort == 0)
210 {
211 std::cout << "Error creating thread: " << GetLastError() << std::endl;
212 PanoUnbind();
213 return 1;
214 }
215
216 if (!BindToServiceWithRetry(BindPort, 7, 1000, 2))
217 {
218 std::cout << "Error Binding to service: " << GetLastError() << std::endl;
219 PanoUnbind();
220 return 1;
221 }
222
223 HANDLE healthCheckThread = CreateThread(
224 NULL, // default security attributes
225 0, // default stack size
226 (LPTHREAD_START_ROUTINE)HealthCheck, // thread function
227 NULL, // no thread function arguments
228 0, // default creation flags
229 NULL // receive thread identifier
230 );
231 Sleep(2000);
232 if (healthCheckThread == NULL || BindPort == 0)
233 {
234 std::cout << "Error creating thread: " << GetLastError() << std::endl;
235
236 PanoUnbind();
237 return 1;
238 }
239
240 HANDLE messageQueueThread = CreateThread(
241 NULL, // default security attributes
242 0, // default stack size
243 (LPTHREAD_START_ROUTINE)MessageQueueThread, // thread function
244 NULL, // no thread function arguments
245 0, // default creation flags
246 NULL // receive thread identifier
247 );
248 Sleep(2000);
249 if (healthCheckThread == NULL || BindPort == 0)
250 {
251 std::cout << "Error creating thread: " << GetLastError() << std::endl;
252 PanoUnbind();
253 return 1;
254 }
255
256 while (true)
257 {
258 if (isConnected == false) {
259 exit(1);
260 }
261 Sleep(2500);
262 }
263
264 return 0;
265}
#define MAX_PATH
Definition callbacks.h:7
bool(* PanoEntryPtr)(PeScan *)
std::unique_ptr< PanoptesService::Stub > stub
Definition container.cpp:17
#define PANOPTES_REG_ROOT
Definition container.cpp:24
void HealthCheck()
The HealthCheck function is a thread that checks the health of the Panoptes main service.
Definition container.cpp:89
ExtensibilityCore::PanoBindPtr PanoBind
Definition container.cpp:11
void MessageQueueThread()
The MessageQueueThread function is a thread that dequeues messages from the message queue and process...
Definition container.cpp:77
bool GetRegistryPortValue(DWORD &portValue)
Get the gRPC port value from the registry.
Definition container.cpp:30
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
The WinMain function is the entry point for the container.
int BindPort
Definition container.cpp:18
std::atomic_bool isConnected
Definition container.cpp:16
bool BindToServiceWithRetry(int containerPort, int maxAttempts, int baseSleep, double multiplier)
The BindToServiceWithRetry function is a function that binds to the Panoptes main service with a retr...
ExtensibilityCore::PanoEntryPtr PanoEntry
Definition container.cpp:12
bool LoadExtensibility(std::string dllFile)
The LoadExtensibility function is a function that loads the extensibility DLL.
ExtensibilityCore::PanoUnbindPtr PanoUnbind
Definition container.cpp:13
VOID RunContainerServer(LPVOID ContainerPort)
The RunContainerServer function is a function that runs the container server.
PeMessageQueue * message_queue_pe_
Definition container.cpp:15
The information about the file to be scanned that passed between the container, extensibility and the...
std::string PePath