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