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
|
#include <EASTL/finally.h>
#include <eastl_compat.hpp>
#include <except.h>
#include "memory.hpp"
#include "native.h"
extern "C" {
NTSTATUS NTAPI WrapperObOpenObjectByPointer(
_In_ PVOID obj, _In_ ULONG HandleAttributes,
_In_ PACCESS_STATE PassedAccessState, _In_ ACCESS_MASK DesiredAccess,
_In_ POBJECT_TYPE objType, _In_ KPROCESSOR_MODE AccessMode,
_Out_ PHANDLE Handle);
NTSTATUS NTAPI WrapperZwProtectVirtualMemory(
_In_ HANDLE ProcessHandle, _In_ _Out_ PVOID *BaseAddress,
_In_ _Out_ PSIZE_T NumberOfBytesToProtect, _In_ ULONG NewAccessProtection,
_Out_ PULONG OldAccessProtection);
NTSTATUS NTAPI WrapperMmCopyVirtualMemory(_In_ PEPROCESS SourceProcess,
_In_ PVOID SourceAddress,
_In_ PEPROCESS TargetProcess,
_In_ PVOID TargetAddress,
_In_ SIZE_T BufferSize,
_In_ KPROCESSOR_MODE PreviousMode,
_Out_ PSIZE_T ReturnSize);
NTKERNELAPI
PPEB NTAPI PsGetProcessPeb(_In_ PEPROCESS Process);
NTSTATUS NTAPI ZwQueryVirtualMemory(_In_ HANDLE ProcessHandle,
_In_ PVOID BaseAddress,
_In_ int MemoryInformationClass,
_Out_ PVOID MemoryInformation,
_In_ SIZE_T MemoryInformationLength,
_Out_ PSIZE_T ReturnLength);
NTSTATUS NTAPI ZwQuerySystemInformation(_In_ int SystemInformationClass,
_Inout_ PVOID SystemInformation,
_In_ ULONG SystemInformationLength,
_Out_opt_ PULONG ReturnLength);
NTKERNELAPI PVOID NTAPI PsGetProcessWow64Process(_In_ PEPROCESS Process);
};
static int g_waitCount = 100;
static LONGLONG g_waitTimeout = (-1LL) * 10LL * 1000LL * 250LL; // 250ms
void SetLdrInitWaitPrefs(int waitCount, LONGLONG waitTimeout) {
g_waitCount = waitCount;
g_waitTimeout = waitTimeout;
}
eastl::vector<Process> GetProcesses() {
eastl::vector<Process> result;
ULONG memoryNeeded = 0;
if (ZwQuerySystemInformation(SystemProcessInformation, NULL, 0,
&memoryNeeded) != STATUS_INFO_LENGTH_MISMATCH ||
!memoryNeeded) {
return {};
}
auto memory = new uint8_t[memoryNeeded];
const auto &fnRet = eastl::make_finally([&] { delete[] memory; });
if (!memory || !NT_SUCCESS(ZwQuerySystemInformation(
SystemProcessInformation, memory, memoryNeeded, NULL))) {
return {};
}
result.reserve(memoryNeeded / sizeof(SYSTEM_PROCESS_INFORMATION));
PSYSTEM_PROCESS_INFORMATION processEntry =
(PSYSTEM_PROCESS_INFORMATION)memory;
do {
Process p;
p.NumberOfThreads = processEntry->ThreadCount;
if (processEntry->ProcessName.Length > 0) {
p.ProcessName = eastl::wstring(processEntry->ProcessName.Buffer);
// DbgPrint("%ws : %llu\n", p.ProcessName.c_str(),
// processEntry->ProcessId);
}
p.UniqueProcessId = processEntry->ProcessId;
p.HandleCount = processEntry->HandleCount;
result.push_back(p);
processEntry = (PSYSTEM_PROCESS_INFORMATION)((uint8_t *)processEntry +
processEntry->NextEntryDelta);
} while (processEntry->NextEntryDelta);
return result;
}
NTSTATUS OpenProcess(_In_ HANDLE pid, _Out_ PEPROCESS *pep, _Out_ HANDLE *process) {
NTSTATUS status = PsLookupProcessByProcessId(pid, pep);
if (NT_SUCCESS(status)) {
status = WrapperObOpenObjectByPointer(
*pep, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, GENERIC_ALL | PROCESS_ALL_ACCESS,
*PsProcessType, KernelMode, process);
}
return status;
}
NTSTATUS CloseProcess(_In_ _Out_ PEPROCESS *pep, _In_ _Out_ HANDLE *process) {
NTSTATUS status = STATUS_UNSUCCESSFUL;
if (pep != NULL && process != NULL) {
ObDereferenceObject(*pep);
*pep = NULL;
status = ZwClose(*process);
*process = NULL;
}
return status;
}
eastl::vector<Page> GetPages(_In_ HANDLE obj, SIZE_T maxPages,
ULONG_PTR startAddress) {
NTSTATUS status;
MEMORY_BASIC_INFORMATION memory;
SIZE_T memoryNeeded = 0;
eastl::vector<Page> pages;
do {
status =
ZwQueryVirtualMemory(obj, (PVOID)startAddress, MemoryBasicInformation,
&memory, sizeof(memory), &memoryNeeded);
if (!NT_SUCCESS(status) || !memoryNeeded) {
return pages;
}
Page p;
p.BaseAddress = (uint64_t)memory.BaseAddress;
p.AllocationBase = (uint64_t)memory.AllocationBase;
p.AllocationProtect = memory.AllocationProtect;
p.RegionSize = memory.RegionSize;
p.State = memory.State;
p.Protect = memory.Protect;
p.Type = memory.Type;
pages.push_back(p);
startAddress += memory.RegionSize;
} while (pages.size() < maxPages);
return pages;
}
eastl::vector<Module> GetModules(_In_ PEPROCESS Process, _In_ BOOLEAN isWow64) {
KAPC_STATE apcstate;
KeStackAttachProcess((PKPROCESS)Process, &apcstate);
const auto &fnRet =
eastl::make_finally([&] { KeUnstackDetachProcess(&apcstate); });
eastl::vector<Module> result;
INT waitCount = 0;
if (isWow64) {
PPEB32 peb32 = (PPEB32)PsGetProcessWow64Process(Process);
if (!peb32) {
return {};
}
PPEB_LDR_DATA32 ldr32 = (PPEB_LDR_DATA32)((ULONG_PTR)peb32->Ldr);
if (!ldr32) {
return {};
}
if (!ldr32->Initialized) {
while (!ldr32->Initialized && waitCount++ < g_waitCount) {
LARGE_INTEGER wait = {.QuadPart = g_waitTimeout};
KeDelayExecutionThread(KernelMode, TRUE, &wait);
}
if (!ldr32->Initialized) {
return {};
}
}
for (PLIST_ENTRY32 listEntry =
(PLIST_ENTRY32)((ULONG_PTR)ldr32->InLoadOrderModuleList.Flink);
listEntry != (PLIST_ENTRY32)((ULONG_PTR)&ldr32->InLoadOrderModuleList);
listEntry = (PLIST_ENTRY32)((ULONG_PTR)listEntry->Flink)) {
PLDR_DATA_TABLE_ENTRY32 ldrEntry32 = CONTAINING_RECORD(
listEntry, LDR_DATA_TABLE_ENTRY32, InLoadOrderLinks);
Module mod;
mod.DllBase = ldrEntry32->DllBase;
mod.EntryPoint = ldrEntry32->EntryPoint;
mod.SizeOfImage = ldrEntry32->SizeOfImage;
mod.FullDllName.assign_convert(eastl::u16string(
(char16_t *)((ULONG_PTR)ldrEntry32->FullDllName.Buffer)));
mod.BaseDllName.assign_convert(eastl::u16string(
(char16_t *)((ULONG_PTR)ldrEntry32->BaseDllName.Buffer)));
mod.Flags = ldrEntry32->Flags;
mod.LoadCount = ldrEntry32->LoadCount;
mod.TlsIndex = ldrEntry32->TlsIndex;
result.emplace_back(mod);
}
} else {
PPEB peb = (PPEB)PsGetProcessPeb(Process);
if (!peb) {
return {};
}
PPEB_LDR_DATA ldr = (PPEB_LDR_DATA)((ULONG_PTR)peb->Ldr);
if (!ldr) {
return {};
}
if (!ldr->Initialized) {
while (!ldr->Initialized && waitCount++ < g_waitCount) {
LARGE_INTEGER wait = {.QuadPart = g_waitTimeout};
KeDelayExecutionThread(KernelMode, TRUE, &wait);
}
if (!ldr->Initialized) {
return {};
}
}
for (PLIST_ENTRY64 listEntry =
(PLIST_ENTRY64)((ULONG_PTR)ldr->InLoadOrderModuleList.Flink);
listEntry != (PLIST_ENTRY64)((ULONG_PTR)&ldr->InLoadOrderModuleList);
listEntry = (PLIST_ENTRY64)listEntry->Flink) {
PLDR_DATA_TABLE_ENTRY ldrEntry64 =
CONTAINING_RECORD(listEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
Module mod;
mod.DllBase = (uint64_t)ldrEntry64->DllBase;
mod.EntryPoint = (uint64_t)ldrEntry64->EntryPoint;
mod.SizeOfImage = ldrEntry64->SizeOfImage;
mod.FullDllName.assign_convert(
eastl::u16string((char16_t *)ldrEntry64->FullDllName.Buffer));
mod.BaseDllName.assign_convert(
eastl::u16string((char16_t *)ldrEntry64->BaseDllName.Buffer));
mod.Flags = ldrEntry64->Flags;
mod.LoadCount = ldrEntry64->LoadCount;
mod.TlsIndex = ldrEntry64->TlsIndex;
result.emplace_back(mod);
}
}
return result;
}
extern "C" int ehandler(_In_ EXCEPTION_POINTERS *ep) {
(void)ep;
return EXCEPTION_EXECUTE_HANDLER;
}
NTSTATUS ProtectVirtualMemory(_In_ PEPROCESS pep,
_In_ uint64_t addr,
_In_ SIZE_T size, _In_ ULONG newProt,
_Out_ ULONG *oldProt) {
KAPC_STATE apcState;
NTSTATUS status;
PVOID paddr = (PVOID)addr;
SIZE_T psize = size;
ULONG prot = 0;
KeStackAttachProcess((PKPROCESS)pep, &apcState);
__dpptry(ehandler, pvm) {
status =
WrapperZwProtectVirtualMemory(ZwCurrentProcess(), &paddr, &psize, newProt, &prot);
*oldProt = prot;
}
__dppexcept(pvm) { status = STATUS_ACCESS_VIOLATION; }
__dpptryend(pvm);
KeUnstackDetachProcess(&apcState);
return status;
}
NTSTATUS RestoreProtectVirtualMemory(_In_ PEPROCESS pep, _In_ uint64_t addr,
_In_ SIZE_T siz, _In_ ULONG old_prot) {
KAPC_STATE apcState;
NTSTATUS status;
PVOID paddr = (PVOID)addr;
SIZE_T psize = siz;
ULONG prot = 0;
KeStackAttachProcess((PKPROCESS)pep, &apcState);
__dpptry(ehandler, rpvm) {
status =
WrapperZwProtectVirtualMemory(ZwCurrentProcess(), &paddr, &psize, old_prot, &prot);
}
__dppexcept(rpvm) { status = STATUS_ACCESS_VIOLATION; }
__dpptryend(rpvm);
KeUnstackDetachProcess(&apcState);
return status;
}
NTSTATUS ReadVirtualMemory(_In_ PEPROCESS pep, _In_ uint64_t sourceAddress,
_In_ UCHAR *targetAddress, _In_ _Out_ SIZE_T *size) {
NTSTATUS status = STATUS_SUCCESS;
SIZE_T bytes = 0;
__dpptry(ehandler, rvm) {
status =
WrapperMmCopyVirtualMemory(pep, (PVOID)sourceAddress, PsGetCurrentProcess(),
(PVOID)targetAddress, *size, KernelMode, &bytes);
}
__dppexcept(rvm) { status = STATUS_UNSUCCESSFUL; }
__dpptryend(rvm);
*size = bytes;
return status;
}
NTSTATUS WriteVirtualMemory(_In_ PEPROCESS pep, _In_ const UCHAR *sourceAddress,
_In_ _Out_ uint64_t targetAddress,
_In_ _Out_ SIZE_T *size) {
NTSTATUS status = STATUS_SUCCESS;
SIZE_T bytes = 0;
__dpptry(ehandler, wvm) {
status =
WrapperMmCopyVirtualMemory(PsGetCurrentProcess(), (PVOID)sourceAddress, pep,
(PVOID)targetAddress, *size, KernelMode, &bytes);
}
__dppexcept(wvm) { status = STATUS_UNSUCCESSFUL; }
__dpptryend(wvm);
*size = bytes;
return status;
}
|