Repository navigation
Expand file tree
/
Copy pathMemory.c
More file actions
231 lines (178 loc) · 4.95 KB
/
Copy pathMemory.c
File metadata and controls
231 lines (178 loc) · 4.95 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 "Memory.h"
#define PTE_BASE 0xFFFFF68000000000ull
typedef struct HardwarePteX64 {
ULONG64 valid : 1; //!< [0]
ULONG64 write : 1; //!< [1]
ULONG64 owner : 1; //!< [2]
ULONG64 write_through : 1; //!< [3]
ULONG64 cache_disable : 1; //!< [4]
ULONG64 accessed : 1; //!< [5]
ULONG64 dirty : 1; //!< [6]
ULONG64 large_page : 1; //!< [7]
ULONG64 global : 1; //!< [8]
ULONG64 copy_on_write : 1; //!< [9]
ULONG64 prototype : 1; //!< [10]
ULONG64 reserved0 : 1; //!< [11]
ULONG64 page_frame_number : 36; //!< [12:47]
ULONG64 reserved1 : 4; //!< [48:51]
ULONG64 software_ws_index : 11; //!< [52:62]
ULONG64 no_execute : 1; //!< [63]
}HardwarePte, *PHardwarePte;
ULONG64 GetPTEBase()
{
static ULONG64 pteBase = 0;
if (pteBase) return pteBase;
RTL_OSVERSIONINFOEXW version = {0};
RtlGetVersion(&version);
/*
Windows 10(20H2) 19042
Windows 10(2004) 19041
Windows 10(1909) 18363
Windows 10(1903) 18362
Windows 10(1809) 17763
Windows 10(1803) 17134
Windows 10(1709) 16299
Windows 10(1703) 15063
Windows 10(1607) 14393
Windows 10(1511) 10586
Windows 10 (1507) 10240
Windows 8.1(更新1) MajorVersion = 6 MinorVersion = 3 BuildNumber = 9600
Windows 8.1 MajorVersion = 6 MinorVersion = 3 BuildNumber = 9200
Windows 8 MajorVersion = 6 MinorVersion = 2 BuildNumber = 9200
*/
if (version.dwBuildNumber == 7600 || version.dwBuildNumber == 7601)
{
pteBase = PTE_BASE;
}
else if (version.dwBuildNumber > 14393)
{
//取PTE
UNICODE_STRING unName = {0};
RtlInitUnicodeString(&unName,L"MmGetVirtualForPhysical");
PUCHAR func = (PUCHAR)MmGetSystemRoutineAddress(&unName);
pteBase = *(PULONG64)(func + 0x22);
}
else
{
pteBase = PTE_BASE;
}
return pteBase;
}
ULONG64 GetPte(ULONG64 VirtualAddress)
{
ULONG64 pteBase = GetPTEBase();
return ((VirtualAddress >> 9) & 0x7FFFFFFFF8) + pteBase;
}
ULONG64 GetPde(ULONG64 VirtualAddress)
{
ULONG64 pteBase = GetPTEBase();
ULONG64 pte = GetPte(VirtualAddress);
return ((pte >> 9) & 0x7FFFFFFFF8) + pteBase;
}
ULONG64 GetPdpte(ULONG64 VirtualAddress)
{
ULONG64 pteBase = GetPTEBase();
ULONG64 pde = GetPde(VirtualAddress);
return ((pde >> 9) & 0x7FFFFFFFF8) + pteBase;
}
ULONG64 GetPml4e(ULONG64 VirtualAddress)
{
ULONG64 pteBase = GetPTEBase();
ULONG64 ppe = GetPdpte(VirtualAddress);
return ((ppe >> 9) & 0x7FFFFFFFF8) + pteBase;
}
BOOLEAN SetExecutePage(ULONG64 VirtualAddress, ULONG size)
{
ULONG64 endAddress = (VirtualAddress + size) & (~0xFFF);
ULONG64 startAddress = VirtualAddress & (~0xFFF);
int count = 0;
while (endAddress >= startAddress)
{
PHardwarePte pde = GetPde(startAddress);
if (MmIsAddressValid(pde) && pde->valid)
{
pde->write = 1;
pde->no_execute = 0;
}
PHardwarePte pte = GetPte(startAddress);
if (MmIsAddressValid(pte) && pte->valid)
{
pte->write = 1;
pte->no_execute = 0;
}
DbgPrintEx(77, 0, "[db]:pde %llx pte %llx %d\r\n", pde, pte, count);
startAddress += PAGE_SIZE;
}
return TRUE;
}
PVOID AllocateMemory(HANDLE pid, SIZE_T size)
{
PEPROCESS Process = NULL;
KAPC_STATE kApcState = { 0 };
PVOID BaseAddress = 0;
NTSTATUS status = PsLookupProcessByProcessId(pid, &Process);
if (!NT_SUCCESS(status))
{
return NULL;
}
if (PsGetProcessExitStatus(Process) != STATUS_PENDING)
{
ObDereferenceObject(Process);
return NULL;
}
KeStackAttachProcess(Process, &kApcState);
status = ZwAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress, 0, &size, MEM_COMMIT, PAGE_READWRITE);
if (NT_SUCCESS(status))
{
memset(BaseAddress,0, size);
SetExecutePage(BaseAddress, size);
}
KeUnstackDetachProcess(&kApcState);
return BaseAddress;
}
NTSTATUS FreeMemory(HANDLE pid, PVOID BaseAddress, SIZE_T size)
{
PEPROCESS Process = NULL;
KAPC_STATE kApcState = { 0 };
NTSTATUS status = PsLookupProcessByProcessId(pid, &Process);
if (!NT_SUCCESS(status))
{
return STATUS_NOT_FOUND;
}
if (PsGetProcessExitStatus(Process) != STATUS_PENDING)
{
ObDereferenceObject(Process);
return STATUS_UNSUCCESSFUL;
}
KeStackAttachProcess(Process, &kApcState);
if (BaseAddress)
{
status = ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &size, MEM_RELEASE);
}
KeUnstackDetachProcess(&kApcState);
return status;
}
PVOID AllocateMemoryNotExecute(HANDLE pid, SIZE_T size)
{
PEPROCESS Process = NULL;
KAPC_STATE kApcState = { 0 };
PVOID BaseAddress = 0;
NTSTATUS status = PsLookupProcessByProcessId(pid, &Process);
if (!NT_SUCCESS(status))
{
return NULL;
}
if (PsGetProcessExitStatus(Process) != STATUS_PENDING)
{
ObDereferenceObject(Process);
return NULL;
}
KeStackAttachProcess(Process, &kApcState);
status = ZwAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress, 0, &size, MEM_COMMIT, PAGE_READWRITE);
if (NT_SUCCESS(status))
{
memset(BaseAddress, 0, size);
}
KeUnstackDetachProcess(&kApcState);
return BaseAddress;
}