예제 #1
0
파일: cve.py 프로젝트: vxsh4d0w/connectors
 def convert_and_send(self, url, work_id):
     try:
         # Downloading json.gz file
         self.helper.log_info("Requesting the file " + url)
         response = urllib.request.urlopen(
             url,
             context=ssl.create_default_context(cafile=certifi.where()))
         image = response.read()
         with open(
                 os.path.dirname(os.path.abspath(__file__)) +
                 "/data.json.gz", "wb") as file:
             file.write(image)
         # Unzipping the file
         self.helper.log_info("Unzipping the file")
         with gzip.open("data.json.gz", "rb") as f_in:
             with open("data.json", "wb") as f_out:
                 shutil.copyfileobj(f_in, f_out)
         # Converting the file to stix2
         self.helper.log_info("Converting the file")
         convert("data.json", "data-stix2.json")
         with open("data-stix2.json") as stix_json:
             contents = stix_json.read()
             self.helper.send_stix2_bundle(
                 contents,
                 entities_types=self.helper.connect_scope,
                 update=self.update_existing_data,
                 work_id=work_id,
             )
         # Remove files
         self.delete_files()
     except Exception as e:
         self.delete_files()
         self.helper.log_error(str(e))
         time.sleep(60)
예제 #2
0
 def convert_and_send(self, url):
     try:
         # Downloading json.gz file
         self.helper.log_info("Requesting the file " + url)
         urllib.request.urlretrieve(
             self.cve_nvd_data_feed,
             os.path.dirname(os.path.abspath(__file__)) + "/data.json.gz",
         )
         # Unzipping the file
         self.helper.log_info("Unzipping the file")
         with gzip.open("data.json.gz", "rb") as f_in:
             with open("data.json", "wb") as f_out:
                 shutil.copyfileobj(f_in, f_out)
         # Converting the file to stix2
         self.helper.log_info("Converting the file")
         convert("data.json", "data-stix2.json")
         with open("data-stix2.json") as stix_json:
             contents = stix_json.read()
             self.helper.send_stix2_bundle(contents,
                                           self.helper.connect_scope,
                                           self.update_existing_data)
         # Remove files
         os.remove("data.json")
         os.remove("data.json.gz")
         os.remove("data-stix2.json")
     except Exception as e:
         self.helper.log_error(str(e))
         time.sleep(60)
예제 #3
0
    def run(self):
        self.helper.log_info('Fetching CVE knowledge...')
        while True:
            try:
                # Get the current timestamp and check
                timestamp = int(time.time())
                current_state = self.helper.get_state()
                if current_state is not None and 'last_run' in current_state:
                    last_run = current_state['last_run']
                    self.helper.log_info(
                        'Connector last run: ' + datetime.utcfromtimestamp(last_run).strftime('%Y-%m-%d %H:%M:%S')
                    )
                else:
                    last_run = None
                    self.helper.log_info('Connector has never run')
                # If the last_run is more than interval-1 day
                if last_run is None or ((timestamp - last_run) > ((int(self.cve_interval) - 1) * 60 * 60 * 24)):
                    # Downloading json.gz file
                    self.helper.log_info('Requesting the file')
                    urllib.request.urlretrieve(
                        self.cve_nvd_data_feed,
                        os.path.dirname(os.path.abspath(__file__)) + '/data.json.gz'
                    )
                    # Unzipping the file
                    self.helper.log_info('Unzipping the file')
                    with gzip.open('data.json.gz', 'rb') as f_in:
                        with open('data.json', 'wb') as f_out:
                            shutil.copyfileobj(f_in, f_out)
                    # Converting the file to stix2
                    self.helper.log_info('Converting the file')
                    convert('data.json', 'data-stix2.json')
                    with open('data-stix2.json') as stix_json:
                        contents = stix_json.read()
                        self.helper.send_stix2_bundle(contents, self.helper.connect_scope, self.update_existing_data)

                    # Remove files
                    os.remove('data.json')
                    os.remove('data.json.gz')
                    os.remove('data-stix2.json')
                    # Store the current timestamp as a last run
                    self.helper.log_info('Connector successfully run, storing last_run as ' + str(timestamp))
                    self.helper.set_state({'last_run': timestamp})
                    self.helper.log_info(
                        'Last_run stored, next run in: ' + str(round(self.get_interval() / 60 / 60 / 24, 2)) + ' days'
                    )
                    time.sleep(60)
                else:
                    new_interval = self.get_interval() - (timestamp - last_run)
                    self.helper.log_info(
                        'Connector will not run, next run in: ' + str(round(new_interval / 60 / 60 / 24, 2)) + ' days'
                    )
                    time.sleep(60)
            except (KeyboardInterrupt, SystemExit):
                self.helper.log_info('Connector stop')
                exit(0)
            except Exception as e:
                self.helper.log_error(str(e))
                time.sleep(60)
예제 #4
0
 def run(self):
     # Downloading json.gz file
     logging.info('Requesting the file')
     urllib.request.urlretrieve(self.config['nvd_data_feed'],os.path.dirname(os.path.abspath(__file__)) + '/data.json.gz')
     # Unzipping the file
     logging.info('Unzipping the file')
     with gzip.open('data.json.gz', 'rb') as f_in:
         with open('data.json', 'wb') as f_out:
             shutil.copyfileobj(f_in, f_out)
     # Converting the file to stix2
     logging.info('Converting the file')
     convert('data.json', 'data-stix2.json')
     with open('data-stix2.json') as stixjson:
         contents = stixjson.read()
         self.opencti_connector_helper.send_stix2_bundle(contents, self.config['entities'])
     
     # Remove files
     os.remove('data.json')
     os.remove('data.json.gz')
     os.remove('data-stix2.json')