Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
yara-scan.cpp
Go to the documentation of this file.
1#include "PanoptesYara.h"
2#include <iostream>
3#include <fstream>
4#include <stdexcept>
5#include <memory>
6
10std::vector<uint8_t> readFileToBuffer(const std::string& filename) {
11 std::ifstream file(filename, std::ios::binary | std::ios::ate);
12 std::vector<uint8_t> buffer;
13
14 if (!file.is_open()) {
15 std::cerr << "Error: Could not open file " << filename << " for reading." << std::endl;
16 return buffer; // Return empty buffer
17 }
18
19 // Get the file size
20 std::streamsize size = file.tellg();
21 file.seekg(0, std::ios::beg);
22
23 // Reserve space in the buffer
24 buffer.resize(size);
25
26 // Read the file
27 if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
28 std::cerr << "Error: Failed to read data from file " << filename << std::endl;
29 buffer.clear(); // Clear the buffer on error
30 }
31
32 file.close();
33 return buffer;
34}
35
38YaraScanner::YaraScanner(const char* rulesPath){
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}
52
55 if (g_yaraRules != nullptr) {
56 yrx_rules_destroy(g_yaraRules);
57 g_yaraRules = nullptr;
58 }
59}
60
64void matchingRule(const struct YRX_RULE* rule, void* user_data) {
65 const uint8_t* ns;
66 size_t ns_len;
67 const uint8_t* ident;
68 size_t ident_len;
69 std::vector<string>* detectedRules = (std::vector<string>*)user_data;
70
71 //yrx_rule_iter_metadata(rule, metaCallback, user_data);
72 yrx_rule_namespace(rule, &ns, &ns_len);
73 yrx_rule_identifier(rule, &ident, &ident_len);
74
75 detectedRules->push_back(std::string(ns, ns + ns_len) + "::" +
76 std::string(ident, ident + ident_len));
77}
78
82std::vector<string> YaraScanner::YaraScanFile(std::string file_path)
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}
YaraScanner(const char *Rules)
Intializes Yara memory and attempts to load supplied yara rules.
Definition yara-scan.cpp:38
std::vector< std::string > YaraScanFile(std::string PathToFile)
Scan a file using YARA rules.
Definition yara-scan.cpp:82
~YaraScanner()
Destructor for the YaraScanner class that destroys the YARA rules.
Definition yara-scan.cpp:54
std::vector< uint8_t > readFileToBuffer(const std::string &filename)
Read a file to a buffer.
Definition yara-scan.cpp:10
void matchingRule(const struct YRX_RULE *rule, void *user_data)
Callback function for the YARA rules.
Definition yara-scan.cpp:64