forked from neuvector/neuvector
/
apps.go
197 lines (179 loc) · 5.57 KB
/
apps.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
package cvetools
import (
"strings"
log "github.com/sirupsen/logrus"
"github.com/neuvector/neuvector/scanner/common"
"github.com/neuvector/neuvector/scanner/detectors"
"github.com/neuvector/neuvector/share"
"github.com/neuvector/neuvector/share/scan"
)
func (cv *CveTools) DetectAppVul(path string, apps []detectors.AppFeatureVersion, namespace string) []vulFullReport {
if apps == nil || len(apps) == 0 {
return nil
}
modVuls, err := common.LoadAppVulsTb(path)
if err != nil {
return nil
}
vuls := make([]vulFullReport, 0)
for i, app := range apps {
// log.WithFields(log.Fields{"namespace": namespace, "package": app}).Info()
// It seems that alpine patches python vulnerabilities actively. Even the library
// version is vulnerable, the source codes are patched.
if strings.HasPrefix(namespace, "alpine:") && strings.HasPrefix(app.ModuleName, "python:") {
continue
}
if mv, found := modVuls[app.ModuleName]; found {
for _, v := range mv {
if len(v.UnaffectedVer) > 0 {
if unaffected := compareAppVersion(app.Version, v.UnaffectedVer); unaffected {
continue
}
}
// ruby reports patched version. The affected version is converted from patched version.
// The conversion logic is not correct.
if strings.HasPrefix(app.ModuleName, "ruby:") && len(v.FixedVer) > 0 {
if fixed := compareAppVersion(app.Version, v.FixedVer); !fixed {
fv := appVul2FullVul(app, v)
vuls = append(vuls, fv)
mcve := detectors.ModuleVul{Name: v.VulName, Status: share.ScanVulStatus_FixExists}
apps[i].ModuleVuls = append(apps[i].ModuleVuls, mcve)
}
} else {
if affected := compareAppVersion(app.Version, v.AffectedVer); affected {
fv := appVul2FullVul(app, v)
vuls = append(vuls, fv)
if len(v.FixedVer) > 0 {
mcve := detectors.ModuleVul{Name: v.VulName, Status: share.ScanVulStatus_FixExists}
apps[i].ModuleVuls = append(apps[i].ModuleVuls, mcve)
} else {
mcve := detectors.ModuleVul{Name: v.VulName, Status: share.ScanVulStatus_Unpatched}
apps[i].ModuleVuls = append(apps[i].ModuleVuls, mcve)
}
}
}
}
}
}
return vuls
}
func appVul2FullVul(app detectors.AppFeatureVersion, mv common.AppModuleVul) vulFullReport {
var fv vulFullReport
fv.Vf.Name = mv.VulName
fv.Vf.Namespace = app.AppName
fv.Vf.Description = mv.Description
fv.Vf.Link = mv.Link
fv.Vf.Severity = mv.Severity
fv.Vf.FixedIn = make([]common.FeaFull, 0)
fv.Vf.FixedIn = append(fv.Vf.FixedIn, moduleVer2FixVer(app, mv))
fv.Vf.Metadata = make(map[string]common.NVDMetadata)
if strings.HasSuffix(app.FileName, scan.WPVerFileSuffix) {
fv.Ft.Feature.Name = "WordPress"
} else {
fv.Ft.Feature.Name = app.FileName
}
fv.Ft.Feature.Namespace.Name = app.AppName
fv.Ft.Version, _ = common.NewVersion(app.Version)
fv.Ft.InBase = app.InBase
var nv common.NVDMetadata
nv.CVSSv2.Score = mv.Score
fv.Vf.Metadata["NVD"] = nv
return fv
}
func moduleVer2FixVer(app detectors.AppFeatureVersion, mv common.AppModuleVul) common.FeaFull {
ft := common.FeaFull{Name: mv.ModuleName, Namespace: app.AppName}
for i, v := range mv.FixedVer {
s := strings.Replace(v.OpCode, "or", "||", -1)
s = strings.Replace(s, "gt", ">", -1)
s = strings.Replace(s, "lt", "<", -1)
s = strings.Replace(s, "eq", "=", -1)
ft.Version += s + v.Version
if i < (len(mv.FixedVer) - 1) {
ft.Version += ";"
}
}
return ft
}
func compareAppVersion(ver string, affectedVer []common.AppModuleVersion) bool {
// NVSHAS-4684, version in database does have revision
/*
//skip the revision, no revision in database
if a := strings.Index(ver, "-"); a > 0 {
ver = ver[:a]
}
*/
var bv common.Version
av, err := common.NewVersion(ver)
if err != nil {
log.WithFields(log.Fields{"error": err, "version": ver}).Error("Failed to parse app version")
return false
}
var hit0, hit1 bool
var lastOp string
for id, mv := range affectedVer {
var prefix string
if mv.Version == "All" {
return true
} else {
// get the prefix out, only for jar
if a := strings.Index(mv.Version, ","); a > 0 {
prefix = mv.Version[a+1:]
mv.Version = mv.Version[:a]
}
bv, err = common.NewVersion(mv.Version)
if err != nil {
log.WithFields(log.Fields{"error": err, "version": mv.Version}).Error("Failed to parse affected version")
continue
}
}
if prefix != "" && !strings.HasPrefix(ver, prefix) {
continue
}
ret := av.Compare(bv)
hit1 = hit0
hit0 = false
if mv.OpCode == "" && ret == 0 {
return true
} else if strings.Contains(mv.OpCode, "lteq") && ret <= 0 {
hit0 = true
} else if strings.Contains(mv.OpCode, "lt") && ret < 0 {
hit0 = true
} else if strings.Contains(mv.OpCode, "eq") && ret == 0 {
hit0 = true
} else if strings.Contains(mv.OpCode, "gteq") && ret >= 0 {
hit0 = true
} else if strings.Contains(mv.OpCode, "gt") && ret > 0 {
hit0 = true
}
// avoid the >=2.7.1,2 and <=2.1,2 case
if prefix != "" {
if hit0 && !strings.Contains(mv.OpCode, "gt") && !strings.Contains(lastOp, "gt") {
return true
} else {
return hit0
}
}
//the case with <= || >= <=
if strings.Contains(mv.OpCode, "or") {
//in case of pairs: (>= && <=) or (>= && <=)
if hit1 && !strings.Contains(lastOp, "lt") {
return true
} else if hit1 && id == 1 {
// the case for: (<) || (> && <)
return true
} else if hit0 && id == (len(affectedVer)-1) {
//the last one
return true
}
} else { //the case >= && <=
if hit1 && hit0 {
return true
} else if hit0 && len(affectedVer) == 1 {
//the last one
return true
}
}
lastOp = mv.OpCode
}
return false
}