-
Notifications
You must be signed in to change notification settings - Fork 43
/
statistics_graph.go
315 lines (297 loc) · 7.44 KB
/
statistics_graph.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
314
315
package cve
import (
"context"
"fmt"
"github.com/yaklang/yaklang/common/consts"
"github.com/yaklang/yaklang/common/cve/cveresources"
"github.com/yaklang/yaklang/common/log"
"sort"
"strings"
"sync"
)
type Graph struct {
Name string `json:"name"`
NameVerbose string `json:"name_verbose"`
Type string `json:"type"`
TypeVerbose string `json:"type_verbose"`
Data []*KVPair `json:"data"`
Reason string `json:"reason"` // 如果数据为空的时候,展示原因
ComplexityGroup string `json:"complexity_group"`
AccessVector string `json:"access_vector"`
}
func severityVerbose(i string) string {
switch strings.ToUpper(i) {
case "MEDIUM":
return "中危"
case "HIGH":
return "高危"
case "LOW":
return "低危"
case "CRITICAL":
return "严重"
default:
return "未知"
}
}
func complexityVerbose(s string) string {
switch s {
case "HIGH":
return "利用难度高"
case "LOW":
return "利用难度低"
case "MIDDLE":
return "有一定漏洞利用难度"
default:
return "未知/无数据"
}
}
func accessVectorVerbose(s string) string {
switch strings.ToUpper(s) {
case "NETWORK":
return "网络"
case "LOCAL":
return "本地"
case "ADJACENT_NETWORK":
return "局域网"
case "PHYSICAL":
return "物理介质"
default:
return "其他/未知"
}
}
var (
cweOnce = new(sync.Once)
cweNameMap = map[string]string{}
cweSolutions = map[string]string{}
)
func cweSolution(s string) string {
_ = cweVerbose(s)
v, ok := cweSolutions[s]
if ok {
return v
} else {
return ""
}
}
func cweVerbose(s string) string {
cweOnce.Do(func() {
db := consts.GetGormCVEDatabase()
if db == nil {
log.Warn("cannot load cwe database")
return
}
for cwe := range cveresources.YieldCWEs(db.Model(&cveresources.CWE{}), context.Background()) {
cweNameMap[cwe.CWEString()] = cwe.NameZh
cweSolutions[cwe.CWEString()] = cwe.CWESolution
}
})
s = strings.ToUpper(s)
if !strings.HasPrefix(s, "CWE-") {
s = "CWE-" + s
}
v, _ := cweNameMap[s]
if v == "" {
return s
}
return v
}
func (s *Statistics) ToGraphs() []*Graph {
// 环形分析器
var graphs []*Graph
g := &Graph{
Name: "AttentionRing",
NameVerbose: "需被关注的CVE信息",
Type: "multi-pie",
TypeVerbose: "多层饼环",
Data: []*KVPair{
{
Key: "NoAuthNetworkHighExploitable",
Value: s.NoAuthNetworkHighExploitableCount,
ShowValue: s.NoAuthNetworkHighExploitableCount,
KeyVerbose: "通过网络无需认证且易于攻击",
},
{
Key: "NoAuthNetwork",
Value: s.NoAuthNetworkCount,
ShowValue: s.NoAuthNetworkCount,
KeyVerbose: "攻击通过网络无需认证",
},
{
Key: "NetworkCount",
Value: s.NetworkCount,
ShowValue: s.NetworkCount,
KeyVerbose: "通过网络攻击",
},
},
}
graphs = append(graphs, g)
// CWE
var pairs []*KVPair
for cweName, i := range s.CWECounter {
pairs = append(pairs, &KVPair{
Key: cweName,
Value: i,
ShowValue: i,
KeyVerbose: cweVerbose(cweName),
Detail: cweSolution(cweName),
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "cwe-analysis",
NameVerbose: "合规漏洞类型大致分布",
Type: "nightingle-rose",
TypeVerbose: "南丁格尔玫瑰图",
Data: pairs,
}
graphs = append(graphs, g)
// accessVector
pairs = []*KVPair{}
for access, count := range s.AccessVectorCounter {
pairs = append(pairs, &KVPair{
Key: access,
Value: count,
ShowValue: count,
KeyVerbose: accessVectorVerbose(access),
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "access-vector-analysis",
NameVerbose: "合规漏洞攻击路径统计",
Type: "card",
TypeVerbose: "通用KV",
Data: pairs,
}
graphs = append(graphs, g)
// 网络攻击复杂度
pairs = []*KVPair{}
for key, count := range s.NetworkComplexityCounter {
pairs = append(pairs, &KVPair{
Key: key,
Value: count,
ShowValue: count,
KeyVerbose: complexityVerbose(key),
JumpLink: key,
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "network-attack-complexity-analysis",
NameVerbose: "合规漏洞利用复杂度统计(可联网攻击)",
Type: "general",
TypeVerbose: "通用KV",
Data: pairs,
AccessVector: "NETWORK",
ComplexityGroup: strings.Join([]string{"未知/无数据", "利用难度低", "利用难度高"}, ","),
}
graphs = append(graphs, g)
// 本地攻击复杂度
pairs = []*KVPair{}
for key, count := range s.LocalComplexityCounter {
pairs = append(pairs, &KVPair{
Key: key,
Value: count,
ShowValue: count,
KeyVerbose: complexityVerbose(key),
JumpLink: key,
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "local-attack-complexity-analysis",
NameVerbose: "合规漏洞利用复杂度统计(仅本地攻击)",
Type: "general",
TypeVerbose: "通用KV",
Data: pairs,
AccessVector: "LOCAL",
ComplexityGroup: strings.Join([]string{"未知/无数据", "利用难度低", "利用难度高"}, ","),
}
graphs = append(graphs, g)
// 攻击复杂度
pairs = []*KVPair{}
for key, count := range s.ComplexityCounter {
pairs = append(pairs, &KVPair{
Key: key,
Value: count,
ShowValue: count,
KeyVerbose: complexityVerbose(key),
JumpLink: key,
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "access-complexity-analysis",
NameVerbose: "合规漏洞利用复杂度统计",
Type: "general",
TypeVerbose: "通用KV",
Data: pairs,
ComplexityGroup: strings.Join([]string{"未知/无数据", "利用难度低", "利用难度高"}, ","),
}
graphs = append(graphs, g)
// 严重程度
/*pairs = []*KVPair{}
for key, count := range s.SeverityCounter {
pairs = append(pairs, &KVPair{
Key: key,
Value: count,
ShowValue: count,
KeyVerbose: severityVerbose(key),
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "severtiy-analysis",
NameVerbose: "合规漏洞严重程度统计",
Type: "general",
TypeVerbose: "通用KV",
Data: pairs,
ComplexityGroup: strings.Join([]string{"严重", "高危", "中危", "低危"}, ","),
}
graphs = append(graphs, g)*/
// Years
pairs = []*KVPair{}
for key, count := range s.YearsSeverityCounter {
var a []*KVPair
yearsCount := 0
for k, v := range count {
a = append(a, &KVPair{
Key: k,
Value: v,
ShowValue: v,
})
yearsCount = yearsCount + v
}
pairs = append(pairs, &KVPair{
Key: key,
Value: yearsCount,
ShowValue: yearsCount,
KeyVerbose: "CVE-" + fmt.Sprint(key),
Data: a,
})
}
sort.SliceStable(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[i].Value
})
g = &Graph{
Name: "years-analysis",
NameVerbose: "CVE 年份统计",
Type: "year-cve",
TypeVerbose: "通用KV",
Data: pairs,
}
graphs = append(graphs, g)
return graphs
}