-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.cpp
More file actions
297 lines (227 loc) · 7.73 KB
/
main.cpp
File metadata and controls
297 lines (227 loc) · 7.73 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
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
#include <cstdio>
#include <vector>
#include <intrin.h>
#include "drv_image.hpp"
#include "util.hpp"
#include "structs.hpp"
#include <cassert>
#include "Capcom/CapcomWrapper.h"
#include "main.h"
#pragma intrinsic(_disable)
#pragma intrinsic(_enable)
typedef struct GET_ROUTINE_DATA {
uintptr_t base;
const char* name;
uint16_t ordinal;
uintptr_t result;
} GetRoutineData;
typedef struct ALLOCATE_POOL_DATA {
POOL_TYPE type;
size_t size;
uintptr_t result;
} AllocatePoolData;
typedef struct COPY_MEMORY_DATA {
uintptr_t target;
uintptr_t source;
size_t size;
} CopyMemoryData;
typedef struct CALL_ENTRY_DATA {
uintptr_t entryPoint;
uintptr_t kernelMemory;
uintptr_t capcomBase;
NTSTATUS result;
} CallEntryData;
typedef struct ZERO_MEMORY_DATA {
uintptr_t ptr;
size_t size;
} ZeroMemoryData;
constexpr auto page_size = 0x1000u;
NON_PAGED_DATA static kernelFuncCall RtlCopyMemory;
NON_PAGED_DATA static kernelFuncCall RtlZeroMemory;
NON_PAGED_DATA static kernelFuncCall RtlFindExportedRoutineByName;
NON_PAGED_DATA static kernelFuncCall ExAllocatePool;
NON_PAGED_DATA static kernelFuncCall DbgPrintEx;
NON_PAGED_CODE void __stdcall GetExportedFunctionByOrdinal(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
GetRoutineData* grd = (GetRoutineData*)userData;
const auto dos_header = (PIMAGE_DOS_HEADER)grd->base;
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
return;
const auto nt_headers = (PIMAGE_NT_HEADERS64)(grd->base + dos_header->e_lfanew);
if (nt_headers->Signature != IMAGE_NT_SIGNATURE)
return;
if (nt_headers->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC)
return;
const auto export_ptr = (PIMAGE_EXPORT_DIRECTORY)(nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress + grd->base);
auto address_of_funcs = (PULONG)(export_ptr->AddressOfFunctions + grd->base);
for (ULONG i = 0; i < export_ptr->NumberOfFunctions; ++i)
{
if (export_ptr->Base + (uint16_t)i == grd->ordinal) {
grd->result = address_of_funcs[i] + grd->base;
return;
}
}
}
NON_PAGED_CODE void __stdcall GetExportedFunctionByName(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
GetRoutineData* grd = (GetRoutineData*)userData;
grd->result = CallWithInterruptsAndSmep(
RtlFindExportedRoutineByName,
grd->base,
grd->name
);
}
NON_PAGED_CODE void __stdcall ExAllocatePoolKrnl(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
AllocatePoolData* apd = (AllocatePoolData*)userData;
apd->result = CallWithInterruptsAndSmep(
ExAllocatePool,
apd->type,
apd->size
);
}
NON_PAGED_CODE void __stdcall RtlCopyMemoryKrnl(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
CopyMemoryData* cmd = (CopyMemoryData*)userData;
CallWithInterruptsAndSmep(
RtlCopyMemory,
cmd->target,
cmd->source,
cmd->size
);
}
NON_PAGED_CODE void __stdcall CallEntryPoint(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
CallEntryData* ced = (CallEntryData*)userData;
//DbgPrintEx(77, 0, "About to call entry point \n%llx \n%llx \n%llx\n", ced->entryPoint, ced->kernelMemory, ced->capcomBase);
ced->result = CallWithInterruptsAndSmep(
(PDRIVER_INITIALIZE)ced->entryPoint,
(_DRIVER_OBJECT*)ced->kernelMemory,
(PUNICODE_STRING)ced->capcomBase
);
}
NON_PAGED_CODE void __stdcall RtlZeroMemoryKrnl(MmGetSystemRoutineAddress_t pMmGetSystemRoutineAddress, PVOID userData) {
ZeroMemoryData* zmd = (ZeroMemoryData*)userData;
CallWithInterruptsAndSmep(
RtlZeroMemory,
zmd->ptr,
zmd->size
);
}
void _ZeroMemory(uintptr_t ptr, size_t size) {
ZeroMemoryData zmd = { 0 };
zmd.ptr = ptr;
zmd.size = size;
RunInKernel(RtlZeroMemoryKrnl, &zmd);
}
NTSTATUS CallEntryPoint(uintptr_t entryPoint, uintptr_t kernelMemory, uintptr_t capcomBase) {
CallEntryData ced;
ced.entryPoint = entryPoint;
ced.kernelMemory = kernelMemory;
ced.capcomBase = capcomBase;
RunInKernel(CallEntryPoint, &ced);
return ced.result;
}
void _RtlCopyMemory(uintptr_t target, uintptr_t source, size_t size) {
CopyMemoryData cmd;
cmd.target = target;
cmd.source = source;
cmd.size = size;
RunInKernel(RtlCopyMemoryKrnl, &cmd);
}
uintptr_t GetExportedFunctionByOrdinal(uintptr_t _base, uint16_t _ordinal) {
GetRoutineData grd = { 0 };
grd.base = _base;
grd.ordinal = _ordinal;
RunInKernel(GetExportedFunctionByOrdinal, &grd);
return grd.result;
}
uintptr_t GetExportedFunctionByName(uintptr_t _base, const char* _name) {
GetRoutineData grd = { 0 };
grd.base = _base;
grd.name = _name;
RunInKernel(GetExportedFunctionByName, &grd);
return grd.result;
}
uintptr_t AllocatePool(size_t size, POOL_TYPE pool_type, const bool page_align, size_t* out_size = nullptr) {
constexpr auto page_size = 0x1000u;
uintptr_t address = { 0 };
if (page_align && size % page_size != 0)
{
auto pages = size / page_size;
size = page_size * ++pages;
}
AllocatePoolData apd;
apd.type = pool_type;
apd.size = size;
RunInKernel(ExAllocatePoolKrnl, &apd);
if (out_size != nullptr)
*out_size = apd.size;
return apd.result;
}
void InitKernelFunction() {
RtlCopyMemory = GetKernelProcAddress<>("RtlCopyMemory");
RtlZeroMemory = GetKernelProcAddress<>("RtlZeroMemory");
RtlFindExportedRoutineByName = GetKernelProcAddress<>("RtlFindExportedRoutineByName");
ExAllocatePool = GetKernelProcAddress<>("ExAllocatePool");
DbgPrintEx = GetKernelProcAddress<>("DbgPrintEx");
//cout << hex << RtlCopyMemory << endl << RtlZeroMemory << endl << RtlFindExportedRoutineByName << endl << ExAllocatePool << endl;
}
int __stdcall main(const int argc, char** argv)
{
if (argc != 2)
{
printf("usage: drvmap.exe <driver>\n");
return 0;
}
InitKernelFunction();
bool capcomload = InitDriver();
printf("[+] loaded capcom driver\n");
const auto _get_module = [](std::string_view name)
{
return GetKernelModule(name);
};
const auto _get_export_name = [](uintptr_t base, const char* name)
{
return GetExportedFunctionByName(base, name);
};
const auto _get_export_ordinal = [](uintptr_t base, uint16_t ord)
{
return GetExportedFunctionByOrdinal(base, ord);
};
sizeof(SYSTEM_INFORMATION_CLASS::SystemBasicInformation);
std::vector<uint8_t> driver_image;
drvmap::util::open_binary_file(argv[1], driver_image);
drvmap::drv_image driver(driver_image);
const auto kernel_memory = AllocatePool(driver.size(), NonPagedPool, true);
cout << hex << kernel_memory << endl;
assert(kernel_memory != 0);
printf("[+] allocated 0x%llX bytes at 0x%I64X\n", driver.size(), kernel_memory);
driver.fix_imports(_get_module, _get_export_name, _get_export_ordinal);
printf("[+] imports fixed\n");
driver.map();
printf("[+] sections mapped in memory\n");
driver.relocate(kernel_memory);
printf("[+] relocated\n");
//const auto _RtlCopyMemory = capcom->get_system_routine<drvmap::structs::RtlCopyMemoryFn>(L"RtlCopyMemory");
const auto size = driver.size();
const auto source = driver.data();
const auto entry_point = kernel_memory + driver.entry_point();
_RtlCopyMemory(kernel_memory, (uintptr_t)source, size);
printf("[+] copied memory\n");
printf("[+] calling entry point at 0x%I64X\n", entry_point);
std::string capcomName(globalCapcomDriverName.begin(), globalCapcomDriverName.end());
auto status = STATUS_SUCCESS;
const auto capcom_base = GetKernelModule(capcomName);
printf("capcom base: %llx\n", capcom_base);
status = CallEntryPoint(entry_point, kernel_memory, capcom_base);
if(NT_SUCCESS(status))
{
printf("[+] successfully created driver object!\n");
const auto header_size = driver.header_size();
_ZeroMemory(kernel_memory, header_size);
printf("[+] wiped headers!\n");
}
else
{
printf("[-] creating of driver object failed! 0x%I32X\n", status);
}
UnloadCapcomDriver();
printf("[+] unloaded capcom driver: %i\n", capcomload);
return 0;
}