Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
Test_NtQueueApcThread.cpp
Go to the documentation of this file.
1// Tests the Hooked_NtQueueApcThread and Hooked_NtQueueApcThreadEx2 post-call check: an APC
2// successfully queued to a thread in another process is reported with the API, the APC routine
3// and the QUEUE_USER_APC_FLAGS_*. The thread's process is found with GetProcessIdOfThread; a
4// handle without THREAD_QUERY_LIMITED_INFORMATION is instead compared with each thread of the
5// current process (NtCompareObjects), so it is only reported when the thread really is remote,
6// with TargetProcessId 0. APCs queued to threads of the current process are not reported.
7//
8// KernelBase's QueueUserAPC and QueueUserAPC2 call NtQueueApcThreadEx2, passing ntdll's APC
9// dispatcher as the routine, so the routine reported for them is the dispatcher rather than the
10// caller's function.
11#include "hook_test_harness.hpp"
12
13typedef VOID(NTAPI* PS_APC_ROUTINE_t)(PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3);
14typedef NTSTATUS(NTAPI* NtQueueApcThread_t)(HANDLE ThreadHandle, PS_APC_ROUTINE_t ApcRoutine,
15 PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3);
16typedef NTSTATUS(NTAPI* NtQueueApcThreadEx2_t)(HANDLE ThreadHandle, HANDLE ReserveHandle,
17 ULONG ApcFlags, PS_APC_ROUTINE_t ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3);
18
19#define TEST_SPECIAL_USER_APC 0x1
20
21static VOID NTAPI NtApc(PVOID, PVOID, PVOID) {}
22static VOID CALLBACK UserApc(ULONG_PTR) {}
23
24static void QueueNt(HANDLE thread) {
25 NtdllFunction<NtQueueApcThread_t>("NtQueueApcThread")(thread, NtApc, NULL, NULL, NULL);
26}
27
28static NTSTATUS QueueNtEx2(HANDLE thread, ULONG flags) {
29 auto queue = NtdllFunction<NtQueueApcThreadEx2_t>("NtQueueApcThreadEx2");
30 if (queue == NULL) {
31 fprintf(stderr, " NtQueueApcThreadEx2 is not exported by this ntdll\n");
32 return (NTSTATUS)0xC0000002; // STATUS_NOT_IMPLEMENTED
33 }
34 return queue(thread, NULL, flags, NtApc, NULL, NULL, NULL);
35}
36
38static void DrainApcs() {
39 SleepEx(0, TRUE);
40}
41
42// The remote thread is suspended, so the APCs queued to it never run
43static const std::vector<HookTestCase> g_Cases = {
44 // NtQueueApcThread
45
46 {
47 "APC queued to a remote thread",
48 [](HookTestContext& context) {
49 QueueNt(context.RemoteThread());
50 },
51 { "NtQueueApcThread: APC queued to a thread in the remote process, routine *, flags 0x0" },
52 },
53 {
54 "APC queued to the current thread (pseudo-handle) is not reported",
55 [](HookTestContext&) {
56 QueueNt(GetCurrentThread());
57 DrainApcs();
58 },
59 {},
60 },
61 {
62 "APC queued through a real handle to the current thread is not reported",
63 [](HookTestContext&) {
64 HANDLE thread = OpenThread(THREAD_SET_CONTEXT | THREAD_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentThreadId());
65 QueueNt(thread);
66 CloseHandle(thread);
67 DrainApcs();
68 },
69 {},
70 },
71 {
72 // Without query access the thread's process cannot be queried, but the handle still
73 // matches one of this process's threads
74 "APC queued through a local thread handle without query access is not reported",
75 [](HookTestContext&) {
76 HANDLE thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetCurrentThreadId());
77 QueueNt(thread);
78 CloseHandle(thread);
79 DrainApcs();
80 },
81 {},
82 },
83 {
84 "APC queued through a remote thread handle without query access is reported",
85 [](HookTestContext& context) {
86 HANDLE thread = OpenThread(THREAD_SET_CONTEXT, FALSE, GetThreadId(context.RemoteThread()));
87 QueueNt(thread);
88 CloseHandle(thread);
89 },
90 { "NtQueueApcThread: APC queued to a thread in the remote process, routine *, flags 0x0" },
91 },
92 {
93 "Failed APC queue to a remote thread is not reported",
94 [](HookTestContext& context) {
95 // The handle lacks THREAD_SET_CONTEXT
96 HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, GetThreadId(context.RemoteThread()));
97 QueueNt(thread);
98 CloseHandle(thread);
99 },
100 {},
101 },
102
103 // NtQueueApcThreadEx2
104
105 {
106 "Ex2: APC queued to a remote thread",
107 [](HookTestContext& context) {
108 QueueNtEx2(context.RemoteThread(), 0);
109 },
110 { "NtQueueApcThreadEx2: APC queued to a thread in the remote process, routine *, flags 0x0" },
111 },
112#ifdef _WIN64
113 {
114 // Special user APCs run without the thread entering an alertable wait
115 "Ex2: special user APC queued to a remote thread",
116 [](HookTestContext& context) {
117 QueueNtEx2(context.RemoteThread(), TEST_SPECIAL_USER_APC);
118 },
119 { "NtQueueApcThreadEx2: APC queued to a thread in the remote process, routine *, flags 0x1" },
120 },
121#else
122 {
123 // Special user APCs are not supported for WOW64 processes, so the call fails
124 // (STATUS_NOT_SUPPORTED) and the post-call check reports nothing
125 "Ex2: special user APC to a 32-bit remote thread fails and is not reported",
126 [](HookTestContext& context) {
127 NTSTATUS status = QueueNtEx2(context.RemoteThread(), TEST_SPECIAL_USER_APC);
128 if (status != (NTSTATUS)0xC00000BB) {
129 fprintf(stderr, " expected STATUS_NOT_SUPPORTED, got 0x%08lx\n", (unsigned long)status);
130 }
131 },
132 {},
133 },
134#endif
135 {
136 "Ex2: APC queued to the current thread is not reported",
137 [](HookTestContext&) {
138 QueueNtEx2(GetCurrentThread(), 0);
139 DrainApcs();
140 },
141 {},
142 },
143 {
144 "Ex2: failed APC queue to a remote thread is not reported",
145 [](HookTestContext& context) {
146 HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, GetThreadId(context.RemoteThread()));
147 QueueNtEx2(thread, 0);
148 CloseHandle(thread);
149 },
150 {},
151 },
152
153 // The Win32 API, which goes through NtQueueApcThreadEx2
154
155 {
156 "QueueUserAPC to a remote thread",
157 [](HookTestContext& context) {
158 if (!QueueUserAPC(UserApc, context.RemoteThread(), 0)) {
159 fprintf(stderr, " QueueUserAPC failed (error %lu)\n", GetLastError());
160 }
161 },
162 { "NtQueueApcThreadEx2: APC queued to a thread in the remote process, routine *, flags 0x0" },
163 },
164 {
165 "QueueUserAPC to the current thread is not reported",
166 [](HookTestContext&) {
167 QueueUserAPC(UserApc, GetCurrentThread(), 0);
168 DrainApcs();
169 },
170 {},
171 },
172};
173
174int main(int argc, char** argv) {
175 return RunHookTests(argc, argv, "NtQueueApcThread", g_Cases);
176}
VOID(NTAPI * PS_APC_ROUTINE_t)(PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
#define TEST_SPECIAL_USER_APC
NTSTATUS(NTAPI * NtQueueApcThread_t)(HANDLE ThreadHandle, PS_APC_ROUTINE_t ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
NTSTATUS(NTAPI * NtQueueApcThreadEx2_t)(HANDLE ThreadHandle, HANDLE ReserveHandle, ULONG ApcFlags, PS_APC_ROUTINE_t ApcRoutine, PVOID ApcArgument1, PVOID ApcArgument2, PVOID ApcArgument3)
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.