| 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.io.UnsupportedEncodingException; |
| 22 | |
import java.net.URLEncoder; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.Collections; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.Set; |
| 27 | |
import java.util.StringTokenizer; |
| 28 | |
import java.util.logging.Level; |
| 29 | |
import java.util.logging.Logger; |
| 30 | |
import org.apache.lucene.document.Document; |
| 31 | |
import org.apache.lucene.index.CorruptIndexException; |
| 32 | |
import org.apache.lucene.queryparser.classic.ParseException; |
| 33 | |
import org.apache.lucene.search.ScoreDoc; |
| 34 | |
import org.apache.lucene.search.TopDocs; |
| 35 | |
import org.owasp.dependencycheck.Engine; |
| 36 | |
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; |
| 37 | |
import org.owasp.dependencycheck.data.cpe.CpeMemoryIndex; |
| 38 | |
import org.owasp.dependencycheck.data.cpe.Fields; |
| 39 | |
import org.owasp.dependencycheck.data.cpe.IndexEntry; |
| 40 | |
import org.owasp.dependencycheck.data.cpe.IndexException; |
| 41 | |
import org.owasp.dependencycheck.data.lucene.LuceneUtils; |
| 42 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 43 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 44 | |
import org.owasp.dependencycheck.dependency.Confidence; |
| 45 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 46 | |
import org.owasp.dependencycheck.dependency.Evidence; |
| 47 | |
import org.owasp.dependencycheck.dependency.EvidenceCollection; |
| 48 | |
import org.owasp.dependencycheck.dependency.Identifier; |
| 49 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 50 | |
import org.owasp.dependencycheck.utils.DependencyVersion; |
| 51 | |
import org.owasp.dependencycheck.utils.DependencyVersionUtil; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public class CPEAnalyzer implements Analyzer { |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 1 | private static final Logger LOGGER = Logger.getLogger(CPEAnalyzer.class.getName()); |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
static final int MAX_QUERY_RESULTS = 25; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
static final String WEIGHTING_BOOST = "^5"; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
static final String CLEANSE_CHARACTER_RX = "[^A-Za-z0-9 ._-]"; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
static final String CLEANSE_NONALPHA_RX = "[^A-Za-z]*"; |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
static final int STRING_BUILDER_BUFFER = 20; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private CpeMemoryIndex cpe; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private CveDB cve; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public static final String NVD_SEARCH_URL = "https://web.nvd.nist.gov/view/vuln/search-results?adv_search=true&cves=on&cpe_version=%s"; |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
@Override |
| 105 | |
public String getName() { |
| 106 | 4 | return "CPE Analyzer"; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public AnalysisPhase getAnalysisPhase() { |
| 116 | 1 | return AnalysisPhase.IDENTIFIER_ANALYSIS; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public void initialize() throws Exception { |
| 126 | 1 | this.open(); |
| 127 | 1 | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
public void open() throws IOException, DatabaseException { |
| 137 | 1 | LOGGER.log(Level.FINE, "Opening the CVE Database"); |
| 138 | 1 | cve = new CveDB(); |
| 139 | 1 | cve.open(); |
| 140 | 1 | LOGGER.log(Level.FINE, "Creating the Lucene CPE Index"); |
| 141 | 1 | cpe = CpeMemoryIndex.getInstance(); |
| 142 | |
try { |
| 143 | 1 | cpe.open(cve); |
| 144 | 0 | } catch (IndexException ex) { |
| 145 | 0 | LOGGER.log(Level.FINE, "IndexException", ex); |
| 146 | 0 | throw new DatabaseException(ex); |
| 147 | 1 | } |
| 148 | 1 | } |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
@Override |
| 154 | |
public void close() { |
| 155 | 1 | if (cpe != null) { |
| 156 | 1 | cpe.close(); |
| 157 | |
} |
| 158 | 1 | if (cve != null) { |
| 159 | 1 | cve.close(); |
| 160 | |
} |
| 161 | 1 | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
protected void determineCPE(Dependency dependency) throws CorruptIndexException, IOException, ParseException { |
| 173 | |
|
| 174 | 2 | String vendors = ""; |
| 175 | 2 | String products = ""; |
| 176 | 7 | for (Confidence confidence : Confidence.values()) { |
| 177 | 6 | if (dependency.getVendorEvidence().contains(confidence)) { |
| 178 | 6 | vendors = addEvidenceWithoutDuplicateTerms(vendors, dependency.getVendorEvidence(), confidence); |
| 179 | |
} |
| 180 | 6 | if (dependency.getProductEvidence().contains(confidence)) { |
| 181 | 5 | products = addEvidenceWithoutDuplicateTerms(products, dependency.getProductEvidence(), confidence); |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | 6 | if (!vendors.isEmpty() && !products.isEmpty()) { |
| 189 | 6 | final List<IndexEntry> entries = searchCPE(vendors, products, dependency.getProductEvidence().getWeighting(), |
| 190 | |
dependency.getVendorEvidence().getWeighting()); |
| 191 | 6 | if (entries == null) { |
| 192 | 0 | continue; |
| 193 | |
} |
| 194 | 6 | boolean identifierAdded = false; |
| 195 | 6 | for (IndexEntry e : entries) { |
| 196 | 23 | if (verifyEntry(e, dependency)) { |
| 197 | 2 | final String vendor = e.getVendor(); |
| 198 | 2 | final String product = e.getProduct(); |
| 199 | 2 | identifierAdded |= determineIdentifiers(dependency, vendor, product, confidence); |
| 200 | |
} |
| 201 | 23 | } |
| 202 | 6 | if (identifierAdded) { |
| 203 | 1 | break; |
| 204 | |
} |
| 205 | |
} |
| 206 | |
} |
| 207 | 2 | } |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
private String addEvidenceWithoutDuplicateTerms(final String text, final EvidenceCollection ec, Confidence confidenceFilter) { |
| 220 | 11 | final String txt = (text == null) ? "" : text; |
| 221 | 11 | final StringBuilder sb = new StringBuilder(txt.length() + (20 * ec.size())); |
| 222 | 11 | sb.append(' ').append(txt).append(' '); |
| 223 | 11 | for (Evidence e : ec.iterator(confidenceFilter)) { |
| 224 | 43 | String value = e.getValue(); |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | 43 | if (value.startsWith("http://")) { |
| 229 | 2 | value = value.substring(7).replaceAll("\\.", " "); |
| 230 | |
} |
| 231 | 43 | if (value.startsWith("https://")) { |
| 232 | 0 | value = value.substring(8).replaceAll("\\.", " "); |
| 233 | |
} |
| 234 | 43 | if (sb.indexOf(" " + value + " ") < 0) { |
| 235 | 36 | sb.append(value).append(' '); |
| 236 | |
} |
| 237 | 43 | } |
| 238 | 11 | return sb.toString().trim(); |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
protected List<IndexEntry> searchCPE(String vendor, String product, |
| 257 | |
Set<String> vendorWeightings, Set<String> productWeightings) { |
| 258 | |
|
| 259 | 6 | final ArrayList<IndexEntry> ret = new ArrayList<IndexEntry>(MAX_QUERY_RESULTS); |
| 260 | |
|
| 261 | 6 | final String searchString = buildSearch(vendor, product, vendorWeightings, productWeightings); |
| 262 | 6 | if (searchString == null) { |
| 263 | 0 | return ret; |
| 264 | |
} |
| 265 | |
try { |
| 266 | 6 | final TopDocs docs = cpe.search(searchString, MAX_QUERY_RESULTS); |
| 267 | 156 | for (ScoreDoc d : docs.scoreDocs) { |
| 268 | 150 | if (d.score >= 0.08) { |
| 269 | 23 | final Document doc = cpe.getDocument(d.doc); |
| 270 | 23 | final IndexEntry entry = new IndexEntry(); |
| 271 | 23 | entry.setVendor(doc.get(Fields.VENDOR)); |
| 272 | 23 | entry.setProduct(doc.get(Fields.PRODUCT)); |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | 23 | entry.setSearchScore(d.score); |
| 281 | 23 | if (!ret.contains(entry)) { |
| 282 | 23 | ret.add(entry); |
| 283 | |
} |
| 284 | |
} |
| 285 | |
} |
| 286 | 6 | return ret; |
| 287 | 0 | } catch (ParseException ex) { |
| 288 | 0 | final String msg = String.format("Unable to parse: %s", searchString); |
| 289 | 0 | LOGGER.log(Level.WARNING, "An error occured querying the CPE data. See the log for more details."); |
| 290 | 0 | LOGGER.log(Level.INFO, msg, ex); |
| 291 | 0 | } catch (IOException ex) { |
| 292 | 0 | final String msg = String.format("IO Error with search string: %s", searchString); |
| 293 | 0 | LOGGER.log(Level.WARNING, "An error occured reading CPE data. See the log for more details."); |
| 294 | 0 | LOGGER.log(Level.INFO, msg, ex); |
| 295 | 0 | } |
| 296 | 0 | return null; |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
protected String buildSearch(String vendor, String product, |
| 314 | |
Set<String> vendorWeighting, Set<String> productWeightings) { |
| 315 | 6 | final String v = vendor; |
| 316 | 6 | final String p = product; |
| 317 | 6 | final StringBuilder sb = new StringBuilder(v.length() + p.length() |
| 318 | |
+ Fields.PRODUCT.length() + Fields.VENDOR.length() + STRING_BUILDER_BUFFER); |
| 319 | |
|
| 320 | 6 | if (!appendWeightedSearch(sb, Fields.PRODUCT, p, productWeightings)) { |
| 321 | 0 | return null; |
| 322 | |
} |
| 323 | 6 | sb.append(" AND "); |
| 324 | 6 | if (!appendWeightedSearch(sb, Fields.VENDOR, v, vendorWeighting)) { |
| 325 | 0 | return null; |
| 326 | |
} |
| 327 | 6 | return sb.toString(); |
| 328 | |
} |
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
private boolean appendWeightedSearch(StringBuilder sb, String field, String searchText, Set<String> weightedText) { |
| 342 | 12 | sb.append(" ").append(field).append(":( "); |
| 343 | |
|
| 344 | 12 | final String cleanText = cleanseText(searchText); |
| 345 | |
|
| 346 | 12 | if ("".equals(cleanText)) { |
| 347 | 0 | return false; |
| 348 | |
} |
| 349 | |
|
| 350 | 12 | if (weightedText == null || weightedText.isEmpty()) { |
| 351 | 0 | LuceneUtils.appendEscapedLuceneQuery(sb, cleanText); |
| 352 | |
} else { |
| 353 | 12 | final StringTokenizer tokens = new StringTokenizer(cleanText); |
| 354 | 114 | while (tokens.hasMoreElements()) { |
| 355 | 102 | final String word = tokens.nextToken(); |
| 356 | 102 | String temp = null; |
| 357 | 102 | for (String weighted : weightedText) { |
| 358 | 225 | final String weightedStr = cleanseText(weighted); |
| 359 | 225 | if (equalsIgnoreCaseAndNonAlpha(word, weightedStr)) { |
| 360 | 22 | temp = LuceneUtils.escapeLuceneQuery(word) + WEIGHTING_BOOST; |
| 361 | 22 | if (!word.equalsIgnoreCase(weightedStr)) { |
| 362 | 0 | temp += " " + LuceneUtils.escapeLuceneQuery(weightedStr) + WEIGHTING_BOOST; |
| 363 | |
} |
| 364 | |
} |
| 365 | 225 | } |
| 366 | 102 | if (temp == null) { |
| 367 | 80 | temp = LuceneUtils.escapeLuceneQuery(word); |
| 368 | |
} |
| 369 | 102 | sb.append(" ").append(temp); |
| 370 | 102 | } |
| 371 | |
} |
| 372 | 12 | sb.append(" ) "); |
| 373 | 12 | return true; |
| 374 | |
} |
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
private String cleanseText(String text) { |
| 383 | 237 | return text.replaceAll(CLEANSE_CHARACTER_RX, " "); |
| 384 | |
} |
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
private boolean equalsIgnoreCaseAndNonAlpha(String l, String r) { |
| 394 | 225 | if (l == null || r == null) { |
| 395 | 0 | return false; |
| 396 | |
} |
| 397 | |
|
| 398 | 225 | final String left = l.replaceAll(CLEANSE_NONALPHA_RX, ""); |
| 399 | 225 | final String right = r.replaceAll(CLEANSE_NONALPHA_RX, ""); |
| 400 | 225 | return left.equalsIgnoreCase(right); |
| 401 | |
} |
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
private boolean verifyEntry(final IndexEntry entry, final Dependency dependency) { |
| 412 | 23 | boolean isValid = false; |
| 413 | |
|
| 414 | 23 | if (collectionContainsString(dependency.getProductEvidence(), entry.getProduct()) |
| 415 | |
&& collectionContainsString(dependency.getVendorEvidence(), entry.getVendor())) { |
| 416 | |
|
| 417 | 2 | isValid = true; |
| 418 | |
} |
| 419 | 23 | return isValid; |
| 420 | |
} |
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
private boolean collectionContainsString(EvidenceCollection ec, String text) { |
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | 25 | if (text == null) { |
| 443 | 0 | return false; |
| 444 | |
} |
| 445 | 25 | final String[] words = text.split("[\\s_-]"); |
| 446 | 25 | final List<String> list = new ArrayList<String>(); |
| 447 | 25 | String tempWord = null; |
| 448 | 83 | for (String word : words) { |
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | 58 | if (tempWord != null) { |
| 454 | 2 | list.add(tempWord + word); |
| 455 | 2 | tempWord = null; |
| 456 | 56 | } else if (word.length() <= 2) { |
| 457 | 2 | tempWord = word; |
| 458 | |
} else { |
| 459 | 54 | list.add(word); |
| 460 | |
} |
| 461 | |
} |
| 462 | 25 | if (tempWord != null && !list.isEmpty()) { |
| 463 | 0 | final String tmp = list.get(list.size() - 1) + tempWord; |
| 464 | 0 | list.add(tmp); |
| 465 | |
} |
| 466 | 25 | boolean contains = true; |
| 467 | 25 | for (String word : list) { |
| 468 | 56 | contains &= ec.containsUsedString(word); |
| 469 | 56 | } |
| 470 | 25 | return contains; |
| 471 | |
} |
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
@Override |
| 481 | |
public void analyze(Dependency dependency, Engine engine) throws AnalysisException { |
| 482 | |
try { |
| 483 | 2 | determineCPE(dependency); |
| 484 | 0 | } catch (CorruptIndexException ex) { |
| 485 | 0 | throw new AnalysisException("CPE Index is corrupt.", ex); |
| 486 | 0 | } catch (IOException ex) { |
| 487 | 0 | throw new AnalysisException("Failure opening the CPE Index.", ex); |
| 488 | 0 | } catch (ParseException ex) { |
| 489 | 0 | throw new AnalysisException("Unable to parse the generated Lucene query for this dependency.", ex); |
| 490 | 2 | } |
| 491 | 2 | } |
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
protected boolean determineIdentifiers(Dependency dependency, String vendor, String product, |
| 506 | |
Confidence currentConfidence) throws UnsupportedEncodingException { |
| 507 | 2 | final Set<VulnerableSoftware> cpes = cve.getCPEs(vendor, product); |
| 508 | 2 | DependencyVersion bestGuess = new DependencyVersion("-"); |
| 509 | 2 | Confidence bestGuessConf = null; |
| 510 | 2 | boolean hasBroadMatch = false; |
| 511 | 2 | final List<IdentifierMatch> collected = new ArrayList<IdentifierMatch>(); |
| 512 | 10 | for (Confidence conf : Confidence.values()) { |
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | 8 | for (Evidence evidence : dependency.getVersionEvidence().iterator(conf)) { |
| 517 | 10 | final DependencyVersion evVer = DependencyVersionUtil.parseVersion(evidence.getValue()); |
| 518 | 10 | if (evVer == null) { |
| 519 | 0 | continue; |
| 520 | |
} |
| 521 | 10 | for (VulnerableSoftware vs : cpes) { |
| 522 | |
DependencyVersion dbVer; |
| 523 | 390 | if (vs.getRevision() != null && !vs.getRevision().isEmpty()) { |
| 524 | 160 | dbVer = DependencyVersionUtil.parseVersion(vs.getVersion() + "." + vs.getRevision()); |
| 525 | |
} else { |
| 526 | 230 | dbVer = DependencyVersionUtil.parseVersion(vs.getVersion()); |
| 527 | |
} |
| 528 | 390 | if (dbVer == null) { |
| 529 | 0 | hasBroadMatch = true; |
| 530 | 0 | final String url = String.format(NVD_SEARCH_URL, URLEncoder.encode(vs.getName(), "UTF-8")); |
| 531 | 0 | final IdentifierMatch match = new IdentifierMatch("cpe", vs.getName(), url, IdentifierConfidence.BROAD_MATCH, conf); |
| 532 | 0 | collected.add(match); |
| 533 | 0 | } else if (evVer.equals(dbVer)) { |
| 534 | 10 | final String url = String.format(NVD_SEARCH_URL, URLEncoder.encode(vs.getName(), "UTF-8")); |
| 535 | 10 | final IdentifierMatch match = new IdentifierMatch("cpe", vs.getName(), url, IdentifierConfidence.EXACT_MATCH, conf); |
| 536 | 10 | collected.add(match); |
| 537 | 10 | } else { |
| 538 | |
|
| 539 | 380 | if (evVer.getVersionParts().size() <= dbVer.getVersionParts().size() |
| 540 | |
&& evVer.matchesAtLeastThreeLevels(dbVer)) { |
| 541 | 80 | if (bestGuessConf == null || bestGuessConf.compareTo(conf) > 0) { |
| 542 | 2 | if (bestGuess.getVersionParts().size() < dbVer.getVersionParts().size()) { |
| 543 | 2 | bestGuess = dbVer; |
| 544 | 2 | bestGuessConf = conf; |
| 545 | |
} |
| 546 | |
} |
| 547 | |
} |
| 548 | |
} |
| 549 | 390 | } |
| 550 | 10 | if (bestGuessConf == null || bestGuessConf.compareTo(conf) > 0) { |
| 551 | 0 | if (bestGuess.getVersionParts().size() < evVer.getVersionParts().size()) { |
| 552 | 0 | bestGuess = evVer; |
| 553 | 0 | bestGuessConf = conf; |
| 554 | |
} |
| 555 | |
} |
| 556 | 10 | } |
| 557 | |
} |
| 558 | 2 | final String cpeName = String.format("cpe:/a:%s:%s:%s", vendor, product, bestGuess.toString()); |
| 559 | 2 | String url = null; |
| 560 | 2 | if (hasBroadMatch) { |
| 561 | 0 | final String cpeUrlName = String.format("cpe:/a:%s:%s", vendor, product); |
| 562 | 0 | url = String.format(NVD_SEARCH_URL, URLEncoder.encode(cpeUrlName, "UTF-8")); |
| 563 | |
} |
| 564 | 2 | if (bestGuessConf == null) { |
| 565 | 0 | bestGuessConf = Confidence.LOW; |
| 566 | |
} |
| 567 | 2 | final IdentifierMatch match = new IdentifierMatch("cpe", cpeName, url, IdentifierConfidence.BEST_GUESS, bestGuessConf); |
| 568 | 2 | collected.add(match); |
| 569 | |
|
| 570 | 2 | Collections.sort(collected); |
| 571 | 2 | final IdentifierConfidence bestIdentifierQuality = collected.get(0).getConfidence(); |
| 572 | 2 | final Confidence bestEvidenceQuality = collected.get(0).getEvidenceConfidence(); |
| 573 | 2 | boolean identifierAdded = false; |
| 574 | 2 | for (IdentifierMatch m : collected) { |
| 575 | 12 | if (bestIdentifierQuality.equals(m.getConfidence()) |
| 576 | |
&& bestEvidenceQuality.equals(m.getEvidenceConfidence())) { |
| 577 | 4 | final Identifier i = m.getIdentifier(); |
| 578 | 4 | if (bestIdentifierQuality == IdentifierConfidence.BEST_GUESS) { |
| 579 | 0 | i.setConfidence(Confidence.LOW); |
| 580 | |
} else { |
| 581 | 4 | i.setConfidence(bestEvidenceQuality); |
| 582 | |
} |
| 583 | 4 | dependency.addIdentifier(i); |
| 584 | 4 | identifierAdded = true; |
| 585 | |
} |
| 586 | 12 | } |
| 587 | 2 | return identifierAdded; |
| 588 | |
} |
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
|
| 593 | 1 | private enum IdentifierConfidence { |
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | 1 | EXACT_MATCH, |
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | 1 | BEST_GUESS, |
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | 1 | BROAD_MATCH |
| 608 | |
} |
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
|
| 613 | 10 | private static class IdentifierMatch implements Comparable<IdentifierMatch> { |
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
|
| 624 | 12 | IdentifierMatch(String type, String value, String url, IdentifierConfidence identifierConfidence, Confidence evidenceConfidence) { |
| 625 | 12 | this.identifier = new Identifier(type, value, url); |
| 626 | 12 | this.confidence = identifierConfidence; |
| 627 | 12 | this.evidenceConfidence = evidenceConfidence; |
| 628 | 12 | } |
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
private Confidence evidenceConfidence; |
| 634 | |
|
| 635 | |
|
| 636 | |
|
| 637 | |
|
| 638 | |
|
| 639 | |
|
| 640 | |
public Confidence getEvidenceConfidence() { |
| 641 | |
return evidenceConfidence; |
| 642 | |
} |
| 643 | |
|
| 644 | |
|
| 645 | |
|
| 646 | |
|
| 647 | |
|
| 648 | |
|
| 649 | |
public void setEvidenceConfidence(Confidence evidenceConfidence) { |
| 650 | |
this.evidenceConfidence = evidenceConfidence; |
| 651 | |
} |
| 652 | |
|
| 653 | |
|
| 654 | |
|
| 655 | |
private IdentifierConfidence confidence; |
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
public IdentifierConfidence getConfidence() { |
| 663 | |
return confidence; |
| 664 | |
} |
| 665 | |
|
| 666 | |
|
| 667 | |
|
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
public void setConfidence(IdentifierConfidence confidence) { |
| 672 | |
this.confidence = confidence; |
| 673 | |
} |
| 674 | |
|
| 675 | |
|
| 676 | |
|
| 677 | |
private Identifier identifier; |
| 678 | |
|
| 679 | |
|
| 680 | |
|
| 681 | |
|
| 682 | |
|
| 683 | |
|
| 684 | |
public Identifier getIdentifier() { |
| 685 | |
return identifier; |
| 686 | |
} |
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
public void setIdentifier(Identifier identifier) { |
| 694 | |
this.identifier = identifier; |
| 695 | |
} |
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
|
| 701 | |
|
| 702 | |
|
| 703 | |
|
| 704 | |
@Override |
| 705 | |
public String toString() { |
| 706 | 0 | return "IdentifierMatch{" + "evidenceConfidence=" + evidenceConfidence |
| 707 | |
+ ", confidence=" + confidence + ", identifier=" + identifier + '}'; |
| 708 | |
} |
| 709 | |
|
| 710 | |
|
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | |
@Override |
| 716 | |
public int hashCode() { |
| 717 | 0 | int hash = 5; |
| 718 | 0 | hash = 97 * hash + (this.evidenceConfidence != null ? this.evidenceConfidence.hashCode() : 0); |
| 719 | 0 | hash = 97 * hash + (this.confidence != null ? this.confidence.hashCode() : 0); |
| 720 | 0 | hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0); |
| 721 | 0 | return hash; |
| 722 | |
} |
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | |
|
| 729 | |
|
| 730 | |
@Override |
| 731 | |
public boolean equals(Object obj) { |
| 732 | 0 | if (obj == null) { |
| 733 | 0 | return false; |
| 734 | |
} |
| 735 | 0 | if (getClass() != obj.getClass()) { |
| 736 | 0 | return false; |
| 737 | |
} |
| 738 | 0 | final IdentifierMatch other = (IdentifierMatch) obj; |
| 739 | 0 | if (this.evidenceConfidence != other.evidenceConfidence) { |
| 740 | 0 | return false; |
| 741 | |
} |
| 742 | 0 | if (this.confidence != other.confidence) { |
| 743 | 0 | return false; |
| 744 | |
} |
| 745 | 0 | if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) { |
| 746 | 0 | return false; |
| 747 | |
} |
| 748 | 0 | return true; |
| 749 | |
} |
| 750 | |
|
| 751 | |
|
| 752 | |
|
| 753 | |
|
| 754 | |
|
| 755 | |
|
| 756 | |
|
| 757 | |
|
| 758 | |
|
| 759 | |
@Override |
| 760 | |
public int compareTo(IdentifierMatch o) { |
| 761 | 10 | int conf = this.confidence.compareTo(o.confidence); |
| 762 | 10 | if (conf == 0) { |
| 763 | 8 | conf = this.evidenceConfidence.compareTo(o.evidenceConfidence); |
| 764 | 8 | if (conf == 0) { |
| 765 | 4 | conf = identifier.compareTo(o.identifier); |
| 766 | |
} |
| 767 | |
} |
| 768 | 10 | return conf; |
| 769 | |
} |
| 770 | |
} |
| 771 | |
} |