Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook_events.cpp File Reference
#include "hook_events.h"
#include "error_message.h"
#include "pano_log.h"
#include "detector_stream.h"
#include "process_images.h"
#include "utils.h"
#include <evntrace.h>
#include <evntcons.h>
#include <tdh.h>
#include <nlohmann/json.hpp>
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

Go to the source code of this file.

Classes

struct  HookSessionProperties

Macros

#define HOOK_SESSION_NAME   L"Panoptes Hook Trace"
#define HOOK_KEYWORD_DETECTION   0x1
#define HOOK_LOSS_CHECK_MS   5000

Functions

ERRORCODE StartHookEvents ()
 Starts a real-time ETW session for the hook DLL's "Panoptes.Hook" TraceLogging provider and a thread that logs each event it delivers. A session left behind by a previous run of the service is stopped and replaced. Requires SYSTEM or Administrators.
void StopHookEvents ()
 Stops the ETW session and waits for the consumer thread to deliver what it buffered.

Macro Definition Documentation

◆ HOOK_KEYWORD_DETECTION

#define HOOK_KEYWORD_DETECTION   0x1

Definition at line 34 of file hook_events.cpp.

◆ HOOK_LOSS_CHECK_MS

#define HOOK_LOSS_CHECK_MS   5000

Definition at line 37 of file hook_events.cpp.

◆ HOOK_SESSION_NAME

#define HOOK_SESSION_NAME   L"Panoptes Hook Trace"

Definition at line 25 of file hook_events.cpp.

Referenced by StartHookEvents().

Function Documentation

◆ StartHookEvents()

ERRORCODE StartHookEvents ( )

Starts a real-time ETW session for the hook DLL's "Panoptes.Hook" TraceLogging provider and a thread that logs each event it delivers. A session left behind by a previous run of the service is stopped and replaced. Requires SYSTEM or Administrators.

Returns
PANO_SUCCESS if the session was started and is being consumed, otherwise an error code

Definition at line 330 of file hook_events.cpp.

331{
332 HookSessionProperties properties;
333 ULONG status = StartTraceW(&g_SessionHandle, HOOK_SESSION_NAME, &properties.Properties);
334 if (status == ERROR_ALREADY_EXISTS)
335 {
336 // Left behind by a previous run of the service
338 ControlTraceW(0, HOOK_SESSION_NAME, &stale.Properties, EVENT_TRACE_CONTROL_STOP);
339
340 properties = HookSessionProperties();
341 status = StartTraceW(&g_SessionHandle, HOOK_SESSION_NAME, &properties.Properties);
342 }
343 if (status != ERROR_SUCCESS)
344 {
345 std::cerr << "Failed to start the hook event trace session. Error code: " << status << std::endl;
346 g_SessionHandle = 0;
347 return (status == ERROR_ACCESS_DENIED) ? NOT_ADMIN : WINDOWS_ERR;
348 }
349
350 status = EnableTraceEx2(g_SessionHandle, &HookProviderGuid, EVENT_CONTROL_CODE_ENABLE_PROVIDER,
351 TRACE_LEVEL_WARNING, HOOK_KEYWORD_DETECTION, 0, 0, NULL);
352 if (status != ERROR_SUCCESS)
353 {
354 std::cerr << "Failed to enable the hook event provider. Error code: " << status << std::endl;
356 return WINDOWS_ERR;
357 }
358
359 EVENT_TRACE_LOGFILEW logFile{};
360 logFile.LoggerName = (LPWSTR)HOOK_SESSION_NAME;
361 logFile.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD;
362 logFile.EventRecordCallback = OnHookEvent;
363
364 g_ConsumerHandle = OpenTraceW(&logFile);
365 if (g_ConsumerHandle == INVALID_PROCESSTRACE_HANDLE)
366 {
367 std::cerr << "Failed to open the hook event trace. Error code: " << GetLastError() << std::endl;
369 return WINDOWS_ERR;
370 }
371
372 g_ConsumerThread = std::thread(ConsumerThread);
373
374 g_ReportedEventsLost = 0;
375 g_ReportedBuffersLost = 0;
376 g_LossStopping = false;
377 g_LossThread = std::thread(LossMonitorThread);
378 return PANO_SUCCESS;
379}
#define PANO_SUCCESS
#define NOT_ADMIN
#define WINDOWS_ERR
#define HOOK_SESSION_NAME
void StopHookEvents()
Stops the ETW session and waits for the consumer thread to deliver what it buffered.
#define HOOK_KEYWORD_DETECTION
EVENT_TRACE_PROPERTIES Properties

References ERRORCODE, HOOK_KEYWORD_DETECTION, HOOK_SESSION_NAME, NOT_ADMIN, PANO_SUCCESS, HookSessionProperties::Properties, StopHookEvents(), and WINDOWS_ERR.

Referenced by WinMain().

◆ StopHookEvents()

void StopHookEvents ( )

Stops the ETW session and waits for the consumer thread to deliver what it buffered.

Definition at line 381 of file hook_events.cpp.

382{
383 // The monitor goes first so it never queries a session that has been stopped
384 if (g_LossThread.joinable())
385 {
386 {
387 std::lock_guard<std::mutex> lock(g_LossLock);
388 g_LossStopping = true;
389 }
390 g_LossWake.notify_all();
391 g_LossThread.join();
392 }
393
394 // Stopping the session flushes its buffers to the consumer, after which ProcessTrace
395 // returns. The stop also returns the session's final counters, so losses since the last
396 // check are still reported
397 if (g_SessionHandle != 0)
398 {
399 HookSessionProperties properties;
400 if (ControlTraceW(g_SessionHandle, NULL, &properties.Properties, EVENT_TRACE_CONTROL_STOP) == ERROR_SUCCESS)
401 {
402 ReportLoss(properties.Properties);
403 }
404 g_SessionHandle = 0;
405 }
406
407 if (g_ConsumerThread.joinable())
408 {
409 g_ConsumerThread.join();
410 }
411
412 if (g_ConsumerHandle != INVALID_PROCESSTRACE_HANDLE)
413 {
414 CloseTrace(g_ConsumerHandle);
415 g_ConsumerHandle = INVALID_PROCESSTRACE_HANDLE;
416 }
417}

References HookSessionProperties::Properties.

Referenced by StartHookEvents().