Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
container.cpp File Reference
#include <grpcpp/grpcpp.h>
#include "panoptes.grpc.pb.h"
#include "panoptes.pb.h"
#include "container_ipc.hpp"
#include <ExtensibilityCore.h>
#include <iostream>
#include <atomic>

Go to the source code of this file.

Macros

#define PANOPTES_REG_ROOT   HKEY_LOCAL_MACHINE

Functions

bool GetRegistryPortValue (DWORD &portValue)
 Get the gRPC port value from the registry.
void MessageQueueThread ()
 The MessageQueueThread function is a thread that dequeues messages from the message queue and processes them in the PanoEntry function of the loaded extensibility DLL.
void HealthCheck ()
 The HealthCheck function is a thread that checks the health of the Panoptes main service.
bool LoadExtensibility (std::string dllFile)
 The LoadExtensibility function is a function that loads the extensibility DLL.
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 retry mechanism.
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 The WinMain function is the entry point for the container.

Variables

ExtensibilityCore::PanoBindPtr PanoBind
ExtensibilityCore::PanoEntryPtr PanoEntry
ExtensibilityCore::PanoUnbindPtr PanoUnbind
PeMessageQueue * message_queue_pe_
std::atomic_bool isConnected = true
std::unique_ptr< PanoptesService::Stub > stub
int BindPort = 0

Macro Definition Documentation

◆ PANOPTES_REG_ROOT

#define PANOPTES_REG_ROOT   HKEY_LOCAL_MACHINE

Definition at line 24 of file container.cpp.

Referenced by CreateRegistryEntryWithPort(), and GetRegistryPortValue().

Function Documentation

◆ BindToServiceWithRetry()

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 retry mechanism.

Parameters
containerPortThe port value that is read from the registry.
maxAttemptsThe maximum number of attempts to bind to the service.
baseSleepThe base sleep time in milliseconds.
multiplierThe multiplier for the sleep time.
Returns
True if the service is bound successfully, false otherwise.

Definition at line 155 of file container.cpp.

155 {
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}
ExtensibilityCore::PanoBindPtr PanoBind
Definition container.cpp:11
int BindPort
Definition container.cpp:18

References BindPort, and PanoBind.

Referenced by WinMain().

◆ GetRegistryPortValue()

bool GetRegistryPortValue ( DWORD & portValue)

Get the gRPC port value from the registry.

Parameters
portValueThe port value that is read from the registry.
Returns
True if the port value is read successfully, false otherwise.

Definition at line 30 of file container.cpp.

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}
#define PANOPTES_REG_ROOT
Definition container.cpp:24

References PANOPTES_REG_ROOT.

Referenced by HealthCheck().

◆ HealthCheck()

void HealthCheck ( )

The HealthCheck function is a thread that checks the health of the Panoptes main service.

Definition at line 89 of file container.cpp.

89 {
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}
std::unique_ptr< PanoptesService::Stub > stub
Definition container.cpp:17
bool GetRegistryPortValue(DWORD &portValue)
Get the gRPC port value from the registry.
Definition container.cpp:30
std::atomic_bool isConnected
Definition container.cpp:16

References GetRegistryPortValue(), isConnected, and stub.

Referenced by WinMain().

◆ LoadExtensibility()

bool LoadExtensibility ( std::string dllFile)

The LoadExtensibility function is a function that loads the extensibility DLL.

Parameters
dllFileThe path to the extensibility DLL.
Returns
True if the extensibility DLL is loaded successfully, false otherwise.

Definition at line 124 of file container.cpp.

124 {
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}
bool(* PanoEntryPtr)(PeScan *)
ExtensibilityCore::PanoEntryPtr PanoEntry
Definition container.cpp:12
ExtensibilityCore::PanoUnbindPtr PanoUnbind
Definition container.cpp:13

References PanoBind, PanoEntry, and PanoUnbind.

Referenced by WinMain().

◆ MessageQueueThread()

void MessageQueueThread ( )

The MessageQueueThread function is a thread that dequeues messages from the message queue and processes them in the PanoEntry function of the loaded extensibility DLL.

Definition at line 77 of file container.cpp.

77 {
79 while (isConnected) {
80 PeScan pe_data = message_queue_pe_->dequeue();
81 if (pe_data.PePath != "")
82 {
83 PanoEntry(&pe_data);
84 }
85 }
86}
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

References isConnected, message_queue_pe_, PanoEntry, and PeScan::PePath.

Referenced by WinMain().

◆ WinMain()

int WINAPI WinMain ( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )

The WinMain function is the entry point for the container.

Parameters
hInstanceThe instance of the application.
hPrevInstanceThe previous instance of the application.
lpCmdLineThe command line arguments.
nCmdShowThe command line arguments.
Returns
The exit code of the application.

Definition at line 178 of file container.cpp.

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
void HealthCheck()
The HealthCheck function is a thread that checks the health of the Panoptes main service.
Definition container.cpp:89
void MessageQueueThread()
The MessageQueueThread function is a thread that dequeues messages from the message queue and process...
Definition container.cpp:77
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...
bool LoadExtensibility(std::string dllFile)
The LoadExtensibility function is a function that loads the extensibility DLL.
VOID RunContainerServer(LPVOID ContainerPort)
The RunContainerServer function is a function that runs the container server.

References BindPort, BindToServiceWithRetry(), HealthCheck(), isConnected, LoadExtensibility(), MAX_PATH, MessageQueueThread(), PanoUnbind, and RunContainerServer().

Variable Documentation

◆ BindPort

int BindPort = 0

Definition at line 18 of file container.cpp.

Referenced by BindToServiceWithRetry(), and WinMain().

◆ isConnected

std::atomic_bool isConnected = true

Definition at line 16 of file container.cpp.

Referenced by HealthCheck(), MessageQueueThread(), and WinMain().

◆ message_queue_pe_

PeMessageQueue* message_queue_pe_

Definition at line 15 of file container.cpp.

Referenced by MessageQueueThread().

◆ PanoBind

Definition at line 11 of file container.cpp.

Referenced by BindToServiceWithRetry(), and LoadExtensibility().

◆ PanoEntry

Definition at line 12 of file container.cpp.

Referenced by LoadExtensibility(), and MessageQueueThread().

◆ PanoUnbind

Definition at line 13 of file container.cpp.

Referenced by LoadExtensibility(), and WinMain().

◆ stub

std::unique_ptr<PanoptesService::Stub> stub

Definition at line 17 of file container.cpp.

Referenced by HealthCheck().