def vuln_an_id(scan_id, project_id): """ The function is filtering all data from OpenVAS and dumping to Archery database. :param scan_id: :return: """ ov_ip = "" ov_user = "" ov_pass = "" all_openvas = OpenvasSettingDb.objects.filter() scan_status = "100" date_time = datetime.datetime.now() for openvas in all_openvas: ov_user = openvas.user ov_pass = openvas.password ov_ip = openvas.host scanner = VulnscanManager(str(ov_ip), str(ov_user), str(ov_pass)) openvas_results = scanner.get_raw_xml(str(scan_id)) hosts = OpenVas_Parser.get_hosts(openvas_results) del_old = NetworkScanDb.objects.filter(scan_id=scan_id) del_old.delete() for host in hosts: scan_dump = NetworkScanDb( ip=host, scanner="Openvas", scan_id=scan_id, date_time=date_time, project_id=project_id, scan_status=scan_status, ) scan_dump.save() OpenVas_Parser.updated_xml_parser(project_id=project_id, scan_id=scan_id, root=openvas_results)
def vuln_an_id(scan_id, project_id, username): """ The function is filtering all data from OpenVAS and dumping to Archery database. :param scan_id: :return: """ global name, host, severity, port, creation_time, modification_time, threat, severity, description, family, cvss_base, cve, bid, xref, tags, banner, date_time, false_positive, duplicate_hash, duplicate_vuln, ov_ip, ov_user, ov_pass all_openvas = openvas_setting_db.objects.filter(username=username) scan_status = "100" date_time = datetime.now() for openvas in all_openvas: ov_user = openvas.user ov_pass = openvas.password ov_ip = openvas.host scanner = VulnscanManager(str(ov_ip), str(ov_user), str(ov_pass)) openvas_results = scanner.get_raw_xml(str(scan_id)) hosts = OpenVas_Parser.get_hosts(openvas_results) del_old = openvas_scan_db.objects.filter(scan_id=scan_id) del_old.delete() for host in hosts: scan_dump = openvas_scan_db(scan_ip=host, scan_id=host, date_time=date_time, project_id=project_id, scan_status=scan_status, username=username) scan_dump.save() OpenVas_Parser.updated_xml_parser(project_id=project_id, scan_id=scan_id, root=openvas_results, username=username)
def vuln_an_id(scan_id): """ The function is filtering all data from OpenVAS and dumping to Archery database. :param scan_id: :return: """ # ov_user = openvas_setting.openvas_username() # ov_pass = openvas_setting.openvas_pass() # ov_ip = openvas_setting.openvas_host() # # lod_ov_user = signing.loads(ov_user) # lod_ov_pass = signing.loads(ov_pass) # lod_ov_ip = signing.loads(ov_ip) all_openvas = openvas_setting_db.objects.all() for openvas in all_openvas: ov_user = openvas.user ov_pass = openvas.password ov_ip = openvas.host lod_ov_user = ov_user lod_ov_pass = ov_pass lod_ov_ip = ov_ip scanner = VulnscanManager(str(lod_ov_ip), str(lod_ov_user), str(lod_ov_pass)) openvas_results = scanner.get_raw_xml(str(scan_id)) for openvas in openvas_results.findall(".//result"): for r in openvas: if r.tag == "name": global name if r.text is None: name = "NA" else: name = r.text if r.tag == "creation_time": global creation_time if r.text is None: creation_time = "NA" else: creation_time = r.text if r.tag == "modification_time": global modification_time if r.text is None: modification_time = "NA" else: modification_time = r.text if r.tag == "host": global host if r.text is None: host = "NA" else: host = r.text if r.tag == "port": global port if r.text is None: port = "NA" else: port = r.text if r.tag == "threat": global threat if r.text is None: threat = "NA" else: threat = r.text if r.tag == "severity": global severity if r.text is None: severity = "NA" else: severity = r.text if r.tag == "description": global description if r.text is None: description = "NA" else: description = r.text for rr in r.getchildren(): if rr.tag == "family": global family if rr.text is None: family = "NA" else: family = rr.text if rr.tag == "cvss_base": global cvss_base if rr.text is None: cvss_base = "NA" else: cvss_base = rr.text if rr.tag == "cve": global cve if rr.text is None: cve = "NA" else: cve = rr.text if rr.tag == "bid": global bid if rr.text is None: bid = "NA" else: bid = rr.text if rr.tag == "xref": global xref if rr.text is None: xref = "NA" else: xref = rr.text if rr.tag == "tags": global tags if rr.text is None: tags = "NA" else: tags = rr.text if rr.tag == "type": global banner if rr.text is None: banner = "NA" else: banner = rr.text date_time = timezone.now() vul_id = uuid.uuid4() s_data = scan_save_db.objects.filter(scan_id=scan_id) for data in s_data: if data.scan_ip == host: dup_data = name + host + severity duplicate_hash = hashlib.sha256(dup_data).hexdigest() save_all = ov_scan_result_db( scan_id=scan_id, vul_id=vul_id, name=name, creation_time=creation_time, modification_time=modification_time, host=host, port=port, threat=threat, severity=severity, description=description, family=family, cvss_base=cvss_base, cve=cve, bid=bid, xref=xref, tags=tags, banner=banner, date_time=date_time, false_positive='No', vuln_status='Open', dup_hash=duplicate_hash) save_all.save() openvas_vul = ov_scan_result_db.objects.filter(scan_id=scan_id) \ .values('name', 'threat').distinct() total_vul = len(openvas_vul) total_high = len(openvas_vul.filter(threat="High")) total_medium = len(openvas_vul.filter(threat="Medium")) total_low = len(openvas_vul.filter(threat="Low")) total_log = len(openvas_vul.filter(threat="Log")) scan_status = str(scanner.get_progress(str(scan_id))) scan_save_db.objects.filter(scan_id=scan_id) \ .update(total_vul=total_vul, high_total=total_high, medium_total=total_medium, low_total=total_low, log_total=total_log, scan_status=scan_status)
def Scan_Launch(scan_ip, project_id, sel_profile): with open(openvas_data, 'r+') as f: data = json.load(f) ov_user = data['open_vas_user'] ov_pass = data['open_vas_pass'] ov_ip = data['open_vas_ip'] lod_ov_user = signing.loads(ov_user) lod_ov_pass = signing.loads(ov_pass) lod_ov_ip = signing.loads(ov_ip) scanner = VulnscanManager(str(lod_ov_ip), str(lod_ov_user), str(lod_ov_pass)) time.sleep(5) profile = None if profile is None: profile = "Full and fast" else: profile = sel_profile scan_id, target_id = scanner.launch_scan(target=str(scan_ip), profile=str(profile)) save_all = scan_save_db(scan_id=str(scan_id), project_id=str(project_id), scan_ip=str(scan_ip), target_id=str(target_id)) save_all.save() while int(scanner.get_progress(str(scan_id))) < 100.0: print 'Scan progress %: ' + str(scanner.get_progress(str(scan_id))) status = str(scanner.get_progress(str(scan_id))) scan_save_db.objects.filter(scan_id=scan_id).update(scan_status=status) time.sleep(5) global status status = "100" scan_save_db.objects.filter(scan_id=scan_id).update(scan_status=status) if profile == "Discovery": print "returning....." else: time.sleep(10) try: openvas_results = scanner.get_raw_xml(str(scan_id)) vul_an_id(scan_id, openvas_results) except Exception as e: print e try: openvas_vul = ov_scan_result_db.objects.filter(scan_id=scan_id).order_by('scan_id') total_vul = len(openvas_vul) total_high = len(openvas_vul.filter(threat="High")) total_medium = len(openvas_vul.filter(threat="Medium")) total_low = len(openvas_vul.filter(threat="Low")) scan_save_db.objects.filter(scan_id=scan_id).update(total_vul=total_vul, high_total=total_high, medium_total=total_medium, low_total=total_low) except Exception as e: print e try: for vul_id in ov_scan_result_db.objects.values_list('vul_id', flat=True).distinct(): ov_scan_result_db.objects.filter( pk=ov_scan_result_db.objects.filter(vul_id=vul_id).values_list('id', flat=True)[1:]).delete() except Exception as e: print e
def Scan_Launch(scan_ip, project_id, sel_profile): with open(openvas_data, 'r+') as f: data = json.load(f) ov_user = data['open_vas_user'] ov_pass = data['open_vas_pass'] ov_ip = data['open_vas_ip'] lod_ov_user = signing.loads(ov_user) lod_ov_pass = signing.loads(ov_pass) lod_ov_ip = signing.loads(ov_ip) scanner = VulnscanManager(str(lod_ov_ip), str(lod_ov_user), str(lod_ov_pass)) time.sleep(5) profile = None if profile is None: profile = "Full and fast" else: profile = sel_profile scan_id, target_id = scanner.launch_scan(target=str(scan_ip), profile=str(profile)) date_time = datetime.datetime.now() save_all = scan_save_db(scan_id=str(scan_id), project_id=str(project_id), scan_ip=str(scan_ip), target_id=str(target_id), date_time=date_time) save_all.save() while int(scanner.get_progress(str(scan_id))) < 100.0: print 'Scan progress %: ' + str(scanner.get_progress(str(scan_id))) status = str(scanner.get_progress(str(scan_id))) scan_save_db.objects.filter(scan_id=scan_id).update(scan_status=status) time.sleep(5) global status status = "100" scan_save_db.objects.filter(scan_id=scan_id).update(scan_status=status) if profile == "Discovery": print "returning....." else: time.sleep(10) try: openvas_results = scanner.get_raw_xml(str(scan_id)) vul_an_id(scan_id, openvas_results) except Exception as e: print e try: openvas_vul = ov_scan_result_db.objects.filter(scan_id=scan_id).values('name', 'severity', 'vuln_color', 'threat', 'host', 'port').distinct() total_vul = len(openvas_vul) total_high = len(openvas_vul.filter(threat="High")) total_medium = len(openvas_vul.filter(threat="Medium")) total_low = len(openvas_vul.filter(threat="Low")) scan_save_db.objects.filter(scan_id=scan_id).update(total_vul=total_vul, high_total=total_high, medium_total=total_medium, low_total=total_low) except Exception as e: print e try: for vul_id in ov_scan_result_db.objects.values_list('vul_id', flat=True).distinct(): ov_scan_result_db.objects.filter( pk=ov_scan_result_db.objects.filter(vul_id=vul_id).values_list('id', flat=True)[1:]).delete() except Exception as e: print e
def vuln_an_id(scan_id, project_id): """ The function is filtering all data from OpenVAS and dumping to Archery database. :param scan_id: :return: """ # ov_user = openvas_setting.openvas_username() # ov_pass = openvas_setting.openvas_pass() # ov_ip = openvas_setting.openvas_host() # # lod_ov_user = signing.loads(ov_user) # lod_ov_pass = signing.loads(ov_pass) # lod_ov_ip = signing.loads(ov_ip) all_openvas = openvas_setting_db.objects.all() for openvas in all_openvas: ov_user = openvas.user ov_pass = openvas.password ov_ip = openvas.host lod_ov_user = ov_user lod_ov_pass = ov_pass lod_ov_ip = ov_ip scanner = VulnscanManager(str(lod_ov_ip), str(lod_ov_user), str(lod_ov_pass)) openvas_results = scanner.get_raw_xml(str(scan_id)) for openvas in openvas_results.findall(".//result"): for r in openvas: if r.tag == "name": global name if r.text is None: name = "NA" else: name = r.text if r.tag == "creation_time": global creation_time if r.text is None: creation_time = "NA" else: creation_time = r.text if r.tag == "modification_time": global modification_time if r.text is None: modification_time = "NA" else: modification_time = r.text if r.tag == "host": global host if r.text is None: host = "NA" else: host = r.text if r.tag == "port": global port if r.text is None: port = "NA" else: port = r.text if r.tag == "threat": global threat if r.text is None: threat = "NA" else: threat = r.text if r.tag == "severity": global severity if r.text is None: severity = "NA" else: severity = r.text if r.tag == "description": global description if r.text is None: description = "NA" else: description = r.text for rr in r.getchildren(): if rr.tag == "family": global family if rr.text is None: family = "NA" else: family = rr.text if rr.tag == "cvss_base": global cvss_base if rr.text is None: cvss_base = "NA" else: cvss_base = rr.text if rr.tag == "cve": global cve if rr.text is None: cve = "NA" else: cve = rr.text if rr.tag == "bid": global bid if rr.text is None: bid = "NA" else: bid = rr.text if rr.tag == "xref": global xref if rr.text is None: xref = "NA" else: xref = rr.text if rr.tag == "tags": global tags if rr.text is None: tags = "NA" else: tags = rr.text if rr.tag == "type": global banner if rr.text is None: banner = "NA" else: banner = rr.text date_time = timezone.now() vul_id = uuid.uuid4() dup_data = name + host + severity + port duplicate_hash = hashlib.sha256(dup_data.encode('utf-8')).hexdigest() match_dup = ov_scan_result_db.objects.filter( vuln_duplicate=duplicate_hash).values('vuln_duplicate').distinct() lenth_match = len(match_dup) if lenth_match == 1: duplicate_vuln = 'Yes' elif lenth_match == 0: duplicate_vuln = 'No' else: duplicate_vuln = 'None' false_p = ov_scan_result_db.objects.filter( false_positive_hash=duplicate_hash) fp_lenth_match = len(false_p) if fp_lenth_match == 1: false_positive = 'Yes' else: false_positive = 'No' save_all = ov_scan_result_db(scan_id=scan_id, vul_id=vul_id, name=name, creation_time=creation_time, modification_time=modification_time, host=host, port=port, threat=threat, severity=severity, description=description, family=family, cvss_base=cvss_base, cve=cve, bid=bid, xref=xref, tags=tags, banner=banner, date_time=date_time, false_positive=false_positive, vuln_status='Open', dup_hash=duplicate_hash, vuln_duplicate=duplicate_vuln, project_id=project_id, ) save_all.save() openvas_vul = ov_scan_result_db.objects.filter(scan_id=scan_id) total_high = len(openvas_vul.filter(threat="High")) total_medium = len(openvas_vul.filter(threat="Medium")) total_low = len(openvas_vul.filter(threat="Low")) log_total = len(openvas_vul.filter(threat="Log")) total_duplicate = len(openvas_vul.filter(vuln_duplicate='Yes')) total_vul = total_high + total_medium + total_low scan_save_db.objects.filter(scan_id=scan_id). \ update(total_vul=total_vul, high_total=total_high, medium_total=total_medium, log_total=log_total, low_total=total_low, total_dup=total_duplicate, ) for row in ov_scan_result_db.objects.all(): if ov_scan_result_db.objects.filter(name=row.name, port=row.port, scan_id=scan_id).count() > 1: row.delete()
def launch_scan(request): all_ip = scan_save_db.objects.all() scanner = VulnscanManager(scan_host, user, password) time.sleep(5) if request.method == 'POST': all_ip = scan_save_db.objects.all() scan_ip = request.POST.get('ip') profile = None if profile is None: profile = "Full and fast" else: profile = request.POST.get('scan_profile') scan_id, target_id = scanner.launch_scan(target=str(scan_ip), profile=str(profile)) save_all = scan_save_db(scan_id=str(scan_id), scan_ip=str(scan_ip), target_id=str(target_id)) save_all.save() while int(scanner.get_progress(str(scan_id))) < 100.0: print 'Scan progress %: ' + str(scanner.get_progress(str(scan_id))) status = str(scanner.get_progress(str(scan_id))) scan_save_db.objects.filter(scan_id=scan_id).update( scan_status=status) time.sleep(5) global status status = "100" scan_save_db.objects.filter(scan_id=scan_id).update(scan_status=status) if profile == "Discovery": print "returning....." else: time.sleep(10) try: openvas_results = scanner.get_raw_xml(str(scan_id)) vul_an_id(scan_id, openvas_results) except Exception as e: print e try: openvas_vul = ov_scan_result_db.objects.filter( Q(scan_id=scan_id)).order_by('scan_id') total_vul = len(openvas_vul) total_high = len(openvas_vul.filter(threat="High")) total_medium = len(openvas_vul.filter(threat="Medium")) total_low = len(openvas_vul.filter(threat="Low")) scan_save_db.objects.filter(scan_id=scan_id).update( total_vul=total_vul, high_total=total_high, medium_total=total_medium, low_total=total_low) except Exception as e: print e try: for vul_id in ov_scan_result_db.objects.values_list( 'vul_id', flat=True).distinct(): ov_scan_result_db.objects.filter( pk=ov_scan_result_db.objects.filter(vul_id=vul_id). values_list('id', flat=True)[1:]).delete() except Exception as e: print e return render_to_response('vul_details.html', {'all_ip': all_ip})