Exemplo n.º 1
0
    def __init__(self,
                 scan_directory,
                 file_path,
                 file_name,
                 api_key,
                 client_id,
                 access_key,
                 generate_token,
                 server,
                 reports_folder,
                 tex_method,
                 tex_folder,
                 features=DEFAULT_FEATURES,
                 reports=DEFAULT_REPORTS,
                 recursive=DEFAULT_RECURSIVE_EMULATION):
        """
        Setting the requested parameters and creating
        :param scan_directory: the requested directory
        :param file_path: the requested file path
        :param file_name: the requested file name
        :param api_key: API Key for the cloud service
        :param server: Check Point SandBlast Appliance ip address
        :param reports_folder: the folder which the reports will be save to
        :param tex_method: the method to be used with Thereat Extraction
        :param tex_directory: the folder which the Scrubbing attachments will be save to
        :param features: the requested features
        :param reports: type of reports
        :param recursive: find files in the requested directory recursively
        """
        if api_key:
            self.headers = {'Authorization': api_key}
        elif client_id and access_key:
            try:
                token_obj = Token()
                api_token = token_obj.generate(client_id, access_key)
                if generate_token:
                    print(api_token)
                    exit(0)
                else:
                    Logger.log(LogLevel.INFO,
                               'Generated token for JWT authentication')
                    self.headers = {'x-access-token': api_token}
            except Exception as e:
                Logger.log(LogLevel.CRITICAL, 'failed to generate JWT token',
                           e)
                exit(-1)
        else:
            self.headers = {}

        self.reports_folder = reports_folder
        self.tex_folder = tex_folder

        if features:
            self.features = features
        else:
            self.features = DEFAULT_FEATURES

        self.payload = Payload(reports, tex_method)
        self.server = server
        self.verify = True

        try:
            if reports_folder and not os.path.exists(reports_folder):
                os.makedirs(reports_folder)
        except Exception as e:
            Logger.log(LogLevel.CRITICAL,
                       'failed to create the needed folders', e)

        max_files = DEFAULT_MAX_FILES
        Logger.log(LogLevel.INFO, 'Calculating hash of files ')
        if scan_directory:
            for root, subdir_list, file_list in os.walk(ur'%s' %
                                                        scan_directory):
                for fn in file_list:
                    if max_files == 0:
                        Logger.log(LogLevel.INFO,
                                   'Max of %d files' % DEFAULT_MAX_FILES)
                        break
                    else:
                        max_files -= 1
                    if os.path.isfile(os.path.join(root, fn)):
                        file_data = FileData(fn.encode('utf-8'),
                                             os.path.join(root, fn),
                                             list(self.features))
                        file_data.compute_hashes()
                        self.pending[file_data.md5] = file_data
                if not recursive or max_files == 0:
                    break