-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPE.ExecutableLoader.pas
More file actions
530 lines (445 loc) · 11.9 KB
/
PE.ExecutableLoader.pas
File metadata and controls
530 lines (445 loc) · 11.9 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
{
Load and map module into current process.
Module MUST have relocations or be RIP-addressed to be loaded normally.
}
unit PE.ExecutableLoader;
interface
uses
System.Classes,
System.Generics.Collections,
WinApi.Windows,
PE.Common,
PE.Image;
type
TMapStatus = (
msOK,
msImageAlreadyMapped,
msError,
msImageSizeError,
msMapSectionsError,
msSectionAllocError,
msProtectSectionsError,
msImportLibraryNotFound,
msImportNameNotFound,
msImportOrdinalNotFound,
msEntryPointFailure
);
TMapStatusHelper = record helper for TMapStatus
function ToString: string;
end;
TExecutableLoadingOption = (
// Write PE header into start of allocated image.
elo_MapHeader,
// Apply fixups after image mapping.
elo_FixRelocations,
// Resolve imported functions after image mapping.
elo_FixImports,
// Apply protection defined in section header to mapped sections.
elo_ProtectSections,
// Call EXE or DLL entry point when image mapped.
elo_CallEntryPoint
);
TExecutableLoadingOptions = set of TExecutableLoadingOption;
const
DEFAULT_OPTIONS = [
elo_MapHeader,
elo_FixRelocations,
elo_FixImports,
elo_ProtectSections,
elo_CallEntryPoint
];
type
TEXEEntry = procedure(); stdcall;
TDLLEntry = function(hInstDLL: HINST; fdwReason: DWORD; lpvReserved: LPVOID): BOOL; stdcall;
TLoadedModules = TDictionary<string, HMODULE>;
TExecutableModule = class
private
FPE: TPEImage;
FOptions: TExecutableLoadingOptions;
FInstance: NativeUInt;
FEntry: Pointer;
FSizeOfImage: UInt32;
FLoadedImports: TLoadedModules;
function Check(const Desc: string; var rslt: TMapStatus; ms: TMapStatus): boolean;
function MapSections(PrefferedVa: UInt64): TMapStatus;
function MapHeader: TMapStatus;
function ProtectSections: TMapStatus;
function Relocate: TMapStatus;
function LoadImports: TMapStatus;
procedure UnloadImports;
public
constructor Create(PE: TPEImage);
destructor Destroy; override;
function IsImageMapped: boolean; inline;
function Load(
PrefferedVa: UInt64 = 0;
Options: TExecutableLoadingOptions = DEFAULT_OPTIONS): TMapStatus;
function Unload: boolean;
property Instance: NativeUInt read FInstance;
end;
implementation
uses
System.SysUtils,
PE.Types.FileHeader,
PE.Utils,
PE.Sections,
PE.Image.Saving,
PE.Imports,
PE.Imports.Func,
PE.Imports.Lib,
PE.MemoryStream,
PE.Section,
PE.Types.Relocations;
function HasBits(Value, mask: DWORD): boolean; inline;
begin
Result := (Value and mask) <> 0;
end;
function CharacteristicsToProtect(CH: DWORD): DWORD;
var
X, R, W, C: boolean;
begin
Result := 0;
X := HasBits(CH, IMAGE_SCN_MEM_EXECUTE);
R := HasBits(CH, IMAGE_SCN_MEM_READ);
W := HasBits(CH, IMAGE_SCN_MEM_WRITE);
C := HasBits(CH, IMAGE_SCN_MEM_NOT_CACHED);
if X then
begin
if R then
begin
if W then
Result := Result or PAGE_EXECUTE_READWRITE
else
Result := Result or PAGE_EXECUTE_READ;
end
else if W then
Result := Result or PAGE_EXECUTE_WRITECOPY
else
Result := Result or PAGE_EXECUTE;
end
else if R then
begin
if W then
Result := Result or PAGE_READWRITE
else
Result := Result or PAGE_READONLY;
end
else if W then
Result := Result or PAGE_WRITECOPY
else
begin
Result := Result or PAGE_NOACCESS;
end;
if C then
Result := Result or PAGE_NOCACHE;
end;
function min(d1, d2: DWORD): DWORD;
begin
if d1 < d2 then
Result := d1
else
Result := d2;
end;
{ TExecutableModule }
function TExecutableModule.Check(const Desc: string; var rslt: TMapStatus;
ms: TMapStatus): boolean;
begin
rslt := ms;
Result := ms = msOK;
if Result then
FPE.Msg.Write(Desc + ' .. OK.')
else
FPE.Msg.Write(Desc + ' .. failed.')
end;
constructor TExecutableModule.Create(PE: TPEImage);
begin
FPE := PE;
FLoadedImports := TLoadedModules.Create;
end;
destructor TExecutableModule.Destroy;
begin
Unload;
FLoadedImports.Free;
inherited;
end;
function TExecutableModule.IsImageMapped: boolean;
begin
Result := FInstance <> 0;
end;
function TExecutableModule.MapSections(PrefferedVa: UInt64): TMapStatus;
var
i: integer;
sec: TPESection;
size: DWORD;
va: pbyte;
begin
Result := msMapSectionsError;
FSizeOfImage := FPE.CalcVirtualSizeOfImage;
if FSizeOfImage = 0 then
exit(msImageSizeError);
// Reserve and commit memory for image.
FInstance := NativeUInt(VirtualAlloc(
Pointer(PrefferedVa),
FSizeOfImage,
MEM_RESERVE or MEM_COMMIT,
PAGE_READWRITE
));
if FInstance = 0 then
exit(msSectionAllocError);
// copy sections and header
// todo: header
for i := 0 to FPE.Sections.Count - 1 do
begin
sec := FPE.Sections[i];
if sec.VirtualSize <> 0 then
begin
va := pbyte(FInstance) + sec.RVA;
size := min(sec.VirtualSize, sec.RawSize);
if not FPE.SeekRVA(sec.RVA) then
exit;
FPE.Read(va, size);
end;
end;
Result := msOK;
end;
function TExecutableModule.MapHeader: TMapStatus;
var
ms: TPECustomMemoryStream;
begin
if not(elo_MapHeader in FOptions) then
exit(msOK);
ms := TPECustomMemoryStream.CreateFromPointer(Pointer(FInstance), FSizeOfImage);
try
// Write header as-is without recalcualtions.
if not SaveHeaders(FPE, ms, False) then
exit(msError);
finally
ms.Free;
end;
exit(msOK);
end;
function TExecutableModule.LoadImports: TMapStatus;
var
ImpLib: TPEImportLibrary;
Fn: TPEImportFunction;
ImpLibName: String;
hmod: HMODULE;
proc: Pointer;
RVA: TRVA;
va: TVA;
ModuleMustBeFreed: boolean;
begin
if not(elo_FixImports in FOptions) then
exit(msOK);
for ImpLib in FPE.Imports.Libs do
begin
ImpLibName := ImpLib.Name;
FPE.Msg.Write('Processing import module: "%s"', [ImpLibName]);
// Check if module already in address space.
hmod := GetModuleHandle(PChar(ImpLibName));
ModuleMustBeFreed := hmod = 0;
// Try make system load lib from default paths.
if hmod = 0 then
hmod := LoadLibrary(PChar(ImpLibName));
// Try load from dir, where image located.
if (hmod = 0) and (FPE.FileName <> '') then
begin
hmod := LoadLibrary(PChar(ExtractFilePath(FPE.FileName) + ImpLibName));
end;
// If lib not found, raise.
if hmod = 0 then
begin
FPE.Msg.Write('Imported module "%s" not loaded.', [ImpLibName]);
// It's either not found, or its dependencies not found.
exit(msImportLibraryNotFound);
end;
// Module found.
if ModuleMustBeFreed then
FLoadedImports.Add(ImpLibName, hmod);
// Process import functions.
RVA := ImpLib.IatRva;
for Fn in ImpLib.Functions do
begin
// Find imported function.
// By Name.
if Fn.Name <> '' then
begin
proc := GetProcAddress(hmod, PChar(Fn.Name));
if proc = nil then
begin
FPE.Msg.Write('Imported name "%s" not found.', [Fn.Name]);
exit(msImportNameNotFound);
end;
end
else
// By Ordinal.
begin
proc := GetProcAddress(hmod, PAnsiChar(Fn.Ordinal));
if proc = nil then
begin
FPE.Msg.Write('Imported ordinal "%d" not found.', [Fn.Ordinal]);
exit(msImportOrdinalNotFound);
end;
end;
// Patch.
va := FInstance + RVA;
if FPE.Is32bit then
PUINT(va)^ := UInt32(proc)
else if FPE.Is64bit then
PUInt64(va)^ := UInt64(proc);
inc(RVA, FPE.ImageWordSize);
end;
// inc(RVA, FPE.ImageWordSize); // null
end;
Result := msOK;
end;
function TExecutableModule.ProtectSections: TMapStatus;
var
i: integer;
sec: TPESection;
prot: cardinal;
va: pbyte;
dw: DWORD;
begin
if not(elo_ProtectSections in FOptions) then
exit(msOK);
for i := 0 to FPE.Sections.Count - 1 do
begin
Result := msProtectSectionsError;
sec := FPE.Sections[i];
if sec.VirtualSize <> 0 then
begin
va := Pointer(FInstance);
inc(va, sec.RVA);
prot := CharacteristicsToProtect(sec.Flags);
if not VirtualProtect(va, sec.VirtualSize, prot, dw) then
exit;
end;
end;
Result := msOK;
end;
function TExecutableModule.Relocate: TMapStatus;
var
Reloc: TReloc;
Delta: UInt32;
pDst: PCardinal;
begin
if not(elo_FixRelocations in FOptions) then
exit(msOK);
Delta := FInstance - FPE.ImageBase;
if Delta = 0 then
exit(msOK); // no relocation needed
for Reloc in FPE.Relocs.Items do
begin
case Reloc.&Type of
IMAGE_REL_BASED_HIGHLOW:
begin
pDst := PCardinal(FInstance + Reloc.RVA);
inc(pDst^, Delta);
end;
else
raise Exception.CreateFmt('Unsupported relocation type: %d', [Reloc.&Type]);
end;
end;
Result := msOK;
end;
function TExecutableModule.Load(
PrefferedVa: UInt64;
Options: TExecutableLoadingOptions): TMapStatus;
var
EntryOK: boolean;
begin
if IsImageMapped then
exit(msImageAlreadyMapped);
Result := msError;
FOptions := Options;
if
Check('Map Sections', Result, MapSections(PrefferedVa)) and
Check('Map Header', Result, MapHeader()) and
Check('Fix Relocation', Result, Relocate()) and
Check('Fix Imports', Result, LoadImports()) and
Check('Protect Sections', Result, ProtectSections()) then
begin
if FPE.EntryPointRVA = 0 then
FEntry := nil
else
FEntry := Pointer(FInstance + FPE.EntryPointRVA);
// If don't need to call entry or there is no entry just skip this part
if (not Assigned(FEntry)) or (not(elo_CallEntryPoint in FOptions)) then
EntryOK := True
// Call Entry Point.
else if FPE.IsDLL then
begin
FPE.Msg.Write('Calling DLL Entry with DLL_PROCESS_ATTACH.');
EntryOK := TDLLEntry(FEntry)(FInstance, DLL_PROCESS_ATTACH, nil);
if not EntryOK then
FPE.Msg.Write('DLL returned FALSE.');
end
else
begin
FPE.Msg.Write('Calling EXE Entry.');
TEXEEntry(FEntry)();
EntryOK := True;
end;
if EntryOK then
exit(msOK)
else
Result := msEntryPointFailure;
end;
// If something failed.
Unload;
end;
function TExecutableModule.Unload: boolean;
begin
if not IsImageMapped then
exit(True);
if (elo_CallEntryPoint in FOptions) and Assigned(FEntry) then
begin
// DLL finalization.
if FPE.IsDLL then
begin
FPE.Msg.Write('Calling DLL Entry with DLL_PROCESS_DETACH.');
TDLLEntry(FEntry)(FInstance, DLL_PROCESS_DETACH, nil);
end;
end;
// Unload imported libraries.
UnloadImports;
// Free image memory
VirtualFree(Pointer(FInstance), FSizeOfImage, MEM_RELEASE);
FInstance := 0;
Result := True;
end;
procedure TExecutableModule.UnloadImports;
var
Pair: TPair<string, HMODULE>;
begin
for Pair in FLoadedImports do
begin
FPE.Msg.Write('Unloading import "%s"', [Pair.Key]);
FreeLibrary(Pair.Value);
end;
FLoadedImports.Clear;
end;
{ TMapStatusHelper }
const
MapStatusText: array [TMapStatus] of string = (
'OK',
'Image already mapped',
'Error',
'Image size error',
'Map sections error',
'Section allocation error',
'Protect sections error',
'Import library not found',
'Import name not found',
'Import ordinal not found',
'Entry point failure'
);
function TMapStatusHelper.ToString: string;
begin
if self in [low(TMapStatus) .. high(TMapStatus)] then
Result := MapStatusText[self]
else
Result := '???';
end;
end.