-
Notifications
You must be signed in to change notification settings - Fork 42
/
cveTools.go
177 lines (162 loc) · 5.08 KB
/
cveTools.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
package cvequeryops
const scriptFormat = `
# port scan plugin
yakit.AutoInitYakit()
CVE = []
addCVE = func(cve, rules, score, severity, titleZh, titleEn) {
CVE = append(CVE, {
"cve": cve,
"rule": rules,
"score": score,
"severity": severity,
"titleZh": titleZh,
"titleEn": titleEn,
})
}
%s
handle = func(result /* *fp.MatchResult */) {
// handle match result
if !result.IsOpen() {
return
}
if result.Fingerprint == nil {
return
}
if str.MatchAllOfRegexp(result.GetServiceName(), "%s") {
x.Foreach(CVE, func(i) {
defer func{
err = recover()
if err != nil {
yakit.Error("%s CVE 合规检查失败:%%v", err)
}
}
flag = true
for _, cpes := range result.Fingerprint.CPEFromUrls{
for _, cpe := range cpes{
if cpe.Product == "%s" && cpe.Version != "*"{
if RuleCompare(cpe.Version,i.rule){
flag = false
}
}
}
}
if flag {
return
}
target := str.HostPort(result.Target, result.Port)
targetOutput := sprintf("(%%v)", target)
risk.NewRisk(
target,
risk.title(sprintf("%%v:%%v%%v", i.cve, i["titleEn"], targetOutput)),
risk.titleVerbose(sprintf("%%v:%%v%%v", i.cve, i["titleZh"], targetOutput)),
risk.type("cve-baseline"),
risk.typeVerbose("CVE基线检查"),
risk.parameter(result.Fingerprint.Banner),
risk.potential(true),
risk.level(i.severity),
risk.details(i),
risk.cve(i.cve),
)
println(i.cve)
})
}
}
func RuleCompare(version, versionRule){
for _, ruleMap := range versionRule {
flag = true
for ruleName, rule := range ruleMap {
flag = flag && VersionRuleCompare(version,rule,ruleName)
}
if flag {
return true
}
}
return false
}
func VersionRuleCompare(version, BoundaryVerison, Op){
switch Op{
case "versionEndIncluding":
if str.VersionLessEqual(version, BoundaryVerison) {
return true
}
case "versionStartIncluding":
if str.VersionGreaterEqual(version, BoundaryVerison) {
return true
}
case "versionStartExcluding":
if str.VersionGreater(version, BoundaryVerison) {
return true
}
case "versionEndExcluding":
if str.VersionLess(version, BoundaryVerison) {
return true
}
case "current":
if version == BoundaryVerison{
return true
}
}
return false
}
`
// MakeCtScript 生成合规插件脚本,要求输入产品名,数据库路径,服务名(扫描获取的服务名),脚本输出路径
//func MakeCtScript(product, dbName, serverName, scriptPath string) {
// //! 设置合规脚本目标产品
//
// var addRuleStrs []string
// formatString := "addCVE(\"%s\", %s, \"%.2f\", \"%s\", \"%s\", %s)\n"
// CVEs, _ := Query(dbName, Product(product))
// for _, cve := range CVEs {
// //// todo 漏洞中文名暂时不用cnnvd,
// //cnnvd, _ := cve.CNNVD(dbName)
// var config cveresources.Configurations
// err := json.Unmarshal(cve.CPEConfigurations, &config)
// if err != nil {
// log.Errorf("config json error:%#v", err)
// return
// }
// var version []map[string]string
// for _, node := range config.Nodes {
// version = append(version, node.GetProductVersion(product)...)
// }
//
// if len(version) == 0 {
// continue
// }
//
// mapFormatStr := `"%s":"%s"`
// var versionRuleStr string
// var ruleListStrs []string
// for _, m := range version {
//
// var insideMapStrs []string
// for k, v := range m {
// insideMapStrs = append(insideMapStrs, fmt.Sprintf(mapFormatStr, k, v))
// }
// ruleListStrs = append(ruleListStrs, "{"+strings.Join(insideMapStrs, ",")+"}")
// }
// versionRuleStr = "[" + strings.Join(ruleListStrs, ",") + "]"
//
// addRuleStrs = append(addRuleStrs, fmt.Sprintf(formatString, cve.CVE.CVE, versionRuleStr, cve.BaseCVSSv2Score, cve.Severity, GetAKA(cve.DescriptionMain), strconv.Quote(cve.DescriptionMain)))
// }
// addRule := strings.Join(addRuleStrs, "\n")
// script := fmt.Sprintf(scriptFormat, addRule, serverName, product, product)
// outputPath := path.Join(scriptPath, product+".yak")
// err := os.WriteFile(outputPath, []byte(script), 0666)
// if err != nil {
// panic(err)
// }
//}
//
//func GetAKA(descriptions string) string {
// if !strings.Contains(descriptions, "aka") {
// return ""
// } else {
// compileRegex := regexp.MustCompile("aka \"(.*?)\"")
// matchAka := compileRegex.FindStringSubmatch(descriptions)
// if len(matchAka) > 0 {
// return matchAka[len(matchAka)-1]
// }
// return ""
// }
//}