1#include "gtest/gtest.h"
6static bool Matches(
const std::string& pattern,
const std::string& path) {
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"));
16TEST(IsPathInList, PrefixWithoutTrailingSeparatorMatchesSiblings) {
18 EXPECT_TRUE(Matches(
"C:\\Foo",
"C:\\FooBar\\a.exe"));
19 EXPECT_FALSE(Matches(
"C:\\Foo\\",
"C:\\FooBar\\a.exe"));
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"));
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"));
35TEST(IsPathInList, WildcardPatternMustMatchWholePath) {
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"));
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"));
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"));
54TEST(IsPathInList, ForwardSlashesInPathMatchBackslashes) {
55 EXPECT_TRUE(Matches(
"C:\\Windows\\",
"C:/Windows/System32/a.dll"));
58TEST(IsPathInList, EmptyListAndEmptyPatternsMatchNothing) {
63TEST(IsPathInList, AnyPatternInListCanMatch) {
64 std::vector<std::string> patterns = {
"C:\\Windows\\",
"*.db-wal" };
87 static const std::vector<const std::vector<std::string>*>
Lists() {
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;
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;
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];
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"));
155 EXPECT_TRUE(LogExcluded(
"c:\\windows\\prefetch\\cmd.exe-1.pf"));
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"));
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"));
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];
194static std::vector<std::string> ParseConfigList(
const std::string& extraMembers, std::vector<std::string>
Configuration::* list) {
198 GetTempFileNameA(tempDir,
"pno", 0, tempFile);
200 std::ofstream out(tempFile);
201 out <<
"{\"Exclusions\": [], \"QuarantineMaliciousFiles\": false";
202 if (!extraMembers.empty()) {
203 out <<
", " << extraMembers;
208 std::vector<std::string> result;
212 result = config.*list;
215 DeleteFileA(tempFile);
218 DeleteFileA(tempFile);
222static std::vector<std::string> ParseDontInject(
const std::string& dontInjectJson) {
226static std::vector<std::string> ParseFileEvents(
const std::string& fileEventsJson) {
230TEST(FileEvents, DefaultsToEveryGroup) {
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());
240TEST(FileEvents, RejectsUnknownGroupsAndNonArrays) {
241 EXPECT_THROW(ParseFileEvents(R
"(["Reads"])"), std::runtime_error);
242 EXPECT_THROW(ParseFileEvents(R"(["create"])"), std::runtime_error);
243 EXPECT_THROW(ParseFileEvents(R
"("Create")"), std::runtime_error);
250TEST(DontInject, AcceptsFileNamesAndFullPaths) {
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");
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\\*");
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);
270 EXPECT_THROW(ParseDontInject(R
"(["<.exe"])"), std::runtime_error);
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);
TEST(IsPathInList, PatternWithoutWildcardMatchesPrefix)
TEST_F(ShippedConfiguration, PatternsHaveNoControlCharacters)
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()