-
Notifications
You must be signed in to change notification settings - Fork 10
/
nt_create_thread_ex.cpp
230 lines (172 loc) · 4.5 KB
/
nt_create_thread_ex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "includes.hpp"
void CODE_SEG(".nt_thrd$1") __stdcall NtCreateThreadEx_Shellcode(SR_REMOTE_DATA* data);
void CODE_SEG(".nt_thrd$2") __stdcall NtCreateThreadEx_Shellcode_End();
DWORD _NtCreateThreadEx(HANDLE h_proc, f_Routine routine, DWORD flags, void* arg_routine, DWORD* out, DWORD timeout)
{
if (!h_proc || !routine || !arg_routine || !out || !timeout)
{
return 0;
}
ProcessInfo PI;
if (!PI.SetProcess(h_proc))
{
return 0;
}
void* fake_start_addr = nullptr;
if (flags & INJ_CTX_FAKE_START_ADDRESS)
{
fake_start_addr = (void*)PI.GetEntryPoint();
if (!fake_start_addr)
{
return 0;
}
}
DWORD thread_id = 0;
if (flags & INJ_CTX_FAKE_THREAD_ID)
{
thread_id = PI.GetTID();
if (!thread_id)
{
return 0;
}
}
DWORD nt_flags = 0;
if (flags & INJ_CTX_HIDE_FROM_DEBUGGER)
{
nt_flags |= THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER;
}
if (flags & INJ_CTX_SKIP_TRHEAD_ATTACH)
{
nt_flags |= THREAD_CREATE_FLAGS_SKIP_THREAD_ATTACH;
}
if (fake_start_addr)
{
nt_flags |= THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
}
void* remote_func_data = VirtualAllocEx(h_proc, NULL, sizeof(SR_REMOTE_DATA), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (!remote_func_data)
{
return 0;
}
SR_REMOTE_DATA data;
_ZeroMemory(&data, sizeof(data));
data.routine = routine;
data.arg_routine = arg_routine;
data.arg_remote_func = (void*)thread_id;
if (!WriteProcessMemory(h_proc, remote_func_data, &data, sizeof(data), NULL))
{
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
return 0;
}
size_t remote_func_size = (DWORD)NtCreateThreadEx_Shellcode_End - (DWORD)NtCreateThreadEx_Shellcode;
void* remote_func = VirtualAllocEx(h_proc, NULL, remote_func_size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!remote_func)
{
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
return 0;
}
if (!WriteProcessMemory(h_proc, remote_func, NtCreateThreadEx_Shellcode, remote_func_size, NULL))
{
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
VirtualFreeEx(h_proc, remote_func, NULL, MEM_RELEASE);
return 0;
}
HANDLE h_thread = 0;
if (NT_FAIL(NATIVE::NtCreateThreadEx(&h_thread, THREAD_ALL_ACCESS, NULL, h_proc, fake_start_addr ? fake_start_addr : remote_func, remote_func_data, nt_flags, NULL, NULL, NULL, NULL)) || !h_thread)
{
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
VirtualFreeEx(h_proc, remote_func, NULL, MEM_RELEASE);
return 0;
}
if (fake_start_addr)
{
bool failed = false;
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_INTEGER;
if (!GetThreadContext(h_thread, &ctx))
{
failed = true;
goto FAIL;
}
#ifdef _WIN64
ctx.Rcx = (DWORD64)remote_func;
#else
ctx.Eax = (DWORD)remote_func;
#endif
if (!SetThreadContext(h_thread, &ctx))
{
failed = true;
goto FAIL;
}
if (SUSP_ERR(ResumeThread(h_thread)))
{
failed = true;
}
FAIL:
if (failed)
{
TerminateThread(h_thread, 0);
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
VirtualFreeEx(h_proc, remote_func, NULL, MEM_RELEASE);
return 0;
}
}
LOG("%d", GetThreadId(h_thread));
DWORD wait_ret = WaitForSingleObject(h_thread, timeout);
if (wait_ret != WAIT_OBJECT_0)
{
TerminateThread(h_thread, 0);
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
VirtualFreeEx(h_proc, remote_func, NULL, MEM_RELEASE);
return 0;
}
VirtualFreeEx(h_proc, remote_func, NULL, MEM_RELEASE);
if (!ReadProcessMemory(h_proc, remote_func_data, &data, sizeof(data), NULL))
{
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
return 0;
}
VirtualFreeEx(h_proc, remote_func_data, NULL, MEM_RELEASE);
if (data.status != SR_WORK_STATUS::WS_EXECUTING_FINISHED)
{
return 0;
}
*out = data.ret;
return 1;
}
void CODE_SEG(".nt_thrd$1") __stdcall NtCreateThreadEx_Shellcode(SR_REMOTE_DATA* data)
{
if (!data)
{
return;
}
data->status = SR_WORK_STATUS::WS_EXECUTING;
#ifdef _WIN64
TEB* teb = (TEB*)__readgsqword(0x30);
#else
TEB* teb = (TEB*)__readfsdword(0x18);
#endif
HANDLE tid = data->arg_remote_func;
if (tid)
{
#ifdef _WIN64
__writegsqword(0x48, (DWORD)tid);
#else
__writefsdword(0x24, (DWORD)tid);
#endif
teb->Cid.UniqueThread = tid;
teb->RealClientId.UniqueThread = tid;
}
data->ret = data->routine(data->arg_routine);
#ifdef _WIN64
data->last_error = __readgsqword(0x68);
#else
data->last_error = __readfsdword(0x34);
#endif
data->status = SR_WORK_STATUS::WS_EXECUTING_FINISHED;
return;
}
void CODE_SEG(".nt_thrd$2") __stdcall NtCreateThreadEx_Shellcode_End()
{
return;
}