Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Configuration.cpp
Go to the documentation of this file.
1#include <Windows.h>
2#include "Configuration.hpp"
3#include "ResourceCore.h"
4// #include "panoptes.pb.h"
5#include <vector>
6#include <sstream>
7#include <algorithm>
8#include <cctype>
9
10#pragma region Path Patterns
11
12static std::string NormalizeSeparators(std::string path)
13{
14 std::replace(path.begin(), path.end(), '/', '\\');
15 return path;
16}
17
18static std::vector<std::string> NormalizePatterns(std::vector<std::string> patterns)
19{
20 for (auto& pattern : patterns) {
21 pattern = NormalizeSeparators(pattern);
22 }
23 return patterns;
24}
25
26static bool CharEqualsIgnoreCase(char a, char b)
27{
28 return std::tolower((unsigned char)a) == std::tolower((unsigned char)b);
29}
30
31// Matches the whole of text against a pattern of literal characters, * and ?. On a mismatch
32// it backtracks to the most recent * and lets it absorb one more character
33static bool WildcardMatch(const std::string& pattern, const std::string& text)
34{
35 size_t p = 0;
36 size_t t = 0;
37 size_t star = std::string::npos;
38 size_t starText = 0;
39
40 while (t < text.size()) {
41 if (p < pattern.size() && pattern[p] == '*') {
42 star = p++;
43 starText = t;
44 }
45 else if (p < pattern.size() && (pattern[p] == '?' || CharEqualsIgnoreCase(pattern[p], text[t]))) {
46 p++;
47 t++;
48 }
49 else if (star != std::string::npos) {
50 p = star + 1;
51 t = ++starText;
52 }
53 else {
54 return false;
55 }
56 }
57
58 while (p < pattern.size() && pattern[p] == '*') {
59 p++;
60 }
61 return p == pattern.size();
62}
63
64bool Configuration::IsPathInList(const std::vector<std::string>& patterns, const std::string& path)
65{
66 std::string normalizedPath = NormalizeSeparators(path);
67
68 for (const auto& pattern : patterns) {
69 if (pattern.empty()) {
70 continue;
71 }
72
73 if (pattern.find_first_of("*?") != std::string::npos) {
74 if (WildcardMatch(pattern, normalizedPath)) {
75 return true;
76 }
77 }
78 else if (normalizedPath.size() >= pattern.size() &&
79 _strnicmp(normalizedPath.c_str(), pattern.c_str(), pattern.size()) == 0) {
80 return true;
81 }
82 }
83 return false;
84}
85
86#pragma endregion
87
90Configuration::Configuration(std::string configurationPath)
91{
92 m_fileStream = std::ifstream(configurationPath);
93}
94
106
109{
110 m_data = nlohmann::json::parse(m_fileStream);
111}
112
123
124const std::vector<std::string>& Configuration::FileEventNames()
125{
126 // The service maps each one to a PANO_FILE_EVENT_GROUP bit (driver.cpp)
127 static const std::vector<std::string> names = {
128 "Create", // files created, overwritten or superseded
129 "Write", // one FileModified event per handle that wrote
130 "DeleteRename", // deletes (including delete-on-close) and renames
131 "PipeMailslot", // named pipe and mailslot creates and opens
132 "Security", // security descriptor (permission/owner) changes
133 "ExtendedAttributes" // extended attribute writes
134 };
135 return names;
136}
137
140 if (!m_data.contains("FileEvents")) {
142 return;
143 }
144
145 if (!m_data["FileEvents"].is_array()) {
146 throw std::runtime_error("FileEvents must be an array of event group names");
147 }
148
149 const auto& valid = FileEventNames();
150 std::vector<std::string> names = m_data["FileEvents"].template get<std::vector<std::string>>();
151 for (const auto& name : names) {
152 if (std::find(valid.begin(), valid.end(), name) == valid.end()) {
153 std::string list;
154 for (const auto& validName : valid) {
155 list += (list.empty() ? "" : ", ") + validName;
156 }
157 throw std::runtime_error("Unknown FileEvents entry \"" + name + "\". Valid entries: " + list);
158 }
159 }
160 m_fileEvents = names;
161}
162
165{
166 if (m_data["Exclusions"].is_array()) {
167 m_exclusions = NormalizePatterns(m_data["Exclusions"].template get<std::vector<std::string>>());
168 }
169 else {
170 throw std::runtime_error("Exclusions was not set in the the configuration");
171 }
172}
173
176 if (m_data["QuarantineMaliciousFiles"].is_boolean()) {
177 m_quartine = m_data["QuarantineMaliciousFiles"].template get<bool>();
178 }
179 else {
180 throw std::runtime_error("QuarantineMaliciousFiles was not set in the the configuration");
181 }
182}
183
186 if (!m_data.contains("LogExclusions")) {
187 return;
188 }
189
190 if (m_data["LogExclusions"].is_array()) {
191 m_logExclusions = NormalizePatterns(m_data["LogExclusions"].template get<std::vector<std::string>>());
192 }
193 else {
194 throw std::runtime_error("LogExclusions must be an array of paths");
195 }
196}
197
200 if (!m_data.contains("DontInject")) {
201 return;
202 }
203
204 if (!m_data["DontInject"].is_array()) {
205 throw std::runtime_error("DontInject must be an array of process image names");
206 }
207
208 std::vector<std::string> names = NormalizePatterns(m_data["DontInject"].template get<std::vector<std::string>>());
209 for (const auto& name : names) {
210 if (name.empty()) {
211 throw std::runtime_error("DontInject entries must not be empty");
212 }
213 // * and ? are wildcards. The driver matches with FsRtlIsNameInExpression, which also
214 // treats < > and " as DOS wildcards; they cannot appear in a file name anyway
215 if (name.find_first_of("<>\"") != std::string::npos) {
216 throw std::runtime_error("DontInject entries must not contain < > or \": \"" + name + "\"");
217 }
218 if (name.find('\\') == std::string::npos) {
219 continue; // A file name, matched in any folder
220 }
221
222 // A full path. The service converts the drive letter to the NT device the driver sees,
223 // so it must be an absolute path on a drive letter: C:\Folder\app.exe
224 bool absoluteDrivePath = name.size() > 3 && std::isalpha((unsigned char)name[0]) &&
225 name[1] == ':' && name[2] == '\\' && name.back() != '\\';
226 if (!absoluteDrivePath) {
227 throw std::runtime_error("DontInject entries must be a file name such as \"msedge.exe\" or a full path such as \"C:\\\\Tools\\\\app.exe\": \"" + name + "\"");
228 }
229 }
230 m_dontInject = names;
231}
232
235 if (!m_data.contains("EnforceDetectionActions")) {
236 return;
237 }
238
239 if (!m_data["EnforceDetectionActions"].is_boolean()) {
240 throw std::runtime_error("EnforceDetectionActions must be true or false");
241 }
242 m_enforceDetectionActions = m_data["EnforceDetectionActions"].template get<bool>();
243}
244
247std::vector<std::string> Configuration::GetJsonKeys() {
248 std::vector<std::string> keys;
249 if (m_data.is_object()) {
250 for (nlohmann::json::const_iterator it = m_data.begin(); it != m_data.end(); ++it) {
251 keys.push_back(it.key());
252 }
253 }
254 else {
255 throw std::runtime_error("The json data object was not set");
256 }
257
258 return keys;
259}
nlohmann::json m_data
static const std::vector< std::string > & FileEventNames()
The names FileEvents accepts, one per group of file system events the driver can report....
static bool IsPathInList(const std::vector< std::string > &patterns, const std::string &path)
Check whether a path matches any pattern in an exclusion list. A pattern with no wildcard matches eve...
Configuration(std::string configurationPath)
Constructor for the Configuration class.
std::vector< std::string > m_exclusions
Path patterns that are not scanned (see IsPathInList).
std::vector< std::string > m_fileEvents
The groups of file system events the driver reports, from FileEventNames(). Every group if FileEvents...
std::vector< std::string > GetJsonKeys()
Get the keys from the configuration file.
void GetLogExclusions()
Get the optional log exclusions from the configuration file.
void ValidateSettings()
Check that each setting has the right type, after IsValidJson. Throws std::runtime_error naming the f...
void Parse()
Parse the configuration file.
void IsValidJson()
Check if the configuration file is valid.
bool m_enforceDetectionActions
Whether the service carries out the kill and quarantine actions of detector rules....
void GetScannerExclusions()
Get the scanner exclusions from the configuration file.
void GetQuarantineMaliciousFiles()
Get the quarantine malicious files from the configuration file.
std::ifstream m_fileStream
std::vector< std::string > m_dontInject
Processes the driver does not inject the hook DLL into. Each entry is either a file name such as "mse...
void GetEnforceDetectionActions()
Get the optional EnforceDetectionActions setting from the configuration file.
void GetFileEvents()
Get the optional FileEvents list from the configuration file.
bool m_quartine
The quarantine malicious files from the configuration file.
std::vector< std::string > m_logExclusions
Path patterns whose driver events are not logged (see IsPathInList). Files matching them are still sc...
void GetDontInject()
Get the optional DontInject list from the configuration file.