-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent.go
85 lines (79 loc) · 1.79 KB
/
event.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
package main
import (
"encoding/hex"
"fmt"
"log"
"net"
"strconv"
"strings"
)
type event struct {
id string
username string
timestamp string
port string
success string
ipaddress string
exec string
cmd string
}
func (e *event) readLine(message string) error {
words := strings.Fields(message)
for _, word := range words {
e.readWord(word)
}
return nil
}
func (e *event) readWord(s string) {
if strings.Contains(s, equals) {
outer := strings.Split(s, equals)
s = strings.Replace(s, prefix, "", 1)
switch outer[0] {
case "type":
if outer[1] == "EOE" {
e.flush()
}
case "msg":
if strings.Contains(s, colon) {
inner := strings.Split(s, colon)
e.id = strings.TrimRight(inner[1], rightParen)
e.timestamp = strings.Split(inner[0], dot)[0]
}
case "uid":
e.username = outer[1]
case "success":
e.success = outer[1]
case "exe":
e.exec = outer[1]
case "comm":
e.cmd = outer[1]
case "saddr":
saddr, _ := hex.DecodeString(strings.TrimLeft(outer[1], "saddr="))
port, _ := strconv.ParseInt(fmt.Sprintf("%x", saddr[2:4]), 16, 0)
e.port = fmt.Sprintf("%v", port)
if strings.HasPrefix(outer[1], ipv4Addr) {
e.ipaddress = net.IP(saddr[4:8]).String()
}
if strings.HasPrefix(outer[1], ipv6Addr) {
e.ipaddress = net.IP(saddr[8:24]).String()
}
}
}
}
func (e *event) flush() *event {
if e.username != "" && e.port != "" && e.ipaddress != "" {
log.Println(e)
}
e.id = ""
e.username = ""
e.timestamp = ""
e.port = ""
e.success = ""
e.ipaddress = ""
e.exec = ""
e.cmd = ""
return e
}
func (e event) String() string {
return (toUnixTimestamp(e.timestamp) + " uid: " + e.username + " destination: " + e.ipaddress + " port: " + e.port + " command: " + e.cmd + " exec: " + e.exec + " success: " + e.success)
}