Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
YaraScanner Class Reference

The YaraScanner class that is used to scan a file using YARA rules. More...

#include <PanoptesYara.h>

Classes

struct  ScanData
 The data that is sent to the Panoptes Service. More...

Public Member Functions

 YaraScanner (const char *Rules)
 Intializes Yara memory and attempts to load supplied yara rules.
 ~YaraScanner ()
 Destructor for the YaraScanner class that destroys the YARA rules.
 YaraScanner (const YaraScanner &)=delete
YaraScanner & operator= (const YaraScanner &)=delete
std::vector< std::string > YaraScanFile (std::string PathToFile)
 Scan a file using YARA rules.

Detailed Description

The YaraScanner class that is used to scan a file using YARA rules.

Definition at line 36 of file PanoptesYara.h.

Constructor & Destructor Documentation

◆ YaraScanner() [1/2]

YaraScanner::YaraScanner ( const char * rulesPath)

Intializes Yara memory and attempts to load supplied yara rules.

Parameters
rulesPath

Definition at line 38 of file yara-scan.cpp.

38 {
39 YRX_RESULT result = YRX_NOT_SUPPORTED;
40
41 auto readBuffer = readFileToBuffer(rulesPath);
42
43 if (readBuffer.empty()) {
44 throw std::runtime_error("Failed to read rules file");
45 }
46
47 result = yrx_rules_deserialize(readBuffer.data(), readBuffer.size(), &g_yaraRules);
48 if (result != YRX_SUCCESS) {
49 throw std::runtime_error("Failed to deserialize YARA rules");
50 }
51}
std::vector< uint8_t > readFileToBuffer(const std::string &filename)
Read a file to a buffer.
Definition yara-scan.cpp:10

References readFileToBuffer().

Referenced by operator=(), and YaraScanner().

◆ ~YaraScanner()

YaraScanner::~YaraScanner ( )

Destructor for the YaraScanner class that destroys the YARA rules.

Definition at line 54 of file yara-scan.cpp.

54 {
55 if (g_yaraRules != nullptr) {
56 yrx_rules_destroy(g_yaraRules);
57 g_yaraRules = nullptr;
58 }
59}

◆ YaraScanner() [2/2]

YaraScanner::YaraScanner ( const YaraScanner & )
delete

References YaraScanner().

Member Function Documentation

◆ operator=()

YaraScanner & YaraScanner::operator= ( const YaraScanner & )
delete

References YaraScanner().

◆ YaraScanFile()

std::vector< string > YaraScanner::YaraScanFile ( std::string file_path)

Scan a file using YARA rules.

Parameters
file_pathThe path to the file to scan
Returns
A vector of strings containing the detected rules

Definition at line 82 of file yara-scan.cpp.

83{
84 std::vector<string> detectedRules;
85
86 if (g_yaraRules == nullptr) {
87 throw std::runtime_error("YARA rules not initialized");
88 }
89
90 YRX_SCANNER* rawScanner = nullptr;
91 if (yrx_scanner_create(g_yaraRules, &rawScanner) != YRX_SUCCESS) {
92 throw std::runtime_error("Failed to create YARA scanner");
93 }
94 // The only owner: destroyed exactly once, on every path out, including exceptions. Each
95 // error path used to destroy it and then throw into a catch that destroyed it again, a
96 // double free that crashed the container (RtlReportCriticalFailure, c0000374)
97 std::unique_ptr<YRX_SCANNER, decltype(&yrx_scanner_destroy)> scanner(rawScanner, yrx_scanner_destroy);
98
99 if (yrx_scanner_on_matching_rule(scanner.get(), matchingRule, &detectedRules) != YRX_SUCCESS) {
100 throw std::runtime_error("Failed to set matching rule callback");
101 }
102
103 std::vector<uint8_t> scanBuffer = readFileToBuffer(file_path);
104 if (scanBuffer.empty()) {
105 // The file could not be read (often deleted or locked between being queued and
106 // scanned), or it is empty, which no rule can match
107 throw std::runtime_error("Failed to read file for scanning: " + file_path);
108 }
109
110 if (yrx_scanner_scan(scanner.get(), scanBuffer.data(), scanBuffer.size()) != YRX_SUCCESS) {
111 throw std::runtime_error("Failed to scan file: " + file_path);
112 }
113
114 return detectedRules;
115}
void matchingRule(const struct YRX_RULE *rule, void *user_data)
Callback function for the YARA rules.
Definition yara-scan.cpp:64

References matchingRule(), and readFileToBuffer().

Referenced by PanoEntry().


The documentation for this class was generated from the following files: