Exemple #1
0
    def post(self):
        """
        任务删除
        """
        done_status = [TaskStatus.DONE, TaskStatus.STOP, TaskStatus.ERROR]
        args = self.parse_args(delete_task_fields)
        task_id_list = args.pop('task_id')
        del_task_data_flag = args.pop('del_task_data')

        for task_id in task_id_list:
            task_data = utils.conn_db('task').find_one(
                {'_id': ObjectId(task_id)})
            if not task_data:
                return utils.build_ret(ErrorMsg.NotFoundTask,
                                       {"task_id": task_id})

            if task_data["status"] not in done_status:
                return utils.build_ret(ErrorMsg.TaskIsRunning,
                                       {"task_id": task_id})

        for task_id in task_id_list:
            utils.conn_db('task').delete_many({'_id': ObjectId(task_id)})
            table_list = [
                "cert", "domain", "fileleak", "ip", "service", "site", "url"
            ]
            if del_task_data_flag:
                for name in table_list:
                    utils.conn_db(name).delete_many({'task_id': task_id})

        return utils.build_ret(ErrorMsg.Success, {"task_id": task_id_list})
Exemple #2
0
    def post(self):
        """
        删除资产站点Tag
        """
        args = self.parse_args(delete_asset_site_tag_fields)
        site_id = args.pop("_id")
        tag = args.pop("tag")

        query = {"_id": ObjectId(site_id)}
        data = utils.conn_db('asset_site').find_one(query)
        if not data:
            return utils.build_ret(ErrorMsg.SiteIdNotFound,
                                   {"site_id": site_id})

        tag_list = []
        old_tag = data.get("tag")
        if old_tag:
            if isinstance(old_tag, str):
                tag_list.append(old_tag)

            if isinstance(old_tag, list):
                tag_list.extend(old_tag)

        if tag not in tag_list:
            return utils.build_ret(ErrorMsg.SiteTagNotExist, {"tag": tag})

        tag_list.remove(tag)

        utils.conn_db('asset_site').update_one(query,
                                               {"$set": {
                                                   "tag": tag_list
                                               }})

        return utils.build_ret(ErrorMsg.Success, {"tag": tag})
Exemple #3
0
    def post(self):
        """
        策略编辑
        """
        args = self.parse_args(edit_policy_fields)
        policy_id = args.pop('policy_id')
        policy_data = args.pop('policy_data', {})
        query = {'_id': ObjectId(policy_id)}
        item = utils.conn_db('policy').find_one(query)

        if not item:
            return utils.build_ret(ErrorMsg.PolicyIDNotFound, {})

        if not policy_data:
            return utils.build_ret(ErrorMsg.PolicyDataIsEmpty, {})
        item = change_dict(item, policy_data)

        poc_config = item["policy"].pop("poc_config", [])
        poc_config = _update_plugin_config(poc_config)
        if isinstance(poc_config, str):
            return utils.build_ret(poc_config, {})
        item["policy"]["poc_config"] = poc_config

        brute_config = item["policy"].pop("brute_config", [])
        brute_config = _update_plugin_config(brute_config)
        if isinstance(brute_config, str):
            return utils.build_ret(brute_config, {})
        item["policy"]["brute_config"] = brute_config

        item["update_date"] = utils.curr_date()
        utils.conn_db('policy').find_one_and_replace(query, item)
        item.pop('_id')

        return utils.build_ret(ErrorMsg.Success, {"data": item})
Exemple #4
0
    def get(self):
        """
        针对资产组删除范围
        """
        args = self.parser.parse_args()
        scope = str(args.pop('scope', "")).lower()
        scope_id = str(args.pop('scope_id', "")).lower()

        scope_data = self.get_scope_data(scope_id)
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                   {"scope_id": scope_id})

        query = {'_id': ObjectId(scope_id)}
        if scope not in scope_data.get("scope_array", []):
            return utils.build_ret(ErrorMsg.NotFoundScope, {
                "scope_id": scope_id,
                "scope": scope
            })

        scope_data["scope_array"].remove(scope)
        scope_data["scope"] = ",".join(scope_data["scope_array"])
        utils.conn_db(self._table).find_one_and_replace(query, scope_data)

        return utils.build_ret(ErrorMsg.Success, {
            "scope_id": scope_id,
            "scope": scope
        })
Exemple #5
0
    def _add_task(self, target_lists, task_data):
        try:
            ip_list, domain_list = self._get_ip_domain_list(target_lists)
        except Exception as e:
            return utils.build_ret(str(e), {})

        ret_items = []
        if domain_list:
            submit_result = self._submit_domain_list(domain_list, task_data)
            if isinstance(submit_result, str):
                return utils.build_ret(ErrorMsg.Error,
                                       {"error": submit_result})
            ret_items.extend(submit_result)

        if ip_list:
            submit_result = self._submit_ip_list(ip_list, task_data)
            if isinstance(submit_result, str):
                return utils.build_ret(ErrorMsg.Error,
                                       {"error": submit_result})
            ret_items.extend(submit_result)

        if not ret_items:
            return utils.build_ret(ErrorMsg.TaskTargetIsEmpty, {})

        return utils.build_ret(ErrorMsg.Success, {"items": ret_items})
Exemple #6
0
    def post(self):
        """
        添加指纹信息
        """
        args = self.parse_args(add_fingerprint_fields)

        human_rule = args.pop('human_rule')
        name = args.pop('name')

        rule_map = parse_human_rule(human_rule)
        if rule_map is None:
            return utils.build_ret(ErrorMsg.RuleInvalid, {"rule": human_rule})

        data = {
            "name": name,
            "rule": rule_map,
            "human_rule": transform_rule_map(rule_map),
            "update_date": utils.curr_date_obj()
        }

        utils.conn_db('fingerprint').insert_one(data)

        finger_id = str(data.pop('_id'))

        data.pop('update_date')

        return utils.build_ret(ErrorMsg.Success, {
            "_id": finger_id,
            "data": data
        })
Exemple #7
0
    def post(self):
        """
        资产组添加
        """
        args = self.parse_args(add_asset_scope_fields)
        name = args.pop('name')
        scope = args.pop('scope')
        black_scope = args.pop('black_scope')

        black_scope_array = []
        if black_scope:
            black_scope_array = re.split(r",|\s", black_scope)

        scope_array = re.split(r",|\s", scope)
        for x in scope_array:
            if not utils.is_valid_domain(x):
                return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": x})

        if not scope_array:
            return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": ""})

        scope_data = {
            "name": name,
            "scope": scope,
            "scope_array": scope_array,
            "black_scope": black_scope,
            "black_scope_array": black_scope_array,
        }
        conn('asset_scope').insert(scope_data)

        scope_id = str(scope_data.pop("_id"))
        scope_data["scope_id"] = scope_id
        data = {"message": "success", "data": scope_data, "code": 200}

        return data
Exemple #8
0
    def get(self, task_id=None):
        """
        任务停止
        """
        done_status = [TaskStatus.DONE, TaskStatus.STOP, TaskStatus.ERROR]

        task_data = utils.conn_db('task').find_one({'_id': ObjectId(task_id)})
        if not task_data:
            return utils.build_ret(ErrorMsg.NotFoundTask, {"task_id": task_id})

        if task_data["status"] in done_status:
            return utils.build_ret(ErrorMsg.TaskIsRunning,
                                   {"task_id": task_id})

        celery_id = task_data.get("celery_id")
        if not celery_id:
            return utils.build_ret(ErrorMsg.CeleryIdNotFound,
                                   {"task_id": task_id})

        control = celerytask.celery.control

        control.revoke(celery_id, signal='SIGTERM', terminate=True)

        utils.conn_db('task').update_one({'_id': ObjectId(task_id)},
                                         {"$set": {
                                             "status": TaskStatus.STOP
                                         }})

        utils.conn_db('task').update_one(
            {'_id': ObjectId(task_id)},
            {"$set": {
                "end_time": utils.curr_date()
            }})

        return {"message": "success", "task_id": task_id, "code": 200}
Exemple #9
0
    def post(self):
        """
        添加域名到资产组中
        """
        args = self.parse_args(add_domain_fields)
        domain = args.pop("domain")
        scope_id = args.pop("scope_id")
        if not utils.is_valid_domain(domain):
            return utils.build_ret(ErrorMsg.DomainInvalid, {"domain": domain})

        scope_data = utils.conn_db('asset_scope').find_one({"_id": ObjectId(scope_id)})
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID, {"scope_id": scope_id})

        if utils.get_fld(domain) not in scope_data["scope"]:
            return utils.build_ret(ErrorMsg.DomainNotFoundViaScope, {"domain": domain})

        domain_data = utils.conn_db("asset_domain").find_one({"domain": domain, "scope_id": scope_id})
        if domain_data:
            return utils.build_ret(ErrorMsg.DomainInScope, {"domain": domain})

        options = {
            "celery_action": CeleryAction.ADD_DOMAIN_TO_SCOPE,
            "data": {
                "domain": domain,
                "scope_id": scope_id
            }
        }
        celerytask.arl_task.delay(options=options)
        return utils.build_ret(ErrorMsg.Success, options["data"])
Exemple #10
0
    def post(self):
        """
        添加域名到资产组中
        """
        args = self.parse_args(add_domain_fields)
        raw_domain = args.pop("domain")
        domain_list = re.split(r",|\s", raw_domain)
        domain_list = list(set(filter(None, domain_list)))
        scope_id = args.pop("scope_id")

        scope_data = utils.conn_db('asset_scope').find_one(
            {"_id": ObjectId(scope_id)})
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                   {"scope_id": scope_id})

        domain_in_scope_list = []
        add_domain_list = []
        for domain in domain_list:
            if not utils.is_valid_domain(domain):
                return utils.build_ret(ErrorMsg.DomainInvalid,
                                       {"domain": domain})

            if utils.get_fld(domain) not in scope_data["scope"]:
                return utils.build_ret(ErrorMsg.DomainNotFoundViaScope,
                                       {"domain": domain})

            domain_data = utils.conn_db("asset_domain").find_one({
                "domain":
                domain,
                "scope_id":
                scope_id
            })
            if domain_data:
                domain_in_scope_list.append(domain)
                continue
            add_domain_list.append(domain)

        for domain in add_domain_list:
            options = {
                "celery_action": CeleryAction.ADD_DOMAIN_TO_SCOPE,
                "data": {
                    "domain": domain,
                    "scope_id": scope_id
                }
            }
            celerytask.arl_task.delay(options=options)

        ret_data = {
            "domain": ",".join(add_domain_list),
            "scope_id": scope_id,
            "domain_in_scope": ",".join(domain_in_scope_list)
        }
        if len(add_domain_list) == 0:
            return utils.build_ret(ErrorMsg.DomainNotFoundNotInScope, ret_data)

        return utils.build_ret(ErrorMsg.Success, ret_data)
Exemple #11
0
    def post(self):
        """
        添加监控周期任务
        """
        args = self.parse_args(add_scheduler_fields)
        scope_id = args.pop("scope_id")
        domain = args.pop("domain")
        interval = args.pop("interval")
        name = args.pop("name")

        if interval < 3600 * 6:
            return utils.build_ret(ErrorMsg.IntervalLessThan3600,
                                   {"interval": interval})

        monitor_domain = utils.arl.get_monitor_domain_by_id(scope_id)
        scope_data = utils.arl.scope_data_by_id(scope_id)

        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                   {"scope_id": scope_id})

        domains = domain.split(",")
        for x in domains:
            curr_domain = x.strip()
            if curr_domain not in scope_data["scope_array"]:
                return utils.build_ret(ErrorMsg.DomainNotFoundViaScope, {
                    "domain": curr_domain,
                    "scope_id": scope_id
                })

            if curr_domain in monitor_domain:
                return utils.build_ret(ErrorMsg.DomainViaJob, {
                    "domain": curr_domain,
                    "scope_id": scope_id
                })

        ret_data = []
        for x in domains:
            curr_name = name
            if not name:
                curr_name = "监控-{}-{}".format(scope_data["name"], x)

            job_id = app_scheduler.add_job(domain=x,
                                           scope_id=scope_id,
                                           options=None,
                                           interval=interval,
                                           name=curr_name)
            ret_data.append({
                "domain": x,
                "scope_id": scope_id,
                "job_id": job_id
            })

        return utils.build_ret(ErrorMsg.Success, ret_data)
Exemple #12
0
    def post(self):
        """
        资产组添加
        """
        args = self.parse_args(add_asset_scope_fields)
        name = args.pop('name')
        scope = args.pop('scope')
        black_scope = args.pop('black_scope')
        scope_type = args.pop('scope_type')

        if scope_type not in [AssetScopeType.IP, AssetScopeType.DOMAIN]:
            scope_type = AssetScopeType.DOMAIN

        black_scope_array = []
        if black_scope:
            black_scope_array = re.split(r",|\s", black_scope)

        scope_array = re.split(r",|\s", scope)
        # 清除空白符
        scope_array = list(filter(None, scope_array))
        new_scope_array = []
        for x in scope_array:
            if scope_type == AssetScopeType.DOMAIN:
                if not utils.is_valid_domain(x):
                    return utils.build_ret(ErrorMsg.DomainInvalid,
                                           {"scope": x})

                new_scope_array.append(x)

            if scope_type == AssetScopeType.IP:
                transfer = utils.ip.transfer_ip_scope(x)
                if transfer is None:
                    return utils.build_ret(ErrorMsg.ScopeTypeIsNotIP,
                                           {"scope": x})

                new_scope_array.append(transfer)

        if not new_scope_array:
            return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": ""})

        scope_data = {
            "name": name,
            "scope_type": scope_type,
            "scope": ",".join(new_scope_array),
            "scope_array": new_scope_array,
            "black_scope": black_scope,
            "black_scope_array": black_scope_array,
        }
        conn('asset_scope').insert(scope_data)

        scope_id = str(scope_data.pop("_id"))
        scope_data["scope_id"] = scope_id

        return utils.build_ret(ErrorMsg.Success, scope_data)
Exemple #13
0
    def post(self):
        args = self.parse_args(add_policy_fields)
        name = args.pop("name")
        policy = args.pop("policy", {})
        if policy is None:
            return utils.build_ret("Missing policy parameter", {})

        domain_config = policy.pop("domain_config", {})
        domain_config = self._update_arg(domain_config, domain_config_fields)
        ip_config = policy.pop("ip_config", {})
        ip_config = self._update_arg(ip_config, ip_config_fields)

        site_config = policy.pop("site_config", {})
        site_config = self._update_arg(site_config, site_config_fields)

        poc_config = policy.pop("poc_config", [])
        if poc_config is None:
            poc_config = []

        poc_config = _update_plugin_config(poc_config)
        if isinstance(poc_config, str):
            return utils.build_ret(poc_config, {})

        brute_config = policy.pop("brute_config", [])
        if brute_config is None:
            brute_config = []
        brute_config = _update_plugin_config(brute_config)
        if isinstance(brute_config, str):
            return utils.build_ret(brute_config, {})

        file_leak = fields.boolean(policy.pop("file_leak", False))
        npoc_service_detection = fields.boolean(
            policy.pop("npoc_service_detection", False))
        desc = args.pop("desc", "")

        item = {
            "name": name,
            "policy": {
                "domain_config": domain_config,
                "ip_config": ip_config,
                "site_config": site_config,
                "poc_config": poc_config,
                "brute_config": brute_config,
                "file_leak": file_leak,
                "npoc_service_detection": npoc_service_detection
            },
            "desc": desc,
            "update_date": utils.curr_date()
        }
        utils.conn_db("policy").insert_one(item)

        return utils.build_ret(ErrorMsg.Success,
                               {"policy_id": str(item["_id"])})
Exemple #14
0
    def post(self):
        args = self.parse_args(delete_task_post_fields)
        scope_id_list = args.pop('scope_id')
        for scope_id in scope_id_list:
            if not self.get_scope_data(scope_id):
                return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                       {"scope_id": scope_id})

        table_list = ["asset_domain", "asset_site", "asset_ip", "scheduler"]

        for scope_id in scope_id_list:
            utils.conn_db(self._table).delete_many({'_id': ObjectId(scope_id)})

            for name in table_list:
                utils.conn_db(name).delete_many({'scope_id': scope_id})

        return utils.build_ret(ErrorMsg.Success, {"scope_id": scope_id_list})
Exemple #15
0
    def get(self):
        """
        清空 PoC 信息
        """
        result = utils.conn_db('poc').delete_many({})

        delete_cnt = result.deleted_count

        return utils.build_ret(ErrorMsg.Success, {"delete_cnt": delete_cnt})
Exemple #16
0
    def post(self):
        """
        删除监控周期任务
        """
        args = self.parse_args(delete_scheduler_fields)
        job_id_list = args.get("job_id", [])

        ret_data = {"job_id": job_id_list}

        for job_id in job_id_list:
            item = app_scheduler.find_job(job_id)
            if not item:
                return utils.build_ret(ErrorMsg.JobNotFound, ret_data)

        for job_id in job_id_list:
            app_scheduler.delete_job(job_id)

        return utils.build_ret(ErrorMsg.Success, ret_data)
Exemple #17
0
    def post(self):
        """
        添加资产范围
        """
        args = self.parse_args(add_scope_fields)
        scope = str(args.pop('scope', "")).lower()

        scope_array = re.split(r",|\s", scope)
        # 清除空白符
        scope_array = list(filter(None, scope_array))
        if not scope_array:
            return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": ""})

        scope_id = args.pop('scope_id', "")

        table = 'asset_scope'
        query = {'_id': ObjectId(scope_id)}
        scope_data = utils.conn_db(table).find_one(query)
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID, {
                "scope_id": scope_id,
                "scope": scope
            })

        logger.info(scope_array)
        for x in scope_array:
            if not utils.is_valid_domain(x):
                return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": x})

            if x in scope_data.get("scope_array", []):
                return utils.build_ret(ErrorMsg.ExistScope, {
                    "scope_id": scope_id,
                    "scope": x
                })

            scope_data["scope_array"].append(x)

        scope_data["scope"] = ",".join(scope_data["scope_array"])
        utils.conn_db(table).find_one_and_replace(query, scope_data)

        return utils.build_ret(ErrorMsg.Success, {
            "scope_id": scope_id,
            "scope": scope
        })
Exemple #18
0
    def post(self):
        """
        停止监控周期任务
        """
        args = self.parse_args(stop_scheduler_fields)
        job_id = args.get("job_id")

        item = app_scheduler.find_job(job_id)
        if not item:
            return utils.build_ret(ErrorMsg.JobNotFound, {"job_id": job_id})

        status = item.get("status", SchedulerStatus.RUNNING)
        if status != SchedulerStatus.RUNNING:
            return utils.build_ret(ErrorMsg.SchedulerStatusNotRunning,
                                   {"job_id": job_id})

        app_scheduler.stop_job(job_id)

        return utils.build_ret(ErrorMsg.Success, {"job_id": job_id})
Exemple #19
0
    def get(self):
        """
        更新 PoC 信息
        """
        n = NPoC()
        plugin_cnt = len(n.plugin_name_list)
        n.sync_to_db()
        n.delete_db()

        return utils.build_ret(ErrorMsg.Success, {"plugin_cnt": plugin_cnt})
Exemple #20
0
    def get(self):
        """
        控制台信息查看
        """

        data = {
            "device_info": utils.device_info()  # 包含 CPU 内存和磁盘信息
        }

        return utils.build_ret(ErrorMsg.Success, data)
Exemple #21
0
    def post(self):
        """
        删除站点
        """
        args = self.parse_args(delete_site_fields)
        id_list = args.pop('_id', [])
        for _id in id_list:
            query = {'_id': ObjectId(_id)}
            utils.conn_db('site').delete_one(query)

        return utils.build_ret(ErrorMsg.Success, {'_id': id_list})
Exemple #22
0
    def post(self):
        """
        删除资产组中的IP
        """
        args = self.parse_args(delete_ip_fields)
        id_list = args.pop('_id', "")
        for _id in id_list:
            query = {'_id': ObjectId(_id)}
            utils.conn_db('asset_ip').delete_one(query)

        return utils.build_ret(ErrorMsg.Success, {'_id': id_list})
Exemple #23
0
 def post(self):
     """
     策略删除
     """
     args = self.parse_args(delete_policy_fields)
     policy_id_list = args.pop('policy_id')
     for policy_id in policy_id_list:
         if not policy_id:
             continue
         utils.conn_db('policy').delete_one({'_id': ObjectId(policy_id)})
     """这里直接返回成功了"""
     return utils.build_ret(ErrorMsg.Success, {})
Exemple #24
0
    def post(self):
        """
        添加站点到资产组中
        """
        args = self.parse_args(add_site_fields)
        site = args.pop("site")
        scope_id = args.pop("scope_id")
        url = utils.normal_url(site).strip("/")
        if not url:
            return utils.build_ret(ErrorMsg.DomainInvalid, {"site": site})

        scope_data = utils.conn_db('asset_scope').find_one(
            {"_id": ObjectId(scope_id)})
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                   {"scope_id": scope_id})

        fld = utils.get_fld(url)
        if not fld:
            return utils.build_ret(ErrorMsg.SiteURLNotDomain, {"site": url})

        if fld not in scope_data["scope"]:
            return utils.build_ret(ErrorMsg.DomainNotFoundViaScope,
                                   {"site": url})

        site_data = utils.conn_db('asset_site').find_one({
            "site": url,
            "scope_id": scope_id
        })
        if site_data:
            return utils.build_ret(ErrorMsg.SiteInScope, {"site": url})

        add_site_to_scope(url, scope_id)

        return utils.build_ret(ErrorMsg.Success, {"site": url})
Exemple #25
0
    def post(self):
        """
        任务同步
        """
        done_status = [TaskStatus.DONE, TaskStatus.STOP, TaskStatus.ERROR]
        args = self.parse_args(sync_task_fields)
        task_id = args.pop('task_id')
        scope_id = args.pop('scope_id')

        query = {'_id': ObjectId(task_id)}
        task_data = utils.conn_db('task').find_one(query)
        if not task_data:
            return utils.build_ret(ErrorMsg.NotFoundTask, {"task_id": task_id})

        asset_scope_data = utils.conn_db('asset_scope').find_one(
            {'_id': ObjectId(scope_id)})
        if not asset_scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID,
                                   {"task_id": task_id})

        if task_data.get("type") != "domain":
            return utils.build_ret(ErrorMsg.TaskTypeIsNotDomain,
                                   {"task_id": task_id})

        if not utils.is_in_scopes(task_data["target"],
                                  asset_scope_data["scope_array"]):
            return utils.build_ret(ErrorMsg.TaskTargetNotInScope,
                                   {"task_id": task_id})

        if task_data["status"] not in done_status:
            return utils.build_ret(ErrorMsg.TaskIsRunning,
                                   {"task_id": task_id})

        task_sync_status = task_data.get("sync_status", TaskSyncStatus.DEFAULT)

        if task_sync_status not in [
                TaskSyncStatus.DEFAULT, TaskSyncStatus.ERROR
        ]:
            return utils.build_ret(ErrorMsg.TaskSyncDealing,
                                   {"task_id": task_id})

        task_data["sync_status"] = TaskSyncStatus.WAITING

        options = {
            "celery_action": CeleryAction.DOMAIN_TASK_SYNC_TASK,
            "data": {
                "task_id": task_id,
                "scope_id": scope_id
            }
        }
        celerytask.arl_task.delay(options=options)

        conn('task').find_one_and_replace(query, task_data)

        return utils.build_ret(ErrorMsg.Success, {"task_id": task_id})
Exemple #26
0
    def post(self):
        """
        任务批量停止
        """
        args = self.parse_args(batch_stop_fields)
        task_id_list = args.pop("task_id", [])

        for task_id in task_id_list:
            if not task_id:
                continue
            stop_task(task_id)
        """这里直接返回成功了"""
        return utils.build_ret(ErrorMsg.Success, {})
Exemple #27
0
    def get(self):
        """
        保存资产站点到结果集
        """
        args = self.parser.parse_args()
        query = self.build_db_query(args)
        items = utils.conn_db('asset_site').distinct("site", query)

        items = list(set([utils.url.cut_filename(x) for x in items]))

        if len(items) == 0:
            return utils.build_ret(ErrorMsg.QueryResultIsEmpty, {})

        data = {"items": items, "type": "asset_site", "total": len(items)}
        result = utils.conn_db('result_set').insert_one(data)

        ret_data = {
            "result_set_id": str(result.inserted_id),
            "result_total": len(items),
            "type": "asset_site"
        }

        return utils.build_ret(ErrorMsg.Success, ret_data)
Exemple #28
0
    def post(self):
        """
        添加资产范围
        """
        args = self.parse_args(add_scope_fields)
        scope = str(args.pop('scope', "")).lower()

        scope_id = args.pop('scope_id', "")

        table = 'asset_scope'
        query = {'_id': ObjectId(scope_id)}
        scope_data = utils.conn_db(table).find_one(query)
        if not scope_data:
            return utils.build_ret(ErrorMsg.NotFoundScopeID, {
                "scope_id": scope_id,
                "scope": scope
            })

        scope_type = scope_data.get("scope_type")
        if scope_type not in [AssetScopeType.IP, AssetScopeType.DOMAIN]:
            scope_type = AssetScopeType.DOMAIN

        scope_array = re.split(r",|\s", scope)
        # 清除空白符
        scope_array = list(filter(None, scope_array))
        if not scope_array:
            return utils.build_ret(ErrorMsg.DomainInvalid, {"scope": ""})

        for x in scope_array:
            new_scope = x
            if scope_type == AssetScopeType.DOMAIN:
                if not utils.is_valid_domain(x):
                    return utils.build_ret(ErrorMsg.DomainInvalid,
                                           {"scope": x})

            if scope_type == AssetScopeType.IP:
                transfer = utils.ip.transfer_ip_scope(x)
                if transfer is None:
                    return utils.build_ret(ErrorMsg.ScopeTypeIsNotIP,
                                           {"scope": x})
                new_scope = transfer

            if new_scope in scope_data.get("scope_array", []):
                return utils.build_ret(ErrorMsg.ExistScope, {
                    "scope_id": scope_id,
                    "scope": x
                })

            scope_data["scope_array"].append(new_scope)

        scope_data["scope"] = ",".join(scope_data["scope_array"])
        utils.conn_db(table).find_one_and_replace(query, scope_data)

        return utils.build_ret(ErrorMsg.Success, {
            "scope_id": scope_id,
            "scope": scope
        })
Exemple #29
0
    def get(self):
        """
        任务同步资产组查询
        """
        args = self.parser.parse_args()
        target = args.pop("target")
        if not utils.is_valid_domain(target):
            return utils.build_ret(ErrorMsg.DomainInvalid, {"target": target})

        args["scope_array"] = utils.get_fld(target)
        args["size"] = 100
        args["order"] = "_id"

        data = self.build_data(args=args, collection='asset_scope')
        ret = []
        for item in data["items"]:
            if utils.is_in_scopes(target, item["scope_array"]):
                ret.append(item)

        data["items"] = ret
        data["total"] = len(ret)
        return data
Exemple #30
0
    def post(self):
        """
        任务提交
        """
        args = self.parse_args(add_task_fields)

        name = args.pop('name')
        target = args.pop('target')
        target = target.strip().lower()

        task_data = {
            "name": name,
            "target": target,
            "start_time": "-",
            "end_time": "-",
            "task_tag": "task",  #标记为正常下发的任务
            "service": [],
            "status": "waiting",
            "options": args,
            "type": "domain"
        }

        logger.info(task_data)

        target_lists = re.split(r",|\s", target)
        # 清除空白符
        target_lists = list(filter(None, target_lists))
        ip_target_list = []
        ret_items = []

        for item in target_lists:
            if not utils.is_valid_domain(
                    item) and not utils.is_vaild_ip_target(item):
                return utils.build_ret(ErrorMsg.TargetInvalid,
                                       data={"target": item})

            if utils.is_vaild_ip_target(item):
                if not utils.not_in_black_ips(item):
                    return utils.build_ret(ErrorMsg.IPInBlackIps,
                                           data={"ip": item})

        for item in target_lists:
            if utils.is_valid_domain(item):
                ret_item = {"target": item, "type": "domain"}
                domain_task_data = task_data.copy()
                domain_task_data["target"] = item
                _task_data = submit_task(domain_task_data)
                ret_item["task_id"] = _task_data.get("task_id", "")
                ret_item["celery_id"] = _task_data.get("celery_id", "")
                ret_items.append(ret_item)

            elif utils.is_vaild_ip_target(item):
                if utils.not_in_black_ips(item):
                    ip_target_list.append(item)
                else:
                    ret_item = {
                        "target": item,
                        "type": "in black ip list",
                        "task_id": "",
                        "celery_id": ""
                    }
                    ret_items.append(ret_item)

            else:
                ret_item = {
                    "target": item,
                    "type": "unknown",
                    "task_id": "",
                    "celery_id": ""
                }
                ret_items.append(ret_item)

        if ip_target_list:
            ip_task_data = task_data.copy()
            ip_task_data["target"] = " ".join(ip_target_list)
            ip_task_data["type"] = "ip"

            ret_item = {
                "target": ip_task_data["target"],
                "type": ip_task_data["type"]
            }

            _task_data = submit_task(ip_task_data)

            ret_item["task_id"] = _task_data.get("task_id", "")
            ret_item["celery_id"] = _task_data.get("celery_id", "")
            ret_items.append(ret_item)

        ret_data = {
            "items": ret_items,
            "options": args,
            "message": "success",
            "code": 200
        }

        return ret_data