def _app_show_browse_compare(self):
        page, per_page = extract_pagination_from_request(
            request, self._config)[0:2]
        try:
            with ConnectTo(CompareDbInterface, self._config) as db_service:
                compare_list = db_service.page_compare_results(skip=per_page *
                                                               (page - 1),
                                                               limit=per_page)
        except Exception as exception:
            error_message = 'Could not query database: {} {}'.format(
                type(exception), str(exception))
            logging.error(error_message)
            return render_template('error.html', message=error_message)

        with ConnectTo(CompareDbInterface, self._config) as connection:
            total = connection.get_total_number_of_results()

        pagination = get_pagination(page=page,
                                    per_page=per_page,
                                    total=total,
                                    record_name='compare results')
        return render_template('database/compare_browse.html',
                               compare_list=compare_list,
                               page=page,
                               per_page=per_page,
                               pagination=pagination)
Example #2
0
    def _app_show_browse_database(self,
                                  query: str = '{}',
                                  only_firmwares=False,
                                  inverted=False):
        page, per_page = extract_pagination_from_request(
            request, self._config)[0:2]
        search_parameters = self._get_search_parameters(
            query, only_firmwares, inverted)
        try:
            firmware_list = self._search_database(
                search_parameters['query'],
                skip=per_page * (page - 1),
                limit=per_page,
                only_firmwares=search_parameters['only_firmware'],
                inverted=search_parameters['inverted'])
            if self._query_has_only_one_result(firmware_list,
                                               search_parameters['query']):
                return redirect(
                    url_for('show_analysis', uid=firmware_list[0][0]))
        except Exception as err:
            error_message = 'Could not query database: {} {}'.format(
                type(err), str(err))
            logging.error(error_message)
            return render_template('error.html', message=error_message)

        with ConnectTo(FrontEndDbInterface, self._config) as connection:
            total = connection.get_number_of_total_matches(
                search_parameters['query'],
                search_parameters['only_firmware'],
                inverted=search_parameters['inverted'])
            device_classes = connection.get_device_class_list()
            vendors = connection.get_vendor_list()

        pagination = get_pagination(page=page,
                                    per_page=per_page,
                                    total=total,
                                    record_name='firmwares')
        search_parameters['query_title'] = json.dumps(
            search_parameters['query_title'],
            indent=2) if search_parameters['query_title'] else None
        return render_template('database/database_browse.html',
                               firmware_list=firmware_list,
                               page=page,
                               per_page=per_page,
                               pagination=pagination,
                               device_classes=device_classes,
                               vendors=vendors,
                               current_class=str(
                                   request.args.get('device_class')),
                               current_vendor=str(request.args.get('vendor')),
                               search_parameters=search_parameters)
Example #3
0
    def browse_searches(self):
        page, per_page, offset = extract_pagination_from_request(
            request, self._config)
        try:
            with ConnectTo(FrontEndDbInterface, self._config) as conn:
                # FIXME Use a proper yara parser
                rule_name_regex = re.compile(r'rule\s+([[a-zA-Z_]\w*)')
                searches = [(r['_id'], r['query_title'],
                             rule_name_regex.findall(r['query_title']))
                            for r in conn.search_query_cache.find(
                                skip=per_page * (page - 1), limit=per_page)]
                total = conn.search_query_cache.count_documents({})
        except Exception as exception:
            error_message = 'Could not query database'
            logging.error(error_message + f'due to exception: {exception}',
                          exc_info=True)
            return render_template('error.html', message=error_message)

        pagination = get_pagination(page=page, per_page=per_page, total=total)
        return render_template('database/database_binary_search_history.html',
                               searches_list=searches,
                               page=page,
                               per_page=per_page,
                               pagination=pagination)