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.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 public class NvdCveAnalyzer extends AbstractAnalyzer {
40
41
42
43 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(NvdCveAnalyzer.class);
44
45
46
47 static final int MAX_QUERY_RESULTS = 100;
48
49
50
51 private CveDB cveDB;
52
53
54
55
56
57
58
59
60
61 public void open() throws SQLException, IOException, DatabaseException, ClassNotFoundException {
62 cveDB = new CveDB();
63 cveDB.open();
64 }
65
66
67
68
69 @Override
70 public void close() {
71 cveDB.close();
72 cveDB = null;
73 }
74
75
76
77
78
79
80 public boolean isOpen() {
81 return cveDB != null;
82 }
83
84
85
86
87
88
89 @Override
90 protected void finalize() throws Throwable {
91 super.finalize();
92 if (isOpen()) {
93 close();
94 }
95 }
96
97
98
99
100
101
102
103
104 @Override
105 public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
106 for (Identifier id : dependency.getIdentifiers()) {
107 if ("cpe".equals(id.getType())) {
108 try {
109 final String value = id.getValue();
110 final List<Vulnerability> vulns = cveDB.getVulnerabilities(value);
111 dependency.getVulnerabilities().addAll(vulns);
112 } catch (DatabaseException ex) {
113 throw new AnalysisException(ex);
114 }
115 }
116 }
117 for (Identifier id : dependency.getSuppressedIdentifiers()) {
118 if ("cpe".equals(id.getType())) {
119 try {
120 final String value = id.getValue();
121 final List<Vulnerability> vulns = cveDB.getVulnerabilities(value);
122 dependency.getSuppressedVulnerabilities().addAll(vulns);
123 } catch (DatabaseException ex) {
124 throw new AnalysisException(ex);
125 }
126 }
127 }
128 }
129
130
131
132
133
134
135 @Override
136 public String getName() {
137 return "NVD CVE Analyzer";
138 }
139
140
141
142
143
144
145 @Override
146 public AnalysisPhase getAnalysisPhase() {
147 return AnalysisPhase.FINDING_ANALYSIS;
148 }
149
150
151
152
153
154
155 @Override
156 public void initialize() throws InitializationException {
157 try {
158 this.open();
159 } catch (SQLException ex) {
160 LOGGER.debug("SQL Exception initializing NvdCveAnalyzer", ex);
161 throw new InitializationException(ex);
162 } catch (IOException ex) {
163 LOGGER.debug("IO Exception initializing NvdCveAnalyzer", ex);
164 throw new InitializationException(ex);
165 } catch (DatabaseException ex) {
166 LOGGER.debug("Database Exception initializing NvdCveAnalyzer", ex);
167 throw new InitializationException(ex);
168 } catch (ClassNotFoundException ex) {
169 LOGGER.debug("Exception initializing NvdCveAnalyzer", ex);
170 throw new InitializationException(ex);
171 }
172 }
173 }