1
|
1 |
|
import re |
2
|
1 |
|
import argparse |
3
|
1 |
|
import tempfile |
4
|
1 |
|
import os |
5
|
1 |
|
import webbrowser |
6
|
1 |
|
import json |
7
|
1 |
|
import shutil |
8
|
1 |
|
from datetime import datetime |
9
|
1 |
|
import sys |
10
|
|
|
|
11
|
1 |
|
from .xml_parser import XmlParser |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class Client(): |
15
|
1 |
|
def __init__(self, args): |
16
|
1 |
|
self.parser = None |
17
|
1 |
|
self.MESSAGES = self._get_message() |
18
|
1 |
|
self.arg = self.parse_arguments(args) |
19
|
1 |
|
self.hide_passing_tests = self.arg.hide_passing_tests |
20
|
1 |
|
self.source_filename = self.arg.source_filename |
21
|
1 |
|
self.rule_name = self.arg.rule_id |
22
|
1 |
|
self.out = self.arg.output |
23
|
1 |
|
self.all_rules = self.arg.all |
24
|
1 |
|
self.all_in_one = None |
25
|
1 |
|
self.off_webbrowser = None |
26
|
1 |
|
self.isatty = sys.stdout.isatty() |
27
|
1 |
|
self.show_failed_rules = False |
28
|
1 |
|
self.show_not_selected_rules = False |
29
|
1 |
|
self.xml_parser = XmlParser( |
30
|
|
|
self.source_filename) |
31
|
1 |
|
self.parts = self.get_src('parts') |
32
|
|
|
|
33
|
1 |
|
def _get_message(self): |
34
|
1 |
|
MESSAGES = { |
35
|
|
|
'description': '', |
36
|
|
|
'--output': '', |
37
|
|
|
'source_filename': '', |
38
|
|
|
} |
39
|
1 |
|
return MESSAGES |
40
|
|
|
|
41
|
1 |
|
def print_red_text(self, text): |
42
|
|
|
CRED = '\033[91m' |
43
|
|
|
CEND = '\033[0m' |
44
|
|
|
print(CRED + str(text) + CEND) |
45
|
|
|
|
46
|
1 |
|
def run_gui_and_return_answers(self): |
47
|
1 |
|
if self.isatty: |
48
|
1 |
|
if self.all_rules: |
49
|
|
|
return self._get_rules() |
50
|
|
|
else: |
51
|
1 |
|
try: |
52
|
1 |
|
import inquirer |
53
|
1 |
|
return inquirer.prompt(self.get_questions()) |
54
|
1 |
|
except ImportError: |
55
|
1 |
|
print(self.get_selection_rules()) |
56
|
1 |
|
return None |
57
|
|
|
else: |
58
|
1 |
|
return self._get_rules() |
59
|
|
|
|
60
|
1 |
|
def _get_rules(self): |
61
|
1 |
|
if self.show_failed_rules: |
62
|
1 |
|
return {'rules': self._get_only_fail_rule(self.search_rules_id())} |
63
|
|
|
else: |
64
|
1 |
|
return {'rules': self.search_rules_id()} |
65
|
|
|
|
66
|
1 |
|
def get_list_of_matched_rules(self): |
67
|
1 |
|
rules = self.search_rules_id() |
68
|
1 |
|
if self.show_failed_rules: |
69
|
1 |
|
rules = self._get_only_fail_rule(rules) |
70
|
1 |
|
return rules |
71
|
|
|
|
72
|
1 |
|
def get_list_of_lines(self): |
73
|
1 |
|
lines = ['== The Rule IDs =='] |
74
|
1 |
|
for rule in self.get_list_of_matched_rules(): |
75
|
1 |
|
lines.append("'" + rule + r'\b' + "'") |
76
|
1 |
|
if self.show_not_selected_rules: |
77
|
1 |
|
for line in self.get_lines_of_wanted_not_selected_rules(): |
78
|
1 |
|
lines.append(line) |
79
|
1 |
|
lines.append( |
80
|
|
|
"You haven't got installed inquirer lib. " |
81
|
|
|
"Please copy id rule with you want use and put it in command") |
82
|
1 |
|
return lines |
83
|
|
|
|
84
|
1 |
|
def get_selection_rules(self): |
85
|
1 |
|
return "\n".join(self.get_list_of_lines()) |
86
|
|
|
|
87
|
1 |
|
def get_lines_of_wanted_not_selected_rules(self): |
88
|
1 |
|
out = [] |
89
|
1 |
|
out.append('== The not selected rule IDs ==') |
90
|
1 |
|
for rule in self._get_wanted_rules_from_array_of_IDs( |
91
|
|
|
self.xml_parser.notselected_rules): |
92
|
1 |
|
out.append(rule + '(Not selected)') |
93
|
1 |
|
return out |
94
|
|
|
|
95
|
1 |
|
def get_choices(self): |
96
|
1 |
|
rules = self.search_rules_id() |
97
|
1 |
|
if self.show_failed_rules: |
98
|
1 |
|
rules = self._get_only_fail_rule(rules) |
99
|
1 |
|
choices = rules |
100
|
1 |
|
if self.show_not_selected_rules: |
101
|
1 |
|
print("\n".join(self.get_lines_of_wanted_not_selected_rules())) |
102
|
1 |
|
return choices |
103
|
|
|
|
104
|
1 |
|
def get_questions(self): |
105
|
1 |
|
choices = self.get_choices() |
106
|
1 |
|
from inquirer.questions import Checkbox as checkbox |
107
|
1 |
|
questions = [ |
108
|
|
|
checkbox( |
109
|
|
|
'rules', |
110
|
|
|
message=( |
111
|
|
|
"= The Rules IDs = (move - UP and DOWN arrows," |
112
|
|
|
" select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"), |
113
|
|
|
choices=choices, |
114
|
|
|
), |
115
|
|
|
] |
116
|
1 |
|
return questions |
117
|
|
|
|
118
|
1 |
|
def _get_only_fail_rule(self, rules): |
119
|
1 |
|
return list( |
120
|
|
|
filter( |
121
|
|
|
lambda rule: self.xml_parser.used_rules[rule]['result'] == 'fail', |
122
|
|
|
rules)) |
123
|
|
|
|
124
|
1 |
|
def _get_wanted_rules_from_array_of_IDs(self, rules): |
125
|
1 |
|
return [ |
126
|
|
|
x for x in rules if re.search( |
127
|
|
|
self.rule_name, x)] |
128
|
|
|
|
129
|
1 |
|
def search_rules_id(self): |
130
|
1 |
|
rules = self._get_wanted_rules_from_array_of_IDs( |
131
|
|
|
self.xml_parser.used_rules.keys()) |
132
|
1 |
|
notselected_rules = self._get_wanted_rules_from_array_of_IDs( |
133
|
|
|
self.xml_parser.notselected_rules) |
134
|
1 |
|
return self._check_rules_id(rules, notselected_rules) |
135
|
|
|
|
136
|
1 |
|
def _check_rules_id(self, rules, notselected_rules): |
137
|
1 |
|
if len(notselected_rules) and not rules: |
138
|
1 |
|
raise ValueError( |
139
|
|
|
('Rule(s) "{}" was not selected, ' |
140
|
|
|
"so there are no results. The rule is" |
141
|
|
|
' "notselected" because it' |
142
|
|
|
" wasn't a part of the executed profile" |
143
|
|
|
" and therefore it wasn't evaluated " |
144
|
|
|
"during the scan.") |
145
|
|
|
.format(notselected_rules)) |
146
|
1 |
|
elif not notselected_rules and not rules: |
147
|
1 |
|
raise ValueError('404 rule "{}" not found!'.format(self.rule_name)) |
148
|
|
|
else: |
149
|
1 |
|
return rules |
150
|
|
|
|
151
|
1 |
|
def save_html_and_open_html(self, oval_tree_dict, src, rule, out): |
152
|
1 |
|
self.save_html_report(oval_tree_dict, src) |
153
|
1 |
|
self.print_output_message_and_open_web_browser(src, rule, out) |
154
|
|
|
|
155
|
1 |
|
def save_html_with_all_rules_in_one( |
156
|
|
|
self, dict_oval_trees, src, rules, out): |
157
|
|
|
self.save_html_report(dict_oval_trees, src, self.all_in_one) |
158
|
|
|
self.print_output_message_and_open_web_browser( |
159
|
|
|
src, self._format_rules_output(rules), out) |
160
|
|
|
|
161
|
1 |
|
def _format_rules_output(self, rules): |
162
|
|
|
out = '' |
163
|
|
|
for rule in rules['rules']: |
164
|
|
|
out += rule + '\n' |
165
|
|
|
return out |
166
|
|
|
|
167
|
1 |
|
def print_output_message_and_open_web_browser(self, src, rule, out): |
168
|
1 |
|
print('Rule(s) "{}" done!'.format(rule)) |
169
|
1 |
|
out.append(src) |
170
|
1 |
|
self.open_web_browser(src) |
171
|
|
|
|
172
|
1 |
|
def open_web_browser(self, src): |
173
|
1 |
|
if not self.off_webbrowser: |
174
|
|
|
try: |
175
|
|
|
webbrowser.get('firefox').open_new_tab(src) |
176
|
|
|
except BaseException: |
177
|
|
|
webbrowser.open_new_tab(src) |
178
|
|
|
|
179
|
1 |
|
def get_src(self, src): |
180
|
1 |
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
181
|
1 |
|
FIXTURE_DIR = os.path.join(_dir, src) |
182
|
1 |
|
return str(FIXTURE_DIR) |
183
|
|
|
|
184
|
1 |
|
def get_save_src(self, rule): |
185
|
1 |
|
date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
186
|
1 |
|
if self.out is not None: |
187
|
1 |
|
os.makedirs(self.out, exist_ok=True) |
188
|
1 |
|
return os.path.join( |
189
|
|
|
self.out, |
190
|
|
|
'graph-of-' + rule + date + '.html') |
191
|
1 |
|
return os.path.join( |
192
|
|
|
os.getcwd(), |
193
|
|
|
'graph-of-' + rule + date + '.html') |
194
|
|
|
|
195
|
1 |
|
def _get_part(self, part): |
196
|
1 |
|
with open(os.path.join(self.parts, part), "r") as data_file: |
197
|
1 |
|
return data_file.readlines() |
198
|
|
|
|
199
|
1 |
|
def _merge_report_parts(self, data): |
200
|
1 |
|
head = self._get_part('head.txt') |
201
|
1 |
|
css = self._get_part('css.txt') |
202
|
1 |
|
boot_strap_style = self._get_part('bootstrapStyle.txt') |
203
|
1 |
|
jsTree_style = self._get_part('jsTreeStyle.txt') |
204
|
1 |
|
jQuery_script = self._get_part('jQueryScript.txt') |
205
|
1 |
|
boot_strap_script = self._get_part('bootstrapScript.txt') |
206
|
1 |
|
jsTree_script = self._get_part('jsTreeScript.txt') |
207
|
1 |
|
body_start = ['</head>', '<body>', data] |
208
|
1 |
|
body = self._get_part('body.txt') |
209
|
1 |
|
script = self._get_part('script.js') |
210
|
1 |
|
footer = ['<script>', *script, '</script>', '</body>', '</html>'] |
211
|
1 |
|
return [ |
212
|
|
|
*head, |
213
|
|
|
*css, |
214
|
|
|
*boot_strap_style, |
215
|
|
|
*jsTree_style, |
216
|
|
|
*jQuery_script, |
217
|
|
|
*boot_strap_script, |
218
|
|
|
*jsTree_script, |
219
|
|
|
*body_start, |
220
|
|
|
*body, |
221
|
|
|
*footer] |
222
|
|
|
|
223
|
1 |
|
def save_html_report(self, dict_, src, status_all_in_one=False): |
224
|
1 |
|
data = ("\n<script>var all_in_one = " + |
225
|
|
|
str(status_all_in_one).lower() + |
226
|
|
|
";</script>" + |
227
|
|
|
"\n<script>var data_of_tree =" + |
228
|
|
|
str(json.dumps(dict_, sort_keys=False, indent=4)) + |
229
|
|
|
";</script>\n") |
230
|
1 |
|
if status_all_in_one: |
231
|
|
|
for rule in dict_: |
232
|
|
|
data += ('<h1>' + |
233
|
|
|
rule.split('-')[2] + |
234
|
|
|
'</h1>' + |
235
|
|
|
'<div id="' + |
236
|
|
|
re.sub(r'[\_\-\.]', "", rule) + |
237
|
|
|
'"></div>') |
238
|
1 |
|
with open(src, "w+") as data_file: |
239
|
1 |
|
data_file.writelines(self._merge_report_parts(data)) |
240
|
|
|
|
241
|
1 |
|
def parse_arguments(self, args): |
242
|
1 |
|
self.prepare_parser() |
243
|
1 |
|
args = self.parser.parse_args(args) |
244
|
1 |
|
return args |
245
|
|
|
|
246
|
1 |
|
def prepare_parser(self): |
247
|
1 |
|
self.parser = argparse.ArgumentParser( |
248
|
|
|
description=self.MESSAGES.get('description')) |
249
|
1 |
|
self.parser.add_argument( |
250
|
|
|
'--all', |
251
|
|
|
action="store_true", |
252
|
|
|
default=False, |
253
|
|
|
help="Process all matched rules.") |
254
|
1 |
|
self.parser.add_argument( |
255
|
|
|
'--hide-passing-tests', |
256
|
|
|
action="store_true", |
257
|
|
|
default=False, |
258
|
|
|
help=( |
259
|
|
|
"Do not display passing tests for better orientation in" |
260
|
|
|
" graphs that contain a large amount of nodes.(Not implemented)")) |
261
|
1 |
|
self.parser.add_argument( |
262
|
|
|
'-o', |
263
|
|
|
'--output', |
264
|
|
|
action="store", |
265
|
|
|
default=None, |
266
|
|
|
help=self.MESSAGES.get('--output')) |
267
|
1 |
|
self.parser.add_argument( |
268
|
|
|
"source_filename", |
269
|
|
|
help=self.MESSAGES.get('source_filename')) |
270
|
1 |
|
self.parser.add_argument( |
271
|
|
|
"rule_id", help=( |
272
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
273
|
|
|
" a part of the ID or a regular expression can be used." |
274
|
|
|
" If brackets are used in the regular expression " |
275
|
|
|
"the regular expression must be quoted.")) |
276
|
|
|
|