Ejemplo n.º 1
0
def nessus_asset_engine(request, task, action):
    """ 调用nessus工具进行网络扫描,进而实现资产发现功能"""

    subtask = task.subtask
    scanner = task.scanner
    scanner_id = scanner.id
    if action == 'running':
        policy = models.Policy.objects.get(id=subtask.policy_id)
        res = nessus.add_nessus_scan(task.task_name, task.task_des,
                                     task.target_address, policy.scanner_id,
                                     policy.policy_name)
        if models.SubTask.update_error_msg(res, subtask.id):
            scan_id = res[1]
            res = nessus.launch_nessus_scan(scan_id, scanner_id)
            if models.SubTask.update_error_msg(res, subtask.id):
                scan_uuid = res[1]['scan_uuid']
                subtask.subtask_status = action
                subtask.scan_id = scan_id
                subtask.save()
                nessus_asset_scan_detail.delay(scan_id, task, scanner_id)
                # nessus_asset_scan_detail(scan_id, tasks, scanner_id)
    elif action == 'pausing':
        scan_id = subtask.scan_id
        do_res = nessus.pause_nessus_scan(scan_id, scanner_id)
        if do_res:
            task.task_status = action
            task.save()
    elif action == 'stopping':
        scan_id = subtask.scan_id
        res = nessus.stop_nessus_scan(scan_id, scanner_id)
        if models.SubTask.update_error_msg(res, subtask.id):
            scan_uuid = res[1]['scan_uuid']
            subtask.subtask_status = action
            subtask.save()
            data = {
                'notice_title': '任务进度通知',
                'notice_body':
                '您对' + task.task_name + '的扫描任务已停止,如需查看结果,请重新执行扫描任务',
                'notice_url': reverse("tasks:task_list"),
                'notice_type': 'notice',
                'task_id': task.id,
            }
            user = task.task_user
            Notice.notice_add(user, data)
            send_notice_mail(user.email, data)
    elif action == 'resuming':
        scan_id = subtask.scan_id
        do_res = nessus.resume_nessus_scan(scan_id, scanner_id)
        if do_res:
            task.task_status = action
            task.save()
Ejemplo n.º 2
0
 def post(self, request):
     title = request.POST.get('title')
     detail = request.POST.get('gcontent')
     context = {}
     if not all([title, detail]):
         context['msg'] = "数据填写不完整,每个都为必填项!"
         return render(request, 'noticeissue.html', context)
     if len(title) > 256:
         context['msg'] = "标题过长!不能超过256个字符"
         return render(request, 'noticeissue.html', context)
     user = User.objects.filter(id=request.user.id)[0]
     notice = Notice(user=user,
                     title=title,
                     detail=detail,
                     update_time=datetime.datetime.now(tz=timezone.utc))
     notice.save()
     return redirect(reverse('notice:index'))
Ejemplo n.º 3
0
def awvs_engine(request, task, action):
    """ 调用awvs api 对web应用扫描"""
    subtask = task.subtask
    scanner = task.scanner
    scanner_id = scanner.id
    if action == 'running':
        res = awvs.add_scan(scanner_id, task.target_address, task.task_des)
        if models.SubTask.update_error_msg(res, subtask.id):
            target_id = res[1]['target_id']
            status, scan_id = awvs.start_scan(scanner_id, target_id)
            if status and scan_id:
                subtask.target_id = target_id
                subtask.scan_id = scan_id
                subtask.subtask_status = action
                subtask.save()
                task.task_status = action
                task.save()
                awvs_scan_detail.delay(scan_id, task, scanner_id)
                # awvs_scan_detail(scan_id, tasks, scanner_id)
    elif action == 'stopping':
        scan_id = subtask.scan_id
        res = awvs.stop_scan(scan_id, scanner_id)
        if models.SubTask.update_error_msg(res, subtask.id):
            subtask.subtask_status = action
            subtask.save()
            data = {
                'notice_title': '任务进度通知',
                'notice_body': '您对' + task.task_name + '的扫描任务已完成,请及时查看结果',
                'notice_url': reverse("tasks:task_list"),
                'notice_type': 'notice',
                'task_id': task.id,
            }
            user = task.task_user
            Notice.notice_add(user, data)
            send_notice_mail(user.email, data)
    else:
        error = '该类任务暂不支持当前操作,请选择取消任务'
        print(error)
Ejemplo n.º 4
0
def save_awvs_vulns(scan_id, task_id):
    task = Task.objects.filter(task_id=task_id).first()
    while True:
        status = awvs.get_scan_status(scan_id, task.task_scanner.id)
        if status == 'completed':
            awvs.get_scan_result(scan_id, task_id, task.task_scanner.id)
            task.task_status = 'completed'
            task.save()
            # type_task_list = {'移动应用':'type1','web应用':'type2','操作系统':'type3'}
            data = {
                'notice_title': '任务进度通知',
                'notice_body': '您对' + task.task_name + '的扫描任务已完成,请及时查看结果',
                'notice_url': reverse("tasks:task_list"),
                'notice_type': 'notice',
                'task_id': task.id,
            }
            user = task.task_user
            Notice.notice_add(user, data)
            send_notice_mail(user.email, data)
            break
        elif status == 'aborted':
            awvs.get_scan_result(scan_id, task_id, task.task_scanner.id)
            task.task_status = 5
            task.save()
            # type_task_list = {'移动应用':'type1','web应用':'type2','操作系统':'type3'}
            data = {
                'notice_title': '任务进度通知',
                'notice_body': '您对' + task.task_name + '的扫描任务已完成,请及时查看结果',
                'notice_url': reverse("tasks:task_list"),
                'notice_type': 'notice',
                'task_id': task.id,
            }
            user = task.task_user
            Notice.notice_add(user, data)
            send_notice_mail(user.email, data)
            break
        else:
            time.sleep(60)
Ejemplo n.º 5
0
def nessus_scan_detail(scan_id, task, scanner_id):
    try:
        subtask = task.subtask
        while True:
            res = nessus.get_scan_status(scan_id, scanner_id)
            if models.SubTask.update_error_msg(res, subtask.id):
                try:
                    res_status = res[1]['info']['status']
                except:
                    time.sleep(5)
                    continue
                if res[1]['info']['status'] == 'canceled' or res[1]['info'][
                        'status'] == 'completed' or res[1]['info'][
                            'status'] == 'stopping':

                    # 获取漏洞信息并保存
                    nessus.get_scan_vuln(scan_id, scanner_id, res)

                    # 更新子任务的运行进度
                    # subtask.subtask_status = res_status
                    subtask.subtask_status = 'completed'
                    subtask.save()

                    # 更新task 状态
                    # subtask_count = models.SubTask.objects.filter(task_id=tasks.id).exclude(subtask_status__in=['completed', 'exported', 'aborted']).count()
                    subtask_count = models.SubTask.objects.filter(
                        task_id=task.id,
                        subtask_status__in=['pending', 'running',
                                            'exporting']).count()
                    if subtask_count == 0:
                        task.task_status = 'completed'
                        task.save()
                    # filter = [{"filter": "host.hostname", "quality": "match", "value": tasks.target_address},
                    # {"filter": "severity", "quality": "eq", "value": "Low"}]
                    # data = {"format": SCAN_REPORT_FORMAT, "filter": filter}
                    data = {
                        "format": "pdf",
                        "chapters":
                        "vuln_hosts_summary;vuln_by_host; compliance_exec; remediations; vuln_by_plugin; compliance",
                        "filter.0.quality": "eq",
                        "filter.0.filter": "hostname",
                        "filter.0.value": task.target_address,
                        "filter.search_type": "and"
                    }
                    logger.error(data)
                    res = nessus.post_export_request(scan_id, scanner_id, data)

                    if models.SubTask.update_error_msg(res, subtask.id):
                        file_id = res[1]['file']
                        logger.info(file_id)
                        while True:
                            res = nessus.get_export_status(
                                scan_id, file_id, scanner_id)
                            if models.SubTask.update_error_msg(
                                    res, subtask.id
                            ) and res[1]['status'] == 'ready':
                                # if res[1]['status'] == 'ready':
                                res = nessus.get_export_download(
                                    scan_id, file_id, scanner_id)
                                if models.SubTask.update_error_msg(
                                        res, subtask.id):
                                    filename = "scan-result-report-{0}-{1}-{2}.{3}".format(
                                        SCAN_REPORT_FORMAT, scanner_id,
                                        file_id, SCAN_REPORT_FORMAT)
                                    filepath = "{0}/{1}".format(
                                        SCAN_RESULT_PDF_FILE_PATH, filename)
                                    destination = open(filepath, 'wb+')
                                    destination.write(res[1])
                                    destination.close()
                                    subtask.report_name = filename
                                    subtask.subtask_status = 'exported'
                                    subtask.save()
                                    data = {
                                        'notice_title': '任务进度通知',
                                        'notice_body': '您对' + task.task_name +
                                        '的扫描任务已完成,请及时查看结果',
                                        'notice_url':
                                        reverse("tasks:task_list"),
                                        'notice_type': 'notice',
                                        'task_id': task.id,
                                    }
                                    user = task.task_user
                                    Notice.notice_add(user, data)
                                    send_notice_mail(user.email, data)
                                    break
                            time.sleep(10)
                        break
            time.sleep(10)
    except Exception as e:
        traceback.print_exc()
Ejemplo n.º 6
0
def awvs_scan_detail(scan_id, task, scanner_id):
    try:
        subtask = task.subtask
        while True:
            res = awvs.get_scan_status(scan_id, scanner_id)
            if models.SubTask.update_error_msg(res, subtask.id):
                try:
                    res_status = res[1]['current_session']['status']
                except Exception as e:
                    time.sleep(5)
                    continue
                if res_status in ['completed', 'aborted']:
                    subtask.subtask_status = res_status
                    subtask.save()

                    # 更新task状态
                    subtask_count = models.SubTask.objects.filter(
                        task_id=task.id).exclude(subtask_status__in=[
                            'completed', 'exported', 'aborted'
                        ]).count()
                    # subtask_count = models.SubTask.objects.filter(task_id=tasks.id, subtask_status__in=['pending', 'running', 'exporting']).count()
                    if subtask_count == 0:
                        task.task_status = 'completed'
                        task.save()

                    res = awvs.report_file_request(scan_id, scanner_id)
                    if models.SubTask.update_error_msg(res, subtask.id):
                        time.sleep(10)
                        while True:
                            res = awvs.report_file_dowload(scanner_id, res[1])
                            if models.SubTask.update_error_msg(
                                    res, subtask.id):
                                if res[1].status_code == 200:
                                    # scan-result-report-{SCAN_REPORT_FORMAT}-{scanner_id}-{scan_id}-{subtask.target_id}.{SCAN_REPORT_FORMAT}
                                    filename = "scan-result-report-{0}-{1}-{2}-{3}.{4}".format(
                                        SCAN_REPORT_FORMAT, scanner_id,
                                        scan_id, subtask.target_id,
                                        SCAN_REPORT_FORMAT)
                                    filepath = "{0}/{1}".format(
                                        SCAN_RESULT_PDF_FILE_PATH, filename)
                                    destination = open(filepath, 'wb+')
                                    destination.write(res[1].content)
                                    destination.close()

                                    # 保存 子任务状态
                                    subtask.report_name = filename
                                    subtask.subtask_status = 'exported'
                                    subtask.save()

                                    # 发送通知和邮件
                                    data = {
                                        'notice_title': '任务进度通知',
                                        'notice_body': '您对' + task.task_name +
                                        '的扫描任务已完成,请及时查看结果',
                                        'notice_url':
                                        reverse("tasks:task_list"),
                                        'notice_type': 'notice',
                                        'task_id': task.id,
                                    }
                                    user = task.task_user
                                    Notice.notice_add(user, data)
                                    send_notice_mail(user.email, data)
                                    break
                            time.sleep(10)
                        break
            time.sleep(10)
    except Exception as e:
        traceback.print_exc()