-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (177 loc) · 5.33 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/fadhilthomas/go-nuclei-slack/config"
"github.com/fadhilthomas/go-nuclei-slack/model"
"github.com/rs/zerolog/log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var (
attachmentList []model.SlackAttachmentBody
blockList []model.SlackBlockBody
vulnerabilityList []model.Output
)
func main() {
config.Set(config.LOG_LEVEL, "info")
webHookURL := config.GetStr(config.SLACK_TOKEN)
database := model.OpenDB()
if database == nil {
return
}
summary := model.SummaryReport{}
file, err := os.Open(config.GetStr(config.FILE_LOCATION))
if err != nil {
log.Error().Str("file", "main").Err(err)
}
fScanner := bufio.NewScanner(file)
for fScanner.Scan() {
result := model.Output{}
err = json.Unmarshal([]byte(fScanner.Text()), &result)
if err != nil {
log.Error().Str("file", "slack").Err(err)
}
vulnerabilityList = append(vulnerabilityList, result)
switch result.Info.Severity {
case "critical":
summary.Critical++
case "high":
summary.High++
case "medium":
summary.Medium++
case "low":
summary.Low++
case "info":
summary.Info++
}
sqlResult, _ := model.QueryVulnerability(database, result.TemplateID, result.Host)
if sqlResult == "new" {
log.Debug().Str("file", "main").Str("vulnerability name", result.TemplateID).Str("vulnerability host", result.Host).Msg("success")
err = model.InsertVulnerability(database, result.TemplateID, result.Host, "open")
if err != nil {
log.Error().Str("file", "main").Err(err)
}
}
summary.Host = result.Host
attachmentList = append(attachmentList, createAttachment(result.Info.Name, strings.Join(result.Info.Tags, ", "), result.Info.Severity, result.Info.Classification.CvssMetrics, strconv.FormatFloat(result.Info.Classification.CvssScore, 'f', -1, 64), result.Host, result.Matched, sqlResult))
}
err = model.UpdateVulnerabilityStatusAll(database)
if err != nil {
log.Error().Str("file", "main").Err(err)
}
for _, vulnerability := range vulnerabilityList {
err = model.UpdateVulnerabilityStatus(database, vulnerability.TemplateID, vulnerability.Host, "open")
if err != nil {
log.Error().Str("file", "main").Err(err)
}
}
blockList = append(blockList, createBlock(summary))
err = sendSlackNotification(webHookURL, attachmentList, blockList)
if err != nil {
log.Error().Str("file", "main").Err(err)
}
}
//nolint:funlen
func createAttachment(name string, tags string, severity string, metric string, score string, host string, matched string, status string) (attachment model.SlackAttachmentBody) {
nameField := model.SlackFieldBody{
Title: "Name",
Value: fmt.Sprintf("%s", name),
Short: false,
}
tagsField := model.SlackFieldBody{
Title: "Tags",
Value: fmt.Sprintf("`%s`", tags),
Short: true,
}
severityField := model.SlackFieldBody{
Title: "Severity",
Value: fmt.Sprintf("`%s`", severity),
Short: true,
}
metricField := model.SlackFieldBody{
Title: "CVSS Metric",
Value: fmt.Sprintf("`%s - %s`", score, metric),
Short: false,
}
hostField := model.SlackFieldBody{
Title: "Host",
Value: fmt.Sprintf("`%s`", host),
Short: true,
}
statusField := model.SlackFieldBody{
Title: "Status",
Value: fmt.Sprintf("`%s`", status),
Short: true,
}
matchedField := model.SlackFieldBody{
Title: "Endpoint",
Value: fmt.Sprintf("`%s`", matched),
Short: false,
}
var color string
switch {
case severity == "critical" || severity == "high":
color = "danger"
case severity == "medium":
color = "warning"
default:
color = "good"
}
var fieldList []model.SlackFieldBody
fieldList = append(fieldList, nameField, tagsField, severityField, metricField, hostField, statusField, matchedField)
attachment = model.SlackAttachmentBody{
Fields: fieldList,
Color: color,
}
return attachment
}
func createBlock(summary model.SummaryReport) (block model.SlackBlockBody) {
summaryField := model.SlackBlockFieldBody{
Type: "mrkdwn",
Text: fmt.Sprintf("> *Open Vulnerability Summary*, @here\n> *Host:* `%s`\n```Severity Count\n-------------------\nCritical %d\nHigh %d\nMedium %d\nLow %d\nInfo %d\n-------------------\nTotal %d```", summary.Host, summary.Critical, summary.High, summary.Medium, summary.Low, summary.Info, summary.Critical+summary.High+summary.Medium+summary.Low+summary.Info),
}
block = model.SlackBlockBody{
Type: "section",
Text: summaryField,
}
return block
}
func sendSlackNotification(webHookURL string, attachmentList []model.SlackAttachmentBody, blockList []model.SlackBlockBody) error {
slackMessage := model.SlackRequestBody{
Title: "Open Vulnerability",
Text: "Open Vulnerability",
Attachments: attachmentList,
Blocks: blockList,
}
slackBody, err := json.Marshal(slackMessage)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, webHookURL, bytes.NewBuffer(slackBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req) //nolint:bodyclose
if err != nil {
return err
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return err
}
log.Debug().Str("file", "main").Msg(buf.String())
if buf.String() != "ok" {
return errors.New("non-ok response returned from slack")
}
return nil
}