-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
207 lines (183 loc) · 4.95 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
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
//go:generate go-bindata -pkg $GOPACKAGE -o assets.go assets/
import (
"io/ioutil"
"log"
"os"
"os/exec"
)
func main() {
installDir := "C:\\SysLogBeat\\"
var err error
err = makeInstallDir(installDir)
if err == nil {
log.Printf("Created '%v' successfully.\n\n", installDir)
} else {
log.Println(err)
os.Exit(1)
}
err = unpackSysmon(installDir)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = installSysmon(installDir)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = unpackWinlogbeat(installDir)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = installWinlogbeat(installDir)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println("Winlogbeat installation finalized. Check kibana server for logs.")
}
func makeInstallDir(installDir string) (err error) {
_, err = os.Open(installDir)
if err == nil {
log.Printf("%v already exists, check if already installed.\n", installDir)
return
}
err = os.Mkdir(installDir, 0761)
if err != nil {
log.Printf("Error creating %v.\n", installDir)
return
}
_, err = os.Open(installDir)
if err != nil {
log.Println("Error accessing %v after creation.\n", installDir)
return
}
return
}
func loadAsset(installPath string, assetName string) (err error) {
// Unpack asset
asset_install_path := installPath + "\\" + assetName
log.Printf("Loading %v from assets.\n", assetName)
data, err := Asset("assets/" + assetName)
if err != nil {
log.Println("Problem loading %v from assets.", assetName)
return
}
// Write asset
err = ioutil.WriteFile(asset_install_path, []byte(data), 0644)
if err != nil {
log.Println("There was a problem writing %v.\n", assetName)
return
} else {
log.Printf("%v written successfully.\n\n", assetName)
return
}
}
func unpackSysmon(installDir string) (err error) {
// Create Sysmon dir
sysmonDir := installDir + "Sysmon"
err = os.Mkdir(sysmonDir, 0761)
if err != nil {
log.Printf("Error creating %v.\n", sysmonDir)
return
} else {
log.Printf("Successfully created %v.\n", sysmonDir)
}
// Unpack Sysmon and config file
err = loadAsset(sysmonDir, "Sysmon.exe")
if err != nil {
return
}
err = loadAsset(sysmonDir, "sysmonconfig-export.xml")
return
}
func installSysmon(installDir string) (err error) {
sysmonExecutable := installDir + "Sysmon\\Sysmon.exe"
sysmonConfig := installDir + "Sysmon\\sysmonconfig-export.xml"
// Start sysmon service.
// Here we call cmd.exe and pass the "/C" flag to indicate we're passing a command.
runSysmon := exec.Command("cmd", "/C", sysmonExecutable, "-i", sysmonConfig, "-accepteula", "-h", "md5,sha256", "-n", "-l")
err = runSysmon.Run()
if err != nil {
log.Println("Error starting Sysmon.")
log.Println("If exit status is 1242, it probably means you need to uninstall sysmon with 'Sysmon.exe -u'.")
log.Println("If exit status is 740, it's probably because you're not running this as admin.")
return
} else {
log.Println("Successfully started Sysmon.")
}
// Install sysmon service.
// sc config Sysmon start= auto
installSysmon := exec.Command("cmd", "/C", "sc", "config", "Sysmon", "start=", "auto")
err = installSysmon.Run()
if err != nil {
log.Println("Error installing Sysmon service.")
return
} else {
log.Println("Successfully installed Sysmon service.\n")
}
return
}
func unpackWinlogbeat(installDir string) (err error) {
// Create Winlogbeat dir
winlogDir := installDir + "Winlogbeat"
err = os.Mkdir(winlogDir, 0761)
if err != nil {
log.Printf("Error creating %v.\n", winlogDir)
return
} else {
log.Printf("Successfully created %v.\n", winlogDir)
}
err = loadAsset(winlogDir, "winlogbeat.exe")
if err != nil {
return
}
err = loadAsset(winlogDir, "winlogbeat.yml")
if err != nil {
return
}
err = loadAsset(winlogDir, "fields.yml")
if err != nil {
return
}
err = loadAsset(winlogDir, "install-service-winlogbeat.ps1")
if err != nil {
return
}
err = loadAsset(winlogDir, "ELK-Stack.crt")
return
}
func installWinlogbeat(installDir string) (err error) {
winlogDir := installDir + "Winlogbeat"
winlogSvc := winlogDir + "\\install-service-winlogbeat.ps1"
// Run Winlogbeat installer
installWLB := exec.Command("powershell", "-Exec", "bypass", "-File", winlogSvc)
err = installWLB.Run()
if err != nil {
log.Println("Error installing Winlogbeat.")
return
} else {
log.Println("Winlogbeat installed successfully.")
}
// Install winlogbeat service
installWLBSvc := exec.Command("powershell", "Set-Service", "-Name", "\"winlogbeat\"", "-StartupType", "automatic")
err = installWLBSvc.Run()
if err != nil {
log.Println("Error installing Winlogbeat service.")
return
} else {
log.Println("Winlogbeat service installed successfully.")
}
// Start winlogbeat service
startWLBSvc := exec.Command("powershell", "Start-Service", "-Name", "\"winlogbeat\"")
err = startWLBSvc.Run()
if err != nil {
log.Println("Error starting Winlogbeat service.")
return
} else {
log.Println("Winlogbeat service started successfully.\n")
}
return
}