forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMap.RegionData.pas
More file actions
452 lines (406 loc) · 12.4 KB
/
Copy pathMemoryMap.RegionData.pas
File metadata and controls
452 lines (406 loc) · 12.4 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
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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : MemoryMap
// * Unit Name : MemoryMap.RegionData.pas
// * Purpose : Êëàññ õðàíèò èíôîðìàöèþ î ðåãèîíå
// * Author : Àëåêñàíäð (Rouse_) Áàãåëü
// * Copyright : © Fangorn Wizards Lab 1998 - 2013.
// * Version : 1.0
// * Home Page : http://rouse.drkb.ru
// * Home Blog : http://alexander-bagel.blogspot.ru
// ****************************************************************************
// * Stable Release : http://rouse.drkb.ru/winapi.php#pmm2
// * Latest Source : https://github.com/AlexanderBagel/ProcessMemoryMap
// ****************************************************************************
//
unit MemoryMap.RegionData;
interface
uses
Winapi.Windows,
System.Classes,
System.TypInfo,
System.SysUtils,
Generics.Collections,
MemoryMap.Heaps,
MemoryMap.Threads,
MemoryMap.PEImage;
type
// Òèï ðåãèîíà
TRegionType = (
rtDefault,
rtHeap, // ðåãèîí ñîäåðæèò ýëåìåíòû êó÷è
rtThread, // ðåãèîí ñîäåðæèò ñòåê ïîòîêà èëè TEB
rtSystem, // ðåãèîí ñîäåðæèò ñèñòåìíûå äàííûå (PEB/KUSER_SHARED_DATA è ò.ï.)
rtExecutableImage // ðåãèîí ñîäåðæèò îáðàç èñïîëíÿåìîãî PE ôàéëà
);
TSystemData = record
Description: ShortString;
Address: Pointer;
end;
TContainItemType = (itHeapBlock, itThreadData,
itStackFrame, itSEHFrame, itSystem);
TContainItem = record
ItemType: TContainItemType;
function Hash: string;
case Integer of
0: (Heap: THeapData);
1: (ThreadData: TThreadData);
2: (StackFrame: TThreadStackEntry);
3: (SEH: TSEHEntry);
4: (System: TSystemData);
end;
TRegionData = class
private
FParent: TRegionData;
FRegionType: TRegionType;
FMBI: TMemoryBasicInformation;
FDetails: string;
FRegionVisible: Boolean;
FHiddenRegionCount: Integer;
FTotalRegionSize: NativeUInt;
FHeap: THeapData;
FThread: TThreadData;
FPEBData: TSystemData;
FSection: TSection;
FContains: TList<TContainItem>;
FDirectories: TList<TDirectory>;
FShared: Boolean;
FSharedCount: Integer;
FFiltered: Boolean;
protected
procedure IncHiddenRegionCount;
procedure IncTotalRegionSize(Value: NativeUInt);
procedure IncTotalHeapSize(Value: NativeUInt);
procedure SetRegionType(Value: TRegionType);
procedure SetMBI(Value: TMemoryBasicInformation);
procedure SetMBIRegionSize(Value: SIZE_T);
procedure SetDetails(Value: string);
procedure SetRegionVisible(Value: Boolean);
procedure SetTotalRegionSize(Value: NativeUInt);
procedure SetParent(Value: TRegionData);
procedure SetHeap(Value: THeapData);
procedure SetThread(Value: TThreadData);
procedure SetThreadIDAndWow(Value: Integer; Wow64: Boolean);
procedure SetPEBData(Value: TSystemData);
procedure SetSection(Value: TSection);
procedure SetShared(const Value: Boolean);
procedure SetSharedCount(const Value: Integer);
procedure SetFiltered(Value: Boolean);
protected
procedure InitFromString(const Value: string);
function GetAsString: string;
public
constructor Create;
destructor Destroy; override;
property RegionType: TRegionType read FRegionType;
property MBI: TMemoryBasicInformation read FMBI;
property Details: string read FDetails;
property RegionVisible: Boolean read FRegionVisible;
property HiddenRegionCount: Integer read FHiddenRegionCount;
property Parent: TRegionData read FParent;
property TotalRegionSize: NativeUInt read FTotalRegionSize;
property Heap: THeapData read FHeap;
property Thread: TThreadData read FThread;
property SystemData: TSystemData read FPEBData;
property Section: TSection read FSection;
property Shared: Boolean read FShared;
property SharedCount: Integer read FSharedCount;
property Directory: TList<TDirectory> read FDirectories;
property Contains: TList<TContainItem> read FContains;
property Filtered: Boolean read FFiltered;
end;
implementation
{ TContainItem }
function TContainItem.Hash: string;
begin
Result := IntToStr(Integer(ItemType));
case ItemType of
itHeapBlock:
Result := Result + IntToStr(Heap.ID) + IntToStr(Heap.Entry.Address) +
IntToStr(Heap.Entry.Size) + IntToStr(Heap.Entry.Flags);
itThreadData:
Result := Result +
GetEnumName(TypeInfo(TThreadInfo), Integer(ThreadData.Flag)) +
IntToStr(ThreadData.ThreadID) + IntToStr(NativeUInt(ThreadData.Address));
itStackFrame:
Result := Result +
IntToStr(StackFrame.ThreadID) + IntToStr(StackFrame.Data.AddrPC.Offset) +
IntToStr(StackFrame.Data.AddrFrame.Offset) +
IntToStr(StackFrame.Data.AddrReturn.Offset);
itSEHFrame:
Result := Result +
IntToStr(SEH.ThreadID) + IntToStr(NativeUInt(SEH.Address)) +
IntToStr(NativeUInt(SEH.Previous)) +
IntToStr(NativeUInt(SEH.Handler));
itSystem:
Result := Result + string(System.Description) +
IntToStr(NativeUInt(System.Address));
end;
end;
{ TRegionData }
constructor TRegionData.Create;
begin
FRegionVisible := True;
FContains := TList<TContainItem>.Create;
FDirectories := TList<TDirectory>.Create;
FHeap.ID := $FFFFFFFF;
end;
destructor TRegionData.Destroy;
begin
FDirectories.Free;
FContains.Free;
inherited;
end;
function TRegionData.GetAsString: string;
var
S: TStringList;
I: Integer;
Dir: TDirectory;
CI: TContainItem;
procedure AddString(const Value: string);
begin
if Value = '' then
S.Add(' ')
else
S.Add(Value);
end;
procedure AddHeapData(Value: THeapData);
begin
S.Add(IntToStr(Value.ID));
S.Add(BoolToStr(Value.Wow64));
S.Add(IntToStr(Value.Entry.Address));
S.Add(IntToStr(Value.Entry.Size));
S.Add(IntToStr(Value.Entry.Flags));
end;
procedure AddThreadData(Value: TThreadData);
begin
S.Add(GetEnumName(TypeInfo(TThreadInfo), Integer(Value.Flag)));
S.Add(IntToStr(Value.ThreadID));
S.Add(IntToStr(ULONG_PTR(Value.Address)));
S.Add(BoolToStr(Value.Wow64));
end;
procedure AddPEBData(Value: TSystemData);
begin
AddString(string(Value.Description));
S.Add(IntToStr(ULONG_PTR(Value.Address)));
end;
begin
S := TStringList.Create;
try
S.Add(GetEnumName(TypeInfo(TRegionType), Integer(RegionType)));
S.Add(IntToStr(ULONG_PTR(MBI.BaseAddress)));
S.Add(IntToStr(ULONG_PTR(MBI.AllocationBase)));
S.Add(IntToStr(MBI.AllocationProtect));
S.Add(IntToStr(MBI.RegionSize));
S.Add(IntToStr(MBI.State));
S.Add(IntToStr(MBI.Protect));
S.Add(IntToStr(MBI.Type_9));
AddString(Details);
S.Add(BoolToStr(RegionVisible));
S.Add(IntToStr(HiddenRegionCount));
S.Add(IntToStr(TotalRegionSize));
AddHeapData(Heap);
AddThreadData(Thread);
AddPEBData(SystemData);
AddString(string(Section.Caption));
S.Add(IntToStr(Section.Address));
S.Add(IntToStr(Section.Size));
try
S.Add(BoolToStr(Section.IsCode));
except
S.Add(BoolToStr(Section.IsCode));
end;
S.Add(BoolToStr(Section.IsData));
S.Add(BoolToStr(Shared));
S.Add(IntToStr(SharedCount));
S.Add(IntToStr(Directory.Count));
for I := 0 to Directory.Count - 1 do
begin
Dir := Directory[I];
AddString(string(Dir.Caption));
S.Add(IntToStr(Dir.Address));
S.Add(IntToStr(Dir.Size));
end;
S.Add(IntToStr(Contains.Count));
for I := 0 to Contains.Count - 1 do
begin
CI := Contains[I];
S.Add(GetEnumName(TypeInfo(TContainItemType), Integer(CI.ItemType)));
case CI.ItemType of
itHeapBlock: AddHeapData(CI.Heap);
itThreadData: AddThreadData(CI.ThreadData);
itSystem: AddPEBData(CI.System);
end;
end;
S.Delimiter := #9;
Result := S.DelimitedText;
finally
S.Free;
end;
end;
procedure TRegionData.IncHiddenRegionCount;
begin
Inc(FHiddenRegionCount);
end;
procedure TRegionData.IncTotalHeapSize(Value: NativeUInt);
begin
Inc(FHeap.Entry.Size, Value);
end;
procedure TRegionData.IncTotalRegionSize(Value: NativeUInt);
begin
Inc(FTotalRegionSize, Value);
end;
procedure TRegionData.InitFromString(const Value: string);
var
S: TStringList;
I, Cursor: Integer;
Dir: TDirectory;
CI: TContainItem;
function CheckEnumType(CheckValue: Integer): Integer;
begin
if CheckValue >= 0 then
Result := CheckValue
else
raise Exception.Create('Wrong enum type.');
end;
function NextValue: string;
begin
Result := Trim(S[Cursor]);
Inc(Cursor);
end;
function GetHeapData: THeapData;
begin
Result.ID := StrToInt64(NextValue);
Result.Wow64 := StrToBool(NextValue);
Result.Entry.Address := StrToInt64(NextValue);
Result.Entry.Size := StrToInt64(NextValue);
Result.Entry.Flags := StrToInt64(NextValue);
end;
function GetThreadData: TThreadData;
begin
Result.Flag := TThreadInfo(CheckEnumType(
GetEnumValue(TypeInfo(TThreadInfo), NextValue)));
Result.ThreadID := StrToInt64(NextValue);
Result.Address := Pointer(StrToInt64(NextValue));
Result.Wow64 := StrToBool(NextValue);
end;
function GetPEBData: TSystemData;
begin
Result.Description := ShortString(ShortString(Trim(NextValue)));
Result.Address := Pointer(StrToInt64(NextValue));
end;
begin
S := TStringList.Create;
try
S.Delimiter := #9;
S.DelimitedText := Value;
Cursor := 0;
FRegionType := TRegionType(CheckEnumType(
GetEnumValue(TypeInfo(TRegionType), NextValue)));
FMBI.BaseAddress := Pointer(StrToInt64(NextValue));
FMBI.AllocationBase := Pointer(StrToInt64(NextValue));
FMBI.AllocationProtect := StrToInt64(NextValue);
FMBI.RegionSize := StrToInt64(NextValue);
FMBI.State := StrToInt64(NextValue);
FMBI.Protect := StrToInt64(NextValue);
FMBI.Type_9 := StrToInt64(NextValue);
FDetails := NextValue;
FRegionVisible := StrToBool(NextValue);
FHiddenRegionCount := StrToInt64(NextValue);
FTotalRegionSize := StrToInt64(NextValue);
FHeap := GetHeapData;
FThread := GetThreadData;
FPEBData := GetPEBData;
FSection.Caption := ShortString(ShortString(Trim(NextValue)));
FSection.Address := StrToInt64(NextValue);
FSection.Size := StrToInt64(NextValue);
FSection.IsCode := StrToBool(NextValue);
FSection.IsData := StrToBool(NextValue);
FShared := StrToBool(NextValue);
FSharedCount := StrToInt64(NextValue);
for I := 0 to StrToInt64(NextValue) - 1 do
begin
Dir.Caption := ShortString(ShortString(Trim(NextValue)));
Dir.Address := StrToInt64(NextValue);
Dir.Size := StrToInt64(NextValue);
Directory.Add(Dir);
end;
for I := 0 to StrToInt64(NextValue) - 1 do
begin
CI.ItemType := TContainItemType(CheckEnumType(
GetEnumValue(TypeInfo(TContainItemType), NextValue)));
case CI.ItemType of
itHeapBlock: CI.Heap := GetHeapData;
itThreadData: CI.ThreadData := GetThreadData;
itSystem: CI.System := GetPEBData;
end;
Contains.Add(CI);
end;
finally
S.Free;
end;
end;
procedure TRegionData.SetHeap(Value: THeapData);
begin
FHeap := Value;
end;
procedure TRegionData.SetDetails(Value: string);
begin
FDetails := Value;
end;
procedure TRegionData.SetFiltered(Value: Boolean);
begin
FFiltered := Value;
end;
procedure TRegionData.SetMBI(Value: TMemoryBasicInformation);
begin
FMBI := Value;
FTotalRegionSize := Value.RegionSize;
end;
procedure TRegionData.SetMBIRegionSize(Value: SIZE_T);
begin
FMBI.RegionSize := Value;
end;
procedure TRegionData.SetRegionType(Value: TRegionType);
begin
FRegionType := Value;
end;
procedure TRegionData.SetParent(Value: TRegionData);
begin
FParent := Value;
end;
procedure TRegionData.SetPEBData(Value: TSystemData);
begin
FPEBData := Value;
end;
procedure TRegionData.SetRegionVisible(Value: Boolean);
begin
FRegionVisible := Value;
end;
procedure TRegionData.SetSection(Value: TSection);
begin
FSection := Value;
end;
procedure TRegionData.SetShared(const Value: Boolean);
begin
FShared := Value;
end;
procedure TRegionData.SetSharedCount(const Value: Integer);
begin
FSharedCount := Value;
end;
procedure TRegionData.SetThread(Value: TThreadData);
begin
FThread := Value;
end;
procedure TRegionData.SetThreadIDAndWow(Value: Integer; Wow64: Boolean);
begin
FThread.ThreadID := Value;
FThread.Wow64 := Wow64;
end;
procedure TRegionData.SetTotalRegionSize(Value: NativeUInt);
begin
FTotalRegionSize := Value;
end;
end.