Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
model_seal.cpp
Go to the documentation of this file.
1// The model embedded in the DLL: an RCDATA resource holding model.onnx encrypted with AES-256-GCM
2// by tools/sealmodel at build time. This keeps the model from being copied off a machine or read
3// as a file, and a modified model fails GCM authentication instead of loading. It doesn't make
4// the model secret from someone who reverse-engineers the DLL: the key is compiled in (masked,
5// from the generated model_key.h), and the plain model is in memory while ONNX Runtime loads it.
6#include "PanoptesML.h"
7#include <bcrypt.h>
8#include <cstdio>
9#include <cstring>
10#include <stdexcept>
11
12#pragma comment(lib, "bcrypt.lib")
13
14#ifdef PANO_ML_EMBEDDED_MODEL
15#include "model_key.h" // generated with the sealed model, in the build folder
16
17namespace {
18 constexpr char kMagic[4] = { 'P', 'M', 'L', 'E' };
19 constexpr uint32_t kVersion = 1;
20 constexpr size_t kHeaderSize = 8, kNonceSize = 12, kTagSize = 16, kKeySize = 32;
21 static_assert(sizeof(g_ModelKeyMasked) == kKeySize && sizeof(g_ModelKeyMask) == kKeySize);
22
23 struct Algorithm {
24 BCRYPT_ALG_HANDLE Handle = NULL;
25 ~Algorithm() { if (Handle) BCryptCloseAlgorithmProvider(Handle, 0); }
26 };
27 struct Key {
28 BCRYPT_KEY_HANDLE Handle = NULL;
29 ~Key() { if (Handle) BCryptDestroyKey(Handle); }
30 };
31
32 void Check(NTSTATUS Status, const char* What)
33 {
34 if (Status < 0) {
35 char text[96];
36 snprintf(text, sizeof(text), "embedded model: %s failed (NTSTATUS 0x%08lx)", What, (unsigned long)Status);
37 throw std::runtime_error(text);
38 }
39 }
40}
41
42bool LoadEmbeddedModel(std::vector<uint8_t>& Model)
43{
44 HMODULE module = NULL;
45 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
46 (LPCWSTR)&LoadEmbeddedModel, &module);
47 HRSRC resource = FindResourceW(module, PANO_ML_MODEL_RESOURCE, RT_RCDATA);
48 if (resource == NULL) {
49 return false;
50 }
51 HGLOBAL loaded = LoadResource(module, resource);
52 const uint8_t* sealed = loaded ? (const uint8_t*)LockResource(loaded) : nullptr;
53 size_t size = SizeofResource(module, resource);
54 uint32_t version = 0;
55 if (sealed && size >= kHeaderSize) {
56 memcpy(&version, sealed + 4, sizeof(version));
57 }
58 if (!sealed || size < kHeaderSize + kNonceSize + kTagSize || memcmp(sealed, kMagic, 4) != 0 || version != kVersion) {
59 throw std::runtime_error("embedded model: not a sealed model (version 1)");
60 }
61 const uint8_t* nonce = sealed + kHeaderSize;
62 const uint8_t* cipherText = nonce + kNonceSize;
63 size_t cipherSize = size - kHeaderSize - kNonceSize - kTagSize;
64 const uint8_t* tag = cipherText + cipherSize;
65
66 // The key only exists in this buffer, briefly
67 uint8_t keyBytes[kKeySize];
68 for (size_t i = 0; i < kKeySize; i++) {
69 keyBytes[i] = ((volatile unsigned char*)g_ModelKeyMasked)[i] ^ ((volatile unsigned char*)g_ModelKeyMask)[i];
70 }
71 Algorithm algorithm;
72 Key key;
73 NTSTATUS status = BCryptOpenAlgorithmProvider(&algorithm.Handle, BCRYPT_AES_ALGORITHM, NULL, 0);
74 if (status >= 0) {
75 status = BCryptSetProperty(algorithm.Handle, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM,
76 sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
77 }
78 if (status >= 0) {
79 status = BCryptGenerateSymmetricKey(algorithm.Handle, &key.Handle, NULL, 0, keyBytes, kKeySize, 0);
80 }
81 SecureZeroMemory(keyBytes, sizeof(keyBytes));
82 Check(status, "AES-GCM setup");
83
84 BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info;
85 BCRYPT_INIT_AUTH_MODE_INFO(info);
86 info.pbNonce = (PUCHAR)nonce;
87 info.cbNonce = (ULONG)kNonceSize;
88 info.pbTag = (PUCHAR)tag;
89 info.cbTag = (ULONG)kTagSize;
90 info.pbAuthData = (PUCHAR)sealed; // the magic and version
91 info.cbAuthData = (ULONG)kHeaderSize;
92
93 Model.resize(cipherSize);
94 ULONG written = 0;
95 status = BCryptDecrypt(key.Handle, (PUCHAR)cipherText, (ULONG)cipherSize, &info, NULL, 0,
96 Model.data(), (ULONG)Model.size(), &written, 0);
97 if (status < 0) {
98 SecureZeroMemory(Model.data(), Model.size());
99 Model.clear();
100 if (status == (NTSTATUS)0xC000A002L) { // STATUS_AUTH_TAG_MISMATCH
101 throw std::runtime_error("embedded model: authentication failed; the DLL's model was modified or corrupted");
102 }
103 Check(status, "decryption");
104 }
105 Model.resize(written);
106 return true;
107}
108
109#else
110
111bool LoadEmbeddedModel(std::vector<uint8_t>&)
112{
113 return false; // built without assets/ml/model.onnx, or with PANOPTES_ML_EMBED_MODEL off
114}
115
116#endif
#define PANO_ML_MODEL_RESOURCE
Name of the embedded model's resource (type RCDATA).
Definition PanoptesML.h:103
bool LoadEmbeddedModel(std::vector< uint8_t > &Model)
The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Mode...
bool LoadEmbeddedModel(std::vector< uint8_t > &)
The model built into the DLL (an encrypted RCDATA resource, see tools/sealmodel), decrypted into Mode...