Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtCreateThreadEx.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtCreateThreadEx post-call check: a thread successfully created in another
2// process is reported with its start address and THREAD_CREATE_FLAGS_*. The target is taken from
3// the ProcessHandle argument. Threads created in the current process are not reported.
5
6typedef NTSTATUS(NTAPI* NtCreateThreadEx_t)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess,
7 PVOID ObjectAttributes, HANDLE ProcessHandle, PVOID StartRoutine, PVOID Argument,
8 ULONG CreateFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, PVOID AttributeList);
9
10#define THREAD_CREATE_FLAGS_CREATE_SUSPENDED 0x1
11
14static LPTHREAD_START_ROUTINE RemoteStart() {
15 return (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "ExitThread");
16}
17
18static DWORD WINAPI ReturnImmediately(LPVOID) {
19 return 0;
20}
21
23static void Join(HANDLE thread) {
24 if (thread != NULL) {
25 WaitForSingleObject(thread, INFINITE);
26 CloseHandle(thread);
27 }
28}
29
30static const std::vector<HookTestCase> g_Cases = {
31 {
32 "Suspended thread created in remote process",
33 [](HookTestContext& context) {
34 HANDLE thread = CreateRemoteThread(context.RemoteProcess(), NULL, 0, RemoteStart(), NULL, CREATE_SUSPENDED, NULL);
35 if (thread != NULL) {
36 CloseHandle(thread);
37 }
38 },
39 { "NtCreateThreadEx: thread created in a remote process, start *, flags 0x1" },
40 },
41 {
42 "Suspended thread created in remote process through ntdll",
43 [](HookTestContext& context) {
44 HANDLE thread = NULL;
45 NtdllFunction<NtCreateThreadEx_t>("NtCreateThreadEx")(&thread, THREAD_ALL_ACCESS, NULL,
46 context.RemoteProcess(), (PVOID)RemoteStart(), NULL, THREAD_CREATE_FLAGS_CREATE_SUSPENDED, 0, 0, 0, NULL);
47 if (thread != NULL) {
48 CloseHandle(thread);
49 }
50 },
51 { "NtCreateThreadEx: thread created in a remote process, start *, flags 0x1" },
52 },
53 {
54 "Thread created in current process is not reported",
55 [](HookTestContext&) {
56 Join(CreateThread(NULL, 0, ReturnImmediately, NULL, 0, NULL));
57 },
58 {},
59 },
60 {
61 "Thread created through a real handle to self is not reported",
62 [](HookTestContext&) {
63 HANDLE self = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
64 Join(CreateRemoteThread(self, NULL, 0, ReturnImmediately, NULL, 0, NULL));
65 CloseHandle(self);
66 },
67 {},
68 },
69 {
70 // An invalid handle is not the current process, so this exercises the post-call check:
71 // the call fails and nothing is reported. (A handle opened with only
72 // PROCESS_QUERY_LIMITED_INFORMATION is not a reliable failure: on Windows 10 22H2,
73 // CreateRemoteThread through one succeeds.)
74 "Failed remote thread creation is not reported",
75 [](HookTestContext&) {
76 HANDLE thread = CreateRemoteThread((HANDLE)(ULONG_PTR)0x7FFC, NULL, 0, RemoteStart(), NULL, CREATE_SUSPENDED, NULL);
77 if (thread != NULL) {
78 CloseHandle(thread);
79 }
80 },
81 {},
82 },
83};
84
85int main(int argc, char** argv) {
86 return RunHookTests(argc, argv, "NtCreateThreadEx", g_Cases);
87}
NTSTATUS(NTAPI * NtCreateThreadEx_t)(PHANDLE ThreadHandle, ACCESS_MASK DesiredAccess, PVOID ObjectAttributes, HANDLE ProcessHandle, PVOID StartRoutine, PVOID Argument, ULONG CreateFlags, SIZE_T ZeroBits, SIZE_T StackSize, SIZE_T MaximumStackSize, PVOID AttributeList)
#define THREAD_CREATE_FLAGS_CREATE_SUSPENDED
int main()
Loads the hook DLL for this architecture from bin/<config>, one directory above the tests folder this...
Resources shared by the cases of one test run, created in the child process. The remote processes are...
T NtdllFunction(const char *name)
Looks up an ntdll export. Calls through the result go through the hooks, because the hooks patch the ...
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.