Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
grpc.hpp File Reference
#include <Windows.h>
#include <string>

Go to the source code of this file.

Classes

class  PanoptesContainerClient

Functions

void SelfQueuePeScan (std::string pePath, std::string fileHash)
VOID RunServiceServer (LPVOID lpParam)
VOID StopServiceServer ()
 Shuts down the server RunServiceServer started, cancelling calls still in flight after a short grace period, so RunServiceServer returns. Safe to call if it never started.
bool MoveFileToQuarantine (std::string filePath)
 Moves a file into C:\ProgramData\Panoptes\Quarantine.

Function Documentation

◆ MoveFileToQuarantine()

bool MoveFileToQuarantine ( std::string filePath)

Moves a file into C:\ProgramData\Panoptes\Quarantine.

Returns
Whether the file was moved

Definition at line 71 of file grpc.cpp.

71 {
72 std::string quarantinePath = "C:\\ProgramData\\Panoptes\\Quarantine";
73
74 std::error_code error;
75 fs::create_directories(quarantinePath, error);
76 std::filesystem::path sourcePath(filePath);
77 std::filesystem::path destinationPath = fs::path(quarantinePath) / sourcePath.filename();
78 try {
79 fs::rename(sourcePath, destinationPath);
80 }
81 catch (const std::filesystem::filesystem_error& e) {
82 std::cerr << "Error moving file to quarantine: " << e.what() << std::endl;
83 return false;
84 }
85 return true;
86}

Referenced by RespondToDetection().

◆ RunServiceServer()

VOID RunServiceServer ( LPVOID lpParam)

Definition at line 598 of file grpc.cpp.

599{
600 serviceContext = reinterpret_cast<PanoptesContext*>(lpParam);
601 PanoptesImpl service;
602 grpc::ServerBuilder builder;
603 int selected_port = 0;
604 std::string server_url = "localhost:0";
605
606 //Setting the server address to localhost:0 will allow the OS to assign an available port
607 builder.AddListeningPort(server_url, grpc::InsecureServerCredentials(), &selected_port);
608 builder.RegisterService(&service);
609
610 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
611 if (server == nullptr) {
612 auto threadError = &serviceContext->threadError;
613 bool threadState = threadError->load();
614 threadError->store(true);
615 return;
616 }
617
618 g_serverPort = selected_port;
619 bool updatedReg = CreateRegistryEntryWithPort(selected_port);
620 if (!updatedReg) {
621 auto threadError = &serviceContext->threadError;
622 bool threadState = threadError->load();
623 threadError->store(true);
624 }
625
626 {
627 std::lock_guard<std::mutex> lock(g_serverLock);
628 if (g_serverStopping) {
629 server->Shutdown();
630 }
631 g_server = server.get();
632 }
633
634 // Returns once StopServiceServer has shut the server down
635 server->Wait();
636
637 std::lock_guard<std::mutex> lock(g_serverLock);
638 g_server = nullptr;
639}
The PanoptesImpl class is a class that implements the PanoptesExtensibility::Service interface from t...
PanoptesContext * serviceContext
Definition grpc.cpp:33
bool CreateRegistryEntryWithPort(DWORD dwPort)
Definition grpc.cpp:152

References CreateRegistryEntryWithPort(), and serviceContext.

Referenced by WinMain().

◆ SelfQueuePeScan()

void SelfQueuePeScan ( std::string pePath,
std::string fileHash )

Definition at line 338 of file grpc.cpp.

338 {
339 auto configuration = serviceContext->config;
340 if (Configuration::IsPathInList(configuration->m_exclusions, pePath)) {
341 return;
342 }
343
344 DWORD containerPort = 0;
345 GetRegistryPortValue(containerPort);
346 std::string server_url = "localhost:" + std::to_string(containerPort);
347 std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(server_url, grpc::InsecureChannelCredentials());
348 selfStub_ = PanoptesService::NewStub(channel);
349
350 AckMessage reply;
351 ClientContext context;
352 PeScanInfo request;
353 request.set_file_hash(fileHash);
354 request.set_portable_executable_path(pePath);
355
356 Status status = selfStub_->QueuePeScan(&context, request, &reply);
357 if (!status.ok()) {
358 std::cout << status.error_code() << ": " << status.error_message() << std::endl;
359 }
360}
static bool IsPathInList(const std::vector< std::string > &patterns, const std::string &path)
Check whether a path matches any pattern in an exclusion list. A pattern with no wildcard matches eve...
bool GetRegistryPortValue(DWORD &portValue)
Definition grpc.cpp:88
std::unique_ptr< PanoptesService::Stub > selfStub_
Definition grpc.cpp:32

References GetRegistryPortValue(), Configuration::IsPathInList(), selfStub_, and serviceContext.

◆ StopServiceServer()

VOID StopServiceServer ( )

Shuts down the server RunServiceServer started, cancelling calls still in flight after a short grace period, so RunServiceServer returns. Safe to call if it never started.

Definition at line 585 of file grpc.cpp.

586{
587 // Event streams only end when the detector disconnects, so they are ended first; otherwise
588 // each would hold up shutdown until the grace period runs out
590
591 std::lock_guard<std::mutex> lock(g_serverLock);
592 g_serverStopping = true;
593 if (g_server != nullptr) {
594 g_server->Shutdown(std::chrono::system_clock::now() + std::chrono::milliseconds(PANO_SERVER_SHUTDOWN_GRACE_MS));
595 }
596}
void StopDetectorStreams()
Wakes every subscriber's Wait with false and refuses new subscribers, so the SubscribeEvents calls re...
#define PANO_SERVER_SHUTDOWN_GRACE_MS
Definition grpc.cpp:583

References PANO_SERVER_SHUTDOWN_GRACE_MS, and StopDetectorStreams().