| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.update.xml; |
| 19 | |
|
| 20 | |
import java.io.IOException; |
| 21 | |
import java.util.List; |
| 22 | |
import java.util.Map; |
| 23 | |
import java.util.logging.Level; |
| 24 | |
import java.util.logging.Logger; |
| 25 | |
import org.apache.lucene.index.CorruptIndexException; |
| 26 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 27 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 28 | |
import org.owasp.dependencycheck.dependency.Reference; |
| 29 | |
import org.owasp.dependencycheck.dependency.Vulnerability; |
| 30 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 31 | |
import org.xml.sax.Attributes; |
| 32 | |
import org.xml.sax.SAXException; |
| 33 | |
import org.xml.sax.SAXNotSupportedException; |
| 34 | |
import org.xml.sax.helpers.DefaultHandler; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 1 | public class NvdCve20Handler extends DefaultHandler { |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 1 | private static final Logger LOGGER = Logger.getLogger(NvdCve20Handler.class.getName()); |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private static final String CURRENT_SCHEMA_VERSION = "2.0"; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 1 | private final Element current = new Element(); |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private StringBuilder nodeText; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private Vulnerability vulnerability; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private Reference reference; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 1 | private boolean hasApplicationCpe = false; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private int totalNumberOfEntries; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public int getTotalNumberOfEntries() { |
| 82 | 0 | return totalNumberOfEntries; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
private int totalNumberOfApplicationEntries; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public int getTotalNumberOfApplicationEntries() { |
| 95 | 0 | return totalNumberOfApplicationEntries; |
| 96 | |
} |
| 97 | |
|
| 98 | |
@Override |
| 99 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
| 100 | 2412 | current.setNode(qName); |
| 101 | 2412 | if (current.isEntryNode()) { |
| 102 | 27 | hasApplicationCpe = false; |
| 103 | 27 | vulnerability = new Vulnerability(); |
| 104 | 27 | vulnerability.setName(attributes.getValue("id")); |
| 105 | 2385 | } else if (current.isVulnProductNode()) { |
| 106 | 727 | nodeText = new StringBuilder(100); |
| 107 | 1658 | } else if (current.isVulnReferencesNode()) { |
| 108 | 90 | final String lang = attributes.getValue("xml:lang"); |
| 109 | 90 | if ("en".equals(lang)) { |
| 110 | 90 | reference = new Reference(); |
| 111 | |
} else { |
| 112 | 0 | reference = null; |
| 113 | |
} |
| 114 | 90 | } else if (reference != null && current.isVulnReferenceNode()) { |
| 115 | 90 | reference.setUrl(attributes.getValue("href")); |
| 116 | 90 | nodeText = new StringBuilder(130); |
| 117 | 1478 | } else if (reference != null && current.isVulnSourceNode()) { |
| 118 | 90 | nodeText = new StringBuilder(30); |
| 119 | 1388 | } else if (current.isVulnSummaryNode()) { |
| 120 | 27 | nodeText = new StringBuilder(500); |
| 121 | 1361 | } else if (current.isNVDNode()) { |
| 122 | 1 | final String nvdVer = attributes.getValue("nvd_xml_version"); |
| 123 | 1 | if (!CURRENT_SCHEMA_VERSION.equals(nvdVer)) { |
| 124 | 0 | throw new SAXNotSupportedException("Schema version " + nvdVer + " is not supported"); |
| 125 | |
} |
| 126 | 1 | } else if (current.isVulnCWENode()) { |
| 127 | 19 | vulnerability.setCwe(attributes.getValue("id")); |
| 128 | 1341 | } else if (current.isCVSSScoreNode()) { |
| 129 | 26 | nodeText = new StringBuilder(5); |
| 130 | 1315 | } else if (current.isCVSSAccessVectorNode()) { |
| 131 | 26 | nodeText = new StringBuilder(20); |
| 132 | 1289 | } else if (current.isCVSSAccessComplexityNode()) { |
| 133 | 26 | nodeText = new StringBuilder(20); |
| 134 | 1263 | } else if (current.isCVSSAuthenticationNode()) { |
| 135 | 26 | nodeText = new StringBuilder(20); |
| 136 | 1237 | } else if (current.isCVSSAvailabilityImpactNode()) { |
| 137 | 26 | nodeText = new StringBuilder(20); |
| 138 | 1211 | } else if (current.isCVSSConfidentialityImpactNode()) { |
| 139 | 26 | nodeText = new StringBuilder(20); |
| 140 | 1185 | } else if (current.isCVSSIntegrityImpactNode()) { |
| 141 | 26 | nodeText = new StringBuilder(20); |
| 142 | |
} |
| 143 | 2412 | } |
| 144 | |
|
| 145 | |
@Override |
| 146 | |
public void characters(char[] ch, int start, int length) throws SAXException { |
| 147 | 3987 | if (nodeText != null) { |
| 148 | 1142 | nodeText.append(ch, start, length); |
| 149 | |
} |
| 150 | 3987 | } |
| 151 | |
|
| 152 | |
@Override |
| 153 | |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 154 | 2412 | current.setNode(qName); |
| 155 | 2412 | if (current.isEntryNode()) { |
| 156 | 27 | totalNumberOfEntries += 1; |
| 157 | 27 | if (hasApplicationCpe) { |
| 158 | 19 | totalNumberOfApplicationEntries += 1; |
| 159 | |
try { |
| 160 | 19 | saveEntry(vulnerability); |
| 161 | 0 | } catch (DatabaseException ex) { |
| 162 | 0 | throw new SAXException(ex); |
| 163 | 0 | } catch (CorruptIndexException ex) { |
| 164 | 0 | throw new SAXException(ex); |
| 165 | 0 | } catch (IOException ex) { |
| 166 | 0 | throw new SAXException(ex); |
| 167 | 19 | } |
| 168 | |
} |
| 169 | 27 | vulnerability = null; |
| 170 | 2385 | } else if (current.isCVSSScoreNode()) { |
| 171 | |
try { |
| 172 | 26 | final float score = Float.parseFloat(nodeText.toString()); |
| 173 | 26 | vulnerability.setCvssScore(score); |
| 174 | 0 | } catch (NumberFormatException ex) { |
| 175 | 0 | LOGGER.log(Level.SEVERE, "Error parsing CVSS Score."); |
| 176 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 177 | 26 | } |
| 178 | 26 | nodeText = null; |
| 179 | 2359 | } else if (current.isCVSSAccessVectorNode()) { |
| 180 | 26 | vulnerability.setCvssAccessVector(nodeText.toString()); |
| 181 | 26 | nodeText = null; |
| 182 | 2333 | } else if (current.isCVSSAccessComplexityNode()) { |
| 183 | 26 | vulnerability.setCvssAccessComplexity(nodeText.toString()); |
| 184 | 26 | nodeText = null; |
| 185 | 2307 | } else if (current.isCVSSAuthenticationNode()) { |
| 186 | 26 | vulnerability.setCvssAuthentication(nodeText.toString()); |
| 187 | 26 | nodeText = null; |
| 188 | 2281 | } else if (current.isCVSSAvailabilityImpactNode()) { |
| 189 | 26 | vulnerability.setCvssAvailabilityImpact(nodeText.toString()); |
| 190 | 26 | nodeText = null; |
| 191 | 2255 | } else if (current.isCVSSConfidentialityImpactNode()) { |
| 192 | 26 | vulnerability.setCvssConfidentialityImpact(nodeText.toString()); |
| 193 | 26 | nodeText = null; |
| 194 | 2229 | } else if (current.isCVSSIntegrityImpactNode()) { |
| 195 | 26 | vulnerability.setCvssIntegrityImpact(nodeText.toString()); |
| 196 | 26 | nodeText = null; |
| 197 | 2203 | } else if (current.isVulnProductNode()) { |
| 198 | 727 | final String cpe = nodeText.toString(); |
| 199 | 727 | if (cpe.startsWith("cpe:/a:")) { |
| 200 | 614 | hasApplicationCpe = true; |
| 201 | 614 | vulnerability.addVulnerableSoftware(cpe); |
| 202 | |
} |
| 203 | 727 | nodeText = null; |
| 204 | 727 | } else if (reference != null && current.isVulnReferencesNode()) { |
| 205 | 90 | vulnerability.addReference(reference); |
| 206 | 90 | reference = null; |
| 207 | 1386 | } else if (reference != null && current.isVulnReferenceNode()) { |
| 208 | 90 | reference.setName(nodeText.toString()); |
| 209 | 90 | nodeText = null; |
| 210 | 1296 | } else if (reference != null && current.isVulnSourceNode()) { |
| 211 | 90 | reference.setSource(nodeText.toString()); |
| 212 | 90 | nodeText = null; |
| 213 | 1206 | } else if (current.isVulnSummaryNode()) { |
| 214 | 27 | vulnerability.setDescription(nodeText.toString()); |
| 215 | 27 | if (nodeText.indexOf("** REJECT **") >= 0) { |
| 216 | 1 | hasApplicationCpe = true; |
| 217 | |
} |
| 218 | 27 | nodeText = null; |
| 219 | |
} |
| 220 | 2412 | } |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
private CveDB cveDB; |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public void setCveDB(CveDB db) { |
| 232 | 0 | cveDB = db; |
| 233 | 0 | } |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
private Map<String, List<VulnerableSoftware>> prevVersionVulnMap; |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void setPrevVersionVulnMap(Map<String, List<VulnerableSoftware>> map) { |
| 245 | 0 | prevVersionVulnMap = map; |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
private void saveEntry(Vulnerability vuln) throws DatabaseException, CorruptIndexException, IOException { |
| 257 | 19 | if (cveDB == null) { |
| 258 | 19 | return; |
| 259 | |
} |
| 260 | 0 | final String cveName = vuln.getName(); |
| 261 | 0 | if (prevVersionVulnMap.containsKey(cveName)) { |
| 262 | 0 | final List<VulnerableSoftware> vulnSoftware = prevVersionVulnMap.get(cveName); |
| 263 | 0 | for (VulnerableSoftware vs : vulnSoftware) { |
| 264 | 0 | vuln.updateVulnerableSoftware(vs); |
| 265 | 0 | } |
| 266 | |
} |
| 267 | 0 | cveDB.updateVulnerability(vuln); |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | 1 | protected static class Element { |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public static final String NVD = "nvd"; |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public static final String ENTRY = "entry"; |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public static final String VULN_PRODUCT = "vuln:product"; |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
public static final String VULN_REFERENCES = "vuln:references"; |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
public static final String VULN_SOURCE = "vuln:source"; |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
public static final String VULN_REFERENCE = "vuln:reference"; |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public static final String VULN_SUMMARY = "vuln:summary"; |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
public static final String VULN_CWE = "vuln:cwe"; |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
public static final String CVSS_SCORE = "cvss:score"; |
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
public static final String CVSS_ACCESS_VECTOR = "cvss:access-vector"; |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
public static final String CVSS_ACCESS_COMPLEXITY = "cvss:access-complexity"; |
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
public static final String CVSS_AUTHENTICATION = "cvss:authentication"; |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
public static final String CVSS_CONFIDENTIALITY_IMPACT = "cvss:confidentiality-impact"; |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
public static final String CVSS_INTEGRITY_IMPACT = "cvss:integrity-impact"; |
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
public static final String CVSS_AVAILABILITY_IMPACT = "cvss:availability-impact"; |
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
private String node; |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
public String getNode() { |
| 347 | 0 | return this.node; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
public void setNode(String node) { |
| 356 | 4824 | this.node = node; |
| 357 | 4824 | } |
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
public boolean isNVDNode() { |
| 365 | 1361 | return NVD.equals(node); |
| 366 | |
} |
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
public boolean isEntryNode() { |
| 374 | 4824 | return ENTRY.equals(node); |
| 375 | |
} |
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
public boolean isVulnProductNode() { |
| 383 | 4588 | return VULN_PRODUCT.equals(node); |
| 384 | |
} |
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
public boolean isVulnReferencesNode() { |
| 392 | 1928 | return VULN_REFERENCES.equals(node); |
| 393 | |
} |
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
public boolean isVulnReferenceNode() { |
| 401 | 360 | return VULN_REFERENCE.equals(node); |
| 402 | |
} |
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
public boolean isVulnSourceNode() { |
| 410 | 180 | return VULN_SOURCE.equals(node); |
| 411 | |
} |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
public boolean isVulnSummaryNode() { |
| 419 | 2594 | return VULN_SUMMARY.equals(node); |
| 420 | |
} |
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
public boolean isVulnCWENode() { |
| 428 | 1360 | return VULN_CWE.equals(node); |
| 429 | |
} |
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
public boolean isCVSSScoreNode() { |
| 437 | 3726 | return CVSS_SCORE.equals(node); |
| 438 | |
} |
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
public boolean isCVSSAccessVectorNode() { |
| 446 | 3674 | return CVSS_ACCESS_VECTOR.equals(node); |
| 447 | |
} |
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
public boolean isCVSSAccessComplexityNode() { |
| 455 | 3622 | return CVSS_ACCESS_COMPLEXITY.equals(node); |
| 456 | |
} |
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
|
| 462 | |
|
| 463 | |
public boolean isCVSSAuthenticationNode() { |
| 464 | 3570 | return CVSS_AUTHENTICATION.equals(node); |
| 465 | |
} |
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
public boolean isCVSSConfidentialityImpactNode() { |
| 473 | 3466 | return CVSS_CONFIDENTIALITY_IMPACT.equals(node); |
| 474 | |
} |
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
public boolean isCVSSIntegrityImpactNode() { |
| 482 | 3414 | return CVSS_INTEGRITY_IMPACT.equals(node); |
| 483 | |
} |
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
public boolean isCVSSAvailabilityImpactNode() { |
| 491 | 3518 | return CVSS_AVAILABILITY_IMPACT.equals(node); |
| 492 | |
} |
| 493 | |
} |
| 494 | |
|
| 495 | |
} |