Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
PanoptesML.h File Reference
#include "ExtensibilityCore.h"
#include <cstdint>
#include <memory>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  PeFeatures
class  MlModel
 An ONNX model that scores a feature vector. The model must take one float tensor of shape [1, FEATURE_COUNT] (or [N, FEATURE_COUNT]) and output a float tensor whose last value is the malicious probability: [1, 1] or [1, 2] (benign, malicious). See README.md. More...
struct  MlScanResult
 One file's result, as sent to the service. More...
struct  PanoMlTestResult
 One file's result for a test tool. Every field has a fixed size, so a caller in any language can declare the same layout. More...
class  PanoptesServiceClient
 The Panoptes Service client used to talk to the service over gRPC. More...

Macros

#define PANO_ML_VERSION   "1.0.0"
#define PANO_ML_MODEL_FILE   L"model.onnx"
 The model file, loaded from the folder PanoptesML.dll is in (ext).
#define PANO_ML_THRESHOLD   0.5f
 Score at or above which a file is reported as malicious.
#define PANO_ML_MAX_FILE_SIZE   (256ULL * 1024 * 1024)
 Files larger than this are not scored (the features are computed over the whole file).
#define PANO_ML_FEATURE_VERSION   1
 Version of the feature layout below. A model is trained for one layout; change this whenever a feature is added, removed or reordered, and retrain.
#define PANO_ML_MODEL_RESOURCE   L"PANOML_MODEL"
 Name of the embedded model's resource (type RCDATA).
#define PANO_ML_TEST_OK   0
 PanoMlScoreFile results.
#define PANO_ML_TEST_FAILED   -1
#define PANO_ML_TEST_NO_MODEL   -2

Enumerations

enum  PanoMlFeature : size_t {
  FEATURE_BYTE_HISTOGRAM = 0 , FEATURE_ENTROPY = 256 , FEATURE_LOG_SIZE , FEATURE_IS_PE ,
  FEATURE_IS_64BIT , FEATURE_IS_DLL , FEATURE_SUBSYSTEM_GUI , FEATURE_SECTION_COUNT ,
  FEATURE_SECTION_ENTROPY_MAX , FEATURE_SECTION_ENTROPY_MIN , FEATURE_SECTION_ENTROPY_MEAN , FEATURE_WRITABLE_EXECUTABLE_SECTIONS ,
  FEATURE_ENTRY_OUTSIDE_EXECUTABLE , FEATURE_IMPORTED_DLLS , FEATURE_IMPORTED_FUNCTIONS , FEATURE_HAS_SIGNATURE ,
  FEATURE_HAS_TLS , FEATURE_HAS_DEBUG , FEATURE_HAS_RELOCATIONS , FEATURE_HAS_RESOURCES ,
  FEATURE_OVERLAY_RATIO , FEATURE_IMAGE_TO_FILE_RATIO , FEATURE_ZERO_TIMESTAMP , FEATURE_COUNT
}
 The static features of one file, in the order the model takes them. See README.md for what each index means. Every value is a float, most scaled to about 0..1. More...

Functions

PeFeatures ExtractPeFeatures (const std::wstring &Path)
 Reads a file and computes its static features. The file is only read, never run or loaded. Throws std::runtime_error when the file can't be read or is too large.
PeFeatures ExtractPeFeatures (const uint8_t *Data, size_t Size)
 The same, from a file's bytes (used by ExtractPeFeatures and the tests).
bool LoadEmbeddedModel (std::vector< uint8_t > &Model)
 The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Model. Returns false when the DLL was built without one, so model.onnx is loaded from its folder instead. Throws std::runtime_error when the embedded model can't be decrypted (modified or corrupted).
std::wstring ModuleDirectory ()
 The folder this DLL was loaded from, with a trailing backslash.
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.
const char * FeatureName (size_t Index)
 The name of feature Index, or nullptr when it is out of range.

Macro Definition Documentation

◆ PANO_ML_FEATURE_VERSION

#define PANO_ML_FEATURE_VERSION   1

Version of the feature layout below. A model is trained for one layout; change this whenever a feature is added, removed or reordered, and retrain.

Definition at line 23 of file PanoptesML.h.

Referenced by PanoMlScoreFile(), and PanoptesServiceClient::SendResults_ML().

◆ PANO_ML_MAX_FILE_SIZE

#define PANO_ML_MAX_FILE_SIZE   (256ULL * 1024 * 1024)

Files larger than this are not scored (the features are computed over the whole file).

Definition at line 17 of file PanoptesML.h.

Referenced by ExtractPeFeatures().

◆ PANO_ML_MODEL_FILE

#define PANO_ML_MODEL_FILE   L"model.onnx"

The model file, loaded from the folder PanoptesML.dll is in (ext).

Definition at line 11 of file PanoptesML.h.

◆ PANO_ML_MODEL_RESOURCE

#define PANO_ML_MODEL_RESOURCE   L"PANOML_MODEL"

Name of the embedded model's resource (type RCDATA).

Definition at line 103 of file PanoptesML.h.

◆ PANO_ML_TEST_FAILED

#define PANO_ML_TEST_FAILED   -1

Definition at line 143 of file PanoptesML.h.

Referenced by PanoMlScoreFile().

◆ PANO_ML_TEST_NO_MODEL

#define PANO_ML_TEST_NO_MODEL   -2

Definition at line 144 of file PanoptesML.h.

Referenced by PanoMlScoreFile().

◆ PANO_ML_TEST_OK

#define PANO_ML_TEST_OK   0

PanoMlScoreFile results.

Definition at line 142 of file PanoptesML.h.

Referenced by PanoMlScoreFile().

◆ PANO_ML_THRESHOLD

#define PANO_ML_THRESHOLD   0.5f

Score at or above which a file is reported as malicious.

Definition at line 14 of file PanoptesML.h.

Referenced by PanoEntry(), PanoMlScoreFile(), and PanoptesServiceClient::SendResults_ML().

◆ PANO_ML_VERSION

#define PANO_ML_VERSION   "1.0.0"

Definition at line 8 of file PanoptesML.h.

Enumeration Type Documentation

◆ PanoMlFeature

enum PanoMlFeature : size_t

The static features of one file, in the order the model takes them. See README.md for what each index means. Every value is a float, most scaled to about 0..1.

Enumerator
FEATURE_BYTE_HISTOGRAM 
FEATURE_ENTROPY 
FEATURE_LOG_SIZE 
FEATURE_IS_PE 
FEATURE_IS_64BIT 
FEATURE_IS_DLL 
FEATURE_SUBSYSTEM_GUI 
FEATURE_SECTION_COUNT 
FEATURE_SECTION_ENTROPY_MAX 
FEATURE_SECTION_ENTROPY_MIN 
FEATURE_SECTION_ENTROPY_MEAN 
FEATURE_WRITABLE_EXECUTABLE_SECTIONS 
FEATURE_ENTRY_OUTSIDE_EXECUTABLE 
FEATURE_IMPORTED_DLLS 
FEATURE_IMPORTED_FUNCTIONS 
FEATURE_HAS_SIGNATURE 
FEATURE_HAS_TLS 
FEATURE_HAS_DEBUG 
FEATURE_HAS_RELOCATIONS 
FEATURE_HAS_RESOURCES 
FEATURE_OVERLAY_RATIO 
FEATURE_IMAGE_TO_FILE_RATIO 
FEATURE_ZERO_TIMESTAMP 
FEATURE_COUNT 

Definition at line 27 of file PanoptesML.h.

27 : 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
32 FEATURE_IS_64BIT, // PE32+
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};
@ 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

Function Documentation

◆ ExtractPeFeatures() [1/2]

PeFeatures ExtractPeFeatures ( const std::wstring & Path)

Reads a file and computes its static features. The file is only read, never run or loaded. Throws std::runtime_error when the file can't be read or is too large.

Definition at line 326 of file pe_features.cpp.

327{
328 // Shared for read and write, so a file still open in the program that wrote it can be read
329 HANDLE file = CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
330 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
331 if (file == INVALID_HANDLE_VALUE) {
332 throw std::runtime_error("cannot open the file (error " + std::to_string(GetLastError()) + ")");
333 }
334
335 LARGE_INTEGER size{};
336 if (!GetFileSizeEx(file, &size)) {
337 CloseHandle(file);
338 throw std::runtime_error("cannot get the file's size");
339 }
340 if ((ULONGLONG)size.QuadPart > PANO_ML_MAX_FILE_SIZE) {
341 CloseHandle(file);
342 throw std::runtime_error("the file is larger than " + std::to_string(PANO_ML_MAX_FILE_SIZE) + " bytes");
343 }
344
345 std::vector<uint8_t> data((size_t)size.QuadPart);
346 size_t read = 0;
347 while (read < data.size()) {
348 DWORD chunk = (DWORD)std::min<size_t>(data.size() - read, 1 << 24);
349 DWORD got = 0;
350 if (!ReadFile(file, data.data() + read, chunk, &got, NULL) || got == 0) {
351 break; // the file shrank; score what was read
352 }
353 read += got;
354 }
355 CloseHandle(file);
356 data.resize(read);
357 return ExtractPeFeatures(data.data(), data.size());
358}
#define PANO_ML_MAX_FILE_SIZE
Files larger than this are not scored (the features are computed over the whole file).
Definition PanoptesML.h:17
PeFeatures ExtractPeFeatures(const uint8_t *Data, size_t Size)
The same, from a file's bytes (used by ExtractPeFeatures and the tests).

References ExtractPeFeatures(), and PANO_ML_MAX_FILE_SIZE.

Referenced by PanoEntry(), and PanoMlScoreFile().

◆ ExtractPeFeatures() [2/2]

PeFeatures ExtractPeFeatures ( const uint8_t * Data,
size_t Size )

The same, from a file's bytes (used by ExtractPeFeatures and the tests).

Definition at line 233 of file pe_features.cpp.

234{
235 PeFeatures features;
236 std::vector<float>& f = features.Values;
237 f.assign(FEATURE_COUNT, 0.0f);
238 FileView file(Data, Size);
239
240 // Byte statistics: computed for every file, PE or not
241 if (Size > 0) {
242 size_t counts[256] = {};
243 for (size_t i = 0; i < Size; i++) {
244 counts[Data[i]]++;
245 }
246 for (size_t b = 0; b < 256; b++) {
247 f[FEATURE_BYTE_HISTOGRAM + b] = (float)((double)counts[b] / (double)Size);
248 }
249 }
250 f[FEATURE_ENTROPY] = Entropy(Data, Size) / 8.0f;
251 f[FEATURE_LOG_SIZE] = (float)(std::log2((double)Size + 1) / 32.0);
252
253 PeLayout pe;
254 if (!ParsePe(file, &pe)) {
255 return features;
256 }
257 features.IsPe = true;
258 f[FEATURE_IS_PE] = 1;
259 f[FEATURE_IS_64BIT] = pe.Is64Bit ? 1.0f : 0.0f;
260 f[FEATURE_IS_DLL] = (pe.FileHeader.Characteristics & IMAGE_FILE_DLL) ? 1.0f : 0.0f;
261 f[FEATURE_SUBSYSTEM_GUI] = pe.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI ? 1.0f : 0.0f;
262 f[FEATURE_ZERO_TIMESTAMP] = pe.FileHeader.TimeDateStamp == 0 ? 1.0f : 0.0f;
263 f[FEATURE_SECTION_COUNT] = std::min((float)pe.Sections.size() / 16.0f, 4.0f);
264
265 // Sections: entropy of each one's raw data, W+X sections, where the entry point is, and
266 // how much of the file lies past the last section (the overlay)
267 float entropyMax = 0, entropyMin = 8, entropySum = 0;
268 size_t withData = 0, writableExecutable = 0;
269 bool entryInExecutable = false;
270 uint64_t endOfSections = 0;
271 for (const auto& section : pe.Sections) {
272 DWORD flags = section.Characteristics;
273 bool executable = (flags & IMAGE_SCN_MEM_EXECUTE) != 0;
274 if (executable && (flags & IMAGE_SCN_MEM_WRITE)) {
275 writableExecutable++;
276 }
277 DWORD virtualSize = std::max(section.Misc.VirtualSize, section.SizeOfRawData);
278 if (executable && pe.AddressOfEntryPoint >= section.VirtualAddress &&
279 pe.AddressOfEntryPoint - section.VirtualAddress < virtualSize) {
280 entryInExecutable = true;
281 }
282
283 uint64_t start = section.PointerToRawData;
284 uint64_t length = section.SizeOfRawData;
285 if (length == 0 || !file.Contains(start, 1)) {
286 continue;
287 }
288 length = std::min<uint64_t>(length, Size - start);
289 endOfSections = std::max(endOfSections, start + length);
290 float entropy = Entropy(Data + start, (size_t)length);
291 entropyMax = std::max(entropyMax, entropy);
292 entropyMin = std::min(entropyMin, entropy);
293 entropySum += entropy;
294 withData++;
295 }
296 if (withData > 0) {
297 f[FEATURE_SECTION_ENTROPY_MAX] = entropyMax / 8.0f;
298 f[FEATURE_SECTION_ENTROPY_MIN] = entropyMin / 8.0f;
299 f[FEATURE_SECTION_ENTROPY_MEAN] = entropySum / (float)withData / 8.0f;
300 }
301 if (!pe.Sections.empty()) {
302 f[FEATURE_WRITABLE_EXECUTABLE_SECTIONS] = (float)writableExecutable / (float)pe.Sections.size();
303 }
304 // A DLL without an entry point has AddressOfEntryPoint 0, which is not suspicious
305 f[FEATURE_ENTRY_OUTSIDE_EXECUTABLE] = (pe.AddressOfEntryPoint != 0 && !entryInExecutable) ? 1.0f : 0.0f;
306 if (Size > 0 && endOfSections < Size) {
307 f[FEATURE_OVERLAY_RATIO] = (float)((double)(Size - endOfSections) / (double)Size);
308 }
309 if (Size > 0) {
310 f[FEATURE_IMAGE_TO_FILE_RATIO] = (float)(std::min((double)pe.SizeOfImage / (double)Size, 16.0) / 16.0);
311 }
312
313 uint32_t dlls = 0, functions = 0;
314 CountImports(pe, file, &dlls, &functions);
315 f[FEATURE_IMPORTED_DLLS] = (float)(std::log2((double)dlls + 1) / 8.0);
316 f[FEATURE_IMPORTED_FUNCTIONS] = (float)(std::log2((double)functions + 1) / 12.0);
317
318 f[FEATURE_HAS_SIGNATURE] = Directory(pe, IMAGE_DIRECTORY_ENTRY_SECURITY) ? 1.0f : 0.0f;
319 f[FEATURE_HAS_TLS] = Directory(pe, IMAGE_DIRECTORY_ENTRY_TLS) ? 1.0f : 0.0f;
320 f[FEATURE_HAS_DEBUG] = Directory(pe, IMAGE_DIRECTORY_ENTRY_DEBUG) ? 1.0f : 0.0f;
321 f[FEATURE_HAS_RELOCATIONS] = Directory(pe, IMAGE_DIRECTORY_ENTRY_BASERELOC) ? 1.0f : 0.0f;
322 f[FEATURE_HAS_RESOURCES] = Directory(pe, IMAGE_DIRECTORY_ENTRY_RESOURCE) ? 1.0f : 0.0f;
323 return features;
324}
A read-only view of the file that refuses reads outside it.
std::vector< float > Values
Definition PanoptesML.h:55
The parts of a PE the features need.
DWORD AddressOfEntryPoint
DWORD SizeOfImage
std::vector< IMAGE_SECTION_HEADER > Sections
WORD Subsystem
IMAGE_FILE_HEADER FileHeader

References PeLayout::AddressOfEntryPoint, FileView::Contains(), FEATURE_BYTE_HISTOGRAM, FEATURE_COUNT, FEATURE_ENTROPY, FEATURE_ENTRY_OUTSIDE_EXECUTABLE, FEATURE_HAS_DEBUG, FEATURE_HAS_RELOCATIONS, FEATURE_HAS_RESOURCES, FEATURE_HAS_SIGNATURE, FEATURE_HAS_TLS, FEATURE_IMAGE_TO_FILE_RATIO, FEATURE_IMPORTED_DLLS, FEATURE_IMPORTED_FUNCTIONS, FEATURE_IS_64BIT, FEATURE_IS_DLL, FEATURE_IS_PE, FEATURE_LOG_SIZE, FEATURE_OVERLAY_RATIO, FEATURE_SECTION_COUNT, FEATURE_SECTION_ENTROPY_MAX, FEATURE_SECTION_ENTROPY_MEAN, FEATURE_SECTION_ENTROPY_MIN, FEATURE_SUBSYSTEM_GUI, FEATURE_WRITABLE_EXECUTABLE_SECTIONS, FEATURE_ZERO_TIMESTAMP, PeLayout::FileHeader, PeLayout::Is64Bit, PeFeatures::IsPe, PeLayout::Sections, PeLayout::SizeOfImage, PeLayout::Subsystem, and PeFeatures::Values.

Referenced by ExtractPeFeatures().

◆ FeatureName()

const char * FeatureName ( size_t Index)

The name of feature Index, or nullptr when it is out of range.

Definition at line 22 of file pe_features.cpp.

23{
24 // byte_00 .. byte_ff for the histogram, built once
25 static const std::vector<std::string> names = [] {
26 std::vector<std::string> all;
27 all.reserve(FEATURE_COUNT);
28 char name[16];
29 for (int value = 0; value < 256; value++) {
30 snprintf(name, sizeof(name), "byte_%02x", value);
31 all.emplace_back(name);
32 }
33 all.insert(all.end(), kNamedFeatures.begin(), kNamedFeatures.end());
34 return all;
35 }();
36 return Index < names.size() ? names[Index].c_str() : nullptr;
37}

References FEATURE_COUNT.

Referenced by PanoMlFeatureName().

◆ LoadEmbeddedModel()

bool LoadEmbeddedModel ( std::vector< uint8_t > & Model)

The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Model. Returns false when the DLL was built without one, so model.onnx is loaded from its folder instead. Throws std::runtime_error when the embedded model can't be decrypted (modified or corrupted).

Definition at line 111 of file model_seal.cpp.

112{
113 return false; // built without assets/ml/model.onnx, or with PANOPTES_ML_EMBED_MODEL off
114}

◆ ModuleDirectory()

std::wstring ModuleDirectory ( )

The folder this DLL was loaded from, with a trailing backslash.

Definition at line 122 of file ml_model.cpp.

123{
124 HMODULE module = NULL;
125 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
126 (LPCWSTR)&ModuleDirectory, &module);
127 WCHAR path[MAX_PATH];
128 DWORD length = GetModuleFileNameW(module, path, MAX_PATH);
129 std::wstring directory(path, length);
130 return directory.substr(0, directory.find_last_of(L'\\') + 1);
131}
std::wstring ModuleDirectory()
The folder this DLL was loaded from, with a trailing backslash.
Definition ml_model.cpp:122
#define MAX_PATH
Definition callbacks.h:7

References MAX_PATH, and ModuleDirectory().

Referenced by ModuleDirectory().

◆ 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_THRESHOLD
Score at or above which a file is reported as malicious.
Definition PanoptesML.h:14
#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
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
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.