forked from vdisasm/pe-image-for-delphi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPE.Parser.Resources.pas
228 lines (189 loc) · 5.42 KB
/
PE.Parser.Resources.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit PE.Parser.Resources;
interface
uses
SysUtils,
PE.Common,
PE.Types,
PE.Types.Directories,
PE.Types.Resources,
PE.Resources;
type
TPEResourcesParser = class(TPEParser)
protected
FBaseRVA: TRVA; // RVA of RSRC section base
FTree: TResourceTree;
// Read resource node entry.
function ReadEntry(
ParentNode: TResourceTreeBranchNode;
RVA: TRVA;
Index: integer;
RDT: PResourceDirectoryTable): TResourceTreeNode;
// Read resource node.
function ReadNode(
ParentNode: TResourceTreeBranchNode;
RVA: TRVA): TParserResult;
function LogInvalidResourceSizesTraverse(Node: TResourceTreeNode): boolean;
procedure LogInvalidResourceSizes;
public
function Parse: TParserResult; override;
end;
implementation
uses
PE.Image;
{ TPEResourcesParser }
procedure TPEResourcesParser.LogInvalidResourceSizes;
begin
FTree.Root.Traverse(LogInvalidResourceSizesTraverse);
end;
function TPEResourcesParser.LogInvalidResourceSizesTraverse(Node: TResourceTreeNode): boolean;
begin
if Node.IsLeaf then
if not TResourceTreeLeafNode(Node).ValidSize then
TPEImage(FPE).Msg.Write(SCategoryResources, 'Bad size of resource (probably packed): %s', [Node.GetPath]);
Result := True;
end;
function TPEResourcesParser.Parse: TParserResult;
var
Img: TPEImage;
dir: TImageDataDirectory;
begin
Img := TPEImage(FPE);
// Check if directory present.
if not Img.DataDirectories.Get(DDIR_RESOURCE, @dir) then
exit(PR_OK);
if dir.IsEmpty then
exit(PR_OK);
// Store base RVA.
FBaseRVA := dir.VirtualAddress;
// Try to seek resource dir.
if not Img.SeekRVA(FBaseRVA) then
exit(PR_ERROR);
// Read root and children.
FTree := Img.ResourceTree;
ReadNode(FTree.Root, FBaseRVA);
// Log invalid leaf nodes.
LogInvalidResourceSizes;
exit(PR_OK);
end;
function TPEResourcesParser.ReadEntry(
ParentNode: TResourceTreeBranchNode;
RVA: TRVA;
Index: integer;
RDT: PResourceDirectoryTable): TResourceTreeNode;
var
Img: TPEImage;
Entry: TResourceDirectoryEntry;
DataEntry: TResourceDataEntry;
SubRVA, DataRVA, NameRVA: TRVA;
LeafNode: TResourceTreeLeafNode;
BranchNode: TResourceTreeBranchNode;
EntryName: string;
begin
Result := nil;
Img := TPEImage(FPE);
// Try to read entry.
if not Img.SeekRVA(RVA + Index * SizeOf(Entry)) then
begin
Img.Msg.Write(SCategoryResources, 'Bad resource entry RVA.');
exit;
end;
if not Img.ReadEx(@Entry, SizeOf(Entry)) then
begin
Img.Msg.Write(SCategoryResources, 'Bad resource entry.');
exit;
end;
// Prepare entry name.
EntryName := '';
if Entry.EntryType = ResourceEntryByName then
begin
NameRVA := FBaseRVA + Entry.NameRVA;
if not Img.SeekRVA(NameRVA) then
begin
Img.Msg.Write(SCategoryResources, 'Bad entry name RVA (0x%x)', [NameRVA]);
exit;
end;
EntryName := Img.ReadUnicodeStringLenPfx2;
end;
// Check if RVA of child is correct.
DataRVA := Entry.DataEntryRVA + FBaseRVA;
if not Img.RVAExists(DataRVA) then
begin
Img.Msg.Write(SCategoryResources, 'Bad entry RVA (0x%x)', [DataRVA]);
exit;
end;
// Handle Leaf or Branch.
if Entry.IsDataEntryRVA then
begin
{
Leaf node
}
DataRVA := Entry.DataEntryRVA + FBaseRVA;
if not(Img.SeekRVA(DataRVA) and Img.ReadEx(@DataEntry, SizeOf(DataEntry))) then
begin
Img.Msg.Write(SCategoryResources, 'Bad resource leaf node.');
exit;
end;
LeafNode := TResourceTreeLeafNode.CreateFromEntry(FPE, DataEntry);
Result := LeafNode;
end
else
begin
{
Branch Node.
}
// Alloc and fill node.
BranchNode := TResourceTreeBranchNode.Create;
if RDT <> nil then
begin
BranchNode.Characteristics := RDT^.Characteristics;
BranchNode.TimeDateStamp := RDT^.TimeDateStamp;
BranchNode.MajorVersion := RDT^.MajorVersion;
BranchNode.MinorVersion := RDT^.MinorVersion;
end;
// Get sub-level RVA.
SubRVA := Entry.SubdirectoryRVA + FBaseRVA;
// Read children.
ReadNode(BranchNode, SubRVA);
Result := BranchNode;
end;
// Set id or name.
if Entry.EntryType = ResourceEntryById then
Result.Id := Entry.IntegerID
else
Result.Name := EntryName;
// Add node.
ParentNode.Add(Result);
end;
function TPEResourcesParser.ReadNode(ParentNode: TResourceTreeBranchNode; RVA: TRVA): TParserResult;
var
Img: TPEImage;
RDT: TResourceDirectoryTable;
i, Total: integer;
begin
Img := TPEImage(FPE);
if not Img.SeekRVA(RVA) then
begin
Img.Msg.Write(SCategoryResources, 'Bad resource directory table RVA (0x%x)', [RVA]);
exit(PR_ERROR);
end;
// Read Directory Table.
if not Img.ReadEx(@RDT, SizeOf(RDT)) then
begin
Img.Msg.Write(SCategoryResources, 'Failed to read resource directory table.');
exit(PR_ERROR);
end;
inc(RVA, SizeOf(RDT));
if (RDT.NumberOfNameEntries = 0) and (RDT.NumberOfIDEntries = 0) then
begin
Img.Msg.Write(SCategoryResources, 'Node have no name/id entries.');
exit(PR_ERROR);
end;
// Total number of entries.
Total := RDT.NumberOfNameEntries + RDT.NumberOfIDEntries;
// Read entries.
for i := 0 to Total - 1 do
if ReadEntry(ParentNode, RVA, i, @RDT) = nil then
exit(PR_ERROR);
exit(PR_OK);
end;
end.