Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
pe_features.cpp File Reference
#include "PanoptesML.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdexcept>

Go to the source code of this file.

Classes

class  FileView
 A read-only view of the file that refuses reads outside it. More...
struct  PeLayout
 The parts of a PE the features need. More...

Functions

const char * FeatureName (size_t Index)
 The name of feature Index, or nullptr when it is out of range.
PeFeatures ExtractPeFeatures (const uint8_t *Data, size_t Size)
 The same, from a file's bytes (used by ExtractPeFeatures and the tests).
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.

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}
@ 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
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().