-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPE.Build.Import.pas
More file actions
253 lines (222 loc) · 5.81 KB
/
PE.Build.Import.pas
File metadata and controls
253 lines (222 loc) · 5.81 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
unit PE.Build.Import;
interface
uses
System.Classes,
System.Generics.Collections,
System.SysUtils,
PE.Common,
PE.Section,
PE.Build.Common,
PE.Utils;
type
TImportBuilder = class(TDirectoryBuilder)
public
// Modified after Build called.
BuiltIatRVA: TRVA;
BuiltIatSize: uint32;
procedure Build(DirRVA: TRVA; Stream: TStream); override;
class function GetDefaultSectionFlags: Cardinal; override;
class function GetDefaultSectionName: string; override;
class function NeedRebuildingIfRVAChanged: Boolean; override;
end;
implementation
uses
// Expand
PE.Image,
PE.Types.FileHeader,
//
PE.Imports,
PE.Imports.Lib,
PE.Imports.Func,
PE.Types.Imports;
{
* Import directory layout
*
* IDT.
* For each library
* Import Descriptor
* Null Import Descriptor
*
* Name pointers.
* For each library
* For each function
* Pointer to function hint/name or ordinal
* Null pointer
*
* IAT.
* For each library
* For each function
* Function address
* Null address
*
* Names.
* For each library
* Library name
* ** align 2 **
* For each function
* Hint: uint16
* Function name: variable length
}
procedure WriteStringAligned2(Stream: TStream; const s: string);
const
null: byte = 0;
var
bytes: TBytes;
begin
bytes := TEncoding.ANSI.GetBytes(s);
StreamWrite(Stream, bytes[0], length(bytes));
StreamWrite(Stream, null, 1);
if Stream.Position mod 2 <> 0 then
StreamWrite(Stream, null, 1);
end;
procedure WriteIDT(
Stream: TStream;
var ofs_idt: uint32;
const idt: TImportDirectoryTable);
begin
Stream.Position := ofs_idt;
StreamWrite(Stream, idt, sizeof(idt));
inc(ofs_idt, sizeof(idt));
end;
procedure WriteNullIDT(Stream: TStream; var ofs_idt: uint32);
var
idt: TImportDirectoryTable;
begin
idt.Clear;
WriteIDT(Stream, ofs_idt, idt);
end;
// Write library name and set idt name pointer, update name pointer offset.
procedure WriteLibraryName(
Stream: TStream;
Lib: TPEImportLibrary;
DirRVA: TRVA;
var ofs_names: uint32;
var idt: TImportDirectoryTable);
begin
// library name
idt.NameRVA := DirRVA + ofs_names;
Stream.Position := ofs_names;
WriteStringAligned2(Stream, Lib.Name);
ofs_names := Stream.Position;
end;
function MakeOrdinalRVA(ordinal: uint16; wordsize: byte): TRVA; inline;
begin
if wordsize = 4 then
result := $80000000 or ordinal
else
result := $8000000000000000 or ordinal;
end;
procedure WriteFunctionNamesOrOrdinalsAndIat(
Stream: TStream;
Lib: TPEImportLibrary;
DirRVA: TRVA;
var ofs_names: uint32;
var ofs_name_pointers: uint32;
var ofs_iat: uint32;
var idt: TImportDirectoryTable;
wordsize: byte);
var
hint: uint16;
fn: TPEImportFunction;
rva_hint_name: TRVA;
begin
if Lib.Functions.Count = 0 then
exit;
idt.ImportLookupTableRVA := DirRVA + ofs_name_pointers;
if (not Lib.Original) then
begin
idt.ImportAddressTable := DirRVA + ofs_iat;
// Update IAT in library.
Lib.IatRva := idt.ImportAddressTable;
end
else
begin
idt.ImportAddressTable := Lib.IatRva;
end;
hint := 0;
for fn in Lib.Functions do
begin
// Write name.
if not fn.Name.IsEmpty then
begin
// If imported by name.
rva_hint_name := DirRVA + ofs_names;
Stream.Position := ofs_names;
StreamWrite(Stream, hint, sizeof(hint));
WriteStringAligned2(Stream, fn.Name);
ofs_names := Stream.Position;
end
else
begin
// If imported by ordinal.
rva_hint_name := MakeOrdinalRVA(fn.ordinal, wordsize);
end;
// Write name pointer.
Stream.Position := ofs_name_pointers;
StreamWrite(Stream, rva_hint_name, wordsize);
inc(ofs_name_pointers, wordsize);
// Write IAT item.
Stream.Position := ofs_iat;
StreamWrite(Stream, rva_hint_name, wordsize);
inc(ofs_iat, wordsize);
end;
rva_hint_name := 0;
// Write null name pointer.
Stream.Position := ofs_name_pointers;
StreamWrite(Stream, rva_hint_name, wordsize);
ofs_name_pointers := Stream.Position;
// Write null IAT item.
Stream.Position := ofs_iat;
StreamWrite(Stream, rva_hint_name, wordsize);
inc(ofs_iat, wordsize);
end;
procedure TImportBuilder.Build(DirRVA: TRVA; Stream: TStream);
var
idt: TImportDirectoryTable;
Lib: TPEImportLibrary;
elements: uint32;
var
ofs_idt: uint32;
ofs_name_pointers: uint32;
ofs_iat, ofs_iat_0: uint32;
ofs_names: uint32;
begin
BuiltIatRVA := 0;
BuiltIatSize := 0;
if FPE.Imports.Libs.Count = 0 then
exit;
// Calculate initial offsets.
elements := 0;
for Lib in FPE.Imports.Libs do
inc(elements, Lib.Functions.Count + 1);
ofs_idt := 0;
ofs_name_pointers := sizeof(idt) * (FPE.Imports.Libs.Count + 1);
ofs_iat := ofs_name_pointers + elements * (FPE.ImageWordSize);
ofs_iat_0 := ofs_iat;
ofs_names := ofs_name_pointers + 2 * elements * (FPE.ImageWordSize);
// Write.
for Lib in FPE.Imports.Libs do
begin
idt.Clear;
WriteLibraryName(Stream, Lib, DirRVA, ofs_names, idt);
WriteFunctionNamesOrOrdinalsAndIat(Stream, Lib, DirRVA,
ofs_names, ofs_name_pointers, ofs_iat, idt, FPE.ImageWordSize);
WriteIDT(Stream, ofs_idt, idt);
end;
WriteNullIDT(Stream, ofs_idt);
self.BuiltIatRVA := DirRVA + ofs_iat_0;
self.BuiltIatSize := ofs_iat - ofs_iat_0;
end;
class function TImportBuilder.GetDefaultSectionFlags: Cardinal;
begin
result := $C0000040;
end;
class function TImportBuilder.GetDefaultSectionName: string;
begin
result := '.idata';
end;
class function TImportBuilder.NeedRebuildingIfRVAChanged: Boolean;
begin
result := True;
end;
end.