Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
PanoptesML.h
Go to the documentation of this file.
1#pragma once
2#include "ExtensibilityCore.h"
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <vector>
7
8#define PANO_ML_VERSION "1.0.0"
9
11#define PANO_ML_MODEL_FILE L"model.onnx"
12
14#define PANO_ML_THRESHOLD 0.5f
15
17#define PANO_ML_MAX_FILE_SIZE (256ULL * 1024 * 1024)
18
19#pragma region Features
20
23#define PANO_ML_FEATURE_VERSION 1
24
27enum PanoMlFeature : size_t {
28 FEATURE_BYTE_HISTOGRAM = 0, // 256 values: how often each byte value occurs, / file size
29 FEATURE_ENTROPY = 256, // Shannon entropy of the whole file, / 8
30 FEATURE_LOG_SIZE, // log2(file size + 1) / 32
31 FEATURE_IS_PE, // 1 if the file parsed as a PE
33 FEATURE_IS_DLL, // IMAGE_FILE_DLL
34 FEATURE_SUBSYSTEM_GUI, // IMAGE_SUBSYSTEM_WINDOWS_GUI
35 FEATURE_SECTION_COUNT, // number of sections / 16
36 FEATURE_SECTION_ENTROPY_MAX, // highest section entropy / 8
37 FEATURE_SECTION_ENTROPY_MIN, // lowest section entropy / 8
38 FEATURE_SECTION_ENTROPY_MEAN, // mean section entropy / 8
39 FEATURE_WRITABLE_EXECUTABLE_SECTIONS,// sections both writable and executable / section count
40 FEATURE_ENTRY_OUTSIDE_EXECUTABLE, // 1 if the entry point is not in an executable section
41 FEATURE_IMPORTED_DLLS, // log2(imported DLLs + 1) / 8
42 FEATURE_IMPORTED_FUNCTIONS, // log2(imported functions + 1) / 12
43 FEATURE_HAS_SIGNATURE, // security directory present (Authenticode, not verified)
44 FEATURE_HAS_TLS, // TLS directory present (TLS callbacks run before the entry point)
45 FEATURE_HAS_DEBUG, // debug directory present
46 FEATURE_HAS_RELOCATIONS, // base relocations present
47 FEATURE_HAS_RESOURCES, // resource directory present
48 FEATURE_OVERLAY_RATIO, // bytes after the last section / file size
49 FEATURE_IMAGE_TO_FILE_RATIO, // SizeOfImage / file size, capped at 16, / 16
50 FEATURE_ZERO_TIMESTAMP, // TimeDateStamp is 0
52};
53
54struct PeFeatures {
55 std::vector<float> Values; // FEATURE_COUNT values
56 bool IsPe = false;
57};
58
61PeFeatures ExtractPeFeatures(const std::wstring& Path);
62
64PeFeatures ExtractPeFeatures(const uint8_t* Data, size_t Size);
65
66#pragma endregion
67
68#pragma region Model
69
73class MlModel {
74public:
77 explicit MlModel(const std::wstring& ModelPath);
79 MlModel(const void* Data, size_t Size, const std::string& Name);
81 MlModel(const MlModel&) = delete;
82 MlModel& operator=(const MlModel&) = delete;
83
85 float Score(const std::vector<float>& Features);
86
87 const std::string& Name() const { return m_name; }
88
89private:
90 struct Impl; // keeps onnxruntime_cxx_api.h out of this header
91 void Inspect(); // checks the loaded session's input and finds its score output
92 std::unique_ptr<Impl> m_impl;
93 std::string m_name;
94};
95
100bool LoadEmbeddedModel(std::vector<uint8_t>& Model);
101
103#define PANO_ML_MODEL_RESOURCE L"PANOML_MODEL"
104
105#pragma endregion
106
108std::wstring ModuleDirectory();
109
112 float Score = 0;
113 bool Malicious = false;
114 bool IsPe = false;
115 std::string Model;
116 std::string Error; // why the file wasn't scored; empty when it was
117};
118
119#pragma region Test interface
120
121// A plain C interface for test tools (tools/mlquery), separate from the container's PanoEntry:
122// PeScan holds std::string, which only code built with the same compiler settings can pass, and
123// PanoEntry sends its result to the service instead of returning it. These run the same feature
124// extraction and model, but return the result and talk to nothing.
125
129 uint32_t StructSize; // set by the caller to sizeof(PanoMlTestResult); any other value is refused
130 int32_t FeatureVersion; // PANO_ML_FEATURE_VERSION
131 int32_t FeatureCount; // FEATURE_COUNT
132 int32_t IsPe; // 1 if the file parsed as a PE
133 int32_t Malicious; // 1 if Score >= Threshold
134 float Score; // the model's malicious probability, 0..1
135 float Threshold; // PANO_ML_THRESHOLD
136 float Features[FEATURE_COUNT]; // the model's input, in PanoMlFeature order
137 char Model[128]; // the model's name (UTF-8, null-terminated)
138 char Error[512]; // why the call failed (UTF-8, null-terminated); empty on success
139};
140
142#define PANO_ML_TEST_OK 0
143#define PANO_ML_TEST_FAILED -1 // nothing usable: see Error (or StructSize was wrong)
144#define PANO_ML_TEST_NO_MODEL -2 // the features were computed but the model couldn't be loaded: see Error
145
146extern "C" {
150 PANO_API int PanoMlScoreFile(const wchar_t* Path, PanoMlTestResult* Result);
151
154 PANO_API int PanoMlFeatureName(int Index, char* Buffer, int BufferSize);
155}
156
158const char* FeatureName(size_t Index);
159
160#pragma endregion
161
164public:
166 bool Hello(ExtensibilityType ExtensibilityType, int ContainerPort);
167 bool SendResults_ML(const std::string& PePath, const std::string& FileHash, const MlScanResult& Result);
168};
#define PANO_API
ExtensibilityType
The type of extensibility.
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 fol...
Definition dllmain.cpp:141
std::wstring ModuleDirectory()
The folder this DLL was loaded from, with a trailing backslash.
Definition ml_model.cpp:122
bool LoadEmbeddedModel(std::vector< uint8_t > &Model)
The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Mode...
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.
Definition dllmain.cpp:181
const char * FeatureName(size_t Index)
The name of feature Index, or nullptr when it is out of range.
PanoMlFeature
The static features of one file, in the order the model takes them. See README.md for what each index...
Definition PanoptesML.h:27
@ FEATURE_IMPORTED_FUNCTIONS
Definition PanoptesML.h:42
@ FEATURE_HAS_RELOCATIONS
Definition PanoptesML.h:46
@ FEATURE_HAS_SIGNATURE
Definition PanoptesML.h:43
@ FEATURE_IS_PE
Definition PanoptesML.h:31
@ FEATURE_LOG_SIZE
Definition PanoptesML.h:30
@ FEATURE_IMPORTED_DLLS
Definition PanoptesML.h:41
@ FEATURE_ENTRY_OUTSIDE_EXECUTABLE
Definition PanoptesML.h:40
@ FEATURE_ENTROPY
Definition PanoptesML.h:29
@ FEATURE_IS_64BIT
Definition PanoptesML.h:32
@ FEATURE_SECTION_ENTROPY_MAX
Definition PanoptesML.h:36
@ FEATURE_HAS_TLS
Definition PanoptesML.h:44
@ FEATURE_IS_DLL
Definition PanoptesML.h:33
@ FEATURE_IMAGE_TO_FILE_RATIO
Definition PanoptesML.h:49
@ FEATURE_WRITABLE_EXECUTABLE_SECTIONS
Definition PanoptesML.h:39
@ FEATURE_HAS_RESOURCES
Definition PanoptesML.h:47
@ FEATURE_OVERLAY_RATIO
Definition PanoptesML.h:48
@ FEATURE_ZERO_TIMESTAMP
Definition PanoptesML.h:50
@ FEATURE_SUBSYSTEM_GUI
Definition PanoptesML.h:34
@ FEATURE_SECTION_ENTROPY_MIN
Definition PanoptesML.h:37
@ FEATURE_HAS_DEBUG
Definition PanoptesML.h:45
@ FEATURE_COUNT
Definition PanoptesML.h:51
@ FEATURE_SECTION_COUNT
Definition PanoptesML.h:35
@ FEATURE_BYTE_HISTOGRAM
Definition PanoptesML.h:28
@ FEATURE_SECTION_ENTROPY_MEAN
Definition PanoptesML.h:38
PeFeatures ExtractPeFeatures(const std::wstring &Path)
Reads a file and computes its static features. The file is only read, never run or loaded....
float Score(const std::vector< float > &Features)
Runs the model on one file's features. Returns the malicious probability.
Definition ml_model.cpp:99
MlModel(const MlModel &)=delete
MlModel & operator=(const MlModel &)=delete
MlModel(const std::wstring &ModelPath)
Loads the model. Throws Ort::Exception or std::runtime_error when it can't be loaded or doesn't take ...
Definition ml_model.cpp:44
const std::string & Name() const
Definition PanoptesML.h:87
The Panoptes Service client used to talk to the service over gRPC.
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.
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
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
std::vector< float > Values
Definition PanoptesML.h:55