Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
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.
 
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 20 of file PanoptesYara.h.

Constructor & Destructor Documentation

◆ YaraScanner()

YaraScanner::YaraScanner ( const char *  rulesPath)

Intializes Yara memory and attempts to load supplied yara rules.

Parameters
rulesPath

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

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

References readFileToBuffer(), and result.

Referenced by PanoEntry().

◆ ~YaraScanner()

YaraScanner::~YaraScanner ( )

Destructor for the YaraScanner class that destroys the YARA rules.

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

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

Member Function Documentation

◆ 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 81 of file yara-scan.cpp.

82{
83 std::vector<string> detectedRules;
84 YRX_RESULT result = YRX_SUCCESS;
85 YRX_SCANNER* scanner = nullptr;
86
87 if (g_yaraRules == nullptr) {
88 throw std::runtime_error("YARA rules not initialized");
89 }
90
91 try {
92 result = yrx_scanner_create(g_yaraRules, &scanner);
93 if (result != YRX_SUCCESS) {
94 throw std::runtime_error("Failed to create YARA scanner");
95 }
96
97 result = yrx_scanner_on_matching_rule(scanner, matchingRule, &detectedRules);
98 if (result != YRX_SUCCESS) {
99 if (scanner != nullptr) {
100 yrx_scanner_destroy(scanner);
101 }
102 throw std::runtime_error("Failed to set matching rule callback");
103 }
104
105 std::vector<uint8_t> scanBuffer = readFileToBuffer(file_path);
106 if (scanBuffer.empty()) {
107 if (scanner != nullptr) {
108 yrx_scanner_destroy(scanner);
109 }
110 throw std::runtime_error("Failed to read file for scanning");
111 }
112
113 result = yrx_scanner_scan(scanner, scanBuffer.data(), scanBuffer.size());
114 if (result != YRX_SUCCESS) {
115 if (scanner != nullptr) {
116 yrx_scanner_destroy(scanner);
117 }
118 throw std::runtime_error("Failed to scan file");
119 }
120 }
121 catch (...) {
122 if (scanner != nullptr) {
123 yrx_scanner_destroy(scanner);
124 }
125 throw; // Re-throw the exception after cleanup
126 }
127
128 // Clean up resources
129 if (scanner != nullptr) {
130 yrx_scanner_destroy(scanner);
131 }
132
133 return detectedRules;
134}
void matchingRule(const struct YRX_RULE *rule, void *user_data)
Callback function for the YARA rules.
Definition yara-scan.cpp:63

References matchingRule(), readFileToBuffer(), and result.

Referenced by PanoEntry(), and Yara::TEST_F().


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