Coverage Report - org.owasp.dependencycheck.analyzer.FalsePositiveAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
FalsePositiveAnalyzer
62%
58/93
38%
29/76
5.2
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 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  
  * This analyzer attempts to remove some well known false positives -
 40  
  * specifically regarding the java runtime.
 41  
  *
 42  
  * @author Jeremy Long (jeremy.long@owasp.org)
 43  
  */
 44  7
 public class FalsePositiveAnalyzer extends AbstractAnalyzer {
 45  
 
 46  
     //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
 47  
     /**
 48  
      * The set of file extensions supported by this analyzer.
 49  
      */
 50  1
     private static final Set<String> EXTENSIONS = null;
 51  
     /**
 52  
      * The name of the analyzer.
 53  
      */
 54  
     private static final String ANALYZER_NAME = "False Positive Analyzer";
 55  
     /**
 56  
      * The phase that this analyzer is intended to run in.
 57  
      */
 58  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.POST_IDENTIFIER_ANALYSIS;
 59  
 
 60  
     /**
 61  
      * Returns a list of file EXTENSIONS supported by this analyzer.
 62  
      *
 63  
      * @return a list of file EXTENSIONS supported by this analyzer.
 64  
      */
 65  
     public Set<String> getSupportedExtensions() {
 66  128
         return EXTENSIONS;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Returns the name of the analyzer.
 71  
      *
 72  
      * @return the name of the analyzer.
 73  
      */
 74  
     public String getName() {
 75  0
         return ANALYZER_NAME;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns whether or not this analyzer can process the given extension.
 80  
      *
 81  
      * @param extension the file extension to test for support
 82  
      * @return whether or not the specified file extension is supported by this
 83  
      * analyzer.
 84  
      */
 85  
     public boolean supportsExtension(String extension) {
 86  0
         return true;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Returns the phase that the analyzer is intended to run in.
 91  
      *
 92  
      * @return the phase that the analyzer is intended to run in.
 93  
      */
 94  
     public AnalysisPhase getAnalysisPhase() {
 95  2
         return ANALYSIS_PHASE;
 96  
     }
 97  
     //</editor-fold>
 98  
 
 99  
     /**
 100  
      * Analyzes the dependencies and removes bad/incorrect CPE associations
 101  
      * based on various heuristics.
 102  
      *
 103  
      * @param dependency the dependency to analyze.
 104  
      * @param engine the engine that is scanning the dependencies
 105  
      * @throws AnalysisException is thrown if there is an error reading the JAR
 106  
      * file.
 107  
      */
 108  
     @Override
 109  
     public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 110  6
         removeJreEntries(dependency);
 111  6
         removeBadMatches(dependency);
 112  6
         removeSpuriousCPE(dependency);
 113  6
         addFalseNegativeCPEs(dependency);
 114  6
     }
 115  
 
 116  
     /**
 117  
      * <p>Intended to remove spurious CPE entries. By spurious we mean
 118  
      * duplicate, less specific CPE entries.</p>
 119  
      * <p>Example:</p>
 120  
      * <code>
 121  
      * cpe:/a:some-vendor:some-product
 122  
      * cpe:/a:some-vendor:some-product:1.5
 123  
      * cpe:/a:some-vendor:some-product:1.5.2
 124  
      * </code>
 125  
      * <p>Should be trimmed to:</p>
 126  
      * <code>
 127  
      * cpe:/a:some-vendor:some-product:1.5.2
 128  
      * </code>
 129  
      *
 130  
      * @param dependency the dependency being analyzed
 131  
      */
 132  
     private void removeSpuriousCPE(Dependency dependency) {
 133  6
         final List<Identifier> ids = new ArrayList<Identifier>();
 134  6
         ids.addAll(dependency.getIdentifiers());
 135  6
         Collections.sort(ids);
 136  6
         final ListIterator<Identifier> mainItr = ids.listIterator();
 137  13
         while (mainItr.hasNext()) {
 138  7
             final Identifier currentId = mainItr.next();
 139  7
             final VulnerableSoftware currentCpe = parseCpe(currentId.getType(), currentId.getValue());
 140  7
             if (currentCpe == null) {
 141  0
                 continue;
 142  
             }
 143  7
             final ListIterator<Identifier> subItr = ids.listIterator(mainItr.nextIndex());
 144  12
             while (subItr.hasNext()) {
 145  5
                 final Identifier nextId = subItr.next();
 146  5
                 final VulnerableSoftware nextCpe = parseCpe(nextId.getType(), nextId.getValue());
 147  5
                 if (nextCpe == null) {
 148  0
                     continue;
 149  
                 }
 150  
                 //TODO fix the version problem below
 151  5
                 if (currentCpe.getVendor().equals(nextCpe.getVendor())) {
 152  0
                     if (currentCpe.getProduct().equals(nextCpe.getProduct())) {
 153  
                         // see if one is contained in the other.. remove the contained one from dependency.getIdentifier
 154  0
                         final String currentVersion = currentCpe.getVersion();
 155  0
                         final String nextVersion = nextCpe.getVersion();
 156  0
                         if (currentVersion == null && nextVersion == null) {
 157  
                             //how did we get here?
 158  0
                         } else if (currentVersion == null && nextVersion != null) {
 159  0
                             dependency.getIdentifiers().remove(currentId);
 160  0
                         } else if (nextVersion == null && currentVersion != null) {
 161  0
                             dependency.getIdentifiers().remove(nextId);
 162  0
                         } else if (currentVersion.length() < nextVersion.length()) {
 163  0
                             if (nextVersion.startsWith(currentVersion) || "-".equals(currentVersion)) {
 164  0
                                 dependency.getIdentifiers().remove(currentId);
 165  
                             }
 166  
                         } else {
 167  0
                             if (currentVersion.startsWith(nextVersion) || "-".equals(nextVersion)) {
 168  0
                                 dependency.getIdentifiers().remove(nextId);
 169  
                             }
 170  
                         }
 171  
                     }
 172  
                 }
 173  5
             }
 174  7
         }
 175  6
     }
 176  
     /**
 177  
      * Regex to identify core java libraries and a few other commonly
 178  
      * misidentified ones.
 179  
      */
 180  1
     public static final Pattern CORE_JAVA = Pattern.compile("^cpe:/a:(sun|oracle|ibm):(j2[ems]e|"
 181  
             + "java(_platfrom_micro_edition|_runtime_environment|_se|virtual_machine|se_development_kit|fx)?|"
 182  
             + "jdk|jre|jsf|jsse)($|:.*)");
 183  
     /**
 184  
      * Regex to identify core java library files. This is currently incomplete.
 185  
      */
 186  1
     public static final Pattern CORE_FILES = Pattern.compile("^((alt[-])?rt|jsf[-].*|jsse|jfxrt|jfr|jce|javaws|deploy|charsets)\\.jar$");
 187  
 
 188  
     /**
 189  
      * Removes any CPE entries for the JDK/JRE unless the filename ends with
 190  
      * rt.jar
 191  
      *
 192  
      * @param dependency the dependency to remove JRE CPEs from
 193  
      */
 194  
     private void removeJreEntries(Dependency dependency) {
 195  6
         final Set<Identifier> identifiers = dependency.getIdentifiers();
 196  6
         final Iterator<Identifier> itr = identifiers.iterator();
 197  13
         while (itr.hasNext()) {
 198  7
             final Identifier i = itr.next();
 199  7
             final Matcher coreCPE = CORE_JAVA.matcher(i.getValue());
 200  7
             final Matcher coreFiles = CORE_FILES.matcher(dependency.getFileName());
 201  7
             if (coreCPE.matches() && !coreFiles.matches()) {
 202  0
                 itr.remove();
 203  
             }
 204  
 
 205  
             //replacecd with the regex above.
 206  
             //            if (("cpe:/a:sun:java".equals(i.getValue())
 207  
             //                    || "cpe:/a:oracle:java".equals(i.getValue())
 208  
             //                    || "cpe:/a:ibm:java".equals(i.getValue())
 209  
             //                    || "cpe:/a:sun:j2se".equals(i.getValue())
 210  
             //                    || "cpe:/a:oracle:j2se".equals(i.getValue())
 211  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:")
 212  
             //                    || i.getValue().startsWith("cpe:/a:sun:j2se:")
 213  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:jre")
 214  
             //                    || i.getValue().startsWith("cpe:/a:sun:java:jdk")
 215  
             //                    || i.getValue().startsWith("cpe:/a:sun:java_se")
 216  
             //                    || i.getValue().startsWith("cpe:/a:oracle:java_se")
 217  
             //                    || i.getValue().startsWith("cpe:/a:oracle:java:")
 218  
             //                    || i.getValue().startsWith("cpe:/a:oracle:j2se:")
 219  
             //                    || i.getValue().startsWith("cpe:/a:oracle:jre")
 220  
             //                    || i.getValue().startsWith("cpe:/a:oracle:jdk")
 221  
             //                    || i.getValue().startsWith("cpe:/a:ibm:java:"))
 222  
             //                    && !dependency.getFileName().toLowerCase().endsWith("rt.jar")) {
 223  
             //                itr.remove();
 224  
             //            }
 225  7
         }
 226  6
     }
 227  
 
 228  
     /**
 229  
      * Parses a CPE string into an IndexEntry.
 230  
      *
 231  
      * @param type the type of identifier
 232  
      * @param value the cpe identifier to parse
 233  
      * @return an VulnerableSoftware object constructed from the identifier
 234  
      */
 235  
     private VulnerableSoftware parseCpe(String type, String value) {
 236  12
         if (!"cpe".equals(type)) {
 237  0
             return null;
 238  
         }
 239  12
         final VulnerableSoftware cpe = new VulnerableSoftware();
 240  
         try {
 241  12
             cpe.parseName(value);
 242  0
         } catch (UnsupportedEncodingException ex) {
 243  0
             Logger.getLogger(FalsePositiveAnalyzer.class.getName()).log(Level.FINEST, null, ex);
 244  0
             return null;
 245  12
         }
 246  12
         return cpe;
 247  
     }
 248  
 
 249  
     /**
 250  
      * Removes bad CPE matches for a dependency. Unfortunately, right now these
 251  
      * are hard-coded patches for specific problems identified when testing this
 252  
      * on a LARGE volume of jar files.
 253  
      *
 254  
      * @param dependency the dependency to analyze
 255  
      */
 256  
     private void removeBadMatches(Dependency dependency) {
 257  6
         final Set<Identifier> identifiers = dependency.getIdentifiers();
 258  6
         final Iterator<Identifier> itr = identifiers.iterator();
 259  
 
 260  
         /* TODO - can we utilize the pom's groupid and artifactId to filter??? most of
 261  
          * these are due to low quality data.  Other idea would be to say any CPE
 262  
          * found based on LOW confidence evidence should have a different CPE type? (this
 263  
          * might be a better solution then just removing the URL for "best-guess" matches).
 264  
          */
 265  
 
 266  
         //Set<Evidence> groupId = dependency.getVendorEvidence().getEvidence("pom", "groupid");
 267  
         //Set<Evidence> artifactId = dependency.getVendorEvidence().getEvidence("pom", "artifactid");
 268  
 
 269  13
         while (itr.hasNext()) {
 270  7
             final Identifier i = itr.next();
 271  
             //TODO move this startswith expression to a configuration file?
 272  7
             if ("cpe".equals(i.getType())) {
 273  7
                 if ((i.getValue().matches(".*c\\+\\+.*")
 274  
                         || i.getValue().startsWith("cpe:/a:jquery:jquery")
 275  
                         || i.getValue().startsWith("cpe:/a:prototypejs:prototype")
 276  
                         || i.getValue().startsWith("cpe:/a:yahoo:yui"))
 277  
                         && dependency.getFileName().toLowerCase().endsWith(".jar")) {
 278  0
                     itr.remove();
 279  7
                 } else if (i.getValue().startsWith("cpe:/a:file:file")
 280  
                         || i.getValue().startsWith("cpe:/a:mozilla:mozilla")
 281  
                         || i.getValue().startsWith("cpe:/a:ssh:ssh")) {
 282  0
                     itr.remove();
 283  
                 }
 284  
             }
 285  7
         }
 286  6
     }
 287  
 
 288  
     /**
 289  
      * There are some known CPE entries, specifically regarding sun and oracle
 290  
      * products due to the acquisition and changes in product names, that based
 291  
      * on given evidence we can add the related CPE entries to ensure a complete
 292  
      * list of CVE entries.
 293  
      *
 294  
      * @param dependency the dependency being analyzed
 295  
      */
 296  
     private void addFalseNegativeCPEs(Dependency dependency) {
 297  6
         final Iterator<Identifier> itr = dependency.getIdentifiers().iterator();
 298  13
         while (itr.hasNext()) {
 299  7
             final Identifier i = itr.next();
 300  7
             if ("cpe".equals(i.getType()) && i.getValue() != null
 301  
                     && (i.getValue().startsWith("cpe:/a:oracle:opensso:")
 302  
                     || i.getValue().startsWith("cpe:/a:oracle:opensso_enterprise:")
 303  
                     || i.getValue().startsWith("cpe:/a:sun:opensso_enterprise:")
 304  
                     || i.getValue().startsWith("cpe:/a:sun:opensso:"))) {
 305  0
                 final String newCpe = String.format("cpe:/a:sun:opensso_enterprise:%s", i.getValue().substring(22));
 306  0
                 final String newCpe2 = String.format("cpe:/a:oracle:opensso_enterprise:%s", i.getValue().substring(22));
 307  0
                 final String newCpe3 = String.format("cpe:/a:sun:opensso:%s", i.getValue().substring(22));
 308  0
                 final String newCpe4 = String.format("cpe:/a:oracle:opensso:%s", i.getValue().substring(22));
 309  
                 try {
 310  0
                     dependency.addIdentifier("cpe",
 311  
                             newCpe,
 312  
                             String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe, "UTF-8")));
 313  0
                     dependency.addIdentifier("cpe",
 314  
                             newCpe2,
 315  
                             String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe2, "UTF-8")));
 316  0
                     dependency.addIdentifier("cpe",
 317  
                             newCpe3,
 318  
                             String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe3, "UTF-8")));
 319  0
                     dependency.addIdentifier("cpe",
 320  
                             newCpe4,
 321  
                             String.format("http://web.nvd.nist.gov/view/vuln/search?cpe=%s", URLEncoder.encode(newCpe4, "UTF-8")));
 322  0
                 } catch (UnsupportedEncodingException ex) {
 323  0
                     Logger.getLogger(FalsePositiveAnalyzer.class
 324  
                             .getName()).log(Level.FINE, null, ex);
 325  0
                 }
 326  
             }
 327  7
         }
 328  6
     }
 329  
 }