Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
service_client_ml.cpp
Go to the documentation of this file.
1#include <grpcpp/grpcpp.h>
2#include "panoptes.grpc.pb.h"
3#include "PanoptesML.h"
4#include <iostream>
5
6using grpc::ClientContext;
7using grpc::Status;
8
9static std::unique_ptr<PanoptesService::Stub> g_Stub;
10
11// Debug builds of the service run unelevated and publish the port under HKCU
12#ifdef _DEBUG
13#define PANOPTES_REG_ROOT HKEY_CURRENT_USER
14#else
15#define PANOPTES_REG_ROOT HKEY_LOCAL_MACHINE
16#endif
17
18// How long a call to the service may take before it is given up
19#define PANO_ML_RPC_TIMEOUT_MS 8000
20
22static bool GetServicePort(DWORD& Port)
23{
24 DWORD size = sizeof(Port);
25 LONG result = RegGetValueA(PANOPTES_REG_ROOT, "SOFTWARE\\Panoptes", "SRV_PORT", RRF_RT_REG_DWORD, NULL, &Port, &size);
26 if (result != ERROR_SUCCESS) {
27 std::cerr << "PanoptesML: cannot read the service port (error " << result << ")" << std::endl;
28 return false;
29 }
30 return true;
31}
32
34{
35 if (g_Stub != nullptr) {
36 return; // one channel for the container's lifetime
37 }
38 DWORD port;
39 if (!GetServicePort(port)) {
40 return;
41 }
42 auto channel = grpc::CreateChannel("localhost:" + std::to_string(port), grpc::InsecureChannelCredentials());
43 g_Stub = PanoptesService::NewStub(channel);
44}
45
46static void SetDeadline(ClientContext& Context)
47{
48 Context.set_deadline(std::chrono::system_clock::now() + std::chrono::milliseconds(PANO_ML_RPC_TIMEOUT_MS));
49}
50
52{
53 if (g_Stub == nullptr) {
54 return false;
55 }
56 ClientContext context;
57 SetDeadline(context);
58 ContainerInfo request;
59 request.set_container_type((ContainerType)Type);
60 request.set_grpc_port(ContainerPort);
61
62 AckMessage reply;
63 Status status = g_Stub->Hello(&context, request, &reply);
64 if (!status.ok()) {
65 // The container retries PanoBind with backoff
66 std::cerr << "PanoptesML: Hello failed: " << status.error_message() << std::endl;
67 return false;
68 }
69 return reply.ack_type() == AckType::SUCCESS;
70}
71
72bool PanoptesServiceClient::SendResults_ML(const std::string& PePath, const std::string& FileHash, const MlScanResult& Result)
73{
74 if (g_Stub == nullptr) {
75 return false;
76 }
77 ClientContext context;
78 SetDeadline(context);
79 ContainerReply request;
80 request.set_portable_executable_path(PePath);
81 request.set_file_hash(FileHash);
82
83 ScanML* scan = request.mutable_ml_scan();
84 scan->set_score(Result.Score);
85 scan->set_threshold(PANO_ML_THRESHOLD);
86 scan->set_malicious(Result.Malicious);
87 scan->set_model(Result.Model);
88 scan->set_feature_version(PANO_ML_FEATURE_VERSION);
89 scan->set_is_pe(Result.IsPe);
90 scan->set_error(Result.Error);
91
92 AckMessage reply;
93 Status status = g_Stub->ScanResults(&context, request, &reply);
94 if (!status.ok()) {
95 std::cerr << "PanoptesML: ScanResults failed: " << status.error_message() << std::endl;
96 return false;
97 }
98 return reply.ack_type() == AckType::SUCCESS;
99}
ExtensibilityType
The type of extensibility.
#define PANO_ML_THRESHOLD
Score at or above which a file is reported as malicious.
Definition PanoptesML.h:14
#define PANO_ML_FEATURE_VERSION
Version of the feature layout below. A model is trained for one layout; change this whenever a featur...
Definition PanoptesML.h:23
PanoptesServiceClient()
The PanoptesServiceClient class is a class that implements the PanoptesServiceClient class.
bool SendResults_ML(const std::string &PePath, const std::string &FileHash, const MlScanResult &Result)
bool Hello(ExtensibilityType extensibilityType, std::string port)
The Hello function sends a Hello message to the Panoptes main service from the container.
#define PANOPTES_REG_ROOT
Definition container.cpp:24
#define PANO_ML_RPC_TIMEOUT_MS
One file's result, as sent to the service.
Definition PanoptesML.h:111
std::string Model
Definition PanoptesML.h:115
std::string Error
Definition PanoptesML.h:116