Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook_test_detail Namespace Reference

Functions

bool WildcardMatch (const char *pattern, const char *text)
 Matches text against a pattern where '*' matches any run of characters.
int RunChild (const std::vector< HookTestCase > &cases)
 Child side: loads the hook DLL from bin/<config>, one directory above the tests folder, then runs every case, announcing each with a marker message.
bool CaptureChild (const char *exePath, size_t caseCount, std::vector< std::vector< std::string > > &messages)
 Parent side: runs the child under a debug loop and returns the hook messages it emitted, grouped by case. Returns false if the child could not be run or did not exit cleanly.

Variables

constexpr const char * kCaseMarker = "@@PANO_CASE "
constexpr const char * kEndMarker = "@@PANO_END"
constexpr const char * kMessagePrefix = "[Panoptes] "

Function Documentation

◆ CaptureChild()

bool hook_test_detail::CaptureChild ( const char * exePath,
size_t caseCount,
std::vector< std::vector< std::string > > & messages )
inline

Parent side: runs the child under a debug loop and returns the hook messages it emitted, grouped by case. Returns false if the child could not be run or did not exit cleanly.

Definition at line 164 of file hook_test_harness.hpp.

164 {
165 messages.assign(caseCount, {});
166
167 char commandLine[MAX_PATH + 32];
168 sprintf_s(commandLine, "\"%s\" --child", exePath);
169
170 // The child's stdout goes to NUL: Debug builds of Detours trace to it while placing hooks.
171 // The harness reports child errors on stderr, which is passed through.
172 SECURITY_ATTRIBUTES inherit{ sizeof(inherit), NULL, TRUE };
173 HANDLE nul = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &inherit, OPEN_EXISTING, 0, NULL);
174 STARTUPINFOA si{ sizeof(si) };
175 si.dwFlags = STARTF_USESTDHANDLES;
176 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
177 si.hStdOutput = nul;
178 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
179
180 PROCESS_INFORMATION pi{};
181 BOOL started = CreateProcessA(NULL, commandLine, NULL, NULL, TRUE, DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &si, &pi);
182 DWORD startError = GetLastError();
183 if (nul != INVALID_HANDLE_VALUE) {
184 CloseHandle(nul);
185 }
186 if (!started) {
187 printf("failed to start the test child (error %lu)\n", startError);
188 return false;
189 }
190
191 bool clean = true;
192 bool ended = false;
193 size_t currentCase = SIZE_MAX;
194 DWORD exitCode = 1;
195 for (;;) {
196 DEBUG_EVENT event;
197 if (!WaitForDebugEvent(&event, INFINITE)) {
198 clean = false;
199 break;
200 }
201
202 DWORD continueStatus = DBG_CONTINUE;
203 switch (event.dwDebugEventCode) {
204 case OUTPUT_DEBUG_STRING_EVENT: {
205 // Only ANSI strings are emitted by the hooks and the harness
206 char text[512] = {};
207 SIZE_T toRead = event.u.DebugString.nDebugStringLength;
208 if (toRead > sizeof(text) - 1) {
209 toRead = sizeof(text) - 1;
210 }
211 SIZE_T bytesRead = 0;
212 ReadProcessMemory(pi.hProcess, event.u.DebugString.lpDebugStringData, text, toRead, &bytesRead);
213 text[bytesRead] = '\0';
214
215 std::string line(text);
216 while (!line.empty() && (line.back() == '\n' || line.back() == '\r' || line.back() == '\0')) {
217 line.pop_back();
218 }
219
220 if (line.rfind(kCaseMarker, 0) == 0) {
221 currentCase = strtoul(line.c_str() + strlen(kCaseMarker), NULL, 10);
222 }
223 else if (line == kEndMarker) {
224 ended = true;
225 currentCase = SIZE_MAX;
226 }
227 else if (line.rfind(kMessagePrefix, 0) == 0) {
228 if (currentCase < caseCount) {
229 messages[currentCase].push_back(line.substr(strlen(kMessagePrefix)));
230 }
231 else if (!ended) {
232 // Emitted during setup (loading the DLL, starting the remote processes).
233 // Teardown messages are not printed: they are the hooks reacting to the DLL
234 // unloading and Detours removing them
235 printf(" [setup] %s\n", line.c_str() + strlen(kMessagePrefix));
236 }
237 }
238 break;
239 }
240 case EXCEPTION_DEBUG_EVENT:
241 if (event.u.Exception.ExceptionRecord.ExceptionCode != EXCEPTION_BREAKPOINT) {
242 if (!event.u.Exception.dwFirstChance) {
243 printf("test child crashed with exception 0x%08lx\n", event.u.Exception.ExceptionRecord.ExceptionCode);
244 clean = false;
245 }
246 continueStatus = DBG_EXCEPTION_NOT_HANDLED;
247 }
248 break;
249 case CREATE_PROCESS_DEBUG_EVENT:
250 if (event.u.CreateProcessInfo.hFile != NULL) {
251 CloseHandle(event.u.CreateProcessInfo.hFile);
252 }
253 break;
254 case LOAD_DLL_DEBUG_EVENT:
255 if (event.u.LoadDll.hFile != NULL) {
256 CloseHandle(event.u.LoadDll.hFile);
257 }
258 break;
259 case EXIT_PROCESS_DEBUG_EVENT:
260 exitCode = event.u.ExitProcess.dwExitCode;
261 break;
262 }
263
264 ContinueDebugEvent(event.dwProcessId, event.dwThreadId, continueStatus);
265 if (event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) {
266 break;
267 }
268 }
269
270 CloseHandle(pi.hThread);
271 CloseHandle(pi.hProcess);
272
273 if (exitCode != 0) {
274 printf("test child exited with code %lu\n", exitCode);
275 clean = false;
276 }
277 return clean;
278}
#define MAX_PATH
Definition callbacks.h:7
constexpr const char * kEndMarker
constexpr const char * kCaseMarker
constexpr const char * kMessagePrefix

References kCaseMarker, kEndMarker, kMessagePrefix, and MAX_PATH.

Referenced by RunHookTests().

◆ RunChild()

int hook_test_detail::RunChild ( const std::vector< HookTestCase > & cases)
inline

Child side: loads the hook DLL from bin/<config>, one directory above the tests folder, then runs every case, announcing each with a marker message.

Definition at line 129 of file hook_test_harness.hpp.

129 {
130 char dllPath[MAX_PATH];
131 DWORD length = GetModuleFileNameA(NULL, dllPath, MAX_PATH);
132 char* lastSlash = (length != 0 && length < MAX_PATH) ? strrchr(dllPath, '\\') : NULL;
133 if (lastSlash == NULL) {
134 fprintf(stderr, " unexpected executable path\n");
135 return 1;
136 }
137 *(lastSlash + 1) = '\0';
138 strcat_s(dllPath, MAX_PATH, "..\\" HOOK_DLL_NAME);
139
140 if (LoadLibraryA(dllPath) == NULL) {
141 fprintf(stderr, " failed to load %s (error %lu)\n", dllPath, GetLastError());
142 return 1;
143 }
144
145 // Start the remote targets before the first case marker, so anything process creation itself
146 // triggers is reported as setup rather than attributed to a case
147 HookTestContext context;
148 context.Start();
149
150 for (size_t i = 0; i < cases.size(); i++) {
151 std::string marker = kCaseMarker + std::to_string(i);
152 OutputDebugStringA(marker.c_str());
153 cases[i].Run(context);
154 }
155
156 // What follows (terminating the targets, unloading DLLs and removing the hooks at exit) is
157 // teardown, not part of the last case
158 OutputDebugStringA(kEndMarker);
159 return 0;
160}
void Start()
Starts the remote targets. Called by the harness before the first case.
#define HOOK_DLL_NAME

References HOOK_DLL_NAME, kCaseMarker, kEndMarker, MAX_PATH, and HookTestContext::Start().

Referenced by RunHookTests().

◆ WildcardMatch()

bool hook_test_detail::WildcardMatch ( const char * pattern,
const char * text )
inline

Matches text against a pattern where '*' matches any run of characters.

Definition at line 110 of file hook_test_harness.hpp.

110 {
111 if (*pattern == '\0') {
112 return *text == '\0';
113 }
114 if (*pattern == '*') {
115 for (const char* t = text;; t++) {
116 if (WildcardMatch(pattern + 1, t)) {
117 return true;
118 }
119 if (*t == '\0') {
120 return false;
121 }
122 }
123 }
124 return *pattern == *text && WildcardMatch(pattern + 1, text + 1);
125}
bool WildcardMatch(const char *pattern, const char *text)
Matches text against a pattern where '*' matches any run of characters.

References WildcardMatch().

Referenced by WildcardMatch().

Variable Documentation

◆ kCaseMarker

const char* hook_test_detail::kCaseMarker = "@@PANO_CASE "
constexpr

Definition at line 105 of file hook_test_harness.hpp.

Referenced by CaptureChild(), and RunChild().

◆ kEndMarker

const char* hook_test_detail::kEndMarker = "@@PANO_END"
constexpr

Definition at line 106 of file hook_test_harness.hpp.

Referenced by CaptureChild(), and RunChild().

◆ kMessagePrefix

const char* hook_test_detail::kMessagePrefix = "[Panoptes] "
constexpr

Definition at line 107 of file hook_test_harness.hpp.

Referenced by CaptureChild().