This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwazuh.go
More file actions
207 lines (180 loc) · 4.4 KB
/
wazuh.go
File metadata and controls
207 lines (180 loc) · 4.4 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
package main
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"time"
)
func startWazuh() {
var runOnce sync.Once
go func() {
path, err := getMyPath()
if err != nil {
h.Error("error getting path: %v", err)
time.Sleep(10 * time.Second)
os.Exit(1)
}
switch runtime.GOOS {
case "windows":
runOnce.Do(func() {
execute(
filepath.Join(path, "wazuh", "windows", "wazuh-agent.exe"),
filepath.Join(path, "wazuh", "windows"),
"install-service",
)
result, errB := execute(
filepath.Join(path, "nssm.exe"),
path,
"start",
"WazuhSvc",
)
if errB {
h.Error("error running wazuh: %s", result)
time.Sleep(10 * time.Second)
os.Exit(1)
}
})
}
}()
}
func stopWazuh() {
var runOnce sync.Once
path, err := getMyPath()
if err != nil {
h.Error("error getting path: %v", err)
time.Sleep(10 * time.Second)
os.Exit(1)
}
switch runtime.GOOS {
case "windows":
runOnce.Do(func() {
result, errB := execute(
filepath.Join(path, "nssm.exe"),
path,
"stop",
"WazuhSvc",
)
if errB {
h.Error("error stopping wazuh: %s", result)
time.Sleep(10 * time.Second)
os.Exit(1)
}
execute(
filepath.Join(path, "wazuh", "windows", "wazuh-agent.exe"),
filepath.Join(path, "wazuh", "windows"),
"uninstall-service",
)
})
}
}
func configureWazuh(ip, key string) error {
path, err := getMyPath()
if err != nil {
return err
}
type WazuhConfig struct {
IP string
}
dKey, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return err
}
config := WazuhConfig{ip}
switch runtime.GOOS {
case "windows":
ossecFile := filepath.Join(path, "wazuh", "windows", "ossec.conf")
ossecTemplateFile := filepath.Join(path, "templates", "wazuh-windows.conf")
err := generateFromTemplate(config, ossecTemplateFile, ossecFile)
if err != nil {
return err
}
err = writeToFile(filepath.Join(path, "wazuh", "windows", "client.keys"), string(dKey[:]))
if err != nil {
return err
}
case "linux":
var templateFile string
configFile := filepath.Join("/", "var", "ossec", "etc", "ossec.conf")
family, err := detectLinuxFamily()
if err != nil {
return err
}
switch family {
case "debian":
templateFile = filepath.Join(path, "templates", "wazuh-debian.conf")
result, errB := execute("apt", path, "update")
if errB {
return fmt.Errorf("%s", result)
}
result, errB = execute("apt", path, "install", "-y", "curl", "apt-transport-https", "lsb-release", "gnupg2", "wget")
if errB {
return fmt.Errorf("%s", result)
}
err := download("https://packages.wazuh.com/key/GPG-KEY-WAZUH")
if err != nil {
return err
}
result, errB = execute("apt-key", path, "add", "GPG-KEY-WAZUH")
if errB {
return fmt.Errorf("%s", result)
}
err = writeToFile(filepath.Join("/", "etc", "apt", "sources.list.d", "wazuh.list"), "deb https://packages.wazuh.com/4.x/apt/ stable main")
if err != nil {
return err
}
result, errB = execute("apt", path, "update")
if errB {
return fmt.Errorf("%s", result)
}
result, errB = execute("apt", path, "install", "-y", "wazuh-agent")
if errB {
return fmt.Errorf("%s", result)
}
case "rhel":
templateFile = filepath.Join(path, "templates", "wazuh-rhel.conf")
result, errB := execute("rpm", path, "--import", "https://packages.wazuh.com/key/GPG-KEY-WAZUH")
if errB {
return fmt.Errorf("%s", result)
}
err = writeToFile(
filepath.Join("/", "etc", "yum.repos.d", "wazuh.repo"),
`[wazuh_repo]
gpgcheck=1
gpgkey=https://packages.wazuh.com/key/GPG-KEY-WAZUH
enabled=1
name=Wazuh repository
baseurl=https://packages.wazuh.com/4.x/yum/
protect=1`,
)
if err != nil {
return err
}
result, errB = execute("yum", path, "install", "-y", "wazuh-agent")
if errB {
return fmt.Errorf("%s", result)
}
}
if family == "debian" || family == "rhel" {
err = generateFromTemplate(config, templateFile, configFile)
if err != nil {
return err
}
err = writeToFile(filepath.Join("/", "var", "ossec", "etc", "client.keys"), string(dKey[:]))
if err != nil {
return err
}
result, errB := execute("systemctl", path, "enable", "wazuh-agent")
if errB {
return fmt.Errorf("%s", result)
}
result, errB = execute("systemctl", path, "restart", "wazuh-agent")
if errB {
return fmt.Errorf("%s", result)
}
}
}
return nil
}