Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
driver_events.cpp File Reference
#include "driver_events.h"
#include "pano_events.h"
#include "error_message.h"
#include "grpc.hpp"
#include "pano_log.h"
#include "detector_stream.h"
#include "process_images.h"
#include "utils.h"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  _PANO_EVENT_REQUEST
struct  _PANO_WRITTEN_FILE

Macros

#define PANO_PENDING_EVENT_REQUESTS   4
#define PANO_WRITE_SETTLE_MS   2000
#define PANO_WRITE_MAX_WINDOW_MS   30000
#define PANO_WRITE_POLL_MS   500

Typedefs

typedef struct _PANO_EVENT_REQUEST PANO_EVENT_REQUEST
typedef struct _PANO_EVENT_REQUEST * PPANO_EVENT_REQUEST
typedef struct _PANO_WRITTEN_FILE PANO_WRITTEN_FILE

Functions

ERRORCODE StartDriverEvents (const Configuration *configuration)
 Opens the Panoptes driver device and starts the threads that read events from it (IOCTL_PANOPTES_GET_EVENT) and queue scans for files that were created or written.
void StopDriverEvents ()
 Cancels the pending event requests and stops the reader threads.

Macro Definition Documentation

◆ PANO_PENDING_EVENT_REQUESTS

#define PANO_PENDING_EVENT_REQUESTS   4

Definition at line 24 of file driver_events.cpp.

◆ PANO_WRITE_MAX_WINDOW_MS

#define PANO_WRITE_MAX_WINDOW_MS   30000

Definition at line 31 of file driver_events.cpp.

◆ PANO_WRITE_POLL_MS

#define PANO_WRITE_POLL_MS   500

Definition at line 32 of file driver_events.cpp.

◆ PANO_WRITE_SETTLE_MS

#define PANO_WRITE_SETTLE_MS   2000

Definition at line 30 of file driver_events.cpp.

Typedef Documentation

◆ PANO_EVENT_REQUEST

◆ PANO_WRITTEN_FILE

◆ PPANO_EVENT_REQUEST

Function Documentation

◆ StartDriverEvents()

ERRORCODE StartDriverEvents ( const Configuration * configuration)

Opens the Panoptes driver device and starts the threads that read events from it (IOCTL_PANOPTES_GET_EVENT) and queue scans for files that were created or written.

Parameters
configurationThe service configuration; its log exclusions are applied to events
Returns
PANO_SUCCESS if the device was opened and the reader started, otherwise an error code

Definition at line 1167 of file driver_events.cpp.

1168{
1169 BuildDeviceMap();
1170 g_LogExclusions = configuration->m_logExclusions;
1171
1172 // The device only allows SYSTEM and Administrators to open it
1173 g_Device = CreateFileW(
1175 GENERIC_READ,
1176 FILE_SHARE_READ | FILE_SHARE_WRITE,
1177 NULL,
1178 OPEN_EXISTING,
1179 FILE_FLAG_OVERLAPPED,
1180 NULL);
1181 if (g_Device == INVALID_HANDLE_VALUE)
1182 {
1183 DWORD error = GetLastError();
1184 std::cerr << "Failed to open the Panoptes driver device. Error code: " << error << std::endl;
1185 return (error == ERROR_ACCESS_DENIED) ? NOT_ADMIN : KERNEL_PIPE;
1186 }
1187
1188 g_CompletionPort = CreateIoCompletionPort(g_Device, NULL, 0, 1);
1189 if (g_CompletionPort == NULL)
1190 {
1191 CloseHandle(g_Device);
1192 g_Device = INVALID_HANDLE_VALUE;
1193 return WINDOWS_ERR;
1194 }
1195
1196 g_Stopping = false;
1197#ifdef _DEBUG
1198 g_EchoStopping = false;
1199 g_EchoThread = std::thread(ConsoleEchoThread);
1200#endif
1201 g_ReaderThread = std::thread(EventReaderThread);
1202 g_ScanThread = std::thread(WrittenFileScanThread);
1203 return PANO_SUCCESS;
1204}
#define PANO_SUCCESS
#define NOT_ADMIN
#define KERNEL_PIPE
#define WINDOWS_ERR
std::vector< std::string > m_logExclusions
Path patterns whose driver events are not logged (see IsPathInList). Files matching them are still sc...
#define PANOPTES_USER_DEVICE_PATH
Definition pano_events.h:16

References ERRORCODE, KERNEL_PIPE, Configuration::m_logExclusions, NOT_ADMIN, PANO_SUCCESS, PANOPTES_USER_DEVICE_PATH, and WINDOWS_ERR.

Referenced by WinMain().

◆ StopDriverEvents()

void StopDriverEvents ( )

Cancels the pending event requests and stops the reader threads.

Definition at line 1206 of file driver_events.cpp.

1207{
1208 if (g_Device == INVALID_HANDLE_VALUE)
1209 {
1210 return;
1211 }
1212
1213 // Cancelling completes every pending request with ERROR_OPERATION_ABORTED, and the
1214 // reader exits once none are outstanding
1215 g_Stopping = true;
1216 CancelIoEx(g_Device, NULL);
1217
1218 if (g_ReaderThread.joinable())
1219 {
1220 g_ReaderThread.join();
1221 }
1222 if (g_ScanThread.joinable())
1223 {
1224 g_ScanThread.join();
1225 }
1226
1227 {
1228 std::lock_guard<std::mutex> lock(g_PendingScansLock);
1229 g_PendingScans.clear();
1230 }
1231
1232#ifdef _DEBUG
1233 // The threads that log are stopped, so the echo thread prints what is left and exits
1234 {
1235 std::lock_guard<std::mutex> lock(g_EchoLock);
1236 g_EchoStopping = true;
1237 }
1238 g_EchoReady.notify_one();
1239 if (g_EchoThread.joinable())
1240 {
1241 g_EchoThread.join();
1242 }
1243#endif
1244
1245 CloseHandle(g_CompletionPort);
1246 CloseHandle(g_Device);
1247 g_CompletionPort = NULL;
1248 g_Device = INVALID_HANDLE_VALUE;
1249}