This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
executable file
·166 lines (136 loc) · 3.31 KB
/
main.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
package main
import (
"./libaudit-go"
"log"
"os"
"syscall"
"encoding/json"
"os/exec"
"strconv"
"log/syslog"
"fmt"
)
var done chan bool
var debug bool
var sysLog *syslog.Writer
func logLine(data string) {
if sysLog == nil {
sysLog, _ = syslog.Dial("", "", syslog.LOG_LOCAL0|syslog.LOG_WARNING, "auditd")
}
if data != "" {
sysLog.Write([]byte(data))
}
}
func EventCallback(msg *netlinkAudit.AuditEvent, ce chan error, args ...interface{}) {
if msg != nil {
// convert to JSON
jsonString, err := json.Marshal(msg.Data)
if err != nil {
log.Println(err)
} else {
log.Println("Type="+msg.Type +" Info="+string(jsonString))
}
//f := args[0].(os.File)
//_, err = f.WriteString(msg.Raw)
logLine(string(jsonString))
if err != nil {
log.Println("Writing Error!!", err)
}
}
}
func main() {
// Request NetlinkSocket
s, err := netlinkAudit.NewNetlinkConnection()
if err != nil {
log.Println(err)
log.Fatalln("Error while availing socket, Exiting")
}
defer s.Close()
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
var out []byte
if len(os.Args) > 1 {
out, err = exec.Command(dir+"/tools/rules2json.py", dir+"/"+os.Args[1]).Output()
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println("Usage: sudo go run main.go audit.rules")
os.Exit(0)
}
var m interface{}
err = json.Unmarshal(out, &m)
rules:= m.(map[string]interface{})
// Enable Audit
err = netlinkAudit.AuditSetEnabled(s, 1)
if err != nil {
log.Fatal("Error while enabling Audit", err)
}
// Check if Audit is enabled
status, err := netlinkAudit.AuditIsEnabled(s)
if err == nil && status == 1 {
log.Println("Enabled Audit")
} else if err == nil && status == 0 {
log.Fatalln("Audit Not Enabled")
} else {
log.Fatalln("Error while fetching status", err)
}
// Set the maximum number of messages
// that the kernel will send per second
var i string
if _, ok := rules["rate"]; ok {
i = rules["rate"].(string)
} else {
i = "600"
}
r,err := strconv.Atoi(i)
if err != nil {
log.Fatalln("Error converting rate limit to integer", err)
}
err = netlinkAudit.AuditSetRateLimit(s, r)
if err != nil {
log.Fatalln("Error Setting Rate Limit", err)
}
// Set max limit audit message queue
if _, ok := rules["buffer"]; ok {
i = rules["rate"].(string)
} else {
i = "420"
}
b, _ := strconv.Atoi(i)
err = netlinkAudit.AuditSetBacklogLimit(s, b)
if err != nil {
log.Fatalln("Error Setting Backlog Limit", err)
}
// Register current pid with audit
err = netlinkAudit.AuditSetPid(s, uint32(syscall.Getpid()))
if err == nil {
log.Println("Set pid successful")
}
//Delete all rules
if _, ok := rules["delete"]; ok {
log.Println("Deleting all rules")
err = netlinkAudit.DeleteAllRules(s)
if err != nil {
log.Fatalln("Deleting Rules Unsuccessful, Exiting", err)
} else {
log.Println("Done setting syscall.")
}
}
// Set audit rules
err = netlinkAudit.SetRules(s, out)
if err != nil {
log.Fatalln("Setting Rule Unsuccessful: ", err)
}
errchan := make(chan error)
// Go rutine to monitor events and feet AuditEvent type events to the callback
netlinkAudit.GetAuditEvents(s, EventCallback, errchan)
/*f, err := os.OpenFile("/tmp/log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
log.Fatalln("Unable to open file")
}
defer f.Close()*/
select {}
}