forked from vdisasm/pe-image-for-delphi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPE.MemoryStream.pas
207 lines (166 loc) · 5.01 KB
/
PE.MemoryStream.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
{
Memory Stream based on already mapped PE image in current process.
Basically it's TMemoryStream with Memory pointing to ImageBase and Size equal
to SizeOfImage.
}
unit PE.MemoryStream;
interface
uses
Classes,
SysUtils;
type
TPECustomMemoryStream = class(TStream)
protected
FMemory: Pointer;
FSize, FPosition: NativeInt;
public
constructor CreateFromPointer(Ptr: Pointer; Size: integer);
procedure SetPointer(Ptr: Pointer; const Size: NativeInt);
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: integer): integer; override;
procedure SaveToStream(Stream: TStream); virtual;
procedure SaveToFile(const FileName: string);
property Memory: Pointer read FMemory;
end;
TPEMemoryStream = class(TPECustomMemoryStream)
private
FModuleToUnload: HMODULE; // needed if module loading is forced.
FModuleFileName: string;
FModuleSize: uint32;
private
procedure CreateFromModulePtr(ModulePtr: Pointer);
public
// Create stream from module in current process.
// If module is not found exception raised.
// To force loading module set ForceLoadingModule to True.
constructor Create(const ModuleName: string; ForceLoadingModule: boolean = False); overload;
// Create from module by known base address.
constructor Create(ModuleBase: NativeUInt); overload;
destructor Destroy; override;
// Simply read SizeOfImage from memory.
class function GetModuleImageSize(ModulePtr: PByte): uint32; static;
end;
implementation
uses
Windows,
PE.Types.DosHeader,
PE.Types.NTHeaders;
{ TPECustomMemoryStream }
procedure TPECustomMemoryStream.SetPointer(Ptr: Pointer; const Size: NativeInt);
begin
FMemory := Ptr;
FSize := Size;
FPosition := 0;
end;
constructor TPECustomMemoryStream.CreateFromPointer(Ptr: Pointer; Size: integer);
begin
inherited Create;
SetPointer(Ptr, Size);
end;
procedure TPECustomMemoryStream.SaveToFile(const FileName: string);
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(Stream);
finally
Stream.Free;
end;
end;
procedure TPECustomMemoryStream.SaveToStream(Stream: TStream);
begin
if FSize <> 0 then
Stream.WriteBuffer(FMemory^, FSize);
end;
function TPECustomMemoryStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
case Origin of
soBeginning:
FPosition := Offset;
soCurrent:
Inc(FPosition, Offset);
soEnd:
FPosition := FSize + Offset;
end;
Result := FPosition;
end;
function TPECustomMemoryStream.Read(var Buffer; Count: integer): Longint;
begin
if Count = 0 then
Exit(0);
Result := FSize - FPosition;
if Result > 0 then
begin
if Result > Count then
Result := Count;
Move(PByte(FMemory)[FPosition], Buffer, Result);
Inc(FPosition, Result);
end;
end;
function TPECustomMemoryStream.Write(const Buffer; Count: integer): integer;
begin
if Count = 0 then
Exit(0);
Result := FSize - FPosition;
if Result > 0 then
begin
if Result > Count then
Result := Count;
Move(Buffer, PByte(FMemory)[FPosition], Result);
Inc(FPosition, Result);
end;
end;
{ TPEMemoryStream }
procedure TPEMemoryStream.CreateFromModulePtr(ModulePtr: Pointer);
begin
if ModulePtr = nil then
raise Exception.CreateFmt('Module "%s" not found in address space',
[FModuleFileName]);
FModuleSize := TPEMemoryStream.GetModuleImageSize(ModulePtr);
SetPointer(ModulePtr, FModuleSize);
end;
constructor TPEMemoryStream.Create(const ModuleName: string;
ForceLoadingModule: boolean);
var
FModulePtr: Pointer;
begin
inherited Create;
FModuleFileName := ModuleName;
FModulePtr := Pointer(GetModuleHandle(PChar(ModuleName)));
FModuleToUnload := 0;
if (FModulePtr = nil) and (ForceLoadingModule) then
begin
FModuleToUnload := LoadLibrary(PChar(ModuleName));
FModulePtr := Pointer(FModuleToUnload);
end;
CreateFromModulePtr(FModulePtr);
end;
constructor TPEMemoryStream.Create(ModuleBase: NativeUInt);
begin
inherited Create;
FModuleFileName := GetModuleName(ModuleBase);
FModuleToUnload := 0; // we didn't load it and won't free it
CreateFromModulePtr(Pointer(ModuleBase));
end;
destructor TPEMemoryStream.Destroy;
begin
if FModuleToUnload <> 0 then
FreeLibrary(FModuleToUnload);
inherited;
end;
class function TPEMemoryStream.GetModuleImageSize(ModulePtr: PByte): uint32;
var
dos: PImageDOSHeader;
nt: PImageNTHeaders;
begin
dos := PImageDOSHeader(ModulePtr);
if not dos.e_magic.IsMZ then
raise Exception.Create('Not PE image');
nt := PImageNTHeaders(ModulePtr + dos^.e_lfanew);
if not nt.Signature.IsPE00 then
raise Exception.Create('Not PE image');
Result := nt^.OptionalHeader.pe32.SizeOfImage;
end;
end.