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