Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Functions
events.h File Reference

Go to the source code of this file.

Functions

bool DeleteTraceSession ()
 
ULONG StartPanoptesTrace (LPVOID lpParam)
 
void StopPanoptesTrace ()
 

Function Documentation

◆ DeleteTraceSession()

bool DeleteTraceSession ( )

◆ StartPanoptesTrace()

ULONG StartPanoptesTrace ( LPVOID  lpParam)

Definition at line 305 of file events.cpp.

305 {
308 auto providers = serviceContext.config->m_eventProviders;
309
310 // Initialize properties for the single trace session
311 ULONG bufferSize = sizeof(EVENT_TRACE_PROPERTIES) + (MAX_PATH * sizeof(WCHAR));
312 EVENT_TRACE_PROPERTIES* pProperties = (EVENT_TRACE_PROPERTIES*)malloc(bufferSize);
313 ZeroMemory(pProperties, bufferSize);
314
315 pProperties->Wnode.BufferSize = bufferSize;
316 pProperties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
317 pProperties->Wnode.ClientContext = 1; // QPC clock resolution
318 pProperties->LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
319 pProperties->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES);
320 pProperties->LogFileNameOffset = 0;
321
322 // Start the trace session
323 TRACEHANDLE hTrace;
324 WCHAR sessionName[] = L"Panoptes";
325 ULONG result = StartTraceW(&hTrace, sessionName, pProperties);
326 if (result != ERROR_SUCCESS)
327 {
328 // Handle error
329 free(pProperties);
330 return 1;
331 }
332
333 // Enable multiple providers
334 for (const auto& provider : providers)
335 {
336 auto [provName, provMatchAny, provMatchAll] = provider;
337
338 GUID provGUID = GetProviderGuid(provName).value_or(GUID{});
339 if (provGUID == GUID{}) {
340 printf("[!] Could not retrieve GUID for %s\n", provName);
341 continue;
342 }
343
344 result = EnableTraceEx2(
345 hTrace,
346 &provGUID,
347 EVENT_CONTROL_CODE_ENABLE_PROVIDER,
348 TRACE_LEVEL_INFORMATION,
349 provMatchAny,
350 provMatchAll,
351 0,
352 NULL
353 );
354 if (result != ERROR_SUCCESS)
355 {
356 // Handle error
357 printf("[!] Could not enable trace for %s\n", provName);
358 continue;
359 }
360 }
361
362 // Set up the trace session
363 EVENT_TRACE_LOGFILEW trace;
364 ZeroMemory(&trace, sizeof(EVENT_TRACE_LOGFILE));
365 trace.LoggerName = (LPWSTR)sessionName;
366 trace.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD;
367 trace.EventRecordCallback = EventRecordCallback;
368
369 // Start processing events
370 TRACEHANDLE hProcessTrace = OpenTraceW(&trace);
371 if (hProcessTrace == INVALID_PROCESSTRACE_HANDLE)
372 {
373 // Handle error
374 ControlTraceW(hTrace, NULL, pProperties, EVENT_TRACE_CONTROL_STOP);
375 free(pProperties);
376 return 1;
377 }
378
379 ProcessTrace(&hProcessTrace, 1, NULL, NULL);
380
381 // Clean up
382 CloseTrace(hProcessTrace);
383 ControlTraceW(hTrace, NULL, pProperties, EVENT_TRACE_CONTROL_STOP);
384 free(pProperties);
385
386 return 1;
387
388}
#define MAX_PATH
Definition callbacks.h:6
std::vector< std::tuple< std::string, unsigned long, unsigned long > > m_eventProviders
The event providers from the configuration file.
VOID WINAPI EventRecordCallback(EVENT_RECORD *pEventRecord)
Definition events.cpp:243
ULONG result
Definition events.cpp:22
EVENT_TRACE_LOGFILEW trace
Definition events.cpp:23
std::optional< GUID > GetProviderGuid(const std::string &providerNameToFind)
Definition events.cpp:41
ULONG bufferSize
Definition events.cpp:22
TRACEHANDLE hTrace
Definition events.cpp:21
ULONG StopAndDeleteTrace()
Definition events.cpp:270
PanoptesContext * serviceContext
Definition grpc.cpp:27
Configuration * config

References bufferSize, PanoptesContext::config, EventRecordCallback(), GetProviderGuid(), hTrace, Configuration::m_eventProviders, MAX_PATH, result, serviceContext, StopAndDeleteTrace(), and trace.

Referenced by WinMain().

◆ StopPanoptesTrace()

void StopPanoptesTrace ( )

Definition at line 297 of file events.cpp.

297 {
298 std::wstring Name = TRACE_NAMEW;
299 ControlTraceW(NULL, Name.c_str(), traceProp, EVENT_TRACE_CONTROL_STOP);
300 if (hTrace != NULL) {
301 CloseTrace(hTrace); // Ensure hTrace is closed
302 }
303}
EVENT_TRACE_PROPERTIES * traceProp
Definition events.cpp:24
#define TRACE_NAMEW

References hTrace, TRACE_NAMEW, and traceProp.