Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_Linter.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2#include "Configuration.hpp"
3#include <Windows.h>
4#include <fstream>
5
6static bool Matches(const std::string& pattern, const std::string& path) {
7 return Configuration::IsPathInList({ pattern }, path);
8}
9
10TEST(IsPathInList, PatternWithoutWildcardMatchesPrefix) {
11 EXPECT_TRUE(Matches("C:\\Windows", "C:\\Windows\\System32\\notepad.exe"));
12 EXPECT_TRUE(Matches("C:\\Windows\\", "C:\\Windows\\System32\\notepad.exe"));
13 EXPECT_FALSE(Matches("C:\\Windows\\", "C:\\Users\\notepad.exe"));
14}
15
16TEST(IsPathInList, PrefixWithoutTrailingSeparatorMatchesSiblings) {
17 // Documented behavior: end a prefix with \ to match only inside that folder
18 EXPECT_TRUE(Matches("C:\\Foo", "C:\\FooBar\\a.exe"));
19 EXPECT_FALSE(Matches("C:\\Foo\\", "C:\\FooBar\\a.exe"));
20}
21
22TEST(IsPathInList, MatchingIgnoresCase) {
23 EXPECT_TRUE(Matches("c:\\windows\\", "C:\\WINDOWS\\System32\\a.dll"));
24 EXPECT_TRUE(Matches("*.DB-WAL", "C:\\data\\cache.db-wal"));
25}
26
27TEST(IsPathInList, StarMatchesAnyRunIncludingSeparators) {
28 EXPECT_TRUE(Matches("C:\\Users\\*\\AppData\\Local\\ConnectedDevicesPlatform\\*",
29 "C:\\Users\\Development\\AppData\\Local\\ConnectedDevicesPlatform\\L.Development\\ActivitiesCache.db-wal"));
30 EXPECT_TRUE(Matches("*.db-wal", "C:\\Users\\Development\\AppData\\Local\\x\\y.db-wal"));
31 EXPECT_TRUE(Matches("*\\AppData\\Local\\Packages\\*\\LocalState\\*",
32 "C:\\Users\\Development\\AppData\\Local\\Packages\\Microsoft.Windows.Photos\\LocalState\\a.db"));
33}
34
35TEST(IsPathInList, WildcardPatternMustMatchWholePath) {
36 // Without a trailing *, a wildcard pattern does not match deeper paths
37 EXPECT_FALSE(Matches("C:\\Users\\*\\AppData", "C:\\Users\\Development\\AppData\\Local\\a.txt"));
38 EXPECT_FALSE(Matches("*.db-wal", "C:\\data\\cache.db-wal.bak"));
39 EXPECT_FALSE(Matches("*.db-wal", "C:\\data\\cache.db"));
40}
41
42TEST(IsPathInList, QuestionMarkMatchesOneCharacter) {
43 EXPECT_TRUE(Matches("C:\\logs\\pano?.log", "C:\\logs\\pano7.log"));
44 EXPECT_FALSE(Matches("C:\\logs\\pano?.log", "C:\\logs\\pano.log"));
45 EXPECT_FALSE(Matches("C:\\logs\\pano?.log", "C:\\logs\\pano42.log"));
46}
47
48TEST(IsPathInList, StarBacktracksPastEarlyMatches) {
49 EXPECT_TRUE(Matches("*\\cache\\*.tmp", "C:\\cache\\x\\cache\\y.tmp"));
50 EXPECT_TRUE(Matches("*a*b*c", "xxaxxbxxbxxc"));
51 EXPECT_FALSE(Matches("*a*b*c", "xxaxxbxxbxx"));
52}
53
54TEST(IsPathInList, ForwardSlashesInPathMatchBackslashes) {
55 EXPECT_TRUE(Matches("C:\\Windows\\", "C:/Windows/System32/a.dll"));
56}
57
58TEST(IsPathInList, EmptyListAndEmptyPatternsMatchNothing) {
59 EXPECT_FALSE(Configuration::IsPathInList({}, "C:\\a.exe"));
60 EXPECT_FALSE(Configuration::IsPathInList({ "" }, "C:\\a.exe"));
61}
62
63TEST(IsPathInList, AnyPatternInListCanMatch) {
64 std::vector<std::string> patterns = { "C:\\Windows\\", "*.db-wal" };
65 EXPECT_TRUE(Configuration::IsPathInList(patterns, "D:\\x.db-wal"));
66 EXPECT_TRUE(Configuration::IsPathInList(patterns, "C:\\Windows\\a.dll"));
67 EXPECT_FALSE(Configuration::IsPathInList(patterns, "D:\\x.exe"));
68}
69
70// The configuration shipped in assets/panoptes.config, checked against real paths
71class ShippedConfiguration : public ::testing::Test {
72protected:
73 static void SetUpTestSuite() {
74 s_config = new Configuration(PANOPTES_SHIPPED_CONFIG);
75 s_config->Parse();
76 }
77 static void TearDownTestSuite() {
78 delete s_config;
79 s_config = nullptr;
80 }
81 static bool LogExcluded(const std::string& path) {
82 return Configuration::IsPathInList(s_config->m_logExclusions, path);
83 }
84 static bool ScanExcluded(const std::string& path) {
85 return Configuration::IsPathInList(s_config->m_exclusions, path);
86 }
87 static const std::vector<const std::vector<std::string>*> Lists() {
88 return { &s_config->m_exclusions, &s_config->m_logExclusions };
89 }
91};
93
94TEST_F(ShippedConfiguration, PatternsHaveNoControlCharacters) {
95 // e.g. "source\repos" in JSON is "source", a carriage return, then "epos": it matches nothing
96 for (const auto* list : Lists()) {
97 for (const auto& pattern : *list) {
98 for (char c : pattern) {
99 EXPECT_GE((unsigned char)c, 0x20) << "control character in pattern: " << pattern;
100 }
101 }
102 }
103}
104
105TEST_F(ShippedConfiguration, WildcardPatternsDoNotEndWithSeparator) {
106 // A pattern with * must match the whole path, so one ending in \ only matches the folder
107 // itself, never the files in it. It should end in \* instead
108 for (const auto* list : Lists()) {
109 for (const auto& pattern : *list) {
110 if (pattern.find_first_of("*?") != std::string::npos) {
111 EXPECT_NE(pattern.back(), '\\') << "wildcard pattern only matches a folder: " << pattern;
112 }
113 }
114 }
115}
116
117TEST_F(ShippedConfiguration, NoDuplicatePatterns) {
118 for (const auto* list : Lists()) {
119 for (size_t i = 0; i < list->size(); i++) {
120 for (size_t j = i + 1; j < list->size(); j++) {
121 EXPECT_NE(_stricmp((*list)[i].c_str(), (*list)[j].c_str()), 0) << "duplicate pattern: " << (*list)[i];
122 }
123 }
124 }
125}
126
127TEST_F(ShippedConfiguration, NoisyPathsAreNotLogged) {
128 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\ConnectedDevicesPlatform\\L.Development\\ActivitiesCache.db"));
129 EXPECT_TRUE(LogExcluded("C:\\Windows\\System32\\winevt\\Logs\\System.evtx"));
130 EXPECT_TRUE(LogExcluded("C:\\Windows\\Prefetch\\NOTEPAD.EXE-D8414F97.pf"));
131 EXPECT_TRUE(LogExcluded("C:\\Windows\\System32\\config\\SOFTWARE.LOG1"));
132 EXPECT_TRUE(LogExcluded("C:\\Windows\\System32\\LogFiles\\WMI\\RtBackup\\EtwRTEventLog-System.etl"));
133 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Comms\\UnistoreDB\\store.vol"));
134 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Comms\\Unistore\\data\\0\\a.dat"));
135 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Packages\\Microsoft.Windows.Search_cw5n1h2txyewy\\LocalState\\a.dat"));
136 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Microsoft\\Windows\\WebCache\\WebCacheV01.dat"));
137 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Microsoft\\Windows\\Explorer\\iconcache_16.db"));
138 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\LocalLow\\Microsoft\\CryptnetUrlCache\\MetaData\\ABCDEF"));
139 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Roaming\\Microsoft\\Windows\\PowerShell\\PSReadLine\\ConsoleHost_history.txt"));
140 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Microsoft\\OneDrive\\logs\\ListSync\\Business1\\a.odl"));
141 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Microsoft\\OneDrive\\logs\\Common\\a.odl"));
142 EXPECT_TRUE(LogExcluded("C:\\Users\\Development\\ntuser.dat.LOG2"));
143 EXPECT_TRUE(LogExcluded("C:\\Windows\\ServiceProfiles\\NetworkService\\NTUSER.DAT"));
144 EXPECT_TRUE(LogExcluded("C:\\Windows\\ServiceProfiles\\NetworkService\\AppData\\Local\\Microsoft\\Windows\\DeliveryOptimization\\Logs\\a.etl"));
145 EXPECT_TRUE(LogExcluded("C:\\Windows\\ServiceState\\EventLog\\Data\\lastalive0.dat"));
146 EXPECT_TRUE(LogExcluded("C:\\ProgramData\\Microsoft\\Windows Defender\\Support\\MPLog-1.log"));
147 EXPECT_TRUE(LogExcluded("C:\\ProgramData\\Microsoft\\Diagnosis\\ETLLogs\\a.etl"));
148 EXPECT_TRUE(LogExcluded("C:\\ProgramData\\Microsoft\\Search\\Data\\Applications\\Windows\\Windows.edb"));
149 EXPECT_TRUE(LogExcluded("C:\\Windows\\System32\\UserMgrLog.etl"));
150 EXPECT_TRUE(LogExcluded("C:\\$Recycle.Bin\\S-1-5-21-1\\$RABC.txt"));
151 EXPECT_TRUE(LogExcluded("C:\\Windows\\Logs\\CBS\\CBS.log"));
152 EXPECT_TRUE(LogExcluded("C:\\Windows\\System32\\sru\\SRUDB.dat"));
153 EXPECT_TRUE(LogExcluded("C:\\Windows\\SoftwareDistribution\\DataStore\\Logs\\edb.log"));
154 // Matching ignores case
155 EXPECT_TRUE(LogExcluded("c:\\windows\\prefetch\\cmd.exe-1.pf"));
156}
157
158TEST_F(ShippedConfiguration, InterestingPathsAreStillLogged) {
159 EXPECT_FALSE(LogExcluded("C:\\Users\\Development\\Desktop\\payload.exe"));
160 EXPECT_FALSE(LogExcluded("C:\\Users\\Development\\Downloads\\invoice.pdf.exe"));
161 EXPECT_FALSE(LogExcluded("C:\\Users\\Development\\AppData\\Local\\Temp\\dropper.dll"));
162 EXPECT_FALSE(LogExcluded("C:\\Users\\Development\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\run.lnk"));
163 EXPECT_FALSE(LogExcluded("C:\\ProgramData\\evil.dll"));
164 EXPECT_FALSE(LogExcluded("C:\\Windows\\System32\\notepad.exe"));
165 EXPECT_FALSE(LogExcluded("C:\\Windows\\Temp\\a.exe"));
166 EXPECT_FALSE(LogExcluded("C:\\Users\\Development\\ntuser.dat"));
167}
168
169TEST_F(ShippedConfiguration, ScanExclusions) {
170 EXPECT_TRUE(ScanExcluded("C:\\Windows\\System32\\notepad.exe"));
171 EXPECT_TRUE(ScanExcluded("C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\Common7\\IDE\\devenv.exe"));
172 EXPECT_TRUE(ScanExcluded("C:\\Program Files (x86)\\Microsoft\\EdgeWebView\\Application\\156.0.4314.50\\msedgewebview2.exe"));
173 EXPECT_TRUE(ScanExcluded("C:\\Users\\Research\\source\\repos\\Panoptes\\bin\\a.exe"));
174 EXPECT_TRUE(ScanExcluded("C:\\Users\\Development\\AppData\\Local\\Packages\\a\\b.dll"));
175 EXPECT_FALSE(ScanExcluded("C:\\Users\\Development\\Downloads\\payload.exe"));
176 EXPECT_FALSE(ScanExcluded("C:\\Users\\Development\\AppData\\Local\\Temp\\dropper.dll"));
177 EXPECT_FALSE(ScanExcluded("C:\\Program Files\\SomeVendor\\app.exe"));
178}
179
180
181TEST_F(ShippedConfiguration, DontInjectNamesAreUniqueFileNames) {
182 // The driver compares names without regard to case
183 const auto& names = s_config->m_dontInject;
184 EXPECT_FALSE(names.empty());
185 for (size_t i = 0; i < names.size(); i++) {
186 for (size_t j = i + 1; j < names.size(); j++) {
187 EXPECT_NE(_stricmp(names[i].c_str(), names[j].c_str()), 0) << "duplicate DontInject name: " << names[i];
188 }
189 }
190}
191
192// Parses a minimal configuration plus extraMembers (JSON object members, e.g.
193// "\"DontInject\": []", or empty) and returns the requested list from it
194static std::vector<std::string> ParseConfigList(const std::string& extraMembers, std::vector<std::string> Configuration::* list) {
195 char tempDir[MAX_PATH];
196 char tempFile[MAX_PATH];
197 GetTempPathA(MAX_PATH, tempDir);
198 GetTempFileNameA(tempDir, "pno", 0, tempFile);
199 {
200 std::ofstream out(tempFile);
201 out << "{\"Exclusions\": [], \"QuarantineMaliciousFiles\": false";
202 if (!extraMembers.empty()) {
203 out << ", " << extraMembers;
204 }
205 out << "}";
206 }
207
208 std::vector<std::string> result;
209 try {
210 Configuration config(tempFile);
211 config.Parse();
212 result = config.*list;
213 }
214 catch (...) {
215 DeleteFileA(tempFile);
216 throw;
217 }
218 DeleteFileA(tempFile);
219 return result;
220}
221
222static std::vector<std::string> ParseDontInject(const std::string& dontInjectJson) {
223 return ParseConfigList("\"DontInject\": " + dontInjectJson, &Configuration::m_dontInject);
224}
225
226static std::vector<std::string> ParseFileEvents(const std::string& fileEventsJson) {
227 return ParseConfigList(fileEventsJson.empty() ? "" : "\"FileEvents\": " + fileEventsJson, &Configuration::m_fileEvents);
228}
229
230TEST(FileEvents, DefaultsToEveryGroup) {
231 EXPECT_EQ(ParseFileEvents(""), Configuration::FileEventNames());
232}
233
234TEST(FileEvents, AcceptsKnownGroups) {
235 auto groups = ParseFileEvents(R"(["Create", "Security"])");
236 EXPECT_EQ(groups, (std::vector<std::string>{ "Create", "Security" }));
237 EXPECT_TRUE(ParseFileEvents("[]").empty()); // every group off
238}
239
240TEST(FileEvents, RejectsUnknownGroupsAndNonArrays) {
241 EXPECT_THROW(ParseFileEvents(R"(["Reads"])"), std::runtime_error);
242 EXPECT_THROW(ParseFileEvents(R"(["create"])"), std::runtime_error); // names are case-sensitive
243 EXPECT_THROW(ParseFileEvents(R"("Create")"), std::runtime_error);
244}
245
246TEST_F(ShippedConfiguration, FileEventsEnablesEveryGroup) {
247 EXPECT_EQ(s_config->m_fileEvents, Configuration::FileEventNames());
248}
249
250TEST(DontInject, AcceptsFileNamesAndFullPaths) {
251 // Raw strings hold JSON, so each \\ is one \ in the parsed value
252 auto names = ParseDontInject(R"(["msedge.exe", "C:\\Tools\\app.exe", "d:/x/y.exe"])");
253 ASSERT_EQ(names.size(), 3u);
254 EXPECT_EQ(names[0], "msedge.exe");
255 EXPECT_EQ(names[1], "C:\\Tools\\app.exe");
256 EXPECT_EQ(names[2], "d:\\x\\y.exe"); // / is normalized to \ like the other path settings
257}
258
259TEST(DontInject, AcceptsWildcards) {
260 auto names = ParseDontInject(R"(["splunk*.exe", "C:\\Program Files\\SplunkUniversalForwarder\\bin\\*", "app?.exe"])");
261 ASSERT_EQ(names.size(), 3u);
262 EXPECT_EQ(names[1], "C:\\Program Files\\SplunkUniversalForwarder\\bin\\*");
263}
264
265TEST(DontInject, RejectsRelativeUncFolderAndDosWildcardEntries) {
266 EXPECT_THROW(ParseDontInject(R"(["Tools\\app.exe"])"), std::runtime_error);
267 EXPECT_THROW(ParseDontInject(R"(["\\\\server\\share\\app.exe"])"), std::runtime_error);
268 EXPECT_THROW(ParseDontInject(R"(["C:\\Tools\\"])"), std::runtime_error);
269 EXPECT_THROW(ParseDontInject(R"(["*:\\Tools\\app.exe"])"), std::runtime_error); // no wildcard drive
270 EXPECT_THROW(ParseDontInject(R"(["<.exe"])"), std::runtime_error); // DOS wildcards in the driver
271 EXPECT_THROW(ParseDontInject(R"(["app\".exe"])"), std::runtime_error);
272 EXPECT_THROW(ParseDontInject(R"([""])"), std::runtime_error);
273 EXPECT_THROW(ParseDontInject(R"("msedge.exe")"), std::runtime_error);
274}
TEST(IsPathInList, PatternWithoutWildcardMatchesPrefix)
TEST_F(ShippedConfiguration, PatternsHaveNoControlCharacters)
#define MAX_PATH
Definition callbacks.h:7
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...
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 > m_dontInject
Processes the driver does not inject the hook DLL into. Each entry is either a file name such as "mse...
static bool ScanExcluded(const std::string &path)
static bool LogExcluded(const std::string &path)
static const std::vector< const std::vector< std::string > * > Lists()
static void TearDownTestSuite()
static Configuration * s_config
static void SetUpTestSuite()