forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuMemoryMapListInfo.pas
More file actions
597 lines (536 loc) · 16.1 KB
/
Copy pathuMemoryMapListInfo.pas
File metadata and controls
597 lines (536 loc) · 16.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
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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : ProcessMM
// * Unit Name : uMemoryMapListInfo.pas
// * Purpose : Сканирование памяти процесса на основе адресов и контрольных сумм
// * Author : Александр (Rouse_) Багель
// * Copyright : © Fangorn Wizards Lab 1998 - 2023.
// * Version : 1.4.30
// * 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 uMemoryMapListInfo;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.Menus,
Generics.Collections, PsAPI,
uUtils,
MemoryMap.Core,
MemoryMap.Utils,
MemoryMap.WorkSet,
MemoryMap.DebugMapData,
uDumpDisplayUtils,
ScaledCtrls,
uBaseForm;
type
TScanSettings = record
MMLFile: string;
ShowDump: Boolean;
DumpSize: Integer;
ShowDisasm: Boolean;
NeedGenerateMML: Boolean;
NeedSave: Boolean;
SavePath: string;
DumpFullRegIfNotShared: Boolean;
DumpFullRegIfWrongCRC: Boolean;
end;
TMMLRecord = record
Comment: string;
Addr: Pointer;
CRC: DWORD;
end;
TdlgMemoryMapListInfo = class(TBaseAppForm)
edReport: TRichEdit;
PopupMenu: TPopupMenu;
mnuCopy: TMenuItem;
SelectAll1: TMenuItem;
SaveMMLDialog: TSaveDialog;
procedure mnuCopyClick(Sender: TObject);
procedure SelectAll1Click(Sender: TObject);
private
ScanSettings: TScanSettings;
MMLData: TList<TMMLRecord>;
Process: THandle;
Workset: TWorkset;
DumpList: TStringList;
NotSharedCount, WrongCRCCount, DumpFailed: Integer;
procedure LoadMMLData;
function ReadMMLRecord(Data: TStringList; Index: Integer): Integer;
procedure SaveMMLData;
procedure Scan;
procedure ProcessMMLRecord(Index: Integer);
procedure SaveReport;
procedure SaveDump(Addr: ULONG_PTR; RawBuff: TMemoryDump);
public
procedure ShowMemoryMapInfo;
procedure GenerateMemoryMapInfo;
end;
var
dlgMemoryMapListInfo: TdlgMemoryMapListInfo;
implementation
uses
FWZipWriter,
uSettings,
uDisplayUtils,
uMemoryMapListInfoSettings,
uProgress;
{$R *.dfm}
{ TdlgMemoryMapListInfo }
procedure TdlgMemoryMapListInfo.GenerateMemoryMapInfo;
function IsVclFunction(const FuncName: string): Boolean;
begin
Result := FuncName.StartsWith('Winapi.');
if not Result then
Result := FuncName.StartsWith('System.');
if not Result then
Result := FuncName.StartsWith('Vcl.');
if not Result then
Result := FuncName.StartsWith('Generics.');
if not Result then
Result := FuncName.StartsWith('SysInit.');
end;
const
FuncPrefix = ' // ';
var
DebugMap: TDebugMap;
FuncDataList: TDictionary<ULONG_PTR, string>;
RegionAddr: ULONG_PTR;
RegionData, MMLPath: string;
Enumerator: TEnumerator<ULONG_PTR>;
MML: TStringList;
ProcessLock: TProcessLockHandleList;
AddrSize: Integer;
Size, RegionSize: NativeUInt;
RawBuff: TMemoryDump;
CRC: DWORD;
begin
if MemoryMapCore.Process64 then
AddrSize := 16
else
AddrSize := 8;
MMLPath := ChangeFileExt(MemoryMapCore.ProcessPath, '.mml');
DebugMap := MemoryMapCore.DebugMapData;
FuncDataList := TDictionary<ULONG_PTR, string>.Create;
try
// создаем список адресов по 4096 байт куда входит хотябы одна функция
for var I := 0 to DebugMap.Items.Count - 1 do
begin
with DebugMap.Items.List[I] do
begin
if IsVclFunction(FunctionName) then
Continue;
RegionAddr := Address and not $FFF;
if not FuncDataList.TryGetValue(RegionAddr, RegionData) then
RegionData := FuncPrefix + FunctionName
else
RegionData := FuncPrefix + FunctionName + sLineBreak + RegionData;
FuncDataList.AddOrSetValue(RegionAddr, RegionData);
end;
end;
Enumerator := FuncDataList.Keys.GetEnumerator;
try
MML := TStringList.Create;
try
Process := OpenProcessWithReconnect;
try
ProcessLock := nil;
if Settings.SuspendProcess then
ProcessLock := SuspendProcess(MemoryMapCore.PID);
try
while Enumerator.MoveNext do
begin
// рассчитываем контрольную сумму каждого блока
Size := 4096;
SetLength(RawBuff, Size);
if not ReadProcessData(Process, Pointer(Enumerator.Current), @RawBuff[0],
Size, RegionSize, rcReadAllwais) then
Continue;
SetLength(RawBuff, Size);
CRC := CRC32(RawBuff);
// помещаем в MML описание блока, какие функции в него входят
// адрес и контрольную сумму блока
FuncDataList.TryGetValue(Enumerator.Current, RegionData);
MML.Add(RegionData);
MML.Add('addr: ' + IntToHex(Enumerator.Current, AddrSize) +
' crc: ' + IntToHex(CRC, 8));
end;
finally
if Assigned(ProcessLock) then
ResumeProcess(ProcessLock);
end;
finally
CloseHandle(Process);
end;
if MML.Count > 0 then
try
MML.SaveToFile(MMLPath);
except
SaveMMLDialog.InitialDir := ExtractFilePath(MMLPath);
SaveMMLDialog.FileName := ExtractFileName(MMLPath);
if SaveMMLDialog.Execute then
begin
MMLPath := SaveMMLDialog.FileName;
MML.SaveToFile(MMLPath);
end;
end;
OpenExplorerAndSelectFile(MMLPath);
finally
MML.Free;
end;
finally
Enumerator.Free;
end;
finally
FuncDataList.Free;
end;
// если файл сгенерировался успешно то открываем его с настройками по умолчанию
if FileExists(MMLPath) then
begin
ScanSettings.MMLFile := MMLPath;
ScanSettings.ShowDump := False;
ScanSettings.DumpSize := 0;
ScanSettings.ShowDisasm := False;
ScanSettings.NeedGenerateMML := False;
ScanSettings.NeedSave := False;
ScanSettings.SavePath := EmptyStr;
ScanSettings.DumpFullRegIfNotShared := False;
ScanSettings.DumpFullRegIfWrongCRC := False;
Scan;
end;
end;
procedure TdlgMemoryMapListInfo.LoadMMLData;
var
S: TStringList;
I: Integer;
begin
S := TStringList.Create;
try
S.LoadFromFile(ScanSettings.MMLFile);
I := 0;
while I < S.Count - 1 do
I := ReadMMLRecord(S, I);
finally
S.Free;
end;
end;
procedure TdlgMemoryMapListInfo.mnuCopyClick(Sender: TObject);
begin
edReport.CopyToClipboard;
end;
procedure TdlgMemoryMapListInfo.ProcessMMLRecord(Index: Integer);
procedure Add(const Value: string);
begin
edReport.Lines.Add(Value);
end;
var
MMLRecord: TMMLRecord;
MBI: TMemoryBasicInformation;
dwLength, CRC: DWORD;
DumpPresent, Shared, DumpSaved, Dasm64Mode: Boolean;
SharedCount: Byte;
RawBuff: TMemoryDump;
Size, RegionSize: NativeUInt;
begin
MMLRecord := MMLData[Index];
dlgProgress.ProgressBar.Position := Index + 1;
dlgProgress.lblProgress.Caption :=
'Process address: ' + IntToHex(UINT_PTR(MMLRecord.Addr), 1);
Application.ProcessMessages;
CRC := DWORD(-1);
Shared := False;
SharedCount := 255;
Size := 4096;
SetLength(RawBuff, Size);
DumpPresent := ReadProcessData(Process, MMLRecord.Addr, @RawBuff[0],
Size, RegionSize, rcReadAllwais);
if DumpPresent then
CRC := CRC32(RawBuff);
dwLength := SizeOf(TMemoryBasicInformation);
if VirtualQueryEx(Process,
Pointer(MMLRecord.Addr), MBI, dwLength) <> dwLength then
RaiseLastOSError;
Workset.GetPageSharedInfo(Pointer(ULONG_PTR(MMLRecord.Addr) and
{$IFDEF WIN32}$FFFFF000{$ELSE}$FFFFFFFFFFFFF000{$ENDIF}), Shared, SharedCount);
if ScanSettings.NeedGenerateMML then
begin
MMLRecord.CRC := CRC;
MMLData[Index] := MMLRecord;
end;
if DumpPresent and Shared and (SharedCount > 0) and
((MMLRecord.CRC <> 0) and (CRC = MMLRecord.CRC)) then
Exit;
Add(EmptyHeader);
Add(dlgProgress.lblProgress.Caption);
Add(EmptyHeader);
Add('');
Add(MMLRecord.Comment);
Add('AllocationBase: ' + UInt64ToStr(ULONG_PTR(MBI.AllocationBase)));
Add('RegionSize: ' + SizeToStr(MBI.RegionSize));
Add('Type: ' + ExtractRegionTypeString(MBI));
Add('Access: ' + ExtractAccessString(MBI.Protect));
Add('Initail Access: ' + ExtractInitialAccessString(MBI.AllocationProtect));
Add('Shared: ' + BoolToStr(Shared, True));
Add('Shared count: ' + IntToStr(SharedCount));
if not DumpPresent then
begin
Inc(DumpFailed);
Add('Dump failed.');
Exit;
end;
DumpSaved := False;
if not Shared or (SharedCount = 0) then
begin
Inc(NotSharedCount);
if ScanSettings.DumpFullRegIfNotShared then
begin
Add('Not shared - see dump file.');
SaveDump(ULONG_PTR(MMLRecord.Addr), RawBuff);
DumpSaved := True;
end;
end;
CRC := CRC32(RawBuff);
if (MMLRecord.CRC <> 0) and (CRC <> MMLRecord.CRC) then
begin
Inc(WrongCRCCount);
if ScanSettings.DumpFullRegIfWrongCRC then
begin
Add('Wrong CRC - see dump file.');
if not DumpSaved then
SaveDump(ULONG_PTR(MMLRecord.Addr), RawBuff);
end
else
Add('Wrong CRC.');
end;
if ScanSettings.ShowDump then
Add(DumpMemoryFromBuff(Process, MMLRecord.Addr, RawBuff, ScanSettings.DumpSize));
if ScanSettings.ShowDisasm then
begin
if not CheckPEImage(Process, MBI.AllocationBase, Dasm64Mode) then
Dasm64Mode := MemoryMapCore.Process64;
Add(DisassemblyFromBuff(Process, RawBuff, MMLRecord.Addr,
MBI.AllocationBase, Dasm64Mode, ScanSettings.DumpSize));
end;
end;
function TdlgMemoryMapListInfo.ReadMMLRecord(
Data: TStringList; Index: Integer): Integer;
var
Line: string;
MML: TMMLRecord;
I, Position: Integer;
Value: Int64;
function GetValue(S: string; Index: Integer): string;
begin
Delete(S, 1, Index);
Index := Pos(' ', S);
if Index > 0 then
Result := Copy(S, 1, Index - 1)
else
Result := S;
end;
begin
MML.Comment := '';
MML.Addr := nil;
MML.CRC := 0;
Result := Data.Count;
for I := Index to Data.Count - 1 do
begin
Line := Trim(LowerCase(Data[I]));
Position := Pos('addr: ', Line);
if Position = 0 then
MML.Comment := MML.Comment + Data[I] + sLineBreak
else
begin
TryStrToInt64('$' + GetValue(Line, Position + 5), Value);
if Value > $100000 then
begin
MML.Addr := Pointer(Value);
Position := Pos('crc: ', Line);
if Position <> 0 then
begin
TryStrToInt64('$' + GetValue(Line, Position + 4), Value);
MML.CRC := DWORD(Value);
end;
MMLData.Add(MML);
end;
Result := I + 1;
Break;
end;
end;
end;
procedure TdlgMemoryMapListInfo.SaveDump(Addr: ULONG_PTR; RawBuff: TMemoryDump);
var
Path: string;
F: TFileStream;
begin
if not ScanSettings.NeedSave then Exit;
SetLength(Path, MAX_PATH);
GetTempPath(MAX_PATH, @Path[1]);
Path := IncludeTrailingPathDelimiter(PChar(Path)) + IntToHex(ULONG_PTR(Addr), 16) + '.bin';
F := TFileStream.Create(Path, fmCreate);
try
F.WriteBuffer(RawBuff[0], Length(RawBuff));
finally
F.Free;
end;
DumpList.Add(ExtractFileName(Path) + '=' + Path);
end;
procedure TdlgMemoryMapListInfo.SaveMMLData;
var
S: TStringList;
I: Integer;
begin
S := TStringList.Create;
try
for I := 0 to MMLData.Count - 1 do
begin
S.Add(MMLData[I].Comment);
S.Add('addr: ' + IntToHex(DWORD(MMLData[I].Addr), 16) +
' crc: ' + IntToHex(MMLData[I].CRC, 8));
end;
S.SaveToFile(ChangeFileExt(ScanSettings.MMLFile, '_updated_crc.mml'));
finally
S.Free;
end;
end;
procedure TdlgMemoryMapListInfo.SaveReport;
var
I: Integer;
Zip: TFWZipWriter;
M: TMemoryStream;
begin
try
if edReport.Lines.Count > 0 then
begin
edReport.Lines.Add('');
edReport.Lines.Add(EmptyHeader);
end
else
edReport.Lines.Add('All clear.');
edReport.Lines.Add('');
edReport.Lines.Add('Done.');
if not ScanSettings.NeedSave then Exit;
Zip := TFWZipWriter.Create;
try
M := TMemoryStream.Create;
try
edReport.Lines.SaveToStream(M);
Zip.AddStream('report.rtf', M);
finally
M.Free;
end;
Zip.AddFiles(DumpList);
Zip.BuildZip(ScanSettings.SavePath);
finally
Zip.Free;
end;
finally
for I := 0 to DumpList.Count - 1 do
DeleteFile(DumpList[I]);
end;
end;
procedure TdlgMemoryMapListInfo.Scan;
var
ProcessLock: TProcessLockHandleList;
I: Integer;
Hint: string;
procedure UpdateHint(Value: Integer; const HintMsg: string);
begin
if Value = 0 then Exit;
if Hint <> '' then
Hint := Hint + ', ';
Hint := Hint + HintMsg + IntToStr(Value);
end;
begin
ProcessLock := nil;
MMLData := TList<TMMLRecord>.Create;
try
LoadMMLData;
if MMLData.Count = 0 then
raise Exception.Create('Empty MML data file.');
Process := OpenProcessWithReconnect;
try
if Settings.SuspendProcess then
ProcessLock := SuspendProcess(MemoryMapCore.PID);
try
DumpList := TStringList.Create;
try
dlgProgress := TdlgProgress.Create(Self);
try
dlgProgress.ProgressBar.Max := MMLData.Count;
dlgProgress.lblProgress.Caption := 'Workset initialization...';
dlgProgress.Show;
Application.ProcessMessages;
Workset := TWorkset.Create(Process);
try
NotSharedCount := 0;
WrongCRCCount := 0;
DumpFailed := 0;
for I := 0 to MMLData.Count - 1 do
ProcessMMLRecord(I);
finally
Workset.Free;
end;
finally
dlgProgress.Free;;
end;
SaveReport;
finally
DumpList.Free;
end;
finally
if Settings.SuspendProcess then
ResumeProcess(ProcessLock);
end;
finally
CloseHandle(Process);
end;
if ScanSettings.NeedGenerateMML then
SaveMMLData;
finally
MMLData.Free;
end;
Hint := '';
UpdateHint(DumpFailed, 'Dump Failed: ');
UpdateHint(NotSharedCount, 'Not Shared: ');
UpdateHint(WrongCRCCount, 'Wrong CRC: ');
if Hint <> '' then
Caption := Caption + ' [' + Hint + ']';
ShowModal;
end;
procedure TdlgMemoryMapListInfo.SelectAll1Click(Sender: TObject);
begin
edReport.SelectAll;
end;
procedure TdlgMemoryMapListInfo.ShowMemoryMapInfo;
begin
dlgMemoryMapListInfoSettings := TdlgMemoryMapListInfoSettings.Create(Self);
try
if dlgMemoryMapListInfoSettings.ShowModal = mrCancel then
begin
Close;
Exit;
end;
ScanSettings.MMLFile := dlgMemoryMapListInfoSettings.edMML.Text;
ScanSettings.ShowDump := dlgMemoryMapListInfoSettings.cbShowMiniDump.Checked;
if dlgMemoryMapListInfoSettings.cbDumpSize.ItemIndex = 9 then
ScanSettings.DumpSize := 0
else
ScanSettings.DumpSize := 16 shl dlgMemoryMapListInfoSettings.cbDumpSize.ItemIndex;
ScanSettings.ShowDisasm := dlgMemoryMapListInfoSettings.cbShowDisasm.Checked;
ScanSettings.NeedGenerateMML := dlgMemoryMapListInfoSettings.cbGenerateMML.Checked;
ScanSettings.NeedSave := dlgMemoryMapListInfoSettings.cbSave.Checked;
ScanSettings.SavePath := dlgMemoryMapListInfoSettings.edSave.Text;
ScanSettings.DumpFullRegIfNotShared := dlgMemoryMapListInfoSettings.cbSaveFullDump.Checked;
ScanSettings.DumpFullRegIfWrongCRC := dlgMemoryMapListInfoSettings.cbSaveIfWrongCRC.Checked;
finally
dlgMemoryMapListInfoSettings.Free;
end;
Scan;
end;
end.