-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
resource.go
318 lines (266 loc) · 9.42 KB
/
resource.go
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
// Copyright 2021 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package pe
import (
"encoding/binary"
"log"
"unsafe"
)
const (
maxAllowedEntries = 0x1000
)
var (
depth = 0
)
// Predefined Resource Types
var (
RTCursor = makeIntResource(1)
RTBitmap = makeIntResource(2)
RTIcon = makeIntResource(3)
RTMenu = makeIntResource(4)
RTDialog = makeIntResource(5)
RTString = makeIntResource(6)
RTFontdir = makeIntResource(7)
RTFont = makeIntResource(8)
RTAccelerator = makeIntResource(9)
RTRCdata = makeIntResource(10)
RTMessagetable = makeIntResource(11)
RTGroupCursor = makeIntResource(12)
RTGroupIcon = makeIntResource(14)
RTVersion = makeIntResource(16)
RTDlgInclude = makeIntResource(17)
RTPlugPlay = makeIntResource(19)
RTVxd = makeIntResource(20)
RTAniCursor = makeIntResource(21)
RTAniIcon = makeIntResource(22)
RTHtml = makeIntResource(23)
RTManifest = makeIntResource(24)
)
// ImageResourceDirectory represents the IMAGE_RESOURCE_DIRECTORY.
// This data structure should be considered the heading of a table because the
// table actually consists of directory entries.
type ImageResourceDirectory struct {
// Resource flags. This field is reserved for future use. It is currently
// set to zero.
Characteristics uint32
// The time that the resource data was created by the resource compiler.
TimeDateStamp uint32
// The major version number, set by the user.
MajorVersion uint16
// The minor version number, set by the user.
MinorVersion uint16
// The number of directory entries immediately following the table that use
// strings to identify Type, Name, or Language entries (depending on the
// level of the table).
NumberOfNamedEntries uint16
// The number of directory entries immediately following the Name entries
// that use numeric IDs for Type, Name, or Language entries.
NumberOfIDEntries uint16
}
// ImageResourceDirectoryEntry represents an entry in the resource directory
// entries.
type ImageResourceDirectoryEntry struct {
// is used to identify either a type of resource, a resource name, or a
// resource's language ID.
Name uint32
//is always used to point to a sibling in the tree, either a directory node
// or a leaf node.
OffsetToData uint32
}
// ImageResourceDataEntry Each Resource Data entry describes an actual unit of
// raw data in the Resource Data area.
type ImageResourceDataEntry struct {
// The address of a unit of resource data in the Resource Data area.
OffsetToData uint32
// The size, in bytes, of the resource data that is pointed to by the Data
// RVA field.
Size uint32
// The code page that is used to decode code point values within the
// resource data. Typically, the code page would be the Unicode code page.
CodePage uint32
// Reserved, must be 0.
Reserved uint32
}
// ResourceDirectory represents resource directory information.
type ResourceDirectory struct {
// IMAGE_RESOURCE_DIRECTORY structure
Struct ImageResourceDirectory
// list of entries
Entries []ResourceDirectoryEntry
}
// ResourceDirectoryEntry represents a resource directory entry.
type ResourceDirectoryEntry struct {
// IMAGE_RESOURCE_DIRECTORY_ENTRY structure.
Struct ImageResourceDirectoryEntry
// If the resource is identified by name this attribute will contain the
// name string. Empty string otherwise. If identified by id, the id is
// available at .Id field.
Name string
// The resource identifier.
ID uint32
// If this entry has a lower level directory this attribute will point to
// the ResourceDirData instance representing it.
Directory ResourceDirectory
// If this entry has no further lower directories and points to the actual
// resource data, this attribute will reference the corresponding
// ResourceDataEntry instance.
Data ResourceDataEntry
}
// ResourceDataEntry represents a resource data entry.
type ResourceDataEntry struct {
// IMAGE_RESOURCE_DATA_ENTRY structure.
Struct ImageResourceDataEntry
// Primary language ID
Lang uint32
Sublang uint32 // Sublanguage ID
}
// makeIntResource mimics the MAKEINTRESOURCE macro.
func makeIntResource(id uintptr) *uint16 {
return (*uint16)(unsafe.Pointer(id))
}
func (pe *File) parseResourceDataEntry(rva uint32) *ImageResourceDataEntry {
dataEntry := ImageResourceDataEntry{}
dataEntrySize := uint32(binary.Size(dataEntry))
offset := pe.getOffsetFromRva(rva)
err := pe.structUnpack(&dataEntry, offset, dataEntrySize)
if err != nil {
log.Println("Error parsing a resource directory data entry, the RVA is invalid")
return nil
}
return &dataEntry
}
func (pe *File) parseResourceDirectoryEntry(rva uint32) *ImageResourceDirectoryEntry {
resource := ImageResourceDirectoryEntry{}
resourceSize := uint32(binary.Size(resource))
offset := pe.getOffsetFromRva(rva)
err := pe.structUnpack(&resource, offset, resourceSize)
if err != nil {
return nil
}
if resource == (ImageResourceDirectoryEntry{}) {
return nil
}
// resource.NameOffset = resource.Name & 0x7FFFFFFF
// resource.__pad = resource.Name & 0xFFFF0000
// resource.Id = resource.Name & 0x0000FFFF
// resource.DataIsDirectory = (resource.OffsetToData & 0x80000000) >> 31
// resource.OffsetToDirectory = resource.OffsetToData & 0x7FFFFFFF
return &resource
}
// Navigating the resource directory hierarchy is like navigating a hard disk.
// There's a master directory (the root directory), which has subdirectories.
// The subdirectories have subdirectories of their own that may point to the
// raw resource data for things like dialog templates.
func (pe *File) doParseResourceDirectory(rva, size, baseRVA, level uint32,
dirs []uint32) (ResourceDirectory, error) {
resourceDir := ImageResourceDirectory{}
resourceDirSize := uint32(binary.Size(resourceDir))
offset := pe.getOffsetFromRva(rva)
err := pe.structUnpack(&resourceDir, offset, resourceDirSize)
if err != nil {
return ResourceDirectory{}, err
}
if baseRVA == 0 {
baseRVA = rva
}
if len(dirs) == 0 {
dirs = append(dirs, rva)
}
// Advance the RVA to the position immediately following the directory
// table header and pointing to the first entry in the table.
rva += resourceDirSize
numberOfEntries := int(resourceDir.NumberOfNamedEntries +
resourceDir.NumberOfIDEntries)
var dirEntries []ResourceDirectoryEntry
// Set a hard limit on the maximum reasonable number of entries.
if numberOfEntries > maxAllowedEntries {
log.Printf(`Error parsing the resources directory.
The directory contains %d entries`, numberOfEntries)
return ResourceDirectory{}, nil
}
for i := 0; i < numberOfEntries; i++ {
res := pe.parseResourceDirectoryEntry(rva)
if res == nil {
log.Println("Error parsing a resource directory entry, the RVA is invalid")
break
}
nameIsString := (res.Name & 0x80000000) >> 31
entryName := ""
entryID := uint32(0)
if nameIsString == 0 {
entryID = res.Name
} else {
nameOffset := res.Name & 0x7FFFFFFF
uStringOffset := pe.getOffsetFromRva(baseRVA + nameOffset)
maxLen, err := pe.ReadUint16(uStringOffset)
if err != nil {
break
}
entryName = pe.readUnicodeStringAtRVA(baseRVA+nameOffset+2,
uint32(maxLen))
}
// A directory entry points to either another resource directory or to
// the data for an individual resource. When the directory entry points
// to another resource directory, the high bit of the second DWORD in
// the structure is set and the remaining 31 bits are an offset to the
// resource directory.
dataIsDirectory := (res.OffsetToData & 0x80000000) >> 31
// The offset is relative to the beginning of the resource section,
// not an RVA.
OffsetToDirectory := res.OffsetToData & 0x7FFFFFFF
if dataIsDirectory > 0 {
// One trick malware can do is to recursively reference
// the next directory. This causes hilarity to ensue when
// trying to parse everything correctly.
// If the original RVA given to this function is equal to
// the next one to parse, we assume that it's a trick.
// Instead of raising a PEFormatError this would skip some
// reasonable data so we just break.
// 9ee4d0a0caf095314fd7041a3e4404dc is the offending sample.
if intInSlice(baseRVA+OffsetToDirectory, dirs) {
break
}
level++
dirs = append(dirs, baseRVA+OffsetToDirectory)
directoryEntry, _ := pe.doParseResourceDirectory(
baseRVA+OffsetToDirectory,
size-(rva-baseRVA),
baseRVA,
level,
dirs)
dirEntries = append(dirEntries, ResourceDirectoryEntry{
Struct: *res,
Name: entryName,
ID: entryID,
Directory: directoryEntry})
} else {
// data is entry
dataEntryStruct := pe.parseResourceDataEntry(baseRVA +
OffsetToDirectory)
entryData := ResourceDataEntry{
Struct: *dataEntryStruct,
Lang: res.Name & 0x3ff,
Sublang: res.Name >> 10,
}
dirEntries = append(dirEntries, ResourceDirectoryEntry{
Struct: *res,
Name: entryName,
ID: entryID,
Data: entryData})
}
rva += uint32(binary.Size(res))
}
return ResourceDirectory{
Struct: resourceDir,
Entries: dirEntries,
}, nil
}
// The resource directory contains resources like dialog templates, icons,
// and bitmaps. The resources are found in a section called .rsrc section.
func (pe *File) parseResourceDirectory(rva, size uint32) error {
var dirs []uint32
Resources, err := pe.doParseResourceDirectory(rva, size, 0, 0, dirs)
pe.Resources = &Resources
return err
}