-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (227 loc) · 7.16 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
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
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
var SysmonConfigs []string = []string{
"https://raw.githubusercontent.com/NextronSystems/aurora-helpers/master/sysmon-config/aurora-sysmon-config.xml",
"https://raw.githubusercontent.com/Neo23x0/sysmon-config/master/sysmonconfig-export.xml",
"https://raw.githubusercontent.com/SwiftOnSecurity/sysmon-config/master/sysmonconfig-export.xml",
"https://raw.githubusercontent.com/NextronSystems/evtx-baseline/master/sysmon-intense.xml",
"https://raw.githubusercontent.com/OTRF/Blacksmith/master/resources/configs/sysmon/sysmon.xml",
}
var SysmonFolder string = "sysmon"
var SysmonZipFile string = "sysmon.zip"
var SysmonConfigFile string = "sysmon-config.xml"
var ListConfigsFlag bool
var SysmonDownloadOnlyFlag bool
var UninstallSysmonFlag bool
var UseConfigIndexFlag int
var SysmonURLFlag string
var SysmonArchFlag string
var ForceInstallFlag bool
var ConfigURLFlag string
func main() {
flag.BoolVar(&ListConfigsFlag, "listconfigs", false, "Lists hardcoded Sysmon config URLs")
flag.BoolVar(&ForceInstallFlag, "force", false, "Uninstalls Sysmon before installing")
flag.BoolVar(&SysmonDownloadOnlyFlag, "sysmondownload", false, "Just downloads Sysmon")
flag.BoolVar(&UninstallSysmonFlag, "uninstall", false, "Uninstall Sysmon")
flag.IntVar(&UseConfigIndexFlag, "config", -1, "Which config should be used")
flag.StringVar(&SysmonURLFlag, "sysmonURL", "https://download.sysinternals.com/files/Sysmon.zip", "URL to download Sysmon zip")
flag.StringVar(&SysmonArchFlag, "arch", "64", "Which Sysmon version to use: 64 or 32")
flag.StringVar(&ConfigURLFlag, "configURL", "", "URL to download config")
flag.Parse()
if ListConfigsFlag {
fmt.Println("Available Sysmon configs:")
for i, e := range SysmonConfigs {
fmt.Printf("[%d] %s\n", i, e)
}
return
}
if UninstallSysmonFlag {
uninstall()
return
}
if SysmonDownloadOnlyFlag {
fmt.Println("Downloading Sysmon zip...")
if err := downloadFile(SysmonURLFlag, SysmonZipFile); err != nil {
fmt.Printf("ERROR: was not able to download Sysmon from : %s : %v\n", SysmonURLFlag, err)
return
}
fmt.Println("Unzipping Sysmon...")
if err := unzip(SysmonZipFile, SysmonFolder); err != nil {
fmt.Printf("ERROR: was not able to download Sysmon from : %s : %v\n", SysmonURLFlag, err)
return
}
if err := os.Remove(SysmonZipFile); err != nil {
fmt.Printf("NOTICE: was not able to remove: %s : %v\n", SysmonZipFile, err)
}
return
}
if UseConfigIndexFlag >= 0 || ConfigURLFlag != "" {
configURL := ""
if UseConfigIndexFlag >= 0 {
configURL = SysmonConfigs[UseConfigIndexFlag]
} else {
configURL = ConfigURLFlag
}
fmt.Printf("Using config: %s\n", configURL)
fmt.Println("Downloading config...")
if err := downloadFile(configURL, SysmonConfigFile); err != nil {
fmt.Printf("ERROR: was not able to download Sysmon config: %s : %v\n", configURL, err)
return
}
fmt.Println("Downloading Sysmon zip...")
if err := downloadFile(SysmonURLFlag, SysmonZipFile); err != nil {
fmt.Printf("ERROR: was not able to download Sysmon from : %s : %v\n", SysmonURLFlag, err)
return
}
fmt.Println("Unzipping Sysmon...")
if err := unzip(SysmonZipFile, SysmonFolder); err != nil {
fmt.Printf("ERROR: was not able to download Sysmon from : %s : %v\n", SysmonURLFlag, err)
return
}
if err := os.Remove(SysmonZipFile); err != nil {
fmt.Printf("NOTICE: was not able to remove: %s : %v\n", SysmonZipFile, err)
}
if ForceInstallFlag {
uninstall()
}
install()
return
}
flag.PrintDefaults()
}
func uninstall() {
fmt.Println("Uninstalling Sysmon...")
switch SysmonArchFlag {
case "64":
c := filepath.Join(SysmonFolder, "Sysmon64.exe")
out, err := uninstallSysmon(c, SysmonConfigFile)
if err != nil {
fmt.Printf("ERROR: was not able to uninstall Sysmon 64: %v\n", err)
fmt.Println(out)
return
}
fmt.Println(out)
case "32":
c := filepath.Join(SysmonFolder, "Sysmon.exe")
out, err := uninstallSysmon(c, SysmonConfigFile)
if err != nil {
fmt.Printf("ERROR: was not able to uninstall Sysmon 32: %v\n", err)
fmt.Println(out)
return
}
fmt.Println(out)
default:
fmt.Printf("ERROR: %s is unkown Sysmon architecture\n", SysmonArchFlag)
return
}
}
func install() {
fmt.Println("Installing Sysmon...")
switch SysmonArchFlag {
case "64":
c := filepath.Join(SysmonFolder, "Sysmon64.exe")
out, err := installSysmon(c, SysmonConfigFile)
if err != nil {
fmt.Printf("ERROR: was not able to install Sysmon 64: %v\n", err)
fmt.Println(out)
return
}
fmt.Println(out)
case "32":
c := filepath.Join(SysmonFolder, "Sysmon.exe")
out, err := installSysmon(c, SysmonConfigFile)
if err != nil {
fmt.Printf("ERROR: was not able to install Sysmon 32: %v\n", err)
fmt.Println(out)
return
}
fmt.Println(out)
default:
fmt.Printf("ERROR: %s is unkown Sysmon architecture\n", SysmonArchFlag)
return
}
}
func uninstallSysmon(sysmonFile, configFile string) (string, error) {
cmd := exec.Command(sysmonFile, "-u")
out, err := cmd.CombinedOutput()
return string(out), err
}
func installSysmon(sysmonFile, configFile string) (string, error) {
cmd := exec.Command(sysmonFile, "-accepteula", "-i", configFile)
out, err := cmd.CombinedOutput()
return string(out), err
}
func unzip(zipFile, dst string) error {
archive, err := zip.OpenReader(zipFile)
if err != nil {
return fmt.Errorf("was not able to zip.OpenReader(): %v", err)
}
defer archive.Close()
for _, f := range archive.File {
filePath := filepath.Join(dst, f.Name)
fmt.Println("Unzipping file ", filePath)
if !strings.HasPrefix(filePath, filepath.Clean(dst)+string(os.PathSeparator)) {
return fmt.Errorf("invalid file path")
}
if f.FileInfo().IsDir() {
fmt.Println("Creating directory...")
os.MkdirAll(filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return fmt.Errorf("was not able to os.MkdirAll(): %v", err)
}
dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("was not able to os.OpenFile(): %v", err)
}
defer dstFile.Close()
fileInArchive, err := f.Open()
if err != nil {
return fmt.Errorf("was not able to f.Open(): %v", err)
}
defer fileInArchive.Close()
if _, err := io.Copy(dstFile, fileInArchive); err != nil {
return fmt.Errorf("was not able to io.Copy(): %v", err)
}
}
return nil
}
func downloadFile(url, dst string) error {
client := &http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("was not able to http.NewRequest(): %v", err)
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("was not able to client.Do(): %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("statuscode != 200 - maybe new download url?")
}
out, err := os.Create(dst)
if err != nil {
return fmt.Errorf("was not able to os.Create(): %v", err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("was not able to io.Copy(): %v", err)
}
return nil
}