-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresources.go
215 lines (186 loc) · 5.99 KB
/
resources.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
package pelican
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/itchio/headway/united"
"github.com/itchio/pelican/pe"
xj "github.com/basgys/goxml2json"
"github.com/pkg/errors"
)
type imageResourceDirectory struct {
Characteristics uint32
TimeDateStamp uint32
MajorVersion uint16
MinorVersion uint16
NumberOfNamedEntries uint16
NumberOfIdEntries uint16
}
type imageResourceDirectoryEntry struct {
NameId uint32
Data uint32
}
type imageResourceDataEntry struct {
Data uint32
Size uint32
CodePage uint32
Reserved uint32
}
type ResourceType uint32
// https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms648009(v=vs.85).aspx
const (
ResourceTypeNone ResourceType = 0
ResourceTypeCursor ResourceType = 1
ResourceTypeBitmap ResourceType = 2
ResourceTypeIcon ResourceType = 3
ResourceTypeMenu ResourceType = 4
ResourceTypeDialog ResourceType = 5
ResourceTypeString ResourceType = 6
ResourceTypeFontDir ResourceType = 7
ResourceTypeFont ResourceType = 8
ResourceTypeAccelerator ResourceType = 9
ResourceTypeRcData ResourceType = 10
ResourceTypeMessageTable ResourceType = 11
ResourceTypeGroupCursor ResourceType = ResourceTypeCursor + 11 // 12
ResourceTypeGroupIcon ResourceType = ResourceTypeIcon + 11 // 14
ResourceTypeVersion ResourceType = 16
ResourceTypeDlgInclude ResourceType = 17
ResourceTypePlugPlay ResourceType = 19
ResourceTypeVXD ResourceType = 20 // vxd = virtual device
ResourceTypeAniCursor ResourceType = 21
ResourceTypeAniIcon ResourceType = 22
ResourceTypeHTML ResourceType = 23
ResourceTypeManifest ResourceType = 24
)
var ResourceTypeNames = map[ResourceType]string{
ResourceTypeCursor: "Cursor",
ResourceTypeBitmap: "Bitmap",
ResourceTypeIcon: "Icon",
ResourceTypeMenu: "Menu",
ResourceTypeDialog: "Dialog",
ResourceTypeString: "String",
ResourceTypeFontDir: "FontDir",
ResourceTypeFont: "Font",
ResourceTypeAccelerator: "Accelerator",
ResourceTypeRcData: "RcData",
ResourceTypeMessageTable: "MessageTable",
ResourceTypeGroupCursor: "GroupCursor",
ResourceTypeGroupIcon: "GroupIcon",
ResourceTypeVersion: "Version",
ResourceTypeDlgInclude: "DlgInclude",
ResourceTypePlugPlay: "PlugPlay",
ResourceTypeVXD: "VXD",
ResourceTypeAniCursor: "AniCursor",
ResourceTypeAniIcon: "AniIcon",
ResourceTypeHTML: "HTML",
ResourceTypeManifest: "Manifest",
}
func (params *ProbeParams) parseResources(info *PeInfo, sect *pe.Section) error {
consumer := params.Consumer
consumer.Debugf("Found resource section (%s)", united.FormatBytes(int64(sect.Size)))
var readDirectory func(offset uint32, level int, resourceType ResourceType) error
readDirectory = func(offset uint32, level int, resourceType ResourceType) error {
prefix := strings.Repeat(" ", level)
log := func(msg string, args ...interface{}) {
consumer.Debugf("%s%s", prefix, fmt.Sprintf(msg, args...))
}
br := io.NewSectionReader(sect, int64(offset), int64(sect.Size)-int64(offset))
ird := new(imageResourceDirectory)
err := binary.Read(br, binary.LittleEndian, ird)
if err != nil {
return errors.WithStack(err)
}
for i := uint16(0); i < ird.NumberOfNamedEntries+ird.NumberOfIdEntries; i++ {
irde := new(imageResourceDirectoryEntry)
err = binary.Read(br, binary.LittleEndian, irde)
if err != nil {
return errors.WithStack(err)
}
if irde.NameId&0x80000000 > 0 {
continue
}
id := irde.NameId & 0xffff
if level == 0 {
typ := ResourceType(id)
if name, ok := ResourceTypeNames[typ]; ok {
log("=> %s", name)
} else {
log("=> type #%d (unknown)", id)
}
} else {
log("=> %d", id)
}
if irde.Data&0x80000000 > 0 {
offset := irde.Data & 0x7fffffff
recResourceType := resourceType
if level == 0 {
recResourceType = ResourceType(id)
}
err := readDirectory(offset, level+1, recResourceType)
if err != nil {
return errors.WithStack(err)
}
continue
}
dbr := io.NewSectionReader(sect, int64(irde.Data), int64(sect.Size)-int64(irde.Data))
irda := new(imageResourceDataEntry)
err = binary.Read(dbr, binary.LittleEndian, irda)
if err != nil {
return errors.WithStack(err)
}
if resourceType == ResourceTypeManifest || resourceType == ResourceTypeVersion {
log("@ %x (%s, %d bytes)", irda.Data, united.FormatBytes(int64(irda.Size)), irda.Size)
dataStart := int64(irda.Data - sect.VirtualAddress)
log("is dataStart 32-bit aligned? %v", dataStart%4 == 0)
sr := io.NewSectionReader(sect, dataStart, int64(irda.Size))
rawData, err := ioutil.ReadAll(sr)
if err != nil {
return errors.WithStack(err)
}
switch resourceType {
case ResourceTypeManifest:
// actually not utf-16,
// but TODO: figure out
// codepage
stringData := string(rawData)
log("=========================")
for _, l := range strings.Split(stringData, "\n") {
log("%s", l)
}
log("=========================")
js, err := xj.Convert(strings.NewReader(stringData))
if err != nil {
if params.Strict {
return errors.WithMessage(err, "while converting manifest to json")
}
consumer.Warnf("Could not convert manifest to json: %+v", err)
} else {
err := interpretManifest(info, js.Bytes())
if err != nil {
if params.Strict {
return errors.WithMessage(err, "while intepreting manifest")
}
consumer.Warnf("Could not interpret manifest: %+v", err)
}
}
case ResourceTypeVersion:
err := params.parseVersion(info, rawData)
if err != nil {
if params.Strict {
return errors.WithMessage(err, "while parsing version block")
}
consumer.Warnf("Could not parse resources: %+v", err)
}
}
}
}
return nil
}
err := readDirectory(0, 0, 0)
if err != nil {
return errors.WithStack(err)
}
return nil
}