-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
312 lines (268 loc) · 8.05 KB
/
main.go
File metadata and controls
312 lines (268 loc) · 8.05 KB
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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
EvtSubscribeToFutureEvents = 1
evtSubscribeActionError = 0
evtSubscribeActionDeliver = 1
evtRenderEventXML = 1
)
var (
modwevtapi = windows.NewLazySystemDLL("wevtapi.dll")
procEvtSubscribe = modwevtapi.NewProc("EvtSubscribe")
procEvtRender = modwevtapi.NewProc("EvtRender")
procEvtClose = modwevtapi.NewProc("EvtClose")
)
type Event struct {
XMLName xml.Name `xml:"Event"`
System SystemData `xml:"System"`
EventData []*EventData `xml:"EventData>Data"`
}
type EventData struct {
Key string `xml:"Name,attr"`
Value string `xml:",chardata"`
}
type ProviderData struct {
ProviderName string `xml:"Name,attr"`
ProviderGUID string `xml:"Guid,attr"`
}
type TimeCreatedData struct {
SystemTime string `xml:"SystemTime,attr"`
}
type CorrelationData struct {
ActivityID string `xml:"ActivityID,attr"`
}
type ExecutionData struct {
ProcessID int `xml:"ProcessID,attr"`
ThreadID int `xml:"ThreadID,attr"`
}
type SecurityData struct{}
type SystemData struct {
Provider ProviderData `xml:"Provider"`
EventID int `xml:"EventID"`
Version int `xml:"Version"`
Level int `xml:"Level"`
Task int `xml:"Task"`
Opcode int `xml:"Opcode"`
Keywords string `xml:"Keywords"`
TimeCreated TimeCreatedData `xml:"TimeCreated"`
EventRecordID int64 `xml:"EventRecordID"`
Correlation CorrelationData `xml:"Correlation"`
Execution ExecutionData `xml:"Execution"`
Channel string `xml:"Channel"`
Computer string `xml:"Computer"`
Security SecurityData `xml:"Security"`
}
type EventJSON struct {
Timestamp string `json:"timestamp"`
ProviderName string `json:"provider_name"`
ProviderGUID string `json:"provider_guid"`
EventID int `json:"event_id"`
Version int `json:"version"`
Level int `json:"level"`
Task int `json:"task"`
Opcode int `json:"opcode"`
Keywords string `json:"keywords"`
TimeCreated string `json:"time_created"`
EventRecordID int64 `json:"record_id"`
Correlation CorrelationData `json:"correlation"`
Execution ExecutionData `json:"execution"`
Channel string `json:"channel"`
Computer string `json:"computer"`
EventData map[string]interface{} `json:"data"`
}
type EventCallback func(event *Event)
type EventSubscription struct {
Channel string
Query string
SubscribeMethod int
Errors chan error
Callback EventCallback
winAPIHandle windows.Handle
}
func (evtSub *EventSubscription) Create() error {
if evtSub.winAPIHandle != 0 {
return fmt.Errorf("windows_events: subscription has already been created")
}
winChannel, err := windows.UTF16PtrFromString(evtSub.Channel)
if err != nil {
return fmt.Errorf("windows_events: invalid channel name: %s", err)
}
winQuery, err := windows.UTF16PtrFromString(evtSub.Query)
if err != nil {
return fmt.Errorf("windows_events: invalid query: %s", err)
}
callback := syscall.NewCallback(evtSub.winAPICallback)
// Debug logging
log.Printf("Debug - Subscribing to channel: %s", evtSub.Channel)
handle, _, err := procEvtSubscribe.Call(
0,
0,
uintptr(unsafe.Pointer(winChannel)),
uintptr(unsafe.Pointer(winQuery)),
0,
0,
callback,
uintptr(EvtSubscribeToFutureEvents),
)
if handle == 0 {
return fmt.Errorf("windows_events: failed to subscribe to events: %v", err)
}
evtSub.winAPIHandle = windows.Handle(handle)
return nil
}
func (evtSub *EventSubscription) Close() error {
if evtSub.winAPIHandle == 0 {
return fmt.Errorf("windows_events: no active subscription to close")
}
ret, _, err := procEvtClose.Call(uintptr(evtSub.winAPIHandle))
if ret == 0 {
return fmt.Errorf("windows_events: error closing handle: %s", err)
}
evtSub.winAPIHandle = 0
return nil
}
func (evtSub *EventSubscription) winAPICallback(action, userContext, event uintptr) uintptr {
switch action {
case evtSubscribeActionError:
evtSub.Errors <- fmt.Errorf("windows_events: error in callback, code: %x", uint16(event))
case evtSubscribeActionDeliver:
bufferSize := uint32(4096)
for {
renderSpace := make([]uint16, bufferSize/2)
bufferUsed := uint32(0)
propertyCount := uint32(0)
ret, _, err := procEvtRender.Call(
0,
event,
evtRenderEventXML,
uintptr(bufferSize),
uintptr(unsafe.Pointer(&renderSpace[0])),
uintptr(unsafe.Pointer(&bufferUsed)),
uintptr(unsafe.Pointer(&propertyCount)),
)
if ret == 0 {
if err == windows.ERROR_INSUFFICIENT_BUFFER {
bufferSize *= 2
continue
}
evtSub.Errors <- fmt.Errorf("windows_events: failed to render event: %w", err)
return 0
}
xmlStr := windows.UTF16ToString(renderSpace)
xmlStr = cleanXML(xmlStr)
dataParsed := new(Event)
if err := xml.Unmarshal([]byte(xmlStr), dataParsed); err != nil {
evtSub.Errors <- fmt.Errorf("windows_events: failed to parse XML: %s", err)
} else {
evtSub.Callback(dataParsed)
}
break
}
default:
evtSub.Errors <- fmt.Errorf("windows_events: unsupported action in callback: %x", uint16(action))
}
return 0
}
func cleanXML(xml string) string {
xml = strings.TrimSpace(xml)
if idx := strings.Index(xml, "<?xml"); idx > 0 {
xml = xml[idx:]
}
xml = strings.Map(func(r rune) rune {
if r < 32 && r != '\n' && r != '\r' && r != '\t' {
return -1
}
return r
}, xml)
return xml
}
var (
exitChan = make(chan os.Signal, 1)
errorsChan = make(chan error, 10)
)
func main() {
signal.Notify(exitChan, os.Interrupt)
channels := []string{"Security", "Application", "System"}
subscriptions := make([]*EventSubscription, 0)
for _, channel := range channels {
sub := &EventSubscription{
Channel: channel,
Query: "*",
Errors: errorsChan,
Callback: func(e *Event) {
eventCallback(e)
},
}
if err := sub.Create(); err != nil {
log.Printf("Error subscribing to channel %s: %s", channel, err)
continue
}
subscriptions = append(subscriptions, sub)
log.Printf("Subscribed to channel: %s", channel)
}
go func() {
for err := range errorsChan {
log.Printf("Subscription error: %s", err)
}
}()
<-exitChan
log.Println("Interrupt received, closing subscriptions...")
for _, sub := range subscriptions {
if err := sub.Close(); err != nil {
log.Printf("Error closing subscription for %s: %v", sub.Channel, err)
}
}
log.Println("Agent finished successfully.")
}
func eventCallback(event *Event) {
eventJSON := EventJSON{
ProviderName: event.System.Provider.ProviderName,
ProviderGUID: event.System.Provider.ProviderGUID,
EventID: event.System.EventID,
Version: event.System.Version,
Level: event.System.Level,
Task: event.System.Task,
Opcode: event.System.Opcode,
Keywords: event.System.Keywords,
TimeCreated: event.System.TimeCreated.SystemTime,
Timestamp: event.System.TimeCreated.SystemTime,
EventRecordID: event.System.EventRecordID,
Correlation: event.System.Correlation,
Execution: event.System.Execution,
Channel: event.System.Channel,
Computer: event.System.Computer,
EventData: make(map[string]interface{}),
}
for _, data := range event.EventData {
if strings.HasPrefix(data.Value, "0x") {
if val, err := strconv.ParseInt(data.Value[2:], 16, 64); err == nil {
eventJSON.EventData[data.Key] = val
continue
}
}
if data.Key != "" {
value := strings.TrimSpace(data.Value)
if value != "" {
eventJSON.EventData[data.Key] = value
}
}
}
jsonData, err := json.MarshalIndent(eventJSON, "", " ")
if err != nil {
log.Printf("Error converting event to JSON: %s", err)
return
}
fmt.Println(string(jsonData))
}