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