| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.analyzer; |
| 20 | |
|
| 21 | |
import java.io.UnsupportedEncodingException; |
| 22 | |
import java.net.URLEncoder; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.Collections; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.ListIterator; |
| 28 | |
import java.util.Set; |
| 29 | |
import java.util.logging.Level; |
| 30 | |
import java.util.logging.Logger; |
| 31 | |
import java.util.regex.Matcher; |
| 32 | |
import java.util.regex.Pattern; |
| 33 | |
import org.owasp.dependencycheck.Engine; |
| 34 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 35 | |
import org.owasp.dependencycheck.dependency.Identifier; |
| 36 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 7 | public class FalsePositiveAnalyzer extends AbstractAnalyzer { |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 1 | private static final Set<String> EXTENSIONS = null; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private static final String ANALYZER_NAME = "False Positive Analyzer"; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.POST_IDENTIFIER_ANALYSIS; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public Set<String> getSupportedExtensions() { |
| 66 | 132 | return EXTENSIONS; |
| 67 | |
} |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public String getName() { |
| 75 | 9 | return ANALYZER_NAME; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public boolean supportsExtension(String extension) { |
| 86 | 9 | return true; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public AnalysisPhase getAnalysisPhase() { |
| 95 | 6 | return ANALYSIS_PHASE; |
| 96 | |
} |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
@Override |
| 109 | |
public void analyze(Dependency dependency, Engine engine) throws AnalysisException { |
| 110 | 15 | removeJreEntries(dependency); |
| 111 | 15 | removeBadMatches(dependency); |
| 112 | 15 | removeWrongVersionMatches(dependency); |
| 113 | 15 | removeSpuriousCPE(dependency); |
| 114 | 15 | addFalseNegativeCPEs(dependency); |
| 115 | 15 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
@SuppressWarnings("null") |
| 134 | |
private void removeSpuriousCPE(Dependency dependency) { |
| 135 | 15 | final List<Identifier> ids = new ArrayList<Identifier>(); |
| 136 | 15 | ids.addAll(dependency.getIdentifiers()); |
| 137 | 15 | Collections.sort(ids); |
| 138 | 15 | final ListIterator<Identifier> mainItr = ids.listIterator(); |
| 139 | 36 | while (mainItr.hasNext()) { |
| 140 | 21 | final Identifier currentId = mainItr.next(); |
| 141 | 21 | final VulnerableSoftware currentCpe = parseCpe(currentId.getType(), currentId.getValue()); |
| 142 | 21 | if (currentCpe == null) { |
| 143 | 0 | continue; |
| 144 | |
} |
| 145 | 21 | final ListIterator<Identifier> subItr = ids.listIterator(mainItr.nextIndex()); |
| 146 | 32 | while (subItr.hasNext()) { |
| 147 | 11 | final Identifier nextId = subItr.next(); |
| 148 | 11 | final VulnerableSoftware nextCpe = parseCpe(nextId.getType(), nextId.getValue()); |
| 149 | 11 | if (nextCpe == null) { |
| 150 | 0 | continue; |
| 151 | |
} |
| 152 | |
|
| 153 | 11 | if (currentCpe.getVendor().equals(nextCpe.getVendor())) { |
| 154 | 3 | if (currentCpe.getProduct().equals(nextCpe.getProduct())) { |
| 155 | |
|
| 156 | 3 | final String currentVersion = currentCpe.getVersion(); |
| 157 | 3 | final String nextVersion = nextCpe.getVersion(); |
| 158 | 3 | if (currentVersion == null && nextVersion == null) { |
| 159 | |
|
| 160 | 0 | Logger.getLogger(FalsePositiveAnalyzer.class |
| 161 | |
.getName()).log(Level.FINE, "currentVersion and nextVersion are both null?"); |
| 162 | 3 | } else if (currentVersion == null && nextVersion != null) { |
| 163 | 3 | dependency.getIdentifiers().remove(currentId); |
| 164 | 0 | } else if (nextVersion == null && currentVersion != null) { |
| 165 | 0 | dependency.getIdentifiers().remove(nextId); |
| 166 | 0 | } else if (currentVersion.length() < nextVersion.length()) { |
| 167 | 0 | if (nextVersion.startsWith(currentVersion) || "-".equals(currentVersion)) { |
| 168 | 0 | dependency.getIdentifiers().remove(currentId); |
| 169 | |
} |
| 170 | |
} else { |
| 171 | 0 | if (currentVersion.startsWith(nextVersion) || "-".equals(nextVersion)) { |
| 172 | 0 | dependency.getIdentifiers().remove(nextId); |
| 173 | |
} |
| 174 | |
} |
| 175 | |
} |
| 176 | |
} |
| 177 | 11 | } |
| 178 | 21 | } |
| 179 | 15 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | 1 | public static final Pattern CORE_JAVA = Pattern.compile("^cpe:/a:(sun|oracle|ibm):(j2[ems]e|" |
| 185 | |
+ "java(_platfrom_micro_edition|_runtime_environment|_se|virtual_machine|se_development_kit|fx)?|" |
| 186 | |
+ "jdk|jre|jsf|jsse)($|:.*)"); |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | 1 | public static final Pattern CORE_FILES = Pattern.compile("^((alt[-])?rt|jsf[-].*|jsse|jfxrt|jfr|jce|javaws|deploy|charsets)\\.jar$"); |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
private void removeJreEntries(Dependency dependency) { |
| 199 | 15 | final Set<Identifier> identifiers = dependency.getIdentifiers(); |
| 200 | 15 | final Iterator<Identifier> itr = identifiers.iterator(); |
| 201 | 37 | while (itr.hasNext()) { |
| 202 | 22 | final Identifier i = itr.next(); |
| 203 | 22 | final Matcher coreCPE = CORE_JAVA.matcher(i.getValue()); |
| 204 | 22 | final Matcher coreFiles = CORE_FILES.matcher(dependency.getFileName()); |
| 205 | 22 | if (coreCPE.matches() && !coreFiles.matches()) { |
| 206 | 0 | itr.remove(); |
| 207 | |
} |
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | 22 | } |
| 230 | 15 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
private VulnerableSoftware parseCpe(String type, String value) { |
| 240 | 32 | if (!"cpe".equals(type)) { |
| 241 | 0 | return null; |
| 242 | |
} |
| 243 | 32 | final VulnerableSoftware cpe = new VulnerableSoftware(); |
| 244 | |
try { |
| 245 | 32 | cpe.parseName(value); |
| 246 | 0 | } catch (UnsupportedEncodingException ex) { |
| 247 | 0 | Logger.getLogger(FalsePositiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 248 | 0 | return null; |
| 249 | 32 | } |
| 250 | 32 | return cpe; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
private void removeBadMatches(Dependency dependency) { |
| 261 | 15 | final Set<Identifier> identifiers = dependency.getIdentifiers(); |
| 262 | 15 | final Iterator<Identifier> itr = identifiers.iterator(); |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | 37 | while (itr.hasNext()) { |
| 274 | 22 | final Identifier i = itr.next(); |
| 275 | |
|
| 276 | 22 | if ("cpe".equals(i.getType())) { |
| 277 | 22 | if ((i.getValue().matches(".*c\\+\\+.*") |
| 278 | |
|| i.getValue().startsWith("cpe:/a:jquery:jquery") |
| 279 | |
|| i.getValue().startsWith("cpe:/a:prototypejs:prototype") |
| 280 | |
|| i.getValue().startsWith("cpe:/a:yahoo:yui") |
| 281 | |
|| i.getValue().startsWith("cpe:/a:file:file") |
| 282 | |
|| i.getValue().startsWith("cpe:/a:mozilla:mozilla") |
| 283 | |
|| i.getValue().startsWith("cpe:/a:cvs:cvs") |
| 284 | |
|| i.getValue().startsWith("cpe:/a:ftp:ftp") |
| 285 | |
|| i.getValue().startsWith("cpe:/a:ssh:ssh")) |
| 286 | |
&& dependency.getFileName().toLowerCase().endsWith(".jar")) { |
| 287 | 0 | itr.remove(); |
| 288 | 22 | } else if (i.getValue().startsWith("cpe:/a:apache:maven") |
| 289 | |
&& !dependency.getFileName().toLowerCase().matches("maven-core-[\\d\\.]+\\.jar")) { |
| 290 | 0 | itr.remove(); |
| 291 | |
} |
| 292 | |
} |
| 293 | 22 | } |
| 294 | 15 | } |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
private void removeWrongVersionMatches(Dependency dependency) { |
| 303 | 15 | final Set<Identifier> identifiers = dependency.getIdentifiers(); |
| 304 | 15 | final Iterator<Identifier> itr = identifiers.iterator(); |
| 305 | |
|
| 306 | 15 | final String fileName = dependency.getFileName(); |
| 307 | 15 | if (fileName != null && fileName.contains("axis2")) { |
| 308 | 3 | while (itr.hasNext()) { |
| 309 | 2 | final Identifier i = itr.next(); |
| 310 | 2 | if ("cpe".equals(i.getType())) { |
| 311 | 2 | final String cpe = i.getValue(); |
| 312 | 2 | if (cpe != null && (cpe.startsWith("cpe:/a:apache:axis:") || "cpe:/a:apache:axis".equals(cpe))) { |
| 313 | 1 | itr.remove(); |
| 314 | |
} |
| 315 | |
} |
| 316 | 2 | } |
| 317 | 14 | } else if (fileName != null && fileName.contains("axis")) { |
| 318 | 0 | while (itr.hasNext()) { |
| 319 | 0 | final Identifier i = itr.next(); |
| 320 | 0 | if ("cpe".equals(i.getType())) { |
| 321 | 0 | final String cpe = i.getValue(); |
| 322 | 0 | if (cpe != null && (cpe.startsWith("cpe:/a:apache:axis2:") || "cpe:/a:apache:axis2".equals(cpe))) { |
| 323 | 0 | itr.remove(); |
| 324 | |
} |
| 325 | |
} |
| 326 | 0 | } |
| 327 | |
} |
| 328 | 15 | } |
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
private void addFalseNegativeCPEs(Dependency dependency) { |
| 339 | 15 | final Iterator<Identifier> itr = dependency.getIdentifiers().iterator(); |
| 340 | 33 | while (itr.hasNext()) { |
| 341 | 18 | final Identifier i = itr.next(); |
| 342 | 18 | if ("cpe".equals(i.getType()) && i.getValue() != null |
| 343 | |
&& (i.getValue().startsWith("cpe:/a:oracle:opensso:") |
| 344 | |
|| i.getValue().startsWith("cpe:/a:oracle:opensso_enterprise:") |
| 345 | |
|| i.getValue().startsWith("cpe:/a:sun:opensso_enterprise:") |
| 346 | |
|| i.getValue().startsWith("cpe:/a:sun:opensso:"))) { |
| 347 | 0 | final String newCpe = String.format("cpe:/a:sun:opensso_enterprise:%s", i.getValue().substring(22)); |
| 348 | 0 | final String newCpe2 = String.format("cpe:/a:oracle:opensso_enterprise:%s", i.getValue().substring(22)); |
| 349 | 0 | final String newCpe3 = String.format("cpe:/a:sun:opensso:%s", i.getValue().substring(22)); |
| 350 | 0 | final String newCpe4 = String.format("cpe:/a:oracle:opensso:%s", i.getValue().substring(22)); |
| 351 | |
try { |
| 352 | 0 | dependency.addIdentifier("cpe", |
| 353 | |
newCpe, |
| 354 | |
String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe, "UTF-8"))); |
| 355 | 0 | dependency.addIdentifier("cpe", |
| 356 | |
newCpe2, |
| 357 | |
String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe2, "UTF-8"))); |
| 358 | 0 | dependency.addIdentifier("cpe", |
| 359 | |
newCpe3, |
| 360 | |
String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe3, "UTF-8"))); |
| 361 | 0 | dependency.addIdentifier("cpe", |
| 362 | |
newCpe4, |
| 363 | |
String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe4, "UTF-8"))); |
| 364 | 0 | } catch (UnsupportedEncodingException ex) { |
| 365 | 0 | Logger.getLogger(FalsePositiveAnalyzer.class |
| 366 | |
.getName()).log(Level.FINE, null, ex); |
| 367 | 0 | } |
| 368 | |
} |
| 369 | 18 | } |
| 370 | 15 | } |
| 371 | |
} |