-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNtCreateProcessEx.cpp
More file actions
183 lines (158 loc) · 4.98 KB
/
Copy pathNtCreateProcessEx.cpp
File metadata and controls
183 lines (158 loc) · 4.98 KB
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
#include <phnt_windows.h>
#include <phnt.h>
#include <cstdio>
#pragma comment(lib, "ntdll.lib")
#define NTCHECK(a) \
{ \
Status = (a); \
if (!NT_SUCCESS(Status)) \
{ \
ReportNTError(Status); \
return Status; \
} \
}
void ReportNTError(NTSTATUS Status)
{
LPWSTR Message = nullptr;
HMODULE NTDLLBase = GetModuleHandleW(L"ntdll.dll");
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
NTDLLBase,
Status,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&Message,
0,
NULL);
if (Message)
{
wprintf(L"Status 0x%X: %s\n", Status, Message);
LocalFree(Message);
}
else
{
wprintf(L"Unknown Status code: 0x%X\n", Status);
}
}
int wmain(int ArgCount, wchar_t *Args[])
{
NTSTATUS Status = {};
PCWSTR ExePath = {};
UNICODE_STRING NtExePath = {};
PWSTR FileName = {};
HANDLE FileHandle = {};
HANDLE SectionHandle = {};
HANDLE ProcessHandle = {};
UNICODE_STRING ImagePathName = {};
UNICODE_STRING DllPath = {};
OBJECT_ATTRIBUTES ObjectAttrs = {};
IO_STATUS_BLOCK IoStatusBlock = {};
ULONG RLength = {};
SIZE_T BytesWritten = {};
PROCESS_BASIC_INFORMATION ProcessInfo = {};
PRTL_USER_PROCESS_PARAMETERS ProcessParameters = {};
SECTION_IMAGE_INFORMATION ImageInfo = {};
HANDLE ThreadHandle = {};
WCHAR ImageNameBuffer[MAX_PATH];
UNICODE_STRING ImageName;
UNICODE_STRING CurrentDir;
SIZE_T ProcessParametersSize;
PVOID RemoteProcessParameters;
SIZE_T RemoteProcessParametersSize;
if (ArgCount != 2)
{
wprintf(L"NtCreateProcessEx.exe <exepath>\n");
return 1;
}
ExePath = Args[1];
NTCHECK(RtlDosPathNameToNtPathName_U_WithStatus(
ExePath,
&NtExePath,
&FileName,
nullptr));
InitializeObjectAttributes(&ObjectAttrs, &NtExePath, OBJ_CASE_INSENSITIVE, nullptr, nullptr);
NTCHECK(NtOpenFile(
&FileHandle,
GENERIC_READ | GENERIC_EXECUTE | SYNCHRONIZE,
&ObjectAttrs,
&IoStatusBlock,
FILE_SHARE_READ,
FILE_SYNCHRONOUS_IO_NONALERT));
NTCHECK(NtCreateSection(
&SectionHandle,
GENERIC_ALL,
nullptr,
nullptr,
PAGE_EXECUTE,
SEC_IMAGE,
FileHandle));
NTCHECK(NtCreateProcessEx(
&ProcessHandle,
GENERIC_ALL,
nullptr,
GetCurrentProcess(),
PROCESS_CREATE_FLAGS_NONE,
SectionHandle,
0,
0,
0));
NTCHECK(NtQueryInformationProcess(
ProcessHandle,
ProcessBasicInformation,
(PVOID)&ProcessInfo,
sizeof(PROCESS_BASIC_INFORMATION),
&RLength));
ImageName.Buffer = ImageNameBuffer;
ImageName.MaximumLength = sizeof(ImageNameBuffer);
ImageName.Length = (USHORT)RtlGetFullPathName_U(Args[1], ImageName.MaximumLength, &ImageNameBuffer[0], NULL);
RtlInitUnicodeString(&CurrentDir, ((PKUSER_SHARED_DATA)USER_SHARED_DATA)->NtSystemRoot);
NTCHECK(RtlCreateProcessParametersEx(
&ProcessParameters,
&ImageName,
&CurrentDir,
&CurrentDir,
&ImageName,
RtlGetCurrentPeb()->ProcessParameters->Environment,
nullptr,
nullptr,
nullptr,
nullptr,
RTL_USER_PROC_PARAMS_NORMALIZED));
ProcessParametersSize = ProcessParameters->MaximumLength + ProcessParameters->EnvironmentSize;
RemoteProcessParameters = nullptr;
RemoteProcessParametersSize = ProcessParametersSize;
NTCHECK(NtAllocateVirtualMemory(
ProcessHandle,
&RemoteProcessParameters,
0,
&RemoteProcessParametersSize,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE));
RtlDeNormalizeProcessParams(ProcessParameters);
ProcessParameters->Environment = (PVOID)((SIZE_T)ProcessParameters->Environment + ((SIZE_T)RemoteProcessParameters - (SIZE_T)ProcessParameters));
NTCHECK(NtWriteVirtualMemory(
ProcessHandle,
RemoteProcessParameters,
ProcessParameters,
ProcessParametersSize,
nullptr));
NTCHECK(NtWriteVirtualMemory(
ProcessHandle,
(PVOID)&ProcessInfo.PebBaseAddress->ProcessParameters,
&RemoteProcessParameters,
sizeof(RemoteProcessParameters),
nullptr));
NTCHECK(RtlDestroyProcessParameters(ProcessParameters));
NTCHECK(NtQueryInformationProcess(ProcessHandle, ProcessImageInformation, &ImageInfo, sizeof(ImageInfo), nullptr));
NTCHECK(NtCreateThreadEx(
&ThreadHandle,
THREAD_ALL_ACCESS,
nullptr,
ProcessHandle,
(PUSER_THREAD_START_ROUTINE)ImageInfo.TransferAddress,
nullptr,
0,
ImageInfo.ZeroBits,
ImageInfo.CommittedStackSize,
ImageInfo.MaximumStackSize,
nullptr));
}