def _get_task_obj(**kwargs): """ :param kwargs: :return: """ task_id = kwargs.get('task_id', None) app_id = kwargs.get('app_id', None) status = kwargs.get('status', None) close_old_connections() try: sql_where = {} if task_id: sql_where['id'] = int(task_id) if app_id: sql_where["app__id"] = int(app_id) if status: sql_where["status__in"] = status if not sql_where: raise QueryConditionIsEmptyException(u'Missing "task_id" key parameters!') item = TaskInfo.objects.filter(**sql_where).first() return item except TaskInfo.DoesNotExist as ex: logger.warn(ex) return None
def get_sys_module_obj(): """ 获取日志模块 :return: """ try: cache_obj = cache.get(CACHE_KEY) if cache_obj: return cache_obj module_list = [ # system 1, 2, 3, 4, 5, 6, 13, # article 10, 11, 12, # mobile 28, 30, 32, # vuln 19, 20, 21, # gitscan 22, 23, # cve 25, ] close_old_connections() item = ContentType.objects.filter(~Q(id__in=module_list)) if item: cache.set(CACHE_KEY, item, 60 * 60 * 24) return item except ContentType.DoesNotExist as ex: logger.warn(ex) return None
def get_module_obj(**kwargs): """ 获取模块 :param kwargs: :return: """ module_id = kwargs.get('module_id', None) try: sql_where = {} if module_id: sql_where['id'] = int(module_id) close_old_connections() item = ContentType.objects.get(**sql_where) return item except ContentType.DoesNotExist as ex: logger.warn(ex) return None
def get_group_obj(**kwargs): """ :param kwargs: :return: """ group_id = kwargs.get('group_id', None) close_old_connections() try: sql_where = {} if group_id: sql_where['id'] = int(group_id) item = Group.objects.get(**sql_where) return item except Group.DoesNotExist as ex: logger.warn(ex) return None
def _get_profile_obj(**kwargs): """ 获取资产 :param kwargs: :return: """ profile_id = kwargs.get('profile_id', None) name = kwargs.get('name', None) close_old_connections() try: sql_where = {} if profile_id: sql_where['id'] = int(profile_id) if name: sql_where['name'] = name if not sql_where: raise QueryConditionIsEmptyException( u'Missing "profile_id, name" key parameters!') item = ScanProfileInfo.objects.get(**sql_where) return item except ScanProfileInfo.DoesNotExist as ex: logger.warn(ex) return None
def _get_t_group_obj(**kwargs): """ 获取资产 :param kwargs: :return: """ group_id = kwargs.get('group_id', None) name = kwargs.get('name', None) close_old_connections() try: sql_where = {} if group_id: sql_where['id'] = int(group_id) if name: sql_where['name'] = name.strip() if not sql_where: raise QueryConditionIsEmptyException( u'Missing "group_id, name" key parameters!') item = TaskGroupInfo.objects.get(**sql_where) return item except TaskGroupInfo.DoesNotExist as ex: logger.warn(ex) return None
def update_profile_obj(**kwargs): """ 获取资产 :param kwargs: :return: """ profile_id = kwargs.get('profile_id', None) host_obj = kwargs.get('host_obj', None) name = kwargs.get('name', None) description = kwargs.get('description', '') exclude_dir = kwargs.get('exclude_dir', '') exclude_ext = kwargs.get('exclude_ext', '') exclude_file = kwargs.get('exclude_file', '') exclude_java_package = kwargs.get('exclude_java_package', '') config = kwargs.get('config', None) enable_commit_issue = parse_bool(kwargs.get('enable_commit_issue', None)) enable_auto_ignore = parse_bool(kwargs.get('enable_auto_ignore', None)) task_timeout = parse_int(kwargs.get('task_timeout', 60 * 60 * 2)) try: sql_where = {} if profile_id: sql_where['id'] = int(profile_id) if not sql_where: raise QueryConditionIsEmptyException( u'Missing "profile_id" key parameters!') item = ScanProfileInfo.objects.get(**sql_where) if item: item.enable_commit_issue = enable_commit_issue item.enable_auto_ignore = enable_auto_ignore if host_obj: item.host = host_obj if name: item.name = name item.description = description item.exclude_dir = exclude_dir item.exclude_ext = exclude_ext item.exclude_file = exclude_file item.exclude_java_package = exclude_java_package item.task_timeout = task_timeout if config: item.config = config item.revision = round(item.revision + 0.1, 2) cache.set('{0}:{1}'.format(SCAN_PROFILE_CACHE[1], item.id), None, 0) cache.set('{0}:{1}'.format(SCAN_PROFILE_CACHE[2], item.id), None, 0) cache.set('{0}:{1}'.format(SCAN_PROFILE_CACHE[3], item.id), None, 0) cache.set('{0}:{1}'.format(SCAN_PROFILE_CACHE[4], item.id), None, 0) cache.set('{0}'.format(SCAN_PROFILE_CACHE[5]), None, 0) update_client_revision_version(action=3, module=3, description='修改“{0}”扫描模板内容'.format( item.name)) item.save() return item except ScanProfileInfo.DoesNotExist as ex: logger.warn(ex) return None
def update_task_obj(**kwargs): """ 获取资产 :param kwargs: :return: """ task_id = kwargs.get('task_id', None) log_file = kwargs.get('log_file', None) executor_ip = kwargs.get('executor_ip', None) status = kwargs.get('status', None) start_time = kwargs.get('start_time', None) end_time = kwargs.get('end_time', None) config = kwargs.get('config', None) reason = kwargs.get('reason', None) title = kwargs.get('title', None) commit_hash = kwargs.get('commit_hash', None) critical = kwargs.get("critical", None) high = kwargs.get("high", None) medium = kwargs.get("medium", None) low = kwargs.get("low", None) info = kwargs.get("info", None) scope = kwargs.get("scope", None) level = kwargs.get("level", None) log_path = kwargs.get('log_path', '') scan_template = kwargs.get('scan_template', '') scan_template_version = kwargs.get('scan_template_version', '') close_old_connections() try: sql_where = {} if task_id: sql_where['id'] = int(task_id) if not sql_where: raise QueryConditionIsEmptyException(u'Missing "task_id" key parameters!') item = TaskInfo.objects.get(**sql_where) if item: if log_file: item.log_file = log_file if executor_ip: item.executor_ip = executor_ip if scan_template: item.template_name = scan_template if scan_template_version: item.template_version = scan_template_version # FIXME 强关联状态,不允许逆修改 if status: item.status = int(status) if config: item.config = config item.error_reason = reason if title: title = title[:255] item.error_title = title if start_time: item.start_time = start_time if item.start_time and end_time: item.end_time = end_time update_app_obj( app_id=item.app.id, last_scan_time=end_time ) if commit_hash: item.hash = commit_hash if critical or critical == 0: item.critical = int(critical) if high or high == 0: item.high = int(high) if medium or medium == 0: item.medium = int(medium) if low or low == 0: item.low = int(low) if info or info == 0: item.info = int(info) if log_path: item.log_file = log_path if item.status == 6: update_app_statistics( app_id=item.app.id, critical=critical, high=high, medium=medium, low=low, info=info, scope=scope, ) item.save(level=level) cache.set('{0}:{1}'.format(SCAN_CACHE[3], task_id), None, 0) return item except TaskInfo.DoesNotExist as ex: logger.warn(ex) return None