1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.analyzer;
19
20 import java.io.IOException;
21 import java.sql.SQLException;
22 import java.util.List;
23 import org.owasp.dependencycheck.Engine;
24 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
25 import org.owasp.dependencycheck.data.nvdcve.CveDB;
26 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
27 import org.owasp.dependencycheck.dependency.Dependency;
28 import org.owasp.dependencycheck.dependency.Identifier;
29 import org.owasp.dependencycheck.dependency.Vulnerability;
30 import org.owasp.dependencycheck.exception.InitializationException;
31 import org.owasp.dependencycheck.utils.Settings;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 public class NvdCveAnalyzer extends AbstractAnalyzer {
42
43
44
45
46 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NvdCveAnalyzer.class);
47
48
49
50 static final int MAX_QUERY_RESULTS = 100;
51
52
53
54 private CveDB cveDB;
55
56
57
58
59
60
61
62
63
64
65 public void open() throws SQLException, IOException, DatabaseException, ClassNotFoundException {
66 cveDB = new CveDB();
67 cveDB.open();
68 }
69
70
71
72
73 @Override
74 public void closeAnalyzer() {
75 cveDB.close();
76 cveDB = null;
77 }
78
79
80
81
82
83
84 public boolean isOpen() {
85 return cveDB != null;
86 }
87
88
89
90
91
92
93 @Override
94 protected void finalize() throws Throwable {
95 super.finalize();
96 if (isOpen()) {
97 close();
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110 @Override
111 protected void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
112 for (Identifier id : dependency.getIdentifiers()) {
113 if ("cpe".equals(id.getType())) {
114 try {
115 final String value = id.getValue();
116 final List<Vulnerability> vulns = cveDB.getVulnerabilities(value);
117 dependency.getVulnerabilities().addAll(vulns);
118 } catch (DatabaseException ex) {
119 throw new AnalysisException(ex);
120 }
121 }
122 }
123 for (Identifier id : dependency.getSuppressedIdentifiers()) {
124 if ("cpe".equals(id.getType())) {
125 try {
126 final String value = id.getValue();
127 final List<Vulnerability> vulns = cveDB.getVulnerabilities(value);
128 dependency.getSuppressedVulnerabilities().addAll(vulns);
129 } catch (DatabaseException ex) {
130 throw new AnalysisException(ex);
131 }
132 }
133 }
134 }
135
136
137
138
139
140
141 @Override
142 public String getName() {
143 return "NVD CVE Analyzer";
144 }
145
146
147
148
149
150
151 @Override
152 public AnalysisPhase getAnalysisPhase() {
153 return AnalysisPhase.FINDING_ANALYSIS;
154 }
155
156
157
158
159
160
161
162 @Override
163 protected String getAnalyzerEnabledSettingKey() {
164 return Settings.KEYS.ANALYZER_NVD_CVE_ENABLED;
165 }
166
167
168
169
170
171
172
173 @Override
174 public void initializeAnalyzer() throws InitializationException {
175 try {
176 this.open();
177 } catch (SQLException ex) {
178 LOGGER.debug("SQL Exception initializing NvdCveAnalyzer", ex);
179 throw new InitializationException(ex);
180 } catch (IOException ex) {
181 LOGGER.debug("IO Exception initializing NvdCveAnalyzer", ex);
182 throw new InitializationException(ex);
183 } catch (DatabaseException ex) {
184 LOGGER.debug("Database Exception initializing NvdCveAnalyzer", ex);
185 throw new InitializationException(ex);
186 } catch (ClassNotFoundException ex) {
187 LOGGER.debug("Exception initializing NvdCveAnalyzer", ex);
188 throw new InitializationException(ex);
189 }
190 }
191 }