View Javadoc
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) 2022 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.update;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.sql.SQLException;
24  import org.owasp.dependencycheck.Engine;
25  import org.owasp.dependencycheck.data.knownexploited.json.KnownExploitedVulnerabilitiesSchema;
26  import org.owasp.dependencycheck.data.nvdcve.CveDB;
27  import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
28  import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
29  import org.owasp.dependencycheck.data.update.cisa.KnownExploitedVulnerabilityParser;
30  import org.owasp.dependencycheck.data.update.exception.CorruptedDatastreamException;
31  import org.owasp.dependencycheck.data.update.exception.UpdateException;
32  import org.owasp.dependencycheck.utils.DateUtil;
33  import org.owasp.dependencycheck.utils.HttpResourceConnection;
34  import org.owasp.dependencycheck.utils.ResourceNotFoundException;
35  import org.owasp.dependencycheck.utils.Settings;
36  import org.owasp.dependencycheck.utils.TooManyRequestsException;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   *
42   * @author jeremy
43   */
44  public class KnownExploitedDataSource implements CachedWebDataSource {
45  
46      /**
47       * The logger.
48       */
49      private static final Logger LOGGER = LoggerFactory.getLogger(KnownExploitedDataSource.class);
50      /**
51       * The default URL for the CISA Known Exploited Vulnerability Catalog.
52       */
53      private static final String DEFAULT_URL = "https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json";
54      /**
55       * A reference to the CVE DB.
56       */
57      private CveDB cveDB;
58      /**
59       * A reference to the ODC settings.
60       */
61      private Settings settings;
62      /**
63       * The properties obtained from the database.
64       */
65      private DatabaseProperties dbProperties = null;
66  
67      @Override
68      public boolean update(Engine engine) throws UpdateException {
69          this.cveDB = engine.getDatabase();
70          this.settings = engine.getSettings();
71          dbProperties = cveDB.getDatabaseProperties();
72          final boolean autoUpdate = settings.getBoolean(Settings.KEYS.AUTO_UPDATE, true);
73          final boolean kevEnabled = settings.getBoolean(Settings.KEYS.ANALYZER_KNOWN_EXPLOITED_ENABLED, true);
74          if (autoUpdate && kevEnabled && shouldUpdate()) {
75              try {
76                  final URL url = new URL(settings.getString(Settings.KEYS.KEV_URL, DEFAULT_URL));
77                  LOGGER.info("Updating CISA Known Exploited Vulnerability list: " + url.toString());
78                  //TODO - add all the proxy config, likely use the same as configured for NVD
79                  final HttpResourceConnection conn = new HttpResourceConnection(settings);
80                  try (InputStream in = conn.fetch(url)) {
81                      final KnownExploitedVulnerabilityParser parser = new KnownExploitedVulnerabilityParser();
82                      final KnownExploitedVulnerabilitiesSchema data = parser.parse(in);
83                      final String currentVersion = dbProperties.getProperty(DatabaseProperties.KEV_VERSION, "");
84                      if (!currentVersion.equals(data.getCatalogVersion())) {
85                          cveDB.updateKnownExploitedVulnerabilities(data.getVulnerabilities());
86                      }
87                      //all dates in the db are now stored in seconds as opposed to previously milliseconds.
88                      dbProperties.save(DatabaseProperties.KEV_LAST_CHECKED, Long.toString(System.currentTimeMillis() / 1000));
89                      return true;
90                  }
91              } catch (TooManyRequestsException | ResourceNotFoundException | IOException
92                      | CorruptedDatastreamException | DatabaseException | SQLException ex) {
93                  throw new UpdateException(ex);
94              }
95          }
96          return false;
97      }
98  
99      @Override
100     public boolean purge(Engine engine) {
101         //do nothing - covered by the NvdApiDataSource.
102         return true;
103     }
104 
105     /**
106      * Checks if the Known Exploited Vulnerabilities (KEV) were last checked
107      * recently. As an optimization, we can avoid repetitive checks against the
108      * KEV. Setting KEV_CHECK_VALID_FOR_HOURS determines the duration since last
109      * check before checking again. A database property stores the timestamp of
110      * the last check.
111      *
112      * @return true to proceed with the check, or false to skip
113      * @throws UpdateException thrown when there is an issue checking for
114      * updates
115      */
116     private boolean shouldUpdate() throws UpdateException {
117         boolean proceed = true;
118         // If the valid setting has not been specified, then we proceed to check...
119         final int validForHours = settings.getInt(Settings.KEYS.KEV_CHECK_VALID_FOR_HOURS, 24);
120         if (cveDB.dataExists() && 0 < validForHours) {
121             // ms Valid = valid (hours) x 60 min/hour x 60 sec/min x 1000 ms/sec
122             final long validForSeconds = validForHours * 60L * 60L;
123             final long lastChecked = dbProperties.getPropertyInSeconds(DatabaseProperties.KEV_LAST_CHECKED);
124             final long now = System.currentTimeMillis() / 1000;
125             proceed = (now - lastChecked) > validForSeconds;
126             if (!proceed) {
127                 LOGGER.info("Skipping Known Exploited Vulnerabilities update check since last check was within {} hours.", validForHours);
128             }
129         }
130         return proceed;
131     }
132 
133 }