-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReadProcess.cpp
More file actions
335 lines (296 loc) · 7.81 KB
/
Copy pathReadProcess.cpp
File metadata and controls
335 lines (296 loc) · 7.81 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
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
#include "ReadProcess.h"
VOID SelfTerminateProcess(IN struct _KAPC *Apc, IN OUT PKNORMAL_ROUTINE *NormalRoutine, IN OUT PVOID *NormalContext, IN OUT PVOID *SystemArgument1, IN OUT PVOID *SystemArgument2)
{
PRWPM_INFO pInfo = (PRWPM_INFO)(Apc->NormalContext);
if (!MmIsAddressValid(pInfo->Address)) {
goto $EXIT;
}
if (pInfo->Type == 0)
{
_try
{
RtlCopyMemory(pInfo->Buffer,pInfo->Address,pInfo->Length);
}
_except(1)
{
goto $EXIT;
}
}
else
{
_try
{
_disable();
__writecr0(__readcr0() & 0xfffffffffffeffff);
RtlCopyMemory(pInfo->Address,pInfo->Buffer,pInfo->Length);
__writecr0(__readcr0() | 0x10000);
_enable();
}
_except(1)
{
goto $EXIT;
}
}
pInfo->Type = 2;
goto $EXIT;
$EXIT:
KeSetEvent(&(pInfo->Event), IO_NO_INCREMENT, FALSE);
}
ULONGLONG ReadProcess::GetModuleBaseWow64(_In_ PEPROCESS pEProcess, _In_ UNICODE_STRING usModuleName)
{
ULONGLONG BaseAddr = 0;
KAPC_STATE KAPC = { 0 };
KeStackAttachProcess(pEProcess, &KAPC);
PPEB32 pPeb = (PPEB32)PsGetProcessWow64Process(pEProcess);
if (pPeb == NULL || pPeb->Ldr == 0)
{
KeUnstackDetachProcess(&KAPC);
return 0;
}
// init module name
// Ergodic ModuleList
for (PLIST_ENTRY32 pListEntry = (PLIST_ENTRY32)((PPEB_LDR_DATA32)pPeb->Ldr)->InLoadOrderModuleList.Flink;
pListEntry != &((PPEB_LDR_DATA32)pPeb->Ldr)->InLoadOrderModuleList;
pListEntry = (PLIST_ENTRY32)pListEntry->Flink)
{
PLDR_DATA_TABLE_ENTRY32 LdrEntry = CONTAINING_RECORD(pListEntry, LDR_DATA_TABLE_ENTRY32, InLoadOrderLinks);
if (LdrEntry->BaseDllName.Buffer == NULL)
{
continue;
}
// Current Module Name in ListFlink
UNICODE_STRING usCurrentName = { 0 };
RtlInitUnicodeString(&usCurrentName, (PWCHAR)LdrEntry->BaseDllName.Buffer);
// cmp module name
if (RtlEqualUnicodeString(&usModuleName, &usCurrentName, TRUE))
{
BaseAddr = (ULONGLONG)LdrEntry->DllBase;
KeUnstackDetachProcess(&KAPC);
return BaseAddr;
}
}
KeUnstackDetachProcess(&KAPC);
return 0;
}
BOOLEAN ReadProcess::WIN10_ReadProcess(ULONG Addr, UINT_PTR bytestoread, PVOID output)
{
NTSTATUS nStatus;
BOOLEAN Flage = TRUE;
KAPC_STATE KAPC = { 0 };
PHYSICAL_ADDRESS PhyAddr;
BOOLEAN bIsAttached = FALSE;
KeStackAttachProcess(this->m_Peprocess, &KAPC);
bIsAttached = TRUE;
PhyAddr = MmGetPhysicalAddress((PVOID)Addr);
if (PhyAddr.QuadPart == 0)
{
Flage = FALSE;
goto TABLE1;
}
PVOID Temp = MmMapIoSpace(PhyAddr, bytestoread, (MEMORY_CACHING_TYPE)0);
if (Temp == NULL)
{
Flage = FALSE;
goto TABLE1;
}
RtlCopyMemory(output, Temp, bytestoread);
MmUnmapIoSpace(Temp, bytestoread);
TABLE1:
if (bIsAttached != FALSE)
{
KeUnstackDetachProcess(&KAPC);
}
return Flage;
}
KIRQL Open() // ¹Ø
{
KIRQL irql = KeRaiseIrqlToDpcLevel();
UINT64 cr0 = __readcr0();
cr0 &= 0xfffffffffffeffff;
__writecr0(cr0);
_disable();
return irql;
}
void Close(KIRQL irql) //¿ª
{
UINT64 cr0 = __readcr0();
cr0 |= 0x10000;
_enable();
__writecr0(cr0);
KeLowerIrql(irql);
}
VOID ReadProcess::WIN10_WritedProcess(ULONG Addr, UINT_PTR bytestoread, PVOID Buffer)
{
NTSTATUS nStatus;
KAPC_STATE KAPC = { 0 };
PHYSICAL_ADDRESS PhyAddr;
BOOLEAN bIsAttached = FALSE;
KeStackAttachProcess(this->m_Peprocess, &KAPC);
bIsAttached = TRUE;
PhyAddr = MmGetPhysicalAddress((PVOID)Addr);
PVOID Temp = MmMapIoSpace(PhyAddr, bytestoread, (MEMORY_CACHING_TYPE)TRUE);
if (Temp == NULL)
goto TABLE1;
KIRQL Irq = Open();
RtlCopyMemory(Temp, Buffer, bytestoread);
Close(Irq);
MmUnmapIoSpace(Temp, bytestoread);
TABLE1:
if (bIsAttached != FALSE)
{
KeUnstackDetachProcess(&KAPC);
}
}
VOID CHAR_TO_UNICODE_STRING(PCHAR ch, PUNICODE_STRING unicodeBuffer)
{
ANSI_STRING ansiBuffer;
UNICODE_STRING buffer_proc;
ULONG len = strlen(ch);
ansiBuffer.Buffer = ch;
ansiBuffer.Length = ansiBuffer.MaximumLength = (USHORT)len;
RtlAnsiStringToUnicodeString(unicodeBuffer, &ansiBuffer, TRUE);
}
NTSTATUS ReadProcess::SetProcess(HANDLE Pid)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
if (this->m_Peprocess != NULL)
{
ObDereferenceObject(this->m_Peprocess);
this->m_Peprocess = NULL;
}
Status = PsLookupProcessByProcessId(Pid, &this->m_Peprocess);
return Status;
}
NTSTATUS ReadProcess::MMCopyProcessMemory(PEPROCESS tagetProcess, PVOID addr, SIZE_T size, PVOID data)
{
SIZE_T BytesRead = 0;
NTSTATUS Status = MmCopyVirtualMemory(tagetProcess,
addr,
this->m_Peprocess,
data,
size,
KernelMode,
&BytesRead);
return Status;
}
NTSTATUS ReadProcess::ReadProcessMemory(IN ULONGLONG Addr, IN ULONG Size, OUT PVOID Buffer)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
if (this->m_Peprocess != NULL) {
Status = MMCopyProcessMemory(this->m_Peprocess, (PVOID)Addr, Size, Buffer);
}
return Status;
}
NTSTATUS ReadProcess::ReadProcessMemory_APC(IN ULONGLONG Addr, IN ULONG Size, OUT PVOID Buffer,int Type)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
if (MmIsAddressValid(this->m_Peprocess))
{
if (this->m_Peprocess != NULL) {
PRWPM_INFO pInfo = (PRWPM_INFO)ExAllocatePool(NonPagedPool, sizeof(RWPM_INFO));
pInfo->Address = (PVOID)Addr;
pInfo->Buffer = Buffer;
pInfo->Length = Size;
pInfo->Type = Type;
KeInitializeEvent(&(pInfo->Event), NotificationEvent, TRUE);
KeClearEvent(&(pInfo->Event));
Status = Thread::InsertApc(m_Peprocess, SelfTerminateProcess, pInfo);
if (NT_SUCCESS(Status)) {
LARGE_INTEGER interval = { 0 };
interval.QuadPart = -10000;
interval.QuadPart *= 1000;
Status = KeWaitForSingleObject(&(pInfo->Event), Executive, KernelMode, 0, &interval);
if (pInfo->Type != 2) {
Status = STATUS_UNSUCCESSFUL;
}
}
if (pInfo) {
ExFreePool(pInfo);
pInfo = NULL;
}
}
}
return Status;
}
BOOLEAN ReadProcess::WIN10_ReadProcessMemory(ULONG Addr, UINT_PTR bytestoread, PVOID output)
{
if (!this->m_Peprocess) {
return FALSE;
}
if (!MmIsAddressValid(output)) {
return FALSE;
}
return this->WIN10_ReadProcess(Addr, bytestoread, output);
}
VOID ReadProcess::WIN10_WriteProcessMemory(ULONG Addr, UINT_PTR bytestoread, PVOID Buffer)
{
if (!this->m_Peprocess) {
return;
}
if (!MmIsAddressValid(Buffer)) {
return;
}
PVOID t_Buffer = ExAllocatePool(NonPagedPool, bytestoread);
RtlCopyMemory(t_Buffer, Buffer, bytestoread);
this->WIN10_WritedProcess(Addr, bytestoread, t_Buffer);
ExFreePool(t_Buffer);
}
ULONGLONG ReadProcess::GetModuleBaseAddr(char *pModuleName)
{
UNICODE_STRING ModuleName = { 0 };
if (!this->m_Peprocess) {
return 0;
}
CHAR_TO_UNICODE_STRING(pModuleName, &ModuleName);
return this->GetModuleBaseWow64(m_Peprocess, ModuleName);
}
VOID ReadProcess::GetDllBase(PVOID Ob)
{
struct ThreadGetDll *Thread = (struct ThreadGetDll *)Ob;
Thread->BaseAddr = Thread->Ob->GetModuleBaseAddr(Thread->DllName);
KeSetEvent(&g_kEvent, 0, TRUE);
PsTerminateSystemThread(STATUS_SUCCESS);
}
ULONGLONG ReadProcess::IoGetModeuleBaseAddr(char * pModuleName)
{
if (!MmIsAddressValid((PVOID)pModuleName)) {
return 0;
}
struct ThreadGetDll Thread;
HANDLE hThread;
NTSTATUS status;
char* t_Buffer = (char*)ExAllocatePool(NonPagedPool, strlen(pModuleName));
RtlCopyMemory(t_Buffer, pModuleName, strlen(pModuleName));
Thread.DllName = t_Buffer;
Thread.Ob = this;
KeInitializeEvent(&g_kEvent, SynchronizationEvent, FALSE);
status = PsCreateSystemThread(&hThread, 0, NULL, NULL, NULL, GetDllBase, &Thread);
if (NT_SUCCESS(status))
{
ZwClose(hThread);
KeWaitForSingleObject(&g_kEvent, Executive, KernelMode, FALSE, NULL);
}
ExFreePool(t_Buffer);
return Thread.BaseAddr;
}
VOID ReadProcess::TpPr(HANDLE Pid)
{
if (!this->m_Peprocess) {
return;
}
My_PEPROCESSW7 Pe;
My_PEPROCESSW7 Game = (My_PEPROCESSW7)this->m_Peprocess;
if (!NT_SUCCESS(PsLookupProcessByProcessId(Pid, (PEPROCESS *)&Pe))) {
return;
}
Pe->UniqueProcessId = Game->UniqueProcessId;
ObDereferenceObject(Pe);
}
VOID ReadProcess::UnReadProcess()
{
if (this->m_Peprocess != NULL)
{
ObDereferenceObject(this->m_Peprocess);
this->m_Peprocess = NULL;
}
}