-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
313 lines (269 loc) · 9.91 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (c) 2024 Kelvin Winborne (aka. "grepStrength")
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"encoding/base64"
"encoding/csv"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/VirusTotal/vt-go"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
type IOCResult struct { //this is the structure for the results of the VirusTotal API check
IOC string
ThreatCategory string
Malicious int64
Suspicious int64
Clean int64
Unknown int64
}
var progressBar *widget.ProgressBar //pointer to the progress bar and label to show the progress of the current IOC being processed
var progressLabel *widget.Label
type VTResponse struct { //this is the VirusTotal response structure for the API check's results
ResponseCode int `json:"response_code"`
Positives int `json:"positives"`
Total int `json:"total"`
Scans map[string]ScanResult `json:"scans"`
Resource string `json:"resource"`
Message string `json:"verbose_msg"`
}
type ScanResult struct {
Detected bool `json:"detected"`
Result string `json:"result"`
}
var ( //these are the Windows API functions to hide the console window when the application is run
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
user32 = syscall.NewLazyDLL("user32.dll")
procShowWindow = user32.NewProc("ShowWindow")
)
func hideConsoleWindow() {
hwnd, _, _ := procGetConsoleWindow.Call()
if hwnd != 0 {
procShowWindow.Call(hwnd, 0)
}
}
func main() {
hideConsoleWindow()
a := app.New()
w := a.NewWindow("RealGoVetter") //creates a window and sets the window title to "RealGoVetter"
apiKey := loadAPIKey()
//creates the window with the API key entry field, save button, and select file button
apiKeyEntry := widget.NewPasswordEntry()
if apiKey != "" {
apiKeyEntry.SetText(apiKey)
}
//creates the progress bar and label to show the progress of the current IOC being processed
progressBar = widget.NewProgressBar()
progressLabel = widget.NewLabel("")
progressBar.Hide()
progressLabel.Hide()
results := make([]IOCResult, 0)
saveAPIBtn := widget.NewButton("Save API Key", func() { //this is always saved in "C:\Users\<USERNAME>\AppData\Roaming\RealGoVetter\config.dat"
saveAPIKey(apiKeyEntry.Text)
dialog.ShowInformation("Success", "API Key Saved", w)
})
selectFileBtn := widget.NewButton("Select IOC File", func() {
dialog.ShowFileOpen(func(reader fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if reader == nil {
return
}
go processIOCs(reader, apiKeyEntry.Text, &results, w) //runs the processIOCs function in a separate goroutine to prevent the UI from freezing
}, w)
})
content := container.NewVBox(
widget.NewLabel("VirusTotal API Key:"),
apiKeyEntry,
saveAPIBtn,
selectFileBtn,
progressBar,
progressLabel,
)
w.SetContent(content)
w.Resize(fyne.NewSize(800, 800)) //sets and keeps the window size to 800x800 pixels to make it easier to read the results
w.ShowAndRun()
}
func processIOCs(reader fyne.URIReadCloser, apiKey string, results *[]IOCResult, window fyne.Window) {
progressBar.Show()
progressLabel.Show()
data, err := ioutil.ReadAll(reader) //reads the data from the file loaded by the user
if err != nil {
dialog.ShowError(err, window)
return
}
lines := strings.Split(string(data), "\n") //splits the data into lines based on the newline character
validLines := 0
for _, line := range lines {
if strings.TrimSpace(line) != "" {
validLines++
}
}
progressBar.Max = float64(validLines) //sets the maximum value of the progress bar to the number of valid lines in the file
progressBar.Value = 0
outputFile := "results_" + time.Now().Format("20060102150405") + ".csv" //automatically sets the output file name to include the current date and time
csvFile, err := os.Create(outputFile)
if err != nil {
dialog.ShowError(err, window)
return
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
// Write header
writer.Write([]string{"IOC", "Threat Category", "Malicious", "Suspicious", "Clean", "Unknown"})
for i, line := range lines {
ioc := strings.TrimSpace(line)
if ioc == "" {
continue
}
progressLabel.SetText(fmt.Sprintf("Processing: %s", ioc)) //shows the progress of the current IOC being processed
result := checkVirusTotal(ioc, apiKey)
*results = append(*results, result)
writer.Write([]string{
result.IOC,
result.ThreatCategory,
fmt.Sprintf("%d", result.Malicious),
fmt.Sprintf("%d", result.Suspicious),
fmt.Sprintf("%d", result.Clean),
fmt.Sprintf("%d", result.Unknown),
})
progressBar.SetValue(float64(i + 1))
}
progressBar.Hide()
progressLabel.Hide()
dialog.ShowInformation("Complete", "Analysis completed. Results saved to "+outputFile, window) //saves the results to a CSV file in the same directory as the executable
}
func checkVirusTotal(ioc string, apiKey string) IOCResult {
client := vt.NewClient(apiKey)
fileObj, err := client.GetObject(vt.URL(fmt.Sprintf("files/%s", ioc)))
if err == nil {
malicious, _ := fileObj.GetInt64("last_analysis_stats.malicious")
suspicious, _ := fileObj.GetInt64("last_analysis_stats.suspicious")
clean, _ := fileObj.GetInt64("last_analysis_stats.undetected")
unknown, _ := fileObj.GetInt64("last_analysis_stats.type_unsupported")
category, _ := fileObj.GetString("type_description")
return IOCResult{
IOC: ioc,
ThreatCategory: category, //basically "Domain" or "IP Address" or "URL" depending on the context
Malicious: malicious,
Suspicious: suspicious,
Clean: clean,
Unknown: unknown,
}
}
urlObj, err := client.GetObject(vt.URL(fmt.Sprintf("urls/%s", ioc)))
if err == nil {
malicious, _ := urlObj.GetInt64("last_analysis_stats.malicious")
suspicious, _ := urlObj.GetInt64("last_analysis_stats.suspicious")
clean, _ := urlObj.GetInt64("last_analysis_stats.undetected")
unknown, _ := urlObj.GetInt64("last_analysis_stats.type_unsupported")
return IOCResult{
IOC: ioc,
ThreatCategory: "URL",
Malicious: malicious,
Suspicious: suspicious,
Clean: clean,
Unknown: unknown,
}
}
domainObj, err := client.GetObject(vt.URL(fmt.Sprintf("domains/%s", ioc)))
if err == nil {
malicious, _ := domainObj.GetInt64("last_analysis_stats.malicious")
suspicious, _ := domainObj.GetInt64("last_analysis_stats.suspicious")
clean, _ := domainObj.GetInt64("last_analysis_stats.undetected")
unknown, _ := domainObj.GetInt64("last_analysis_stats.type_unsupported")
return IOCResult{
IOC: ioc,
ThreatCategory: "Domain",
Malicious: malicious,
Suspicious: suspicious,
Clean: clean,
Unknown: unknown,
}
}
ipObj, err := client.GetObject(vt.URL(fmt.Sprintf("ip_addresses/%s", ioc)))
if err == nil {
malicious, _ := ipObj.GetInt64("last_analysis_stats.malicious")
suspicious, _ := ipObj.GetInt64("last_analysis_stats.suspicious")
clean, _ := ipObj.GetInt64("last_analysis_stats.undetected")
unknown, _ := ipObj.GetInt64("last_analysis_stats.type_unsupported")
return IOCResult{
IOC: ioc,
ThreatCategory: "IP Address",
//DetectionRatio: fmt.Sprintf("%d/%d", malicious+suspicious, total), //removed because the "10/0" kept getting represented as MON-YR (eg "Oct-00") in Excel
Malicious: malicious,
Suspicious: suspicious,
Clean: clean,
Unknown: unknown,
}
}
return IOCResult{
IOC: ioc,
ThreatCategory: "Not Found",
//DetectionRatio: "0/0", //removed because the "10/0" kept getting represented as MON-YR (eg "Oct-00") in Excel
Malicious: 0,
Suspicious: 0,
Clean: 0,
Unknown: 0,
}
}
func getConfigPath() string {
appData, err := os.UserConfigDir()
if err != nil {
appData = "."
}
return filepath.Join(appData, "RealGoVetter")
}
func saveAPIKey(key string) {
configDir := getConfigPath()
os.MkdirAll(configDir, 0700)
configFile := filepath.Join(configDir, "config.dat") //this is always saved in "C:\Users\<USERNAME>\AppData\Roaming\RealGoVetter\config.dat" as a hidden file with the API key base64 encoded
encoded := base64.StdEncoding.EncodeToString([]byte(key))
ioutil.WriteFile(configFile, []byte(encoded), 0600)
//sets the config.dat file to be hidden
configFileUTF16, err := syscall.UTF16PtrFromString(configFile)
if err == nil {
syscall.SetFileAttributes(configFileUTF16, syscall.FILE_ATTRIBUTE_HIDDEN)
}
}
func loadAPIKey() string { //this function loads the API key from the saved config.dat file
configFile := filepath.Join(getConfigPath(), "config.dat")
data, err := ioutil.ReadFile(configFile)
if err != nil {
return ""
}
decoded, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return ""
}
return string(decoded)
}