forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuKnownData.pas
More file actions
436 lines (390 loc) · 12.1 KB
/
Copy pathuKnownData.pas
File metadata and controls
436 lines (390 loc) · 12.1 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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : ProcessMM
// * Unit Name : uKnownData.pas
// * Purpose : Диалог для отображения списка всех известных структур
// * Author : Александр (Rouse_) Багель
// * Copyright : © Fangorn Wizards Lab 1998 - 2023.
// * Version : 1.4.33
// * 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 uKnownData;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ComCtrls, Generics.Collections, ClipBrd, System.ImageList,
Vcl.ImgList, Vcl.Menus,
VirtualTrees,
VirtualTrees.Types,
MemoryMap.Core,
MemoryMap.RegionData,
MemoryMap.Threads,
MemoryMap.PEImage,
uRegionProperties,
uBaseForm, VirtualTrees.BaseAncestorVCL, VirtualTrees.BaseTree,
VirtualTrees.AncestorVCL;
type
TdlgKnownData = class(TBaseAppForm)
il16: TImageList;
tvData: TVirtualStringTree;
pmCopy: TPopupMenu;
mnuGotoAddress: TMenuItem;
mnuSeparator1: TMenuItem;
mnuCopyAddress: TMenuItem;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tvDataDblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure tvDataGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure tvDataGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean;
var ImageIndex: TImageIndex);
procedure FormShow(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure mnuGotoAddressClick(Sender: TObject);
procedure pmCopyPopup(Sender: TObject);
procedure mnuCopyAddressClick(Sender: TObject);
private type
PNodeData = ^TNodeData;
TNodeData = record
Address: Pointer;
Caption: string;
ImageIndex, OverlayIndex: Integer;
IsCode, IsEntryPoint, IsTlsCallBack: Boolean;
class function Default: TNodeData; static;
end;
private
FThreadsNode: PVirtualNode;
FImagesNode: PVirtualNode;
FSystemNode: PVirtualNode;
FNodeDataList: TList<TNodeData>;
function GetImagesNode: PVirtualNode;
function GetThreadNode: PVirtualNode;
function GetSystemNode: PVirtualNode;
function GetSelectedNodeIndex: Integer;
function GetNodeByData(Root: PVirtualNode;
const Data: TNodeData): PVirtualNode; overload;
function GetNodeByCaption(Root: PVirtualNode;
const Caption: string): PVirtualNode;
function Add(Root: PVirtualNode; const Data: TNodeData): PVirtualNode;
public
procedure ShowKnownData;
end;
var
dlgKnownData: TdlgKnownData;
implementation
const
Overlay32 = 0;
Overlay64 = 1;
{$R *.dfm}
{ TdlgKnownData }
function TdlgKnownData.Add(Root: PVirtualNode;
const Data: TNodeData): PVirtualNode;
begin
Result := tvData.AddChild(Root, nil);
PInteger(Result.GetData)^ := FNodeDataList.Add(Data);
end;
procedure TdlgKnownData.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
dlgKnownData := nil;
end;
procedure TdlgKnownData.FormCreate(Sender: TObject);
begin
FNodeDataList := TList<TNodeData>.Create;
tvData.NodeDataSize := 4;
il16.Overlay(8, Overlay32);
il16.Overlay(9, Overlay64);
end;
procedure TdlgKnownData.FormDestroy(Sender: TObject);
begin
FNodeDataList.Free;
end;
procedure TdlgKnownData.FormKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #27 then Close;
end;
procedure TdlgKnownData.FormShow(Sender: TObject);
procedure ProcessThreadData(Value: TThreadData);
var
ThreadNode: PVirtualNode;
Data: TNodeData;
begin
ThreadNode := GetNodeByCaption(GetThreadNode, IntToHex(Value.ThreadID));
Data := TNodeData.Default;
Data.Address := Value.Address;
Data.IsCode := False;
Data.ImageIndex := 6;
case Value.Flag of
tiExceptionList:
Data.Caption := 'Exception';
tiTEB:
if Value.Wow64 then
Data.Caption := 'Teb Wow'
else
begin
Data.Caption := 'Teb';
if not MemoryMapCore.Process64 then
Data.OverlayIndex := Overlay64;
end;
tiThreadProc:
begin
Data.Caption := 'ThreadProc';
Data.IsCode := True;
Data.ImageIndex := 7;
end;
tiOleTlsData:
begin
if Value.Wow64 then
Data.Caption := 'OleTlsData Wow'
else
begin
Data.Caption := 'OleTlsData';
if not MemoryMapCore.Process64 then
Data.OverlayIndex := Overlay64;
end;
end
else
Exit;
end;
Data.Caption := Format('%s -> [0x%s]',
[Data.Caption, IntToHex(ULONG64(Data.Address), 1)]);
Add(ThreadNode, Data);
end;
procedure UpdateImageRoot(Node: PVirtualNode);
begin
with FNodeDataList.List[PInteger(Node.GetData)^] do
begin
ImageIndex := 2;
if IsEntryPoint then
Inc(ImageIndex);
if IsTlsCallBack then
Inc(ImageIndex);
end;
end;
procedure ProcessSystemData(Value: TSystemData);
var
Data: TNodeData;
begin
Data := TNodeData.Default;
Data.Address := Value.Address;
Data.Caption := Format('%s -> [0x%s]',
[string(Value.Description), IntToHex(ULONG64(Data.Address), 1)]);
Data.ImageIndex := 6;
GetNodeByData(GetSystemNode, Data);
end;
var
Region: TRegionData;
Node: PVirtualNode;
EntryPoint: TDirectory;
Data: TNodeData;
Is64Process: Boolean;
begin
Is64Process := MemoryMapCore.Process64;
tvData.BeginUpdate;
try
for var I := 0 to MemoryMapCore.TotalCount - 1 do
begin
Region := MemoryMapCore.GetRegionAtUnfilteredIndex(I);
case Region.RegionType of
rtThread:
if Region.Thread.Flag <> tiNoData then
ProcessThreadData(Region.Thread);
rtExecutableImage, rtExecutableImage64:
begin
// заполняем рут
Finalize(Data);
Data := TNodeData.Default;
Data.Caption := Region.Details;
if (Region.RegionType = rtExecutableImage) and Is64Process then
Data.OverlayIndex := Overlay32;
if (Region.RegionType = rtExecutableImage64) and not Is64Process then
Data.OverlayIndex := Overlay64;
Node := GetNodeByData(GetImagesNode, Data);
UpdateImageRoot(Node);
Finalize(Data);
Data := TNodeData.Default;
Data.Address := Region.MBI.AllocationBase;
Data.Caption := Format('PE_Header -> [0x%s]',
[IntToHex(ULONG64(Data.Address), 1)]);
Data.IsCode := False;
Data.ImageIndex := 6;
Add(Node, Data);
end;
rtSystem:
if Region.SystemData.Address <> nil then
ProcessSystemData(Region.SystemData);
end;
// так и как дополнительные данные
for var Contains in Region.Contains do
begin
if Contains.ItemType = itThreadData then
ProcessThreadData(Contains.ThreadData);
if Contains.ItemType = itSystem then
ProcessSystemData(Contains.System);
end;
for EntryPoint in Region.Directory do
if EntryPoint.Flag <> dfDirectory then
begin
if Region.Parent = nil then
Node := GetNodeByCaption(GetImagesNode, 'Invalid Data!!!')
else
Node := GetNodeByCaption(GetImagesNode, Region.Parent.Details);
with FNodeDataList.List[PInteger(Node.GetData)^] do
begin
if EntryPoint.Flag = dfEntryPoint then
IsEntryPoint := True;
if EntryPoint.Flag = dfTlsCallback then
IsTlsCallBack := True;
end;
UpdateImageRoot(Node);
Finalize(Data);
Data := TNodeData.Default;
Data.Address := Pointer(EntryPoint.Address);
Data.Caption := Format('%s -> [0x%s]',
[string(EntryPoint.Caption), IntToHex(EntryPoint.Address, 1)]);
Data.ImageIndex := 7;
Data.IsCode := True;
Add(Node, Data);
end;
end;
finally
tvData.EndUpdate;
end;
end;
function TdlgKnownData.GetImagesNode: PVirtualNode;
var
Data: TNodeData;
begin
if FImagesNode = nil then
begin
Data := TNodeData.Default;
Data.Caption := 'Images';
Data.ImageIndex := 1;
FImagesNode := Add(nil, Data);
end;
Result := FImagesNode;
tvData.Expanded[FImagesNode] := True;
end;
function TdlgKnownData.GetNodeByCaption(Root: PVirtualNode;
const Caption: string): PVirtualNode;
var
Data: TNodeData;
begin
Data := TNodeData.Default;
Data.Caption := Caption;
Result := GetNodeByData(Root, Data);
end;
function TdlgKnownData.GetNodeByData(Root: PVirtualNode;
const Data: TNodeData): PVirtualNode;
var
Tmp: PVirtualNode;
begin
Tmp := tvData.GetNext(Root, True);
while Assigned(Tmp) do
begin
if Data.Caption = FNodeDataList.List[PInteger(Tmp.GetData)^].Caption then
Exit(Tmp);
Tmp := tvData.GetNext(Tmp, True);
end;
Result := Add(Root, Data);
end;
function TdlgKnownData.GetSelectedNodeIndex: Integer;
var
E: TVTVirtualNodeEnumerator;
begin
Result := -1;
E := tvData.SelectedNodes.GetEnumerator;
if E.MoveNext then
Result := PInteger(tvData.GetNodeData(E.Current))^;
end;
function TdlgKnownData.GetSystemNode: PVirtualNode;
var
Data: TNodeData;
begin
if FSystemNode = nil then
begin
Data := TNodeData.Default;
Data.Caption := 'System';
Data.ImageIndex := 5;
FSystemNode := Add(nil, Data);
end;
Result := FSystemNode;
tvData.Expanded[FSystemNode] := True;
end;
function TdlgKnownData.GetThreadNode: PVirtualNode;
var
Data: TNodeData;
begin
if FThreadsNode = nil then
begin
Data := TNodeData.Default;
Data.Caption := 'Threads';
Data.ImageIndex := 0;
FThreadsNode := Add(nil, Data);
end;
Result := FThreadsNode;
tvData.Expanded[FThreadsNode] := True;
end;
procedure TdlgKnownData.mnuCopyAddressClick(Sender: TObject);
begin
var I := GetSelectedNodeIndex;
if I < 0 then Exit;
Clipboard.AsText := IntToHex(Int64(FNodeDataList.List[I].Address));
end;
procedure TdlgKnownData.mnuGotoAddressClick(Sender: TObject);
begin
tvDataDblClick(nil);
end;
procedure TdlgKnownData.pmCopyPopup(Sender: TObject);
begin
mnuGotoAddress.Enabled := GetSelectedNodeIndex >= 0;
mnuCopyAddress.Enabled := mnuGotoAddress.Enabled;
end;
procedure TdlgKnownData.ShowKnownData;
begin
Show;
end;
procedure TdlgKnownData.tvDataDblClick(Sender: TObject);
begin
var I := GetSelectedNodeIndex;
if I < 0 then Exit;
if FNodeDataList.List[I].Address = nil then Exit;
dlgRegionProps := TdlgRegionProps.Create(Application);
dlgRegionProps.ShowPropertyAtAddr(FNodeDataList.List[I].Address);
end;
procedure TdlgKnownData.tvDataGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: TImageIndex);
begin
if Kind = ikOverlay then
ImageIndex := FNodeDataList.List[PInteger(Node.GetData)^].OverlayIndex
else
if Kind in [ikNormal, ikSelected] then
ImageIndex := FNodeDataList.List[PInteger(Node.GetData)^].ImageIndex;
end;
procedure TdlgKnownData.tvDataGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
begin
var I := PInteger(Node.GetData)^;
if Column = 1 then
CellText := IntToHex(Int64(FNodeDataList.List[I].Address))
else
CellText := FNodeDataList.List[I].Caption;
end;
{ TdlgKnownData.TNodeData }
class function TdlgKnownData.TNodeData.Default: TNodeData;
begin
ZeroMemory(@Result, SizeOf(TNodeData));
Result.OverlayIndex := -1;
end;
end.