-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Processes.Modules.pas
688 lines (564 loc) · 18.9 KB
/
NtUtils.Processes.Modules.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
unit NtUtils.Processes.Modules;
{
These functions allow listing modules that are loaded into other processes.
}
interface
uses
Ntapi.WinNt, Ntapi.ntpsapi, NtUtils, NtUtils.Ldr, Ntapi.ntrtl,
DelphiApi.Reflection;
const
PROCESS_ENUMERATE_MODULES = PROCESS_QUERY_LIMITED_INFORMATION or
PROCESS_VM_READ;
type
TLdrxModuleInfo = NtUtils.Ldr.TLdrxModuleInfo;
TUnloadedModule = record
Sequence: Cardinal;
BaseAddress: Pointer;
[Bytes] SizeOfImage: NativeUInt;
TimeDateStamp: TUnixTime;
[Hex] CheckSum: Cardinal;
ImageName: String;
Version: TRtlUnloadEventVersion;
end;
// Enumerate modules loaded by a process
function NtxEnumerateModulesProcess(
[Access(PROCESS_ENUMERATE_MODULES)] const hxProcess: IHandle;
out Modules: TArray<TLdrxModuleInfo>;
[out, opt] IsWoW64: PBoolean = nil
): TNtxStatus;
// Enumerate native modules loaded by a process
function NtxEnumerateModulesProcessNative(
[Access(PROCESS_ENUMERATE_MODULES)] const hxProcess: IHandle;
out Modules: TArray<TLdrxModuleInfo>
): TNtxStatus;
{$IFDEF Win64}
// Enumerate WoW64 modules loaded by a process
function NtxEnumerateModulesProcessWoW64(
[Access(PROCESS_ENUMERATE_MODULES)] const hxProcess: IHandle;
out Modules: TArray<TLdrxModuleInfo>
): TNtxStatus;
{$ENDIF}
// A parent checker to use with TArrayHelper.BuildTree<TLdrxModuleInfo>
function IsParentModule(const Parent, Child: TLdrxModuleInfo): Boolean;
// Enumerate modules that were unloaded by a process
function NtxEnumerateUnloadedModulesProcess(
[Access(PROCESS_ENUMERATE_MODULES)] const hxProcess: IHandle;
out UnloadedModules: TArray<TUnloadedModule>
): TNtxStatus;
// Enumerate native modules that were unloaded by a process
function NtxEnumerateUnloadedModulesProcessNative(
[Access(PROCESS_VM_READ)] const hxProcess: IHandle;
out UnloadedModules: TArray<TUnloadedModule>
): TNtxStatus;
{$IFDEF Win64}
// Enumerate WoW64 modules that were unloaded by a process
function NtxEnumerateUnloadedModulesProcessWoW64(
[Access(PROCESS_VM_READ)] const hxProcess: IHandle;
out UnloadedModules: TArray<TUnloadedModule>
): TNtxStatus;
{$ENDIF}
implementation
uses
Ntapi.ntdef, Ntapi.ntpebteb, Ntapi.ntldr, Ntapi.ntstatus, Ntapi.ntwow64,
Ntapi.ntmmapi, Ntapi.Versions, NtUtils.Processes, NtUtils.Processes.Info,
NtUtils.SysUtils, NtUtils.Memory, NtUtils.Sections, NtUtils.ImageHlp,
NtUtils.Synchronization, DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxEnumerateModulesProcess;
var
IsTargetWoW64: Boolean;
begin
Result := RtlxAssertWoW64Compatible(hxProcess, IsTargetWoW64);
if not Result.IsSuccess then
Exit;
if Assigned(IsWoW64) then
IsWoW64^ := IsTargetWoW64;
{$IFDEF Win64}
if IsTargetWoW64 then
Result := NtxEnumerateModulesProcessWoW64(hxProcess, Modules)
else
{$ENDIF}
Result := NtxEnumerateModulesProcessNative(hxProcess, Modules);
end;
function NtxEnumerateModulesProcessNative;
var
BasicInfo: TProcessBasicInformation;
pLdr: PPebLdrData;
Ldr: TPebLdrData;
i: Integer;
pStart, pCurrent: PListEntry;
Current: TLdrDataTableEntry;
OsVersion: TWindowsVersion;
EntrySize: NativeUInt;
xMemory: IMemory;
begin
// Find the PEB
Result := NtxProcess.Query(hxProcess, ProcessBasicInformation, BasicInfo);
if not Result.IsSuccess then
Exit;
if not Assigned(BasicInfo.PebBaseAddress) then
begin
Result.Location := 'NtxEnumerateModulesProcess';
Result.Status := STATUS_UNSUCCESSFUL;
Exit;
end;
// Read a pointer to the loader data
Result := NtxMemory.Read(hxProcess, @BasicInfo.PebBaseAddress.Ldr, pLdr);
if not Result.IsSuccess then
Exit;
// Read the loader data itself
Result := NtxMemory.Read(hxProcess, pLdr, Ldr);
if Result.Matches(STATUS_PARTIAL_COPY, 'NtReadVirtualMemory') and
not Ldr.Initialized then
begin
// The loader is not initialized yet, probably we work with
// a newly created suspended process.
SetLength(Modules, 0);
// TODO: fallback to enumerating mapped images
Result.Status := STATUS_MORE_ENTRIES;
Exit;
end;
if not Result.IsSuccess then
Exit;
// Entry size depends on the OS version
OsVersion := RtlOsVersion;
// Calculate it using offsets
if OsVersion >= OsWin10RS2 then
EntrySize := SizeOf(TLdrDataTableEntry)
else if OsVersion >= OsWin10TH1 then
EntrySize := NativeUInt(@PLdrDataTableEntry(nil).SigningLevel)
else if OsVersion >= OsWin8 then
EntrySize := NativeUInt(@PLdrDataTableEntry(nil).ImplicitPathOptions)
else
EntrySize := NativeUInt(@PLdrDataTableEntry(nil).BaseNameHashValue);
// Traverse the list
i := 0;
pStart := @pLdr.InLoadOrderModuleList;
pCurrent := Ldr.InLoadOrderModuleList.Flink;
SetLength(Modules, 0);
// Allocate a buffer with enough space to hold any addressable UNICODE_STRING
xMemory := Auto.AllocateDynamic(High(Word));
while (pStart <> pCurrent) and (i <= MAX_MODULES) do
begin
// Read the entry
Result := NtxReadMemory(hxProcess, pCurrent, TMemory.From(@Current,
EntrySize));
if not Result.IsSuccess then
Exit;
// Save it
SetLength(Modules, Length(Modules) + 1);
with Modules[High(Modules)] do
begin
DllBase := Current.DllBase;
EntryPoint := Current.EntryPoint;
SizeOfImage := Current.SizeOfImage;
// Retrieve full module name
if NtxReadMemory(hxProcess, Current.FullDllName.Buffer,
TMemory.From(xMemory.Data, Current.FullDllName.Length)).IsSuccess then
begin
Current.FullDllName.Buffer := xMemory.Data;
FullDllName := Current.FullDllName.ToString;
end;
// Retrieve short module name
if NtxReadMemory(hxProcess, Current.BaseDllName.Buffer,
TMemory.From(xMemory.Data, Current.BaseDllName.Length)).IsSuccess then
begin
Current.BaseDllName.Buffer := xMemory.Data;
BaseDllName := Current.BaseDllName.ToString;
end;
Flags := Current.Flags;
TimeDateStamp := Current.TimeDateStamp;
LoadTime := Current.LoadTime;
ParentDllBase := Current.ParentDllBase;
OriginalBase := Current.OriginalBase;
if OsVersion >= OsWin8 then
LoadReason := Current.LoadReason;
end;
// Go to the next one
pCurrent := Current.InLoadOrderLinks.Flink;
Inc(i);
end;
end;
{$IFDEF Win64}
function NtxEnumerateModulesProcessWoW64;
var
Peb32: PPeb32;
pLdr: Wow64Pointer<PPebLdrData32>;
Ldr: TPebLdrData32;
i: Integer;
pStart, pCurrent: PListEntry32;
Current: TLdrDataTableEntry32;
OsVersion: TWindowsVersion;
EntrySize: NativeUInt;
Str: TNtUnicodeString;
xMemory: IMemory;
begin
// Find the 32-bit PEB
Result := NtxProcess.Query(hxProcess, ProcessWow64Information, Peb32);
if not Result.IsSuccess then
Exit;
if not Assigned(Peb32) then
begin
Result.Location := 'NtxEnumerateModulesProcessWoW64';
Result.Status := STATUS_UNSUCCESSFUL;
Exit;
end;
// Read a pointer to the WoW64 loader data
Result := NtxMemory.Read(hxProcess, @Peb32.Ldr, pLdr);
if not Result.IsSuccess then
Exit;
// Read the loader data itself
Result := NtxMemory.Read(hxProcess, pLdr, Ldr);
if Result.Matches(STATUS_PARTIAL_COPY, 'NtReadVirtualMemory') and
not Ldr.Initialized then
begin
// The loader is not initialized yet, probably we work with
// a newly created suspended process.
SetLength(Modules, 0);
// TODO: fallback to enumerating mapped images
Result.Status := STATUS_MORE_ENTRIES;
Exit;
end;
if not Result.IsSuccess then
Exit;
// Entry size depends on the OS version
OsVersion := RtlOsVersion;
// Calculate it using offsets in 32-bit structure
if OsVersion >= OsWin10RS2 then
EntrySize := SizeOf(TLdrDataTableEntry32)
else if OsVersion >= OsWin10TH1 then
EntrySize := NativeUInt(@PLdrDataTableEntry32(nil).SigningLevel)
else if OsVersion >= OsWin8 then
EntrySize := NativeUInt(@PLdrDataTableEntry32(nil).ImplicitPathOptions)
else
EntrySize := NativeUInt(@PLdrDataTableEntry32(nil).BaseNameHashValue);
// Traverse the list
i := 0;
pStart := @pLdr.Self.InLoadOrderModuleList;
pCurrent := Pointer(Ldr.InLoadOrderModuleList.Flink);
SetLength(Modules, 0);
// Allocate a buffer with enough space to hold any addressable UNICODE_STRING
xMemory := Auto.AllocateDynamic(High(Word));
Str.Buffer := xMemory.Data;
while (pStart <> pCurrent) and (i <= MAX_MODULES) do
begin
// Read the entry
Result := NtxReadMemory(hxProcess, pCurrent, TMemory.From(@Current,
EntrySize));
if not Result.IsSuccess then
Exit;
// Save it
SetLength(Modules, Length(Modules) + 1);
with Modules[High(Modules)] do
begin
DllBase := Pointer(Current.DllBase);
EntryPoint := Current.EntryPoint.Self;
SizeOfImage := Current.SizeOfImage;
// Retrieve full module name
if NtxReadMemory(hxProcess, Pointer(Current.FullDllName.Buffer),
TMemory.From(Str.Buffer, Current.FullDllName.Length)).IsSuccess then
begin
Str.Length := Current.FullDllName.Length;
Str.MaximumLength := Current.FullDllName.MaximumLength;
FullDllName := Str.ToString;
end;
// Retrieve short module name
if NtxReadMemory(hxProcess, Pointer(Current.BaseDllName.Buffer),
TMemory.From(Str.Buffer, Current.BaseDllName.Length)).IsSuccess then
begin
Str.Length := Current.BaseDllName.Length;
Str.MaximumLength := Current.BaseDllName.MaximumLength;
BaseDllName := Str.ToString;
end;
Flags := Current.Flags;
TimeDateStamp := Current.TimeDateStamp;
LoadTime := Current.LoadTime;
ParentDllBase := Pointer(Current.ParentDllBase);
OriginalBase := Current.OriginalBase;
if OsVersion >= OsWin8 then
LoadReason := Current.LoadReason;
end;
// Go to the next one
pCurrent := Pointer(Current.InLoadOrderLinks.Flink);
Inc(i);
end;
end;
{$ENDIF}
function IsParentModule;
begin
Result := (Child.ParentDllBase = Parent.DllBase);
end;
{ Unloaded modules }
function NtxEnumerateUnloadedModulesProcess;
var
IsTargetWoW64: Boolean;
begin
Result := RtlxAssertWoW64Compatible(hxProcess, IsTargetWoW64);
if not Result.IsSuccess then
Exit;
{$IFDEF Win64}
if IsTargetWoW64 then
Result := NtxEnumerateUnloadedModulesProcessWoW64(hxProcess,
UnloadedModules)
else
{$ENDIF}
Result := NtxEnumerateUnloadedModulesProcessNative(hxProcess,
UnloadedModules);
end;
function NtxEnumerateUnloadedModulesProcessNative;
var
RtlpUnloadEventTraceExSize: PCardinal;
RtlpUnloadEventTraceExNumber: PCardinal;
RtlpUnloadEventTraceEx: PPRtlUnloadEventTrace;
Size: Cardinal;
Count, ActualCount: Cardinal;
TraceRef: PRtlUnloadEventTrace;
Trace: IMemory<PRtlUnloadEventTrace>;
TraceEntry: PRtlUnloadEventTrace;
i: Integer;
begin
// Get the pointer to the trace definitions from ntdll
RtlGetUnloadEventTraceEx(RtlpUnloadEventTraceExSize,
RtlpUnloadEventTraceExNumber, RtlpUnloadEventTraceEx);
// Get the trace pointer
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceEx, TraceRef);
if not Result.IsSuccess then
Exit;
// Nothing in the trace
if not Assigned(TraceRef) then
begin
UnloadedModules := nil;
Exit;
end;
// Get the element number
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceExNumber, Count);
if not Result.IsSuccess then
Exit;
if Count = 0 then
begin
// Nothing in the trace
UnloadedModules := nil;
Exit;
end;
// Get the element size
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceExSize, Size);
if not Result.IsSuccess then
Exit;
if (Size < SizeOf(TRtlUnloadEventTrace)) or
(UInt64(Size) * Count > BUFFER_LIMIT) then
begin
Result.Location := 'NtxEnumerateUnloadedModulesProcessNative';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
// Read the trace
Result := NtxReadMemoryAuto(hxProcess, TraceRef, Size * Count, IMemory(Trace));
if not Result.IsSuccess then
Exit;
// Compute the number of elements with valid data
ActualCount := 0;
TraceEntry := Trace.Data;
for i := 0 to Pred(Count) do
begin
if Assigned(TraceEntry.BaseAddress) then
Inc(ActualCount);
Inc(PByte(TraceEntry), Size);
end;
// Save them
SetLength(UnloadedModules, ActualCount);
ActualCount := 0;
TraceEntry := Trace.Data;
for i := 0 to Pred(Count) do
begin
if Assigned(TraceEntry.BaseAddress) then
with UnloadedModules[ActualCount] do
begin
Sequence := TraceEntry.Sequence;
BaseAddress := TraceEntry.BaseAddress;
SizeOfImage := TraceEntry.SizeOfImage;
TimeDateStamp := TraceEntry.TimeDateStamp;
CheckSum := TraceEntry.CheckSum;
Version := TraceEntry.Version;
ImageName := RtlxCaptureString(TraceEntry.ImageName, 32);
Inc(ActualCount);
end;
Inc(PByte(TraceEntry), Size);
end;
end;
{$IFDEF Win64}
type
// The definition for 32-bit assembly of RtlGetUnloadEventTraceEx
TRtlGetUnloadEventTraceExAsm32 = packed record
// mov edi,edi; push ebp; mov ebp,esp; mov eax,[ebp+08]
const PROLOG_VALUE = $08458BEC8B55FF8B;
// mov [eax], XX
const OP_MOV_VALUE = $00C7;
public
[Reserved(PROLOG_VALUE)] Prolog: UInt64;
[Reserved(OP_MOV_VALUE)] OpMov1: Word;
RtlpUnloadEventTraceExSize: Wow64Pointer<PCardinal>;
Padding1: array [0..2] of Byte;
[Reserved(OP_MOV_VALUE)] OpMov2: Word;
RtlpUnloadEventTraceExNumber: Wow64Pointer<PCardinal>;
Padding2: array [0..2] of Byte;
[Reserved(OP_MOV_VALUE)] OpMov3: Word;
RtlpUnloadEventTraceEx: Wow64Pointer<PRtlUnloadEventTrace32>;
end;
PRtlGetUnloadEventTraceExAsm32 = ^TRtlGetUnloadEventTraceExAsm32;
var
// The cache for 32-bit RtlGetUnloadEventTraceEx values
RtlpUnloadEventTraceEx32CacheInitialized: TRtlRunOnce;
RtlpUnloadEventTraceExSize32Cache: PCardinal;
RtlpUnloadEventTraceExNumber32Cache: PCardinal;
RtlpUnloadEventTraceEx32Cache: PPRtlUnloadEventTrace32;
[ThreadSafe]
function RtlxGetUnloadEventTraceEx32(
out RtlpUnloadEventTraceExSize32: PCardinal;
out RtlpUnloadEventTraceExNumber32: PCardinal;
out RtlpUnloadEventTraceEx32: PPRtlUnloadEventTrace32
): TNtxStatus;
var
xNtdll32: IMemory;
ExportEntries: TArray<TExportEntry>;
Code: PRtlGetUnloadEventTraceExAsm32;
InitState: IAcquiredRunOnce;
i: Integer;
begin
if not RtlxRunOnceBegin(@RtlpUnloadEventTraceEx32CacheInitialized,
InitState) then
begin
// Retrieve the cached definitions
RtlpUnloadEventTraceExSize32 := RtlpUnloadEventTraceExSize32Cache;
RtlpUnloadEventTraceExNumber32 := RtlpUnloadEventTraceExNumber32Cache;
RtlpUnloadEventTraceEx32 := RtlpUnloadEventTraceEx32Cache;
Exit(NtxSuccess);
end;
// Map WoW64 ntdll for parsing
Result := RtlxMapKnownDll(xNtdll32, ntdll, True);
if not Result.IsSuccess then
Exit;
// Parse the export table
Result := RtlxEnumerateExportImage(ExportEntries, xNtdll32.Region, True);
if not Result.IsSuccess then
Exit;
// Locate RtlGetUnloadEventTraceEx export
i := RtlxFindExportedNameIndex(ExportEntries, 'RtlGetUnloadEventTraceEx');
if (i < 0) or ExportEntries[i].Forwards then
begin
Result.Location := 'RtlxGetUnloadEventTraceEx32';
Result.Status := STATUS_ENTRYPOINT_NOT_FOUND;
Exit;
end;
// Locate the code for RtlGetUnloadEventTraceEx
Result := RtlxExpandVirtualAddress(Pointer(Code), xNtdll32.Region, True,
ExportEntries[i].VirtualAddress, SizeOf(TRtlGetUnloadEventTraceExAsm32));
if not Result.IsSuccess then
Exit;
try
// Make sure we are parsing the correct code
if (Code.Prolog <> Code.PROLOG_VALUE) or
(Code.OpMov1 <> Code.OP_MOV_VALUE) or
(Code.OpMov2 <> Code.OP_MOV_VALUE) or
(Code.OpMov3 <> Code.OP_MOV_VALUE) then
begin
Result.Location := 'RtlxGetUnloadEventTraceEx32';
Result.Status := STATUS_UNKNOWN_REVISION;
Exit;
end;
// Locate system-wide WoW64 trace definitions
RtlpUnloadEventTraceExSize32 := Code.RtlpUnloadEventTraceExSize;
RtlpUnloadEventTraceExNumber32 := Code.RtlpUnloadEventTraceExNumber;
RtlpUnloadEventTraceEx32 := Code.RtlpUnloadEventTraceEx;
// Cache the result for future use
RtlpUnloadEventTraceExSize32Cache := RtlpUnloadEventTraceExSize32;
RtlpUnloadEventTraceExNumber32Cache := RtlpUnloadEventTraceExNumber32;
RtlpUnloadEventTraceEx32Cache := RtlpUnloadEventTraceEx32;
// Notify successful cache initialization
InitState.Complete;
except
Result.Location := 'RtlxGetUnloadEventTraceEx32';
Result.Status := STATUS_UNHANDLED_EXCEPTION;
end;
end;
function NtxEnumerateUnloadedModulesProcessWoW64;
var
RtlpUnloadEventTraceExSize32: PCardinal;
RtlpUnloadEventTraceExNumber32: PCardinal;
RtlpUnloadEventTraceEx32: PPRtlUnloadEventTrace32;
Size: Cardinal;
Count, ActualCount: Cardinal;
TraceRef: Wow64Pointer<PRtlUnloadEventTrace32>;
Trace: IMemory<PRtlUnloadEventTrace32>;
TraceEntry: PRtlUnloadEventTrace32;
i: Integer;
begin
UnloadedModules := nil;
// Get the pointer to the trace definitions from WoW64 ntdll
Result := RtlxGetUnloadEventTraceEx32(RtlpUnloadEventTraceExSize32,
RtlpUnloadEventTraceExNumber32, RtlpUnloadEventTraceEx32);
if not Result.IsSuccess then
Exit;
// Get the trace pointer
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceEx32, TraceRef);
if not Result.IsSuccess then
Exit;
// Nothing in the trace
if TraceRef.Value = 0 then
Exit;
// Get the element number
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceExNumber32, Count);
if not Result.IsSuccess then
Exit;
// Nothing in the trace
if Count = 0 then
Exit;
// Get the element size
Result := NtxMemory.Read(hxProcess, RtlpUnloadEventTraceExSize32, Size);
if not Result.IsSuccess then
Exit;
if (Size < SizeOf(TRtlUnloadEventTrace32)) or
(UInt64(Size) * Count > BUFFER_LIMIT) then
begin
Result.Location := 'NtxEnumerateUnloadedModulesProcessNative';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
// Read the trace
Result := NtxReadMemoryAuto(hxProcess, TraceRef.Self,
Size * Count, IMemory(Trace));
if not Result.IsSuccess then
Exit;
// Compute the number of elements with valid data
ActualCount := 0;
TraceEntry := Trace.Data;
for i := 0 to Pred(Count) do
begin
if TraceEntry.BaseAddress.Value <> 0 then
Inc(ActualCount);
Inc(PByte(TraceEntry), Size);
end;
// Save them
SetLength(UnloadedModules, ActualCount);
ActualCount := 0;
TraceEntry := Trace.Data;
for i := 0 to Pred(Count) do
begin
if TraceEntry.BaseAddress.Value <> 0 then
with UnloadedModules[ActualCount] do
begin
Sequence := TraceEntry.Sequence;
BaseAddress := TraceEntry.BaseAddress.Self;
SizeOfImage := TraceEntry.SizeOfImage;
TimeDateStamp := TraceEntry.TimeDateStamp;
CheckSum := TraceEntry.CheckSum;
Version := TraceEntry.Version;
ImageName := RtlxCaptureString(TraceEntry.ImageName, 32);
Inc(ActualCount);
end;
Inc(PByte(TraceEntry), Size);
end;
end;
{$ENDIF}
end.