11static std::once_flag g_ModelLoad;
12static std::string g_ModelError;
19static MlModel* g_Model =
nullptr;
23 std::call_once(g_ModelLoad, [] {
26 std::wstring path = L
"the embedded model";
27 std::vector<uint8_t> embedded;
34 g_Model =
new MlModel(embedded.data(), embedded.size(),
"embedded model.onnx");
40 catch (
const std::exception& e) {
41 g_ModelError = e.what();
42 std::wcerr << L
"PanoptesML: cannot load " << path << L
": ";
43 std::cerr << e.what() << std::endl;
45 if (!embedded.empty()) {
46 SecureZeroMemory(embedded.data(), embedded.size());
52static std::wstring Widen(
const std::string& Text)
54 int length = MultiByteToWideChar(CP_UTF8, 0, Text.c_str(), (
int)Text.size(), NULL, 0);
55 std::wstring out(length, L
'\0');
56 MultiByteToWideChar(CP_UTF8, 0, Text.c_str(), (
int)Text.size(), out.data(), length);
60BOOL APIENTRY
DllMain(HMODULE module, DWORD reason, LPVOID reserved)
62 UNREFERENCED_PARAMETER(reserved);
63 if (reason == DLL_PROCESS_ATTACH) {
64 DisableThreadLibraryCalls(module);
94 if (model ==
nullptr) {
95 result.
Error =
"cannot load the model: " + g_ModelError;
101 if (!features.
IsPe) {
104 result.
Error =
"not a PE file; the model only scores PE files";
113 catch (
const std::exception& e) {
116 result.
Error = e.what();
117 std::cerr <<
"PanoptesML scan of " << data->
PePath <<
" failed: " << e.what() << std::endl;
124 catch (
const std::exception& e) {
125 std::cerr <<
"PanoptesML: reporting " << data->
PePath <<
" failed: " << e.what() << std::endl;
130#pragma region Test interface
134 "PanoMlTestResult changed: update tools/mlquery/main.go");
136static void CopyText(
char* Destination,
size_t Size,
const std::string& Text)
138 strncpy_s(Destination, Size, Text.c_str(), _TRUNCATE);
147 ZeroMemory(Result,
sizeof(*Result));
152 if (Path ==
nullptr) {
153 CopyText(Result->
Error,
sizeof(Result->
Error),
"no path");
161 Result->
IsPe = features.
IsPe ? 1 : 0;
164 if (model ==
nullptr) {
165 CopyText(Result->
Error,
sizeof(Result->
Error),
"cannot load the model: " + g_ModelError);
175 catch (
const std::exception& e) {
176 CopyText(Result->
Error,
sizeof(Result->
Error), e.what());
183 const char* name = Index >= 0 ?
FeatureName((
size_t)Index) :
nullptr;
184 if (name ==
nullptr || Buffer ==
nullptr) {
187 size_t length = strlen(name);
188 if (BufferSize <= 0 || length >= (
size_t)BufferSize) {
191 memcpy(Buffer, name, length + 1);
#define PANO_ML_THRESHOLD
Score at or above which a file is reported as malicious.
#define PANO_ML_TEST_OK
PanoMlScoreFile results.
std::wstring ModuleDirectory()
The folder this DLL was loaded from, with a trailing backslash.
#define PANO_ML_FEATURE_VERSION
Version of the feature layout below. A model is trained for one layout; change this whenever a featur...
bool LoadEmbeddedModel(std::vector< uint8_t > &Model)
The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Mode...
#define PANO_ML_TEST_FAILED
#define PANO_ML_MODEL_FILE
The model file, loaded from the folder PanoptesML.dll is in (ext).
#define PANO_ML_TEST_NO_MODEL
const char * FeatureName(size_t Index)
The name of feature Index, or nullptr when it is out of range.
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,...
float Score(const std::vector< float > &Features)
Runs the model on one file's features. Returns the malicious probability.
const std::string & Name() const
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.
ExtensibilityCore::PanoBindPtr PanoBind
ExtensibilityCore::PanoEntryPtr PanoEntry
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...
PANO_API bool PanoUnbind()
Called once when the container shuts down.
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.
BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved)
One file's result, as sent to the service.
One file's result for a test tool. Every field has a fixed size, so a caller in any language can decl...
float Features[FEATURE_COUNT]
std::vector< float > Values
The information about the file to be scanned that passed between the container, extensibility and the...