Coverage Report - org.owasp.dependencycheck.analyzer.FalsePositiveAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
FalsePositiveAnalyzer
57%
67/116
33%
41/122
8.222
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 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  
  * This analyzer attempts to remove some well known false positives - specifically regarding the java runtime.
 40  
  *
 41  
  * @author Jeremy Long <jeremy.long@owasp.org>
 42  
  */
 43  
 public class FalsePositiveAnalyzer extends AbstractAnalyzer {
 44  
 
 45  
     /**
 46  
      * The Logger.
 47  
      */
 48  1
     private static final Logger LOGGER = Logger.getLogger(FalsePositiveAnalyzer.class.getName());
 49  
     //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer">
 50  
     /**
 51  
      * The name of the analyzer.
 52  
      */
 53  
     private static final String ANALYZER_NAME = "False Positive Analyzer";
 54  
     /**
 55  
      * The phase that this analyzer is intended to run in.
 56  
      */
 57  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.POST_IDENTIFIER_ANALYSIS;
 58  
 
 59  
     /**
 60  
      * Returns the name of the analyzer.
 61  
      *
 62  
      * @return the name of the analyzer.
 63  
      */
 64  
     public String getName() {
 65  5
         return ANALYZER_NAME;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns the phase that the analyzer is intended to run in.
 70  
      *
 71  
      * @return the phase that the analyzer is intended to run in.
 72  
      */
 73  
     public AnalysisPhase getAnalysisPhase() {
 74  2
         return ANALYSIS_PHASE;
 75  
     }
 76  
     //</editor-fold>
 77  
 
 78  
     /**
 79  
      * Analyzes the dependencies and removes bad/incorrect CPE associations based on various heuristics.
 80  
      *
 81  
      * @param dependency the dependency to analyze.
 82  
      * @param engine the engine that is scanning the dependencies
 83  
      * @throws AnalysisException is thrown if there is an error reading the JAR file.
 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  
      * <p>
 96  
      * Intended to remove spurious CPE entries. By spurious we mean duplicate, less specific CPE entries.</p>
 97  
      * <p>
 98  
      * Example:</p>
 99  
      * <code>
 100  
      * cpe:/a:some-vendor:some-product
 101  
      * cpe:/a:some-vendor:some-product:1.5
 102  
      * cpe:/a:some-vendor:some-product:1.5.2
 103  
      * </code>
 104  
      * <p>
 105  
      * Should be trimmed to:</p>
 106  
      * <code>
 107  
      * cpe:/a:some-vendor:some-product:1.5.2
 108  
      * </code>
 109  
      *
 110  
      * @param dependency the dependency being analyzed
 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  
                 //TODO fix the version problem below
 132  1
                 if (currentCpe.getVendor().equals(nextCpe.getVendor())) {
 133  0
                     if (currentCpe.getProduct().equals(nextCpe.getProduct())) {
 134  
                         // see if one is contained in the other.. remove the contained one from dependency.getIdentifier
 135  0
                         final String currentVersion = currentCpe.getVersion();
 136  0
                         final String nextVersion = nextCpe.getVersion();
 137  0
                         if (currentVersion == null && nextVersion == null) {
 138  
                             //how did we get here?
 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  
      * Regex to identify core java libraries and a few other commonly misidentified ones.
 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  
      * Regex to identify core java library files. This is currently incomplete.
 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  
      * Removes any CPE entries for the JDK/JRE unless the filename ends with rt.jar
 171  
      *
 172  
      * @param dependency the dependency to remove JRE CPEs from
 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  
             //replaced with the regex above.
 186  
             //            if (("cpe:/a:sun:java".equals(i.getValue())
 187  
             //                    || "cpe:/a:oracle:java".equals(i.getValue())
 188  
             //                    || "cpe:/a:ibm:java".equals(i.getValue())
 189  
             //                    || "cpe:/a:sun:j2se".equals(i.getValue())
 190  
             //                    || "cpe:/a:oracle:j2se".equals(i.getValue())
 191  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:")
 192  
             //                    || i.getValue().startsWith("cpe:/a:sun:j2se:")
 193  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:jre")
 194  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:jdk")
 195  
             //                    || i.getValue().startsWith("cpe:/a:sun:java_se")
 196  
             //                    || i.getValue().startsWith("cpe:/a:oracle:java_se")
 197  
             //                    || i.getValue().startsWith("cpe:/a:oracle:java:")
 198  
             //                    || i.getValue().startsWith("cpe:/a:oracle:j2se:")
 199  
             //                    || i.getValue().startsWith("cpe:/a:oracle:jre")
 200  
             //                    || i.getValue().startsWith("cpe:/a:oracle:jdk")
 201  
             //                    || i.getValue().startsWith("cpe:/a:ibm:java:"))
 202  
             //                    && !dependency.getFileName().toLowerCase().endsWith("rt.jar")) {
 203  
             //                itr.remove();
 204  
             //            }
 205  3
         }
 206  3
     }
 207  
 
 208  
     /**
 209  
      * Parses a CPE string into an IndexEntry.
 210  
      *
 211  
      * @param type the type of identifier
 212  
      * @param value the cpe identifier to parse
 213  
      * @return an VulnerableSoftware object constructed from the identifier
 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  
      * Removes bad CPE matches for a dependency. Unfortunately, right now these are hard-coded patches for specific
 231  
      * problems identified when testing this on a LARGE volume of jar files.
 232  
      *
 233  
      * @param dependency the dependency to analyze
 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  
         /* TODO - can we utilize the pom's groupid and artifactId to filter??? most of
 240  
          * these are due to low quality data.  Other idea would be to say any CPE
 241  
          * found based on LOW confidence evidence should have a different CPE type? (this
 242  
          * might be a better solution then just removing the URL for "best-guess" matches).
 243  
          */
 244  
         //Set<Evidence> groupId = dependency.getVendorEvidence().getEvidence("pom", "groupid");
 245  
         //Set<Evidence> artifactId = dependency.getVendorEvidence().getEvidence("pom", "artifactid");
 246  6
         while (itr.hasNext()) {
 247  3
             final Identifier i = itr.next();
 248  
             //TODO move this startsWith expression to a configuration file?
 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  
      * Removes CPE matches for the wrong version of a dependency. Currently, this only covers Axis 1 & 2.
 278  
      *
 279  
      * @param dependency the dependency to analyze
 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  
      * There are some known CPE entries, specifically regarding sun and oracle products due to the acquisition and
 311  
      * changes in product names, that based on given evidence we can add the related CPE entries to ensure a complete
 312  
      * list of CVE entries.
 313  
      *
 314  
      * @param dependency the dependency being analyzed
 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  
 }