-
Notifications
You must be signed in to change notification settings - Fork 17
/
messages_windows.go
285 lines (237 loc) · 6.85 KB
/
messages_windows.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
// +build windows
package evtx
import (
"os"
"path/filepath"
"strings"
lru "github.com/hashicorp/golang-lru"
errors "github.com/pkg/errors"
"golang.org/x/sys/windows/registry"
"www.velocidex.com/golang/binparsergen/reader"
pe "www.velocidex.com/golang/go-pe"
)
func NewWindowsMessageResolver() *WindowsMessageResolver {
cache, err := lru.New(100)
if err != nil {
panic(err)
}
return &WindowsMessageResolver{
// string->MessageSet
cache: cache,
}
}
type WindowsMessageResolver struct {
cache *lru.Cache
}
func (self *WindowsMessageResolver) getMessageSets(
provider, channel string) (*MessageSet, error) {
// Get provider from cache - the cache key is both provider and
// channel.
key := channel + provider
message_set_any, pres := self.cache.Get(key)
if !pres {
var err error
message_set_any, err = GetMessagesByGUID(provider, channel)
if err != nil {
// Try to get the messages by provider name
message_set_any, err = GetMessages(provider, channel)
if err != nil {
// Cache the failure by storing nil in the map
self.cache.Add(key, nil)
return nil, err
}
}
self.cache.Add(key, message_set_any)
}
// Negative cache
if message_set_any == nil {
return nil, errors.New("Not found")
}
return message_set_any.(*MessageSet), nil
}
func (self *WindowsMessageResolver) GetMessage(
provider, channel string, event_id int) string {
message_set, err := self.getMessageSets(provider, channel)
if err != nil {
return ""
}
// Get the event if it is there
res, pres := message_set.Messages[event_id]
if pres {
return res.Message
}
return ""
}
func (self *WindowsMessageResolver) GetParameter(
provider, channel string, parameter_id int) string {
message_set, err := self.getMessageSets(provider, channel)
if err != nil {
return ""
}
if message_set.Parameters == nil {
return ""
}
res, pres := message_set.Parameters[parameter_id]
if pres {
return res.Message
}
return ""
}
func (self *WindowsMessageResolver) Close() {}
type MessageSet struct {
Provider string
Channel string
Messages map[int]*pe.Message
Parameters map[int]*pe.Message
}
// ExpandLocations Produces a list of possible locations the message
// file may be. We process all of them because sometimes event
// messages are split across multiple dlls. For example, a generic
// message table may exist in C:\Windows\System32\XXX.dll but a
// localized message table also exists in
// C:\Windows\System32\en-us\XXX.dll.mui
func ExpandLocations(message_file string) []string {
// Expand environment variables in paths.
replace_env_vars := func(paths []string) []string {
system_root := os.Getenv("SystemRoot")
windir := os.Getenv("WinDir")
programfiles := os.Getenv("programfiles")
programfiles_x86 := os.Getenv("ProgramFiles(x86)")
result := []string{}
for _, path := range paths {
path = system_root_re.ReplaceAllLiteralString(
path, system_root)
path = windir_re.ReplaceAllLiteralString(
path, windir)
if programfiles_re.FindString(path) != "" {
result = append(result,
programfiles_re.ReplaceAllLiteralString(
path, programfiles))
result = append(result,
programfiles_re.ReplaceAllLiteralString(
path, programfiles_x86))
} else {
result = append(result, path)
}
}
return result
}
// When paths refer to system32 the message table may instead
// reside in the 32 bit version.
split_system32 := func(paths []string) []string {
result := []string{}
for _, path := range paths {
result = append(result, path)
// Sometimes messages are found in the 32 bit folders.
if system32_re.FindString(path) != "" {
result = append(result, system32_re.ReplaceAllLiteralString(
path, "\\SysWow64\\"))
}
}
return result
}
include_muis := func(paths []string) []string {
result := []string{}
for _, path := range paths {
result = append(result, path)
// Sometimes messages are found in the MUI
// files include those as well.
dll_name := filepath.Base(path)
dir_name := filepath.Dir(path)
result = append(result, filepath.Join(
dir_name, "en-US", dll_name+".mui"))
}
return result
}
// Message file values may be separated by ;
return include_muis(split_system32(replace_env_vars(
strings.Split(message_file, ";"))))
}
func GetMessagesByGUID(provider_guid, channel string) (*MessageSet, error) {
key_path := `Software\Microsoft\Windows\CurrentVersion\WinEVT\Publishers\{` + provider_guid + "}"
provider_key, err := registry.OpenKey(registry.LOCAL_MACHINE, key_path,
registry.READ|registry.ENUMERATE_SUB_KEYS|registry.WOW64_64KEY)
if err != nil {
return nil, err
}
defer provider_key.Close()
message_files, _, err := provider_key.GetStringValue("MessageFileName")
if err != nil {
return nil, err
}
parameter_files, _, err := provider_key.GetStringValue("ParameterFileName")
if err != nil {
parameter_files = ""
}
provider, _, err := provider_key.GetStringValue("")
if err != nil {
provider = provider_guid
}
return expandLocations(message_files, parameter_files, provider, channel)
}
func expandLocations(
message_files, parameter_files,
provider, channel string) (*MessageSet, error) {
result := &MessageSet{
Provider: provider,
Channel: channel,
Messages: make(map[int]*pe.Message),
Parameters: make(map[int]*pe.Message),
}
populateMessages(message_files, result.Messages)
if parameter_files != "" {
populateMessages(parameter_files, result.Parameters)
}
return result, nil
}
func populateMessages(message_files string, set map[int]*pe.Message) {
for _, message_file := range ExpandLocations(message_files) {
fd, err := os.Open(message_file)
if err != nil {
continue
}
defer fd.Close()
// fmt.Printf("Populating messages from %v\n", message_file)
reader, err := reader.NewPagedReader(fd, 4096, 100)
if err != nil {
continue
}
pe_file, err := pe.NewPEFile(reader)
if err != nil {
continue
}
messages := pe_file.GetMessages()
if len(messages) > 10000 {
continue
}
for _, msg := range messages {
set[msg.EventId] = msg
}
}
}
func GetMessages(provider, channel string) (*MessageSet, error) {
root_key, err := registry.OpenKey(registry.LOCAL_MACHINE,
`SYSTEM\CurrentControlSet\Services\EventLog`,
registry.READ|registry.ENUMERATE_SUB_KEYS|registry.WOW64_64KEY)
if err != nil {
return nil, err
}
defer root_key.Close()
channel_key, err := registry.OpenKey(root_key, channel,
registry.READ|registry.ENUMERATE_SUB_KEYS|registry.WOW64_64KEY)
if err != nil {
return nil, err
}
defer channel_key.Close()
provider_key, err := registry.OpenKey(channel_key, provider,
registry.READ|registry.ENUMERATE_SUB_KEYS|registry.WOW64_64KEY)
if err != nil {
return nil, err
}
defer provider_key.Close()
message_files, _, err := provider_key.GetStringValue("EventMessageFile")
if err != nil {
return nil, err
}
return expandLocations(message_files, "", provider, channel)
}