Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
dllmain.cpp File Reference
#include "PanoptesML.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <mutex>
#include <stdexcept>

Go to the source code of this file.

Functions

BOOL APIENTRY DllMain (HMODULE module, DWORD reason, LPVOID reserved)
PANO_API int PanoBind (int ContainerPort)
 Registers with the Panoptes Service: the extensibility type and the port of the container this module runs in.
PANO_API bool PanoEntry (PeScan *data)
 Scores one file with the model and reports the result to the service. The file is only read: its static features are computed from its bytes and headers.
PANO_API int PanoMlScoreFile (const wchar_t *Path, PanoMlTestResult *Result)
 Computes a file's features and scores them with the model (embedded, or model.onnx from the DLL's folder). The file is only read. Thread-safe.
PANO_API int PanoMlFeatureName (int Index, char *Buffer, int BufferSize)
 Copies the name of feature Index (e.g. "byte_4d", "section_entropy_max") into Buffer.
PANO_API bool PanoUnbind ()
 Called once when the container shuts down.

Function Documentation

◆ DllMain()

BOOL APIENTRY DllMain ( HMODULE module,
DWORD reason,
LPVOID reserved )

Definition at line 60 of file dllmain.cpp.

61{
62 UNREFERENCED_PARAMETER(reserved);
63 if (reason == DLL_PROCESS_ATTACH) {
64 DisableThreadLibraryCalls(module);
65 }
66 return TRUE;
67}

◆ PanoBind()

PANO_API int PanoBind ( int ContainerPort)

Registers with the Panoptes Service: the extensibility type and the port of the container this module runs in.

Returns
The container port, or 0 if the service didn't accept it

Definition at line 72 of file dllmain.cpp.

73{
75 if (!client.Hello(ExtensibilityType::EXTENSIBILITY_TYPE_ML, ContainerPort)) {
76 return 0;
77 }
78 return ContainerPort;
79}
@ EXTENSIBILITY_TYPE_ML
The Panoptes Service client used to talk to the service over gRPC.
bool Hello(ExtensibilityType extensibilityType, std::string port)
The Hello function sends a Hello message to the Panoptes main service from the container.

References EXTENSIBILITY_TYPE_ML, PanoptesServiceClient::Hello(), and PANO_API.

◆ PanoEntry()

PANO_API bool PanoEntry ( PeScan * data)

Scores one file with the model and reports the result to the service. The file is only read: its static features are computed from its bytes and headers.

A result is reported for every file, including one that couldn't be scored (Error says why), so the service and a scanner waiting on it know the ML scan has finished.

Returns
True if the file was scored and the result reported

Definition at line 87 of file dllmain.cpp.

88{
89 MlScanResult result;
90 bool scored = false;
91 // No exception may leave this extern "C" export: it would end the container
92 try {
93 MlModel* model = GetModel();
94 if (model == nullptr) {
95 result.Error = "cannot load the model: " + g_ModelError;
96 }
97 else {
98 result.Model = model->Name();
99 PeFeatures features = ExtractPeFeatures(Widen(data->PePath));
100 result.IsPe = features.IsPe;
101 if (!features.IsPe) {
102 // The model is trained on PE files only (EMBER2024's Win32, Win64 and .NET
103 // records); its score for anything else means nothing
104 result.Error = "not a PE file; the model only scores PE files";
105 }
106 else {
107 result.Score = model->Score(features.Values);
108 result.Malicious = result.Score >= PANO_ML_THRESHOLD;
109 scored = true;
110 }
111 }
112 }
113 catch (const std::exception& e) {
114 // Ort::Exception derives from std::exception. A file deleted or locked before its scan,
115 // or one over the size limit, is common
116 result.Error = e.what();
117 std::cerr << "PanoptesML scan of " << data->PePath << " failed: " << e.what() << std::endl;
118 }
119
120 try {
122 return client.SendResults_ML(data->PePath, data->FileHash, result) && scored;
123 }
124 catch (const std::exception& e) {
125 std::cerr << "PanoptesML: reporting " << data->PePath << " failed: " << e.what() << std::endl;
126 return false;
127 }
128}
#define PANO_ML_THRESHOLD
Score at or above which a file is reported as malicious.
Definition PanoptesML.h:14
PeFeatures ExtractPeFeatures(const std::wstring &Path)
Reads a file and computes its static features. The file is only read, never run or loaded....
An ONNX model that scores a feature vector. The model must take one float tensor of shape [1,...
Definition PanoptesML.h:73
float Score(const std::vector< float > &Features)
Runs the model on one file's features. Returns the malicious probability.
Definition ml_model.cpp:99
const std::string & Name() const
Definition PanoptesML.h:87
bool SendResults_ML(const std::string &PePath, const std::string &FileHash, const MlScanResult &Result)
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
std::vector< float > Values
Definition PanoptesML.h:55
std::string FileHash
std::string PePath

References MlScanResult::Error, ExtractPeFeatures(), PeScan::FileHash, MlScanResult::IsPe, PeFeatures::IsPe, MlScanResult::Malicious, MlScanResult::Model, MlModel::Name(), PANO_API, PANO_ML_THRESHOLD, PeScan::PePath, MlModel::Score(), MlScanResult::Score, PanoptesServiceClient::SendResults_ML(), and PeFeatures::Values.

◆ PanoMlFeatureName()

PANO_API int PanoMlFeatureName ( int Index,
char * Buffer,
int BufferSize )

Copies the name of feature Index (e.g. "byte_4d", "section_entropy_max") into Buffer.

Returns
The name's length, or -1 if Index is out of range or Buffer too small

Definition at line 181 of file dllmain.cpp.

182{
183 const char* name = Index >= 0 ? FeatureName((size_t)Index) : nullptr;
184 if (name == nullptr || Buffer == nullptr) {
185 return -1;
186 }
187 size_t length = strlen(name);
188 if (BufferSize <= 0 || length >= (size_t)BufferSize) {
189 return -1;
190 }
191 memcpy(Buffer, name, length + 1);
192 return (int)length;
193}
const char * FeatureName(size_t Index)
The name of feature Index, or nullptr when it is out of range.

References FeatureName(), and PANO_API.

◆ PanoMlScoreFile()

PANO_API int PanoMlScoreFile ( const wchar_t * Path,
PanoMlTestResult * Result )

Computes a file's features and scores them with the model (embedded, or model.onnx from the DLL's folder). The file is only read. Thread-safe.

Returns
PANO_ML_TEST_OK, PANO_ML_TEST_FAILED or PANO_ML_TEST_NO_MODEL

Definition at line 141 of file dllmain.cpp.

142{
143 // A caller built against another layout gets nothing written, rather than a struct overrun
144 if (Result == nullptr || Result->StructSize != sizeof(PanoMlTestResult)) {
145 return PANO_ML_TEST_FAILED;
146 }
147 ZeroMemory(Result, sizeof(*Result));
148 Result->StructSize = sizeof(PanoMlTestResult);
150 Result->FeatureCount = FEATURE_COUNT;
152 if (Path == nullptr) {
153 CopyText(Result->Error, sizeof(Result->Error), "no path");
154 return PANO_ML_TEST_FAILED;
155 }
156
157 // No exception may leave an extern "C" export
158 try {
159 PeFeatures features = ExtractPeFeatures(std::wstring(Path));
160 std::copy(features.Values.begin(), features.Values.end(), Result->Features);
161 Result->IsPe = features.IsPe ? 1 : 0;
162
163 MlModel* model = GetModel();
164 if (model == nullptr) {
165 CopyText(Result->Error, sizeof(Result->Error), "cannot load the model: " + g_ModelError);
167 }
168 // The raw score is kept for testing, but as in PanoEntry a file that isn't a PE gets no
169 // verdict: the model is trained on PE files only
170 Result->Score = model->Score(features.Values);
171 Result->Malicious = features.IsPe && Result->Score >= PANO_ML_THRESHOLD ? 1 : 0;
172 CopyText(Result->Model, sizeof(Result->Model), model->Name());
173 return PANO_ML_TEST_OK;
174 }
175 catch (const std::exception& e) {
176 CopyText(Result->Error, sizeof(Result->Error), e.what());
177 return PANO_ML_TEST_FAILED;
178 }
179}
#define PANO_ML_TEST_OK
PanoMlScoreFile results.
Definition PanoptesML.h:142
#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
#define PANO_ML_TEST_FAILED
Definition PanoptesML.h:143
#define PANO_ML_TEST_NO_MODEL
Definition PanoptesML.h:144
@ FEATURE_COUNT
Definition PanoptesML.h:51
One file's result for a test tool. Every field has a fixed size, so a caller in any language can decl...
Definition PanoptesML.h:128
int32_t FeatureCount
Definition PanoptesML.h:131
uint32_t StructSize
Definition PanoptesML.h:129
char Model[128]
Definition PanoptesML.h:137
char Error[512]
Definition PanoptesML.h:138
float Features[FEATURE_COUNT]
Definition PanoptesML.h:136
int32_t FeatureVersion
Definition PanoptesML.h:130

References PanoMlTestResult::Error, ExtractPeFeatures(), FEATURE_COUNT, PanoMlTestResult::FeatureCount, PanoMlTestResult::Features, PanoMlTestResult::FeatureVersion, PanoMlTestResult::IsPe, PeFeatures::IsPe, PanoMlTestResult::Malicious, PanoMlTestResult::Model, MlModel::Name(), PANO_API, PANO_ML_FEATURE_VERSION, PANO_ML_TEST_FAILED, PANO_ML_TEST_NO_MODEL, PANO_ML_TEST_OK, PANO_ML_THRESHOLD, MlModel::Score(), PanoMlTestResult::Score, PanoMlTestResult::StructSize, PanoMlTestResult::Threshold, and PeFeatures::Values.

◆ PanoUnbind()

PANO_API bool PanoUnbind ( )

Called once when the container shuts down.

Definition at line 198 of file dllmain.cpp.

199{
200 // No scan runs after this: the container calls it once, when it shuts down
201 delete g_Model;
202 g_Model = nullptr;
203 return true;
204}

References PANO_API.