forked from can1357/ThePerfectInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryController.h
491 lines (414 loc) · 11.8 KB
/
MemoryController.h
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#pragma once
#include <iostream>
#include <intrin.h>
#include <inttypes.h>
#include <functional>
#include "Error.h"
#include "LockedMemory.h"
#include "KernelRoutines.h"
#include "CapcomLoader.h"
#include "KernelHelper.h"
#define PFN_TO_PAGE(pfn) ( pfn << 12 )
#define OBJ_KERNEL_HANDLE 0x00000200L
#define OBJ_CASE_INSENSITIVE 0x00000040L
#pragma pack(push, 1)
typedef union CR3_
{
uint64_t value;
struct
{
uint64_t ignored_1 : 3;
uint64_t write_through : 1;
uint64_t cache_disable : 1;
uint64_t ignored_2 : 7;
uint64_t pml4_p : 40;
uint64_t reserved : 12;
};
} PTE_CR3;
typedef union VIRT_ADDR_
{
uint64_t value;
void *pointer;
struct
{
uint64_t offset : 12;
uint64_t pt_index : 9;
uint64_t pd_index : 9;
uint64_t pdpt_index : 9;
uint64_t pml4_index : 9;
uint64_t reserved : 16;
};
} VIRT_ADDR;
typedef uint64_t PHYS_ADDR;
typedef union PML4E_
{
uint64_t value;
struct
{
uint64_t present : 1;
uint64_t rw : 1;
uint64_t user : 1;
uint64_t write_through : 1;
uint64_t cache_disable : 1;
uint64_t accessed : 1;
uint64_t ignored_1 : 1;
uint64_t reserved_1 : 1;
uint64_t ignored_2 : 4;
uint64_t pdpt_p : 40;
uint64_t ignored_3 : 11;
uint64_t xd : 1;
};
} PML4E;
typedef union PDPTE_
{
uint64_t value;
struct
{
uint64_t present : 1;
uint64_t rw : 1;
uint64_t user : 1;
uint64_t write_through : 1;
uint64_t cache_disable : 1;
uint64_t accessed : 1;
uint64_t dirty : 1;
uint64_t page_size : 1;
uint64_t ignored_2 : 4;
uint64_t pd_p : 40;
uint64_t ignored_3 : 11;
uint64_t xd : 1;
};
} PDPTE;
typedef union PDE_
{
uint64_t value;
struct
{
uint64_t present : 1;
uint64_t rw : 1;
uint64_t user : 1;
uint64_t write_through : 1;
uint64_t cache_disable : 1;
uint64_t accessed : 1;
uint64_t dirty : 1;
uint64_t page_size : 1;
uint64_t ignored_2 : 4;
uint64_t pt_p : 40;
uint64_t ignored_3 : 11;
uint64_t xd : 1;
};
} PDE;
typedef union PTE_
{
uint64_t value;
VIRT_ADDR vaddr;
struct
{
uint64_t present : 1;
uint64_t rw : 1;
uint64_t user : 1;
uint64_t write_through : 1;
uint64_t cache_disable : 1;
uint64_t accessed : 1;
uint64_t dirty : 1;
uint64_t pat : 1;
uint64_t global : 1;
uint64_t ignored_1 : 3;
uint64_t page_frame : 40;
uint64_t ignored_3 : 11;
uint64_t xd : 1;
};
} PTE;
#pragma pack(pop)
typedef struct _OBJECT_ATTRIBUTES
{
ULONG Length;
HANDLE RootDirectory;
PVOID ObjectName;
ULONG Attributes;
PVOID SecurityDescriptor;
PVOID SecurityQualityOfService;
} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES;
typedef LARGE_INTEGER PHYSICAL_ADDRESS, *PPHYSICAL_ADDRESS;
typedef struct _PHYSICAL_MEMORY_RANGE
{
PHYSICAL_ADDRESS BaseAddress;
LARGE_INTEGER NumberOfBytes;
} PHYSICAL_MEMORY_RANGE, *PPHYSICAL_MEMORY_RANGE;
struct MemoryController
{
template<typename T>
T& ReadPhysicalUnsafe( uint64_t Pa )
{
return *( T* ) ( PhysicalMemoryBegin + Pa );
}
PUCHAR PhysicalMemoryBegin;
SIZE_T PhysicalMemorySize;
uint64_t TargetDirectoryBase;
uint64_t CurrentDirectoryBase;
uint64_t CurrentEProcess;
uint64_t UniqueProcessIdOffset;
uint64_t DirectoryTableBaseOffset;
uint64_t ActiveProcessLinksOffset;
NTSTATUS CreationStatus;
uint64_t FindEProcess( uint64_t Pid )
{
uint64_t EProcess = this->CurrentEProcess;
do
{
if ( this->ReadVirtual<uint64_t>( ( PUCHAR ) EProcess + this->UniqueProcessIdOffset ) == Pid )
return EProcess;
LIST_ENTRY Le = this->ReadVirtual<LIST_ENTRY>( ( PUCHAR ) EProcess + this->ActiveProcessLinksOffset );
EProcess = ( uint64_t ) Le.Flink - this->ActiveProcessLinksOffset;
}
while ( EProcess != this->CurrentEProcess );
return 0;
}
void AttachTo( uint64_t EProcess )
{
this->TargetDirectoryBase = this->ReadVirtual<uint64_t>( ( PUCHAR ) EProcess + this->DirectoryTableBaseOffset );
}
void Detach()
{
this->TargetDirectoryBase = this->CurrentDirectoryBase;
}
struct PageTableInfo
{
PML4E* Pml4e;
PDPTE* Pdpte;
PDE* Pde;
PTE* Pte;
};
PageTableInfo QueryPageTableInfo( PVOID Va )
{
PageTableInfo Pi = { 0,0,0,0 };
VIRT_ADDR Addr = { ( uint64_t ) Va };
PTE_CR3 Cr3 = { TargetDirectoryBase };
{
uint64_t a = PFN_TO_PAGE( Cr3.pml4_p ) + sizeof( PML4E ) * Addr.pml4_index;
if ( a > this->PhysicalMemorySize )
return Pi;
PML4E& e = ReadPhysicalUnsafe<PML4E>( a );
if ( !e.present )
return Pi;
Pi.Pml4e = &e;
}
{
uint64_t a = PFN_TO_PAGE( Pi.Pml4e->pdpt_p ) + sizeof( PDPTE ) * Addr.pdpt_index;
if ( a > this->PhysicalMemorySize )
return Pi;
PDPTE& e = ReadPhysicalUnsafe<PDPTE>( a );
if ( !e.present )
return Pi;
Pi.Pdpte = &e;
}
{
uint64_t a = PFN_TO_PAGE( Pi.Pdpte->pd_p ) + sizeof( PDE ) * Addr.pd_index;
if ( a > this->PhysicalMemorySize )
return Pi;
PDE& e = ReadPhysicalUnsafe<PDE>( a );
if ( !e.present )
return Pi;
Pi.Pde = &e;
if ( Pi.Pde->page_size )
return Pi;
}
{
uint64_t a = PFN_TO_PAGE( Pi.Pde->pt_p ) + sizeof( PTE ) * Addr.pt_index;
if ( a > this->PhysicalMemorySize )
return Pi;
PTE& e = ReadPhysicalUnsafe<PTE>( a );
if ( !e.present )
return Pi;
Pi.Pte = &e;
}
return Pi;
}
uint64_t VirtToPhys( PVOID Va )
{
auto Info = QueryPageTableInfo( Va );
if ( !Info.Pde )
return 0;
uint64_t Pa = 0;
if ( Info.Pde->page_size )
{
Pa = PFN_TO_PAGE( Info.Pde->pt_p );
Pa += ( uint64_t ) Va & ( 0x200000 - 1 );
}
else
{
if ( !Info.Pte )
return 0;
Pa = PFN_TO_PAGE( Info.Pte->page_frame );
Pa += ( uint64_t ) Va & ( 0x1000 - 1 );
}
return Pa;
}
void IterPhysRegion( PVOID StartVa, SIZE_T Size, std::function<void( PVOID Va, uint64_t, SIZE_T )> Fn )
{
PUCHAR It = ( PUCHAR ) StartVa;
PUCHAR End = It + Size;
while ( It < End )
{
SIZE_T Size = ( PUCHAR ) ( ( ( uint64_t ) It + 0x1000 ) & ( ~0xFFF ) ) - It;
if ( ( It + Size ) > End )
Size = End - It;
uint64_t Pa = VirtToPhys( It );
Fn( It, Pa, Size );
It += Size;
}
}
void AttachIfCanRead( uint64_t EProcess, PVOID Adr )
{
this->AttachTo( EProcess );
if ( !this->VirtToPhys( Adr ) )
this->Detach();
}
SIZE_T ReadVirtual( PVOID Src, PVOID Dst, SIZE_T Size )
{
PUCHAR It = ( PUCHAR ) Dst;
SIZE_T BytesRead = 0;
this->IterPhysRegion( Src, Size, [ & ] ( PVOID Va, uint64_t Pa, SIZE_T Sz )
{
if ( Pa )
{
BytesRead += Sz;
memcpy( It, PhysicalMemoryBegin + Pa, Sz );
It += Sz;
}
} );
return BytesRead;
}
SIZE_T WriteVirtual( PVOID Src, PVOID Dst, SIZE_T Size )
{
PUCHAR It = ( PUCHAR ) Src;
SIZE_T BytesRead = 0;
this->IterPhysRegion( Dst, Size, [ & ] ( PVOID Va, uint64_t Pa, SIZE_T Sz )
{
if ( Pa )
{
BytesRead += Sz;
memcpy( PhysicalMemoryBegin + Pa, It, Sz );
It += Sz;
}
} );
return BytesRead;
}
template<typename T>
T ReadVirtual( PVOID From )
{
char Buffer[ sizeof( T ) ];
this->ReadVirtual( From, Buffer, sizeof( T ) );
return *( T* ) ( Buffer );
}
template<typename T>
void WriteVirtual( PVOID To, const T& Data )
{
this->WriteVirtual( ( PVOID ) &Data, To, sizeof( T ) );
}
};
static MemoryController Mc_InitContext( CapcomContext** CpCtxReuse = 0, KernelContext** KrCtxReuse = 0 )
{
assert( Np_LockSections() );
KernelContext* KrCtx = Kr_InitContext();
CapcomContext* CpCtx = Cl_InitContext();
assert( CpCtx );
assert( KrCtx );
Khu_Init( CpCtx, KrCtx );
printf( "[+] Mapping physical memory to user-mode!\n" );
NON_PAGED_DATA static MemoryController Controller = { 0 };
NON_PAGED_DATA static auto k_ZwOpenSection = KrCtx->GetProcAddress<>( "ZwOpenSection" );
NON_PAGED_DATA static auto k_ZwMapViewOfSection = KrCtx->GetProcAddress<>( "ZwMapViewOfSection" );
NON_PAGED_DATA static auto k_ZwClose = KrCtx->GetProcAddress<>( "ZwClose" );
NON_PAGED_DATA static auto k_PsGetCurrentProcess = KrCtx->GetProcAddress<>( "PsGetCurrentProcess" );
NON_PAGED_DATA static auto k_PsGetCurrentProcessId = KrCtx->GetProcAddress<>( "PsGetCurrentProcessId" );
NON_PAGED_DATA static auto k_PsGetProcessId = KrCtx->GetProcAddress<>( "PsGetProcessId" );
NON_PAGED_DATA static auto k_MmGetPhysicalMemoryRanges = KrCtx->GetProcAddress<PPHYSICAL_MEMORY_RANGE( *)( )>( "MmGetPhysicalMemoryRanges" );
NON_PAGED_DATA static wchar_t PhysicalMemoryName[] = L"\\Device\\PhysicalMemory";
NON_PAGED_DATA static OBJECT_ATTRIBUTES PhysicalMemoryAttributes;
NON_PAGED_DATA static UNICODE_STRING PhysicalMemoryNameUnicode;
PhysicalMemoryNameUnicode.Buffer = PhysicalMemoryName;
PhysicalMemoryNameUnicode.Length = sizeof( PhysicalMemoryName ) - 2;
PhysicalMemoryNameUnicode.MaximumLength = sizeof( PhysicalMemoryName );
PhysicalMemoryAttributes.Length = sizeof( PhysicalMemoryAttributes );
PhysicalMemoryAttributes.Attributes = OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE;
PhysicalMemoryAttributes.ObjectName = &PhysicalMemoryNameUnicode;
PhysicalMemoryAttributes.RootDirectory = 0;
PhysicalMemoryAttributes.SecurityDescriptor = 0;
PhysicalMemoryAttributes.SecurityQualityOfService = 0;
CpCtx->ExecuteInKernel( NON_PAGED_LAMBDA()
{
auto Range = k_MmGetPhysicalMemoryRanges();
while ( Range->NumberOfBytes.QuadPart )
{
Controller.PhysicalMemorySize = max( Controller.PhysicalMemorySize, Range->BaseAddress.QuadPart + Range->NumberOfBytes.QuadPart );
Range++;
}
HANDLE PhysicalMemoryHandle = 0;
Controller.CreationStatus = Khk_CallPassive( k_ZwOpenSection, &PhysicalMemoryHandle, uint64_t( SECTION_ALL_ACCESS ), &PhysicalMemoryAttributes );
if ( !Controller.CreationStatus )
{
Controller.CreationStatus = Khk_CallPassive
(
k_ZwMapViewOfSection,
PhysicalMemoryHandle,
NtCurrentProcess(),
&Controller.PhysicalMemoryBegin,
0ull,
0ull,
0ull,
&Controller.PhysicalMemorySize,
1ull,
0,
PAGE_READWRITE
);
if ( !Controller.CreationStatus )
{
Controller.CurrentEProcess = k_PsGetCurrentProcess();
Controller.CurrentDirectoryBase = __readcr3();
uint64_t Pid = k_PsGetProcessId( Controller.CurrentEProcess );
uint32_t PidOffset = *( uint32_t* ) ( ( PUCHAR ) k_PsGetProcessId + 3 );
if ( PidOffset < 0x400 && *( uint64_t* ) ( Controller.CurrentEProcess + PidOffset ) == Pid )
{
Controller.UniqueProcessIdOffset = PidOffset;
Controller.ActiveProcessLinksOffset = Controller.UniqueProcessIdOffset + 0x8;
}
for ( int i = 0; i < 0x400; i += 0x8 )
{
uint64_t* Ptr = (uint64_t*)(Controller.CurrentEProcess + i);
if ( !Controller.UniqueProcessIdOffset && Ptr[ 0 ] & 0xFFFFFFFF == Pid && ( Ptr[ 1 ] > 0xffff800000000000 ) && ( Ptr[ 2 ] > 0xffff800000000000 ) && ( ( Ptr[ 1 ] & 0xF ) == ( Ptr[ 2 ] & 0xF ) ) )
{
Controller.UniqueProcessIdOffset = i;
Controller.ActiveProcessLinksOffset = Controller.UniqueProcessIdOffset + 0x8;
}
else if ( !Controller.DirectoryTableBaseOffset && Ptr[ 0 ] == __readcr3() )
{
Controller.DirectoryTableBaseOffset = i;
}
}
}
k_ZwClose( PhysicalMemoryHandle );
}
} );
if ( !Controller.UniqueProcessIdOffset )
Controller.CreationStatus = 1;
if ( !Controller.DirectoryTableBaseOffset )
Controller.CreationStatus = 2;
printf( "[+] PhysicalMemoryBegin: %16llx\n", Controller.PhysicalMemoryBegin );
printf( "[+] PhysicalMemorySize: %16llx\n", Controller.PhysicalMemorySize );
printf( "[+] CurrentProcessCr3: %16llx\n", Controller.CurrentDirectoryBase );
printf( "[+] CurrentEProcess: %16llx\n", Controller.CurrentEProcess );
printf( "[+] DirectoryTableBase@ %16llx\n", Controller.DirectoryTableBaseOffset );
printf( "[+] UniqueProcessId@ %16llx\n", Controller.UniqueProcessIdOffset );
printf( "[+] ActiveProcessLinks@ %16llx\n", Controller.ActiveProcessLinksOffset );
printf( "[+] Status: %16llx\n", Controller.CreationStatus );
Controller.TargetDirectoryBase = Controller.CurrentDirectoryBase;
if ( !CpCtxReuse )
Cl_FreeContext( CpCtx );
else
*CpCtxReuse = CpCtx;
if ( !KrCtxReuse )
Kr_FreeContext( KrCtx );
else
*KrCtxReuse = KrCtx;
return Controller;
}