1
|
1 |
|
from __future__ import print_function, unicode_literals |
2
|
1 |
|
import re |
3
|
1 |
|
import oval_graph.xml_parser |
4
|
1 |
|
import oval_graph.oval_graph |
5
|
1 |
|
import oval_graph.converter |
6
|
1 |
|
import webbrowser |
7
|
1 |
|
import json |
8
|
1 |
|
import argparse |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
class client(): |
12
|
1 |
|
def __init__(self, args): |
13
|
1 |
|
self.arg = self.parse_arguments(args) |
14
|
1 |
|
self.remove_pass_tests = self.arg.remove_pass_tests |
15
|
1 |
|
self.show_fail_rules = self.arg.show_fail_rules |
16
|
1 |
|
self.show_not_selected_rules = self.arg.show_not_selected_rules |
17
|
1 |
|
self.off_webbrowser = self.arg.off_web_browser |
18
|
1 |
|
self.source_filename = self.arg.source_filename |
19
|
1 |
|
self.tree = self.arg.tree |
20
|
1 |
|
self.rule_name = self.arg.rule_id |
21
|
1 |
|
self.xml_parser = oval_graph.xml_parser.xml_parser( |
22
|
|
|
self.source_filename) |
23
|
1 |
|
if self.tree: |
24
|
1 |
|
self.html_interpreter = 'tree_html_interpreter' |
25
|
|
|
else: |
26
|
1 |
|
self.html_interpreter = 'graph_html_interpreter' |
27
|
1 |
|
if self.remove_pass_tests: |
28
|
|
|
raise NotImplementedError('Not implemented!') |
29
|
|
|
|
30
|
1 |
|
def run_gui_and_return_answers(self): |
31
|
1 |
|
try: |
32
|
1 |
|
import inquirer |
33
|
|
|
return inquirer.prompt(self.get_questions()) |
34
|
1 |
|
except ImportError: |
35
|
1 |
|
print('== The Rule IDs ==') |
36
|
1 |
|
rules = self.search_rules_id() |
37
|
1 |
|
if self.show_fail_rules: |
38
|
1 |
|
rules = self.get_only_fail_rule(rules) |
39
|
1 |
|
for rule in rules: |
40
|
1 |
|
print(rule['id_rule'] + r'\b') |
41
|
1 |
|
if self.show_not_selected_rules: |
42
|
1 |
|
print('== The not selected rule IDs ==') |
43
|
1 |
|
for rule in self._get_wanted_not_selected_rules(): |
44
|
1 |
|
print(rule['id_rule'] + '(Not selected)') |
45
|
1 |
|
return None |
46
|
|
|
|
47
|
1 |
|
def get_questions(self): |
48
|
1 |
|
rules = self.search_rules_id() |
49
|
1 |
|
if self.show_fail_rules: |
50
|
1 |
|
rules = self.get_only_fail_rule(rules) |
51
|
1 |
|
choices_ = [] |
52
|
1 |
|
for rule in rules: |
53
|
1 |
|
choices_.append(rule['id_rule']) |
54
|
1 |
|
if self.show_not_selected_rules: |
55
|
1 |
|
print('== The not selected rule IDs ==') |
56
|
1 |
|
for rule in self._get_wanted_not_selected_rules(): |
57
|
1 |
|
print(rule['id_rule'] + '(Not selected)') |
58
|
1 |
|
from inquirer.questions import Checkbox as checkbox |
59
|
1 |
|
questions = [ |
60
|
|
|
checkbox( |
61
|
|
|
'rules', |
62
|
|
|
message=( |
63
|
|
|
"= The Rules IDs = (move - UP and DOWN arrows," |
64
|
|
|
" select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"), |
65
|
|
|
choices=choices_, |
66
|
|
|
), |
67
|
|
|
] |
68
|
1 |
|
return questions |
69
|
|
|
|
70
|
1 |
|
def get_only_fail_rule(self, rules): |
71
|
1 |
|
return list(filter(lambda rule: rule['result'] == 'fail', rules)) |
72
|
|
|
|
73
|
1 |
|
def _get_wanted_rules(self): |
74
|
1 |
|
return [ |
75
|
|
|
x for x in self.xml_parser.get_used_rules() if re.search( |
76
|
|
|
self.rule_name, x['id_rule'])] |
77
|
|
|
|
78
|
1 |
|
def _get_wanted_not_selected_rules(self): |
79
|
1 |
|
return [ |
80
|
|
|
x for x in self.xml_parser.get_notselected_rules() if re.search( |
81
|
|
|
self.rule_name, x['id_rule'])] |
82
|
|
|
|
83
|
1 |
|
def search_rules_id(self): |
84
|
1 |
|
rules = self._get_wanted_rules() |
85
|
1 |
|
notselected_rules = self._get_wanted_not_selected_rules() |
86
|
1 |
|
if len(notselected_rules) and not rules: |
87
|
1 |
|
raise ValueError( |
88
|
|
|
('err- rule(s) "{}" was not selected, ' |
89
|
|
|
"so there are no results. The rule is" |
90
|
|
|
' "notselected" because it' |
91
|
|
|
" wasn't a part of the executed profile" |
92
|
|
|
" and therefore it wasn't evaluated " |
93
|
|
|
"during the scan.") |
94
|
|
|
.format(notselected_rules[0]['id_rule'])) |
95
|
1 |
|
elif not notselected_rules and not rules: |
96
|
1 |
|
raise ValueError('err- 404 rule not found!') |
97
|
|
|
else: |
98
|
1 |
|
return rules |
99
|
|
|
|
100
|
1 |
|
def create_dict_of_rule(self, rule_id): |
101
|
1 |
|
converter = oval_graph.converter.converter( |
102
|
|
|
oval_graph.oval_graph.build_nodes_form_xml( |
103
|
|
|
self.source_filename, rule_id)) |
104
|
1 |
|
if self.tree: |
105
|
1 |
|
return converter.to_JsTree_dict() |
106
|
1 |
|
return converter.to_sigma_dict(0, 0) |
107
|
|
|
|
108
|
1 |
|
def save_dict(self, dict): |
109
|
1 |
|
with open(self.xml_parser.get_src(self.html_interpreter + '/data.js'), "w+") as data_file: |
110
|
1 |
|
data_file.write("var data_json =" + str(json.dumps( |
111
|
|
|
dict, sort_keys=False, indent=4) + ";")) |
112
|
|
|
|
113
|
1 |
|
def prepare_data(self, rules): |
114
|
1 |
|
try: |
115
|
1 |
|
for rule in rules['rules']: |
116
|
1 |
|
oval_tree = self.create_dict_of_rule(rule) |
117
|
1 |
|
self.save_dict(oval_tree) |
118
|
1 |
|
self.open_web_browser() |
119
|
1 |
|
print('Rule "{}" done!'.format(rule)) |
120
|
1 |
|
except Exception as error: |
121
|
1 |
|
raise ValueError('Rule: "{}" Error: "{}"'.format(rule, error)) |
122
|
|
|
|
123
|
1 |
|
def open_web_browser(self): |
124
|
1 |
|
if not self.off_webbrowser: |
125
|
|
|
try: |
126
|
|
|
webbrowser.get('firefox').open_new_tab(self.xml_parser.get_src( |
127
|
|
|
self.html_interpreter + '/index.html')) |
128
|
|
|
except BaseException: |
129
|
|
|
webbrowser.open_new_tab(self.xml_parser.get_src( |
130
|
|
|
self.src_html_interpreter + '/index.html')) |
131
|
|
|
|
132
|
1 |
|
def parse_arguments(self, args): |
133
|
1 |
|
parser = argparse.ArgumentParser( |
134
|
|
|
description="Client for visualization of SCAP rule evaluation results") |
135
|
1 |
|
parser.add_argument( |
136
|
|
|
'--show-fail-rules', |
137
|
|
|
action="store_true", |
138
|
|
|
default=False, |
139
|
|
|
help="Show only FAIL rules") |
140
|
1 |
|
parser.add_argument( |
141
|
|
|
'--show-not-selected-rules', |
142
|
|
|
action="store_true", |
143
|
|
|
default=False, |
144
|
|
|
help="Show notselected rules. These rules will not be visualized.") |
145
|
1 |
|
parser.add_argument( |
146
|
|
|
'--off-web-browser', |
147
|
|
|
action="store_true", |
148
|
|
|
default=False, |
149
|
|
|
help="It does not start the web browser.") |
150
|
1 |
|
parser.add_argument( |
151
|
|
|
'--tree', |
152
|
|
|
action="store_true", |
153
|
|
|
default=False, |
154
|
|
|
help="Render the graph in a form of directory tree") |
155
|
1 |
|
parser.add_argument( |
156
|
|
|
'--remove-pass-tests', |
157
|
|
|
action="store_true", |
158
|
|
|
default=False, |
159
|
|
|
help=( |
160
|
|
|
"Do not display passing tests for better orientation in" |
161
|
|
|
" graphs that contain a large amount of nodes.(Not implemented)")) |
162
|
1 |
|
parser.add_argument("source_filename", help="ARF scan file") |
163
|
1 |
|
parser.add_argument( |
164
|
|
|
"rule_id", help=( |
165
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
166
|
|
|
" a part of the ID or a regular expression can be used." |
167
|
|
|
" If brackets are used in the regular expression " |
168
|
|
|
"the regular expression must be quoted.")) |
169
|
1 |
|
args = parser.parse_args(args) |
170
|
|
|
|
171
|
|
|
return args |
172
|
|
|
|