-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
executable file
·124 lines (112 loc) · 5.21 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import re
import pprint
import logging
from PyQt5 import QtCore, QtGui, QtWidgets
""" Utilities module of static methods """
class Utils(object):
"""Utilities class """
@staticmethod
def update_status(application_path="", main_app=None, status=None, progress=None):
if status:
FORMAT = "[%(asctime)s ] %(levelname)s - %(filename)s; %(lineno)s: %(name)s.%(module)s.%(funcName)s(): %(message)s"
logging.basicConfig(filename=f'{application_path}/scans2reports.log', level=logging.INFO, format=FORMAT)
logging.info(status)
if main_app and main_app.main_window:
if status:
main_app.main_window.statusBar().showMessage(status)
if progress is not None:
main_app.main_window.progressBar.setValue( progress )
QtGui.QGuiApplication.processEvents()
@staticmethod
def fqdn(current_host ):
fqdn_val = 'UNKNOWN'
#TODO scap and ckl
if 'name' in current_host.attrib:
if next(iter(current_host.xpath("./HostProperties/tag[@name='host-fqdn']/text()")),''):
fqdn_val = str( next(iter(current_host.xpath("./HostProperties/tag[@name='host-fqdn']/text()")),'') ).lower()
elif next(iter(current_host.xpath("./HostProperties/tag[@name='hostname']/text()")),''):
fqdn_val = str(next(iter(current_host.xpath("./HostProperties/tag[@name='hostname']/text()")),'')).lower()
elif next(iter(current_host.xpath("./HostProperties/tag[@name='host-ip']/text()")),''):
fqdn_val = str(next(iter(current_host.xpath("./HostProperties/tag[@name='host-ip']/text()")),'')).lower()
return fqdn_val
@staticmethod
def is_ip(val):
""" determines if value is an IP address or not"""
regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)'''
if(re.search(regex, val)):
return True
else:
return False
@staticmethod
def clamp(number, minn, maxn):
""" ensures integer stays within range """
return max(min(maxn, number), minn)
@staticmethod
def parse_bool(source):
""" parses non boolean values into boolean results """
return bool(source.lower() in ['true', '1', 't', 'y', 'yes'])
@staticmethod
def status(source, form):
""" will convert from multiple status formats into a specified format """
if source is not None and form is not None:
cross_walk_from = {
'O' : 0, 'FAIL':0, 'OPEN': 0, 'ONGOING' : 0,
'NR' : 1, 'NOT_REVIEWED' : 1, 'NOT REVIEWED' : 1,
'NA' : 2, 'NOTAPPLICABLE': 2, 'NOT_APPLICABLE': 2, 'NOT APPLICABLE' : 2,
'C' : 3, 'NOTAFINDING': 3, 'CLOSED' : 3, 'PASS' : 3, 'COMPLETED' : 3,
'E' : 4, 'ERROR' : 4
}
cross_walk_to = {
'ABBREV' : {0: 'O', 1: 'NR', 2: 'NA', 3: 'C', 4: 'E'},
'HUMAN' : {
0: 'Ongoing', 1: 'Not Reviewed', 2: 'Not Applicable', 3: 'Closed', 4: 'Error'
},
'RAW' : {
0: 'Open', 1: 'Not_Reviewed', 2: 'Not_Applicable', 3: 'Closed', 4: 'Error'
},
}
return cross_walk_to[form][cross_walk_from[str(source).upper().strip()]]
return 'Status Unknown'
@staticmethod
def risk_val(source, form):
""" will convert from multiple risk formats into a specified format """
if source is not None and form is not None:
cross_walk_from = {
'' : 0, 'UNKNOWN':0,
'VL': 0, 'L': 1, 'M': 2, 'H': 3, 'VH': 4,
'NONE': 0, 'INFO': 0, 'LOW': 1, 'MEDIUM': 2, 'HIGH': 3,
'CRITICAL': 4, 'VERY HIGH' : 4,
'CATIV': 0, 'CATIII': 1, 'CATII': 2, 'CATI': 3,
'IV': 0, 'III': 1, 'II': 2, 'I': 3,
'CAT IV': 0, 'CAT III': 1, 'CAT II': 2, 'CAT I': 3,
'MODERATE': 2, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4,
'0':0, '1':1, '2':2, '3':3, '4':4
}
cross_walk_to = {
'VL-VH': {
0: 'VL', 1: 'L', 2: 'M', 3: 'H', 4: 'VH',
},
'VL VH': {
0: 'Very Low', 1: 'Low', 2: 'Medium', 3: 'High', 4: 'Very High',
},
'POAM': {
0: 'Very Low', 1: 'Low', 2: 'Moderate', 3: 'High', 4: 'Very High',
},
'N-C': {
0: 'None', 1: 'Low', 2: 'Medium', 3: 'High', 4: 'Critical',
},
'CAT': {
0: 'CAT IV', 1: 'CAT III', 2: 'CAT II', 3: 'CAT I', 4: 'CAT I',
},
'MIN': {
0: 'IV', 1: 'III', 2: 'II', 3: 'I', 4: 'I',
},
'NUM':{
0: 0, 1: 1, 2: 2, 3: 3, 4: 4,
},
}
return cross_walk_to[form][cross_walk_from[str(source).upper().strip()]]
return 'Risk Unknown'