Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
hook_test_harness.hpp File Reference
#include <Windows.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

class  HookTestContext
 Resources shared by the cases of one test run, created in the child process. The remote processes are started by Start, before the first case, so what process creation itself triggers (CreateProcess writes to the child's PEB) is reported as setup rather than attributed to a case. More...
struct  HookTestCase
 One test case. Run executes in the child with the hooks placed. Expected lists the "[Panoptes] " messages (without that prefix) the case must produce, in order; '*' matches any run of characters, e.g. for addresses. More...

Namespaces

namespace  hook_test_detail

Macros

#define HOOK_ARCH   "x86"
#define HOOK_DLL_NAME   "PanoptesDLL" HOOK_ARCH ".dll"

Functions

template<typename T>
T NtdllFunction (const char *name)
 Looks up an ntdll export. Calls through the result go through the hooks, because the hooks patch the export itself.
PROCESS_INFORMATION StartSuspended (const std::string &commandLine)
 Starts a suspended process. Returns an empty PROCESS_INFORMATION on failure.
bool hook_test_detail::WildcardMatch (const char *pattern, const char *text)
 Matches text against a pattern where '*' matches any run of characters.
int hook_test_detail::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 hook_test_detail::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.
int RunHookTests (int argc, char **argv, const char *apiName, const std::vector< HookTestCase > &cases)
 Runs the cases and prints a PASS/FAIL line for each. Call from main.

Variables

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

Macro Definition Documentation

◆ HOOK_ARCH

#define HOOK_ARCH   "x86"

Definition at line 17 of file hook_test_harness.hpp.

Referenced by main(), and RunHookTests().

◆ HOOK_DLL_NAME

#define HOOK_DLL_NAME   "PanoptesDLL" HOOK_ARCH ".dll"

Definition at line 20 of file hook_test_harness.hpp.

Referenced by main(), and hook_test_detail::RunChild().

Function Documentation

◆ NtdllFunction()

template<typename T>
T NtdllFunction ( const char * name)
inline

Looks up an ntdll export. Calls through the result go through the hooks, because the hooks patch the export itself.

Definition at line 25 of file hook_test_harness.hpp.

25 {
26 return (T)GetProcAddress(GetModuleHandleA("ntdll.dll"), name);
27}

Referenced by MapView(), MapViewEx(), UnmapView(), and UnmapViewEx().

◆ RunHookTests()

int RunHookTests ( int argc,
char ** argv,
const char * apiName,
const std::vector< HookTestCase > & cases )
inline

Runs the cases and prints a PASS/FAIL line for each. Call from main.

Returns
The process exit code: 0 if every case passed (or the run was skipped), 1 otherwise.

Definition at line 284 of file hook_test_harness.hpp.

284 {
285 using namespace hook_test_detail;
286
287 if (argc >= 2 && strcmp(argv[1], "--child") == 0) {
288 return RunChild(cases);
289 }
290
291 printf("%s hook tests (%s)\n", apiName, HOOK_ARCH);
292
293#ifndef _DEBUG
294 printf("SKIPPED: the hooks only emit detection messages in Debug builds\n");
295 return 0;
296#else
297 char exePath[MAX_PATH];
298 DWORD length = GetModuleFileNameA(NULL, exePath, MAX_PATH);
299 if (length == 0 || length == MAX_PATH) {
300 printf("failed to get the executable path (error %lu)\n", GetLastError());
301 return 1;
302 }
303
304 std::vector<std::vector<std::string>> messages;
305 bool clean = CaptureChild(exePath, cases.size(), messages);
306
307 int failures = 0;
308 for (size_t i = 0; i < cases.size(); i++) {
309 const HookTestCase& testCase = cases[i];
310 const std::vector<std::string>& actual = messages[i];
311
312 bool passed = actual.size() == testCase.Expected.size();
313 for (size_t m = 0; passed && m < actual.size(); m++) {
314 passed = WildcardMatch(testCase.Expected[m].c_str(), actual[m].c_str());
315 }
316
317 printf(" [%s] %s\n", passed ? "PASS" : "FAIL", testCase.Name);
318 if (!passed) {
319 failures++;
320 for (const std::string& expected : testCase.Expected) {
321 printf(" expected: %s\n", expected.c_str());
322 }
323 if (testCase.Expected.empty()) {
324 printf(" expected: no messages\n");
325 }
326 for (const std::string& line : actual) {
327 printf(" actual: %s\n", line.c_str());
328 }
329 if (actual.empty()) {
330 printf(" actual: no messages\n");
331 }
332 }
333 }
334
335 if (!clean) {
336 failures++;
337 }
338
339 printf("%s\n", failures == 0 ? "PASS" : "FAIL");
340 return failures == 0 ? 0 : 1;
341#endif
342}
#define MAX_PATH
Definition callbacks.h:7
#define HOOK_ARCH
int RunChild(const std::vector< HookTestCase > &cases)
Child side: loads the hook DLL from bin/<config>, one directory above the tests folder,...
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,...
One test case. Run executes in the child with the hooks placed. Expected lists the "[Panoptes] " mess...
const char * Name
std::vector< std::string > Expected

References hook_test_detail::CaptureChild(), HookTestCase::Expected, HOOK_ARCH, MAX_PATH, HookTestCase::Name, and hook_test_detail::RunChild().

Referenced by main().

◆ StartSuspended()

PROCESS_INFORMATION StartSuspended ( const std::string & commandLine)
inline

Starts a suspended process. Returns an empty PROCESS_INFORMATION on failure.

Definition at line 30 of file hook_test_harness.hpp.

30 {
31 std::string mutableLine = commandLine;
32 STARTUPINFOA si{ sizeof(si) };
33 PROCESS_INFORMATION pi{};
34 if (!CreateProcessA(NULL, &mutableLine[0], NULL, NULL, FALSE, CREATE_SUSPENDED | CREATE_NO_WINDOW,
35 NULL, NULL, &si, &pi)) {
36 fprintf(stderr, " failed to start %s (error %lu)\n", commandLine.c_str(), GetLastError());
37 pi = {};
38 }
39 return pi;
40}

Referenced by HookTestContext::Start().