Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_Linter.cpp File Reference
#include "gtest/gtest.h"
#include "Configuration.hpp"
#include <Windows.h>
#include <fstream>

Go to the source code of this file.

Classes

class  ShippedConfiguration

Functions

 TEST (IsPathInList, PatternWithoutWildcardMatchesPrefix)
 TEST (IsPathInList, PrefixWithoutTrailingSeparatorMatchesSiblings)
 TEST (IsPathInList, MatchingIgnoresCase)
 TEST (IsPathInList, StarMatchesAnyRunIncludingSeparators)
 TEST (IsPathInList, WildcardPatternMustMatchWholePath)
 TEST (IsPathInList, QuestionMarkMatchesOneCharacter)
 TEST (IsPathInList, StarBacktracksPastEarlyMatches)
 TEST (IsPathInList, ForwardSlashesInPathMatchBackslashes)
 TEST (IsPathInList, EmptyListAndEmptyPatternsMatchNothing)
 TEST (IsPathInList, AnyPatternInListCanMatch)
 TEST_F (ShippedConfiguration, PatternsHaveNoControlCharacters)
 TEST_F (ShippedConfiguration, WildcardPatternsDoNotEndWithSeparator)
 TEST_F (ShippedConfiguration, NoDuplicatePatterns)
 TEST_F (ShippedConfiguration, NoisyPathsAreNotLogged)
 TEST_F (ShippedConfiguration, InterestingPathsAreStillLogged)
 TEST_F (ShippedConfiguration, ScanExclusions)
 TEST_F (ShippedConfiguration, DontInjectNamesAreUniqueFileNames)
 TEST (FileEvents, DefaultsToEveryGroup)
 TEST (FileEvents, AcceptsKnownGroups)
 TEST (FileEvents, RejectsUnknownGroupsAndNonArrays)
 TEST_F (ShippedConfiguration, FileEventsEnablesEveryGroup)
 TEST (DontInject, AcceptsFileNamesAndFullPaths)
 TEST (DontInject, AcceptsWildcards)
 TEST (DontInject, RejectsRelativeUncFolderAndDosWildcardEntries)

Function Documentation

◆ TEST() [1/16]

TEST ( DontInject ,
AcceptsFileNamesAndFullPaths  )

Definition at line 250 of file Test_Linter.cpp.

250 {
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}

◆ TEST() [2/16]

TEST ( DontInject ,
AcceptsWildcards  )

Definition at line 259 of file Test_Linter.cpp.

259 {
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}

◆ TEST() [3/16]

TEST ( DontInject ,
RejectsRelativeUncFolderAndDosWildcardEntries  )

Definition at line 265 of file Test_Linter.cpp.

265 {
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() [4/16]

TEST ( FileEvents ,
AcceptsKnownGroups  )

Definition at line 234 of file Test_Linter.cpp.

234 {
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}

◆ TEST() [5/16]

TEST ( FileEvents ,
DefaultsToEveryGroup  )

Definition at line 230 of file Test_Linter.cpp.

230 {
231 EXPECT_EQ(ParseFileEvents(""), Configuration::FileEventNames());
232}
static const std::vector< std::string > & FileEventNames()
The names FileEvents accepts, one per group of file system events the driver can report....

References Configuration::FileEventNames().

◆ TEST() [6/16]

TEST ( FileEvents ,
RejectsUnknownGroupsAndNonArrays  )

Definition at line 240 of file Test_Linter.cpp.

240 {
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}

◆ TEST() [7/16]

TEST ( IsPathInList ,
AnyPatternInListCanMatch  )

Definition at line 63 of file Test_Linter.cpp.

63 {
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}
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...

References Configuration::IsPathInList().

◆ TEST() [8/16]

TEST ( IsPathInList ,
EmptyListAndEmptyPatternsMatchNothing  )

Definition at line 58 of file Test_Linter.cpp.

58 {
59 EXPECT_FALSE(Configuration::IsPathInList({}, "C:\\a.exe"));
60 EXPECT_FALSE(Configuration::IsPathInList({ "" }, "C:\\a.exe"));
61}

References Configuration::IsPathInList().

◆ TEST() [9/16]

TEST ( IsPathInList ,
ForwardSlashesInPathMatchBackslashes  )

Definition at line 54 of file Test_Linter.cpp.

54 {
55 EXPECT_TRUE(Matches("C:\\Windows\\", "C:/Windows/System32/a.dll"));
56}

◆ TEST() [10/16]

TEST ( IsPathInList ,
MatchingIgnoresCase  )

Definition at line 22 of file Test_Linter.cpp.

22 {
23 EXPECT_TRUE(Matches("c:\\windows\\", "C:\\WINDOWS\\System32\\a.dll"));
24 EXPECT_TRUE(Matches("*.DB-WAL", "C:\\data\\cache.db-wal"));
25}

◆ TEST() [11/16]

TEST ( IsPathInList ,
PatternWithoutWildcardMatchesPrefix  )

Definition at line 10 of file Test_Linter.cpp.

10 {
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}

◆ TEST() [12/16]

TEST ( IsPathInList ,
PrefixWithoutTrailingSeparatorMatchesSiblings  )

Definition at line 16 of file Test_Linter.cpp.

16 {
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}

◆ TEST() [13/16]

TEST ( IsPathInList ,
QuestionMarkMatchesOneCharacter  )

Definition at line 42 of file Test_Linter.cpp.

42 {
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}

◆ TEST() [14/16]

TEST ( IsPathInList ,
StarBacktracksPastEarlyMatches  )

Definition at line 48 of file Test_Linter.cpp.

48 {
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}

◆ TEST() [15/16]

TEST ( IsPathInList ,
StarMatchesAnyRunIncludingSeparators  )

Definition at line 27 of file Test_Linter.cpp.

27 {
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}

◆ TEST() [16/16]

TEST ( IsPathInList ,
WildcardPatternMustMatchWholePath  )

Definition at line 35 of file Test_Linter.cpp.

35 {
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}

◆ TEST_F() [1/8]

TEST_F ( ShippedConfiguration ,
DontInjectNamesAreUniqueFileNames  )

Definition at line 181 of file Test_Linter.cpp.

181 {
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}

◆ TEST_F() [2/8]

TEST_F ( ShippedConfiguration ,
FileEventsEnablesEveryGroup  )

Definition at line 246 of file Test_Linter.cpp.

246 {
247 EXPECT_EQ(s_config->m_fileEvents, Configuration::FileEventNames());
248}

References Configuration::FileEventNames().

◆ TEST_F() [3/8]

TEST_F ( ShippedConfiguration ,
InterestingPathsAreStillLogged  )

Definition at line 158 of file Test_Linter.cpp.

158 {
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}

◆ TEST_F() [4/8]

TEST_F ( ShippedConfiguration ,
NoDuplicatePatterns  )

Definition at line 117 of file Test_Linter.cpp.

117 {
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}

◆ TEST_F() [5/8]

TEST_F ( ShippedConfiguration ,
NoisyPathsAreNotLogged  )

Definition at line 127 of file Test_Linter.cpp.

127 {
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}

◆ TEST_F() [6/8]

TEST_F ( ShippedConfiguration ,
PatternsHaveNoControlCharacters  )

Definition at line 94 of file Test_Linter.cpp.

94 {
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}

◆ TEST_F() [7/8]

TEST_F ( ShippedConfiguration ,
ScanExclusions  )

Definition at line 169 of file Test_Linter.cpp.

169 {
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}

◆ TEST_F() [8/8]

TEST_F ( ShippedConfiguration ,
WildcardPatternsDoNotEndWithSeparator  )

Definition at line 105 of file Test_Linter.cpp.

105 {
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}