-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathPE.Parser.Export.pas
181 lines (146 loc) · 4.38 KB
/
PE.Parser.Export.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
unit PE.Parser.Export;
interface
uses
PE.Common,
PE.Types,
PE.Types.Directories,
PE.Types.Export;
type
TPEExportParser = class(TPEParser)
public
function Parse: TParserResult; override;
end;
implementation
uses
PE.Image,
PE.ExportSym;
{ TPEExportParser }
function TPEExportParser.Parse: TParserResult;
var
PE: TPEImage;
ExpIDD: TImageDataDirectory;
ExpDir: TImageExportDirectory;
i, base: uint32;
ordnl: uint16;
RVAs: array of uint32;
NamePointerRVAs: array of uint32;
OrdinalTableRVAs: array of uint16;
Exp: array of TPEExportSym;
Item: TPEExportSym;
begin
PE := TPEImage(FPE);
// Clear exports.
PE.ExportSyms.Clear;
// Get export dir.
if not PE.DataDirectories.Get(DDIR_EXPORT, @ExpIDD) then
exit(PR_OK);
// No exports is ok.
if ExpIDD.IsEmpty then
exit(PR_OK);
// If can't find Export dir, failure.
if not PE.SeekRVA(ExpIDD.VirtualAddress) then
exit(PR_ERROR);
// If can't read whole table, failure.
if not PE.ReadEx(@ExpDir, Sizeof(ExpDir)) then
begin
PE.Msg.Write('Export Parser: not enough space to read dir. header.');
exit(PR_ERROR);
end;
// If no addresses, ok.
if ExpDir.AddressTableEntries = 0 then
begin
PE.Msg.Write('Export Parser: directory present, but there are no functions.');
exit(PR_OK);
end;
if ExpDir.ExportFlags <> 0 then
begin
PE.Msg.Write('Export Parser: reserved directory flags <> 0');
exit(PR_ERROR);
end;
// Read lib exported name.
if (ExpDir.NameRVA <> 0) then
begin
if not PE.SeekRVA(ExpDir.NameRVA) then
begin
PE.Msg.Write('Export Parser: Wrong RVA of dll exported name = 0x%x', [ExpDir.NameRVA]);
exit(PR_ERROR);
end;
PE.ExportedName := PE.ReadAnsiString;
end;
base := ExpDir.OrdinalBase;
// Check if there's too many exports.
if (ExpDir.AddressTableEntries >= SUSPICIOUS_MIN_LIMIT_EXPORTS) or
(ExpDir.NumberOfNamePointers >= SUSPICIOUS_MIN_LIMIT_EXPORTS) then
begin
exit(PR_SUSPICIOUS);
end;
SetLength(Exp, ExpDir.AddressTableEntries);
SetLength(RVAs, ExpDir.AddressTableEntries);
// load RVAs of exported data
if not(PE.SeekRVA(ExpDir.ExportAddressTableRVA) and
PE.ReadEx(@RVAs[0], 4 * ExpDir.AddressTableEntries)) then
exit(PR_ERROR);
if ExpDir.NumberOfNamePointers <> 0 then
begin
// name/ordinal only
SetLength(NamePointerRVAs, ExpDir.NumberOfNamePointers);
SetLength(OrdinalTableRVAs, ExpDir.NumberOfNamePointers);
// load RVAs of name pointers
if not((PE.SeekRVA(ExpDir.NamePointerRVA)) and
PE.ReadEx(@NamePointerRVAs[0], 4 * ExpDir.NumberOfNamePointers)) then
exit(PR_ERROR);
// load ordinals according to names
if not((PE.SeekRVA(ExpDir.OrdinalTableRVA)) and
PE.ReadEx(@OrdinalTableRVAs[0], 2 * ExpDir.NumberOfNamePointers)) then
exit(PR_ERROR);
end;
if ExpDir.AddressTableEntries <> 0 then
begin
for i := 0 to ExpDir.AddressTableEntries - 1 do
begin
Item := TPEExportSym.Create;
Item.Ordinal := i + base;
Item.RVA := RVAs[i];
Exp[i] := Item;
// if rva in export section, it's forwarder
Exp[i].Forwarder := ExpIDD.Contain(RVAs[i]);
end;
end;
// read names
if ExpDir.NumberOfNamePointers <> 0 then
begin
for i := 0 to ExpDir.NumberOfNamePointers - 1 do
begin
if (NamePointerRVAs[i] <> 0) then
begin
ordnl := OrdinalTableRVAs[i];
// Check if ordinal is correct.
if ordnl >= length(Exp) then
continue;
if not Exp[ordnl].IsValid then
continue;
// Read export name.
if not PE.SeekRVA(NamePointerRVAs[i]) then
exit(PR_ERROR);
Exp[ordnl].Name := PE.ReadAnsiString;
// Read forwarder, if it is.
if Exp[ordnl].Forwarder then
begin
// if it is forwarder, rva will point inside of export dir.
if not PE.SeekRVA(Exp[ordnl].RVA) then
exit(PR_ERROR);
Exp[ordnl].ForwarderName := PE.ReadAnsiString;
Exp[ordnl].RVA := 0; // no real address
end;
end;
end;
end;
// finally array to list
for i := low(Exp) to high(Exp) do
if Exp[i].IsValid then
PE.ExportSyms.Add(Exp[i])
else
Exp[i].Free;
exit(PR_OK);
end;
end.