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