10#pragma region Path Patterns
12static std::string NormalizeSeparators(std::string path)
14 std::replace(path.begin(), path.end(),
'/',
'\\');
18static std::vector<std::string> NormalizePatterns(std::vector<std::string> patterns)
20 for (
auto& pattern : patterns) {
21 pattern = NormalizeSeparators(pattern);
26static bool CharEqualsIgnoreCase(
char a,
char b)
28 return std::tolower((
unsigned char)a) == std::tolower((
unsigned char)b);
33static bool WildcardMatch(
const std::string& pattern,
const std::string& text)
37 size_t star = std::string::npos;
40 while (t < text.size()) {
41 if (p < pattern.size() && pattern[p] ==
'*') {
45 else if (p < pattern.size() && (pattern[p] ==
'?' || CharEqualsIgnoreCase(pattern[p], text[t]))) {
49 else if (star != std::string::npos) {
58 while (p < pattern.size() && pattern[p] ==
'*') {
61 return p == pattern.size();
66 std::string normalizedPath = NormalizeSeparators(path);
68 for (
const auto& pattern : patterns) {
69 if (pattern.empty()) {
73 if (pattern.find_first_of(
"*?") != std::string::npos) {
74 if (WildcardMatch(pattern, normalizedPath)) {
78 else if (normalizedPath.size() >= pattern.size() &&
79 _strnicmp(normalizedPath.c_str(), pattern.c_str(), pattern.size()) == 0) {
127 static const std::vector<std::string> names = {
140 if (!
m_data.contains(
"FileEvents")) {
145 if (!
m_data[
"FileEvents"].is_array()) {
146 throw std::runtime_error(
"FileEvents must be an array of event group names");
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()) {
154 for (
const auto& validName : valid) {
155 list += (list.empty() ?
"" :
", ") + validName;
157 throw std::runtime_error(
"Unknown FileEvents entry \"" + name +
"\". Valid entries: " + list);
166 if (
m_data[
"Exclusions"].is_array()) {
167 m_exclusions = NormalizePatterns(
m_data[
"Exclusions"].
template get<std::vector<std::string>>());
170 throw std::runtime_error(
"Exclusions was not set in the the configuration");
176 if (
m_data[
"QuarantineMaliciousFiles"].is_boolean()) {
180 throw std::runtime_error(
"QuarantineMaliciousFiles was not set in the the configuration");
186 if (!
m_data.contains(
"LogExclusions")) {
190 if (
m_data[
"LogExclusions"].is_array()) {
191 m_logExclusions = NormalizePatterns(
m_data[
"LogExclusions"].
template get<std::vector<std::string>>());
194 throw std::runtime_error(
"LogExclusions must be an array of paths");
200 if (!
m_data.contains(
"DontInject")) {
204 if (!
m_data[
"DontInject"].is_array()) {
205 throw std::runtime_error(
"DontInject must be an array of process image names");
208 std::vector<std::string> names = NormalizePatterns(
m_data[
"DontInject"].
template get<std::vector<std::string>>());
209 for (
const auto& name : names) {
211 throw std::runtime_error(
"DontInject entries must not be empty");
215 if (name.find_first_of(
"<>\"") != std::string::npos) {
216 throw std::runtime_error(
"DontInject entries must not contain < > or \": \"" + name +
"\"");
218 if (name.find(
'\\') == std::string::npos) {
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 +
"\"");
235 if (!
m_data.contains(
"EnforceDetectionActions")) {
239 if (!
m_data[
"EnforceDetectionActions"].is_boolean()) {
240 throw std::runtime_error(
"EnforceDetectionActions must be true or false");
248 std::vector<std::string> keys;
250 for (nlohmann::json::const_iterator it =
m_data.begin(); it !=
m_data.end(); ++it) {
251 keys.push_back(it.key());
255 throw std::runtime_error(
"The json data object was not set");
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.