-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Memory.pas
632 lines (513 loc) · 16.5 KB
/
NtUtils.Memory.pas
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
unit NtUtils.Memory;
{
This module includes function for process memory management.
}
interface
uses
Ntapi.WinNt, Ntapi.ntmmapi, Ntapi.ntseapi, Ntapi.Versions, NtUtils;
type
{$SCOPEDENUMS ON}
TWSBlockFlags = set of (
Valid,
Bad,
Locked,
LargePage,
Shared,
SharedOriginal
);
{$SCOPEDENUMS OFF}
TWorkingSetBlockEx = record
VirtualAddress: Pointer;
Flags: TWSBlockFlags;
Protection: TMemoryProtection;
ShareCount: Cardinal;
Priority: Cardinal;
Node: Cardinal;
[MinOSVersion(OsWin1019H1)] Win32GraphicsProtection: TMemoryProtection;
end;
// Allocate memory in a process
function NtxAllocateMemory(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
Size: NativeUInt;
out xMemory: IMemory;
Protection: TMemoryProtection = PAGE_READWRITE;
Address: Pointer = nil;
AllocationType: TAllocationType= MEM_COMMIT
): TNtxStatus;
// Manually free memory in a process
function NtxFreeMemory(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
[in] Address: Pointer;
Size: NativeUInt;
FreeType: TAllocationType = MEM_FREE
): TNtxStatus;
// Change memory protection
function NtxProtectMemory(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
[in] Address: Pointer;
Size: NativeUInt;
Protection: TMemoryProtection;
[out, opt] PreviousProtection: PMemoryProtection = nil
): TNtxStatus;
// Change memory protection and automatically undo it later
function NtxProtectMemoryAuto(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
[in] Address: Pointer;
Size: NativeUInt;
Protection: TMemoryProtection;
out Reverter: IAutoReleasable
): TNtxStatus;
// Read memory
function NtxReadMemory(
[Access(PROCESS_VM_READ)] const hxProcess: IHandle;
[in] Address: Pointer;
const TargetBuffer: TMemory
): TNtxStatus;
// Read memory to a dynamic buffer
function NtxReadMemoryAuto(
[Access(PROCESS_VM_READ)] const hxProcess: IHandle;
[in] Address: Pointer;
Size: NativeUInt;
out Buffer: IMemory
): TNtxStatus;
// Write memory
function NtxWriteMemory(
[Access(PROCESS_VM_WRITE)] const hxProcess: IHandle;
[in] Address: Pointer;
const SourceBuffer: TMemory
): TNtxStatus;
// Flush instruction cache
function NtxFlushInstructionCache(
[Access(PROCESS_VM_WRITE)] const hxProcess: IHandle;
[in] Address: Pointer;
Size: NativeUInt
): TNtxStatus;
// Lock memory pages in working set or physical memory
[RequiredPrivilege(SE_LOCK_MEMORY_PRIVILEGE, rpWithExceptions)]
function NtxLockMemory(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
var Memory: TMemory;
MapType: TMapLockType = MAP_PROCESS
): TNtxStatus;
// Unlock locked memory pages
[RequiredPrivilege(SE_LOCK_MEMORY_PRIVILEGE, rpWithExceptions)]
function NtxUnlockMemory(
[Access(PROCESS_VM_OPERATION)] const hxProcess: IHandle;
var Memory: TMemory;
MapType: TMapLockType = MAP_PROCESS
): TNtxStatus;
{ ------------------------------- Information ------------------------------- }
// Query variable-size memory information
function NtxQueryMemory(
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
[in] Address: Pointer;
InfoClass: TMemoryInformationClass;
out xMemory: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Query mapped filename
function NtxQueryFileNameMemory(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
[in] Address: Pointer;
out Filename: String
): TNtxStatus;
// Query information about an address
function NtxQueryWorkingSetEx(
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
[in, opt] Address: Pointer;
out Attributes: TWorkingSetBlockEx
): TNtxStatus;
// Query information about multiple addresses
function NtxQueryWorkingSetExMany(
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
const Addresses: TArray<Pointer>;
out Attributes: TArray<TWorkingSetBlockEx>
): TNtxStatus;
// Make a for-in iterator for enumerating process's memory regions.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
function NtxIterateMemory(
[out, opt] Status: PNtxStatus;
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
[in, opt] StartAddress: Pointer = nil
): IEnumerable<TMemoryBasicInformation>;
// Enumerate all process's memory regions
function NtxEnumerateMemory(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
out Blocks: TArray<TMemoryBasicInformation>;
[in, opt] StartAddress: Pointer = nil
): TNtxStatus;
{ ----------------------------- Generic wrapper ----------------------------- }
type
NtxMemory = class abstract
// Query fixed-size information
class function Query<T>(
[Access(PROCESS_QUERY_LIMITED_INFORMATION)] const hxProcess: IHandle;
[in] Address: Pointer;
InfoClass: TMemoryInformationClass;
out Buffer: T
): TNtxStatus; static;
// Read a fixed-size structure
class function Read<T>(
[Access(PROCESS_VM_READ)] const hProcess: IHandle;
[in] Address: Pointer;
out Buffer: T
): TNtxStatus; static;
// Write a fixed-size structure
class function Write<T>(
[Access(PROCESS_VM_WRITE)] const hProcess: IHandle;
[in] Address: Pointer;
const Buffer: T
): TNtxStatus; static;
end;
{ Other }
// Open a session object
function NtxOpenSession(
out hxSession: IHandle;
DesiredAccess: TSessionAccessMask;
const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Open a memory partition
[MinOSVersion(OsWin10TH1)]
function NtxOpenPartition(
out hxPartition: IHandle;
DesiredAccess: TPartitionAccessMask;
const Name: String;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
implementation
uses
Ntapi.ntpsapi, Ntapi.ntdef, Ntapi.ntstatus, DelphiUtils.AutoObjects,
NtUtils.Objects, NtUtils.Ldr;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
// Auto-releasable memory in a remote process
TRemoteAutoMemory = class(TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
FProcess: IHandle;
constructor Capture(const hxProcess: IHandle; const Region: TMemory);
procedure Release; override;
end;
{ TRemoteAutoMemory<P> }
constructor TRemoteAutoMemory.Capture;
begin
inherited Capture(Region.Address, Region.Size);
FProcess := hxProcess;
end;
procedure TRemoteAutoMemory.Release;
begin
if Assigned(FProcess) and Assigned(FData) then
NtxFreeMemory(FProcess, FData, FSize);
FProcess := nil;
FData := nil;
inherited;
end;
{ Functions }
function NtxAllocateMemory;
var
Region: TMemory;
begin
Region := TMemory.From(Address, Size);
Result.Location := 'NtAllocateVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
Result.Status := NtAllocateVirtualMemory(hxProcess.Handle, Region.Address, 0,
Region.Size, AllocationType, Protection);
if Result.IsSuccess then
xMemory := TRemoteAutoMemory.Capture(hxProcess, Region);
end;
function NtxFreeMemory;
var
Memory: TMemory;
begin
Memory.Address := Address;
Memory.Size := Size;
Result.Location := 'NtFreeVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
Result.Status := NtFreeVirtualMemory(HandleOrDefault(hxProcess),
Memory.Address, Memory.Size, FreeType);
end;
type
TAutoProtectMemory = class (TCustomAutoMemory, IAutoReleasable)
FProcess: IHandle;
FProtection: TMemoryProtection;
constructor Create(
hxProcess: IHandle;
Address: Pointer;
Size: NativeUInt;
Protection: TMemoryProtection
);
procedure Release; override;
end;
constructor TAutoProtectMemory.Create;
begin
inherited Capture(Address, Size);
FProcess := hxProcess;
FProtection := Protection;
end;
function NtxProtectMemory;
var
OldProtection: TMemoryProtection;
begin
Result.Location := 'NtProtectVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
Result.Status := NtProtectVirtualMemory(HandleOrDefault(hxProcess), Address,
Size, Protection, OldProtection);
if Result.IsSuccess and Assigned(PreviousProtection) then
PreviousProtection^ := OldProtection;
end;
function NtxProtectMemoryAuto;
var
PreviousProtection: TMemoryProtection;
begin
Result := NtxProtectMemory(hxProcess, Address, Size, Protection,
@PreviousProtection);
if Result.IsSuccess and (Protection <> PreviousProtection) then
Reverter := TAutoProtectMemory.Create(hxProcess, Address, Size,
PreviousProtection);
end;
procedure TAutoProtectMemory.Release;
var
Dummy: TMemoryProtection;
begin
if Assigned(FProcess) and Assigned(FData) then
NtProtectVirtualMemory(FProcess.Handle, FData, FSize, FProtection, Dummy);
FProcess := nil;
FData := nil;
inherited;
end;
function NtxReadMemory;
begin
Result.Location := 'NtReadVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_READ);
Result.Status := NtReadVirtualMemory(HandleOrDefault(hxProcess), Address,
TargetBuffer.Address, TargetBuffer.Size, nil);
end;
function NtxReadMemoryAuto;
begin
Buffer := Auto.AllocateDynamic(Size);
Result := NtxReadMemory(hxProcess, Address, Buffer.Region);
if not Result.IsSuccess then
Buffer := nil;
end;
function NtxWriteMemory;
begin
Result.Location := 'NtWriteVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_WRITE);
Result.Status := NtWriteVirtualMemory(HandleOrDefault(hxProcess), Address,
SourceBuffer.Address, SourceBuffer.Size, nil);
end;
function NtxFlushInstructionCache;
begin
Result.Location := 'NtFlushInstructionCache';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_WRITE);
Result.Status := NtFlushInstructionCache(HandleOrDefault(hxProcess), Address,
Size);
end;
function NtxLockMemory;
begin
Result.Location := 'NtLockVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
if MapType = MAP_SYSTEM then
Result.LastCall.ExpectedPrivilege := SE_LOCK_MEMORY_PRIVILEGE;
Result.Status := NtLockVirtualMemory(HandleOrDefault(hxProcess),
Memory.Address, Memory.Size, MapType);
end;
function NtxUnlockMemory;
begin
Result.Location := 'NtUnlockVirtualMemory';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_VM_OPERATION);
if MapType = MAP_SYSTEM then
Result.LastCall.ExpectedPrivilege := SE_LOCK_MEMORY_PRIVILEGE;
Result.Status := NtUnlockVirtualMemory(HandleOrDefault(hxProcess),
Memory.Address, Memory.Size, MapType);
end;
{ Information }
function NtxQueryMemory;
var
Required: NativeUInt;
begin
Result.Location := 'NtQueryVirtualMemory';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
case InfoClass of
MemoryWorkingSetInformation, MemoryWorkingSetExInformation:
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_QUERY_INFORMATION);
else
Result.LastCall.Expects<TProcessAccessMask>(
PROCESS_QUERY_LIMITED_INFORMATION);
end;
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Required := 0;
Result.Status := NtQueryVirtualMemory(HandleOrDefault(hxProcess), Address,
InfoClass, xMemory.Data, xMemory.Size, @Required);
until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod);
end;
function NtxQueryFileNameMemory;
var
xMemory: IMemory<PNtUnicodeString>;
begin
Result := NtxQueryMemory(hxProcess, Address, MemoryMappedFilenameInformation,
IMemory(xMemory));
if Result.IsSuccess then
Filename := xMemory.Data.ToString;
end;
function GrowWorkingSet(
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
begin
Result := SizeOf(TMemoryWorkingSetInformation) + SizeOf(NativeUInt)*
PMemoryWorkingSetInformation(Memory.Data).NumberOfEntries;
Inc(Result, Result shr 4); // + 6%;
end;
function NtxQueryWorkingSetEx;
var
BulkAttributes: TArray<TWorkingSetBlockEx>;
begin
Result := NtxQueryWorkingSetExMany(hxProcess, [Address], BulkAttributes);
if Result.IsSuccess then
Attributes := BulkAttributes[0];
end;
function NtxQueryWorkingSetExMany;
var
i: Integer;
Blocks: TArray<TMemoryWorkingSetExInformation>;
begin
SetLength(Blocks, Length(Addresses));
for i := 0 to High(Blocks) do
Blocks[i].VirtualAddress := Addresses[i];
Result.Location := 'NtQueryVirtualMemory';
Result.LastCall.UsesInfoClass(MemoryWorkingSetExInformation, icQuery);
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_QUERY_INFORMATION);
Result.Status := NtQueryVirtualMemory(HandleOrDefault(hxProcess), nil,
MemoryWorkingSetExInformation, Blocks, Length(Blocks) *
SizeOf(TMemoryWorkingSetExInformation), nil);
if not Result.IsSuccess then
Exit;
SetLength(Attributes, Length(Blocks));
for i := 0 to High(Attributes) do
with Attributes[i] do
begin
VirtualAddress := Blocks[i].VirtualAddress;
Flags := [];
// bit 0
if BitTest(Blocks[i].VirtualAttributes and $1) then
Include(Flags, Valid);
// bits 1..3
ShareCount := (Blocks[i].VirtualAttributes and $E) shr 1;
// bits 4..14
Protection := (Blocks[i].VirtualAttributes and $7FF0) shr 4;
// bit 15
if BitTest(Blocks[i].VirtualAttributes and $8000) then
Include(Flags, Shared);
// bits 16..21
Node := (Blocks[i].VirtualAttributes and $3F0000) shr 16;
// bit 22
if BitTest(Blocks[i].VirtualAttributes and $400000) then
Include(Flags, Locked);
// bit 23
if BitTest(Blocks[i].VirtualAttributes and $800000) then
Include(Flags, LargePage);
// bits 24..26
Priority := (Blocks[i].VirtualAttributes and $7000000) shr 24;
// bits 27..29 are reserved
// bit 30
if BitTest(Blocks[i].VirtualAttributes and $40000000) then
Include(Flags, SharedOriginal);
// bit 31
if BitTest(Blocks[i].VirtualAttributes and $80000000) then
Include(Flags, Bad);
{$IFDEF Win64}
// bits 32..35
Priority := (Blocks[i].VirtualAttributes and $F00000000) shr 32;
{$ELSE}
Win32GraphicsProtection := 0;
{$ENDIF}
end;
end;
function NtxIterateMemory;
var
Address: Pointer;
begin
Address := StartAddress;
Result := NtxAuto.Iterate<TMemoryBasicInformation>(Status,
function (out Info: TMemoryBasicInformation): TNtxStatus
begin
// Retrieve information about the address block
Result := NtxMemory.Query(hxProcess, Address,
MemoryBasicInformation, Info);
// Going into kernel addresses fails with "invalid parameter" and should
// gracefully stop enumeration
if (UIntPtr(Address) >= MM_USER_PROBE_ADDRESS) and
(Result.Status = STATUS_INVALID_PARAMETER) then
Result.Status := STATUS_NO_MORE_ENTRIES;
if not Result.IsSuccess then
Exit;
// Advance to the next address block
Address := PByte(Info.BaseAddress) + Info.RegionSize;
end
);
end;
function NtxEnumerateMemory;
var
Block: TMemoryBasicInformation;
begin
Blocks := nil;
for Block in NtxIterateMemory(@Result, hxProcess, StartAddress) do
begin
SetLength(Blocks, Succ(Length(Blocks)));
Blocks[High(Blocks)] := Block;
end;
end;
{ NtxMemory }
class function NtxMemory.Query<T>;
begin
Result.Location := 'NtQueryVirtualMemory';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_QUERY_LIMITED_INFORMATION);
Result.Status := NtQueryVirtualMemory(HandleOrDefault(hxProcess), Address,
InfoClass, @Buffer, SizeOf(Buffer), nil);
end;
class function NtxMemory.Read<T>;
begin
Result := NtxReadMemory(hProcess, Address, TMemory.Reference(Buffer));
end;
class function NtxMemory.Write<T>;
begin
Result := NtxWriteMemory(hProcess, Address, TMemory.Reference(Buffer));
end;
function NtxOpenSession;
var
ObjAttr: PObjectAttributes;
hSession: THandle;
begin
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtOpenSession';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtOpenSession(hSession, DesiredAccess, ObjAttr^);
if Result.IsSuccess then
hxSession := Auto.CaptureHandle(hSession);
end;
function NtxOpenPartition;
var
ObjAttr: PObjectAttributes;
hPartition: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_NtOpenPartition);
if not Result.IsSuccess then
Exit;
Result := AttributeBuilder(ObjectAttributes).UseName(Name).Build(ObjAttr);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtOpenPartition';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtOpenPartition(hPartition, DesiredAccess, ObjAttr^);
if Result.IsSuccess then
hxPartition := Auto.CaptureHandle(hPartition);
end;
end.