forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMap.PEImage.pas
More file actions
174 lines (158 loc) · 4.85 KB
/
Copy pathMemoryMap.PEImage.pas
File metadata and controls
174 lines (158 loc) · 4.85 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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : MemoryMap
// * Unit Name : MemoryMap.PEImage.pas
// * Purpose : Êëàññ ñîáèðàåò äàííûå ïî ñåêöèÿì è äèðåêòîðèÿì PE ôàéëà
// * 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.PEImage;
interface
uses
Winapi.Windows,
Generics.Collections,
MemoryMap.Utils,
Winapi.ImageHlp;
type
TSection = record
Caption: ShortString;
Address: NativeUInt;
Size: NativeUInt;
IsCode: Boolean;
IsData: Boolean;
end;
TDirectory = record
Caption: ShortString;
Address: NativeUInt;
Size: NativeUInt;
end;
TDirectoryArray = record
Count: Integer;
Data: array [0..14] of TDirectory;
end;
TPEImage = class
private
FImageBase: Pointer;
FImageInfo: LOADED_IMAGE;
FSections: TList<TSection>;
FDirectoryes: TList<TDirectoryArray>;
FEntryPoints: TList<Pointer>;
protected
procedure EnumSections;
procedure EnumDirectoryes;
public
constructor Create;
destructor Destroy; override;
procedure GetInfoFromImage(const FileName: string; ImageBase: Pointer;
FirstSectionSize: NativeUInt);
function GetSectionArAddr(Value: Pointer): TSection;
property Sections: TList<TSection> read FSections;
property Directoryes: TList<TDirectoryArray> read FDirectoryes;
property EntryPoints: TList<Pointer> read FEntryPoints;
end;
implementation
{ TPEImage }
constructor TPEImage.Create;
begin
FSections := TList<TSection>.Create;
FDirectoryes := TList<TDirectoryArray>.Create;
FEntryPoints := TList<Pointer>.Create;
end;
destructor TPEImage.Destroy;
begin
FEntryPoints.Free;
FDirectoryes.Free;
FSections.Free;
inherited;
end;
procedure TPEImage.EnumDirectoryes;
const
DirectoryStr: array [0..14] of string =
('export', 'import', 'resource', 'exception',
'security', 'basereloc', 'debug', 'copyright',
'globalptr', 'tls', 'load_config', 'bound_import',
'iat', 'delay_import', 'com');
var
I: Integer;
dwDirSize: DWORD;
DirAddr: Pointer;
Directory: TDirectoryArray;
begin
ZeroMemory(@Directory, SizeOf(TDirectoryArray));
Directory.Count := 0;
for I := 0 to 14 do
begin
// Ïîëó÷àåì àäðåñ äèðåêòîðèè
DirAddr := ImageDirectoryEntryToData(FImageInfo.MappedAddress,
True, I, dwDirSize);
if DirAddr <> nil then
begin
Inc(Directory.Count);
Directory.Data[I].Caption := ShortString(DirectoryStr[I]);
Directory.Data[I].Address := NativeUint(FImageBase) +
NativeUint(DirAddr) - NativeUint(FImageInfo.MappedAddress);
Directory.Data[I].Size := dwDirSize;
end;
end;
FDirectoryes.Add(Directory);
end;
procedure TPEImage.EnumSections;
var
ImageSectionHeader: PImageSectionHeader;
I: Integer;
Section: TSection;
begin
ImageSectionHeader := FImageInfo.Sections;
for I := 0 to Integer(FImageInfo.NumberOfSections) - 1 do
begin
Section.Caption := ShortString(PAnsiChar(@ImageSectionHeader^.Name[0]));
Section.Address := NativeUint(FImageBase) + ImageSectionHeader^.VirtualAddress;
Section.Size := AlignedSectionSize(FImageInfo, ImageSectionHeader^.SizeOfRawData);
Section.IsCode := IsExecute(ImageSectionHeader^.Characteristics);
Section.IsData := IsWrite(ImageSectionHeader^.Characteristics);
FSections.Add(Section);
Inc(ImageSectionHeader);
end;
end;
procedure TPEImage.GetInfoFromImage(const FileName: string; ImageBase: Pointer;
FirstSectionSize: NativeUInt);
var
Section: TSection;
begin
FImageBase := ImageBase;
if MapAndLoad(PAnsiChar(AnsiString(FileName)), nil, @FImageInfo, True, True) then
try
Section.Caption := 'PEHeader';
Section.Address := NativeUint(ImageBase);
Section.Size := FirstSectionSize;
Section.IsCode := False;
Section.IsData := False;
FSections.Add(Section);
if FImageInfo.FileHeader.OptionalHeader.AddressOfEntryPoint <> 0 then
FEntryPoints.Add(Pointer(NativeUInt(ImageBase) +
FImageInfo.FileHeader.OptionalHeader.AddressOfEntryPoint));
EnumSections;
EnumDirectoryes;
finally
UnMapAndLoad(@FImageInfo);
end;
end;
function TPEImage.GetSectionArAddr(Value: Pointer): TSection;
var
I: TSection;
begin
ZeroMemory(@Result, SizeOf(TSection));
for I in FSections do
if I.Address <= NativeUInt(Value) then
if I.Address + I.Size > NativeUInt(Value) then
Exit(I);
end;
end.