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
|
1 |
|
from .converter import Converter |
13
|
|
|
|
14
|
|
|
|
15
|
1 |
|
class Client(): |
16
|
1 |
|
def __init__(self, args): |
17
|
1 |
|
self.parser = None |
18
|
1 |
|
self.MESSAGES = self._get_message() |
19
|
1 |
|
self.arg = self.parse_arguments(args) |
20
|
1 |
|
self.remove_pass_tests = self.arg.remove_pass_tests |
21
|
1 |
|
self.source_filename = self.arg.source_filename |
22
|
1 |
|
self.rule_name = self.arg.rule_id |
23
|
1 |
|
self.out = self.arg.output |
24
|
1 |
|
self.all_rules = self.arg.all |
25
|
1 |
|
self.isatty = sys.stdout.isatty() |
26
|
1 |
|
self.show_fail_rules = False |
27
|
1 |
|
self.show_not_selected_rules = False |
28
|
1 |
|
self.xml_parser = XmlParser( |
29
|
|
|
self.source_filename) |
30
|
1 |
|
if self.remove_pass_tests: |
31
|
|
|
raise NotImplementedError('Not implemented!') |
32
|
|
|
|
33
|
1 |
|
def _get_message(self): |
34
|
1 |
|
MESSAGES = { |
35
|
|
|
'description': '', |
36
|
|
|
'--output': '', |
37
|
|
|
'source_filename': '', |
38
|
|
|
} |
39
|
1 |
|
return MESSAGES |
40
|
|
|
|
41
|
|
|
|
42
|
1 |
|
def run_gui_and_return_answers(self): |
43
|
1 |
|
if self.isatty: |
44
|
1 |
|
if self.all_rules: |
45
|
|
|
return {'rules': [ |
46
|
|
|
rule['id_rule'] for rule in self.search_rules_id()]} |
47
|
|
|
else: |
48
|
1 |
|
try: |
49
|
1 |
|
import inquirer |
50
|
|
|
return inquirer.prompt(self.get_questions()) |
51
|
1 |
|
except ImportError: |
52
|
1 |
|
print(self.get_selection_rules()) |
53
|
1 |
|
return None |
54
|
|
|
else: |
55
|
|
|
return {'rules': [ |
56
|
|
|
rule['id_rule'] for rule in self.search_rules_id()]} |
57
|
|
|
|
58
|
1 |
|
def get_list_of_matched_rules(self): |
59
|
1 |
|
rules = self.search_rules_id() |
60
|
1 |
|
if self.show_fail_rules: |
61
|
1 |
|
rules = self._get_only_fail_rule(rules) |
62
|
1 |
|
return rules |
63
|
|
|
|
64
|
1 |
|
def get_list_of_lines(self): |
65
|
1 |
|
lines = ['== The Rule IDs =='] |
66
|
1 |
|
for rule in self.get_list_of_matched_rules(): |
67
|
1 |
|
lines.append("'" + rule['id_rule'] + r'\b' + "'") |
68
|
1 |
|
if self.show_not_selected_rules: |
69
|
1 |
|
for line in self.get_lines_of_wanted_not_selected_rules(): |
70
|
1 |
|
lines.append(line) |
71
|
1 |
|
lines.append( |
72
|
|
|
"You haven't got installed inquirer lib. " |
73
|
|
|
"Please copy id rule with you want use and put it in command") |
74
|
1 |
|
return lines |
75
|
|
|
|
76
|
1 |
|
def get_selection_rules(self): |
77
|
1 |
|
return "\n".join(self.get_list_of_lines()) |
78
|
|
|
|
79
|
1 |
|
def get_lines_of_wanted_not_selected_rules(self): |
80
|
1 |
|
out = [] |
81
|
1 |
|
out.append('== The not selected rule IDs ==') |
82
|
1 |
|
for rule in self._get_wanted_not_selected_rules(): |
83
|
1 |
|
out.append(rule['id_rule'] + '(Not selected)') |
84
|
1 |
|
return out |
85
|
|
|
|
86
|
1 |
|
def get_choices(self): |
87
|
1 |
|
rules = self.search_rules_id() |
88
|
1 |
|
if self.show_fail_rules: |
89
|
1 |
|
rules = self._get_only_fail_rule(rules) |
90
|
1 |
|
choices = [] |
91
|
1 |
|
for rule in rules: |
92
|
1 |
|
choices.append(rule['id_rule']) |
93
|
1 |
|
if self.show_not_selected_rules: |
94
|
1 |
|
print("\n".join(self.get_lines_of_wanted_not_selected_rules())) |
95
|
1 |
|
return choices |
96
|
|
|
|
97
|
1 |
|
def get_questions(self): |
98
|
1 |
|
choices = self.get_choices() |
99
|
1 |
|
from inquirer.questions import Checkbox as checkbox |
100
|
1 |
|
questions = [ |
101
|
|
|
checkbox( |
102
|
|
|
'rules', |
103
|
|
|
message=( |
104
|
|
|
"= The Rules IDs = (move - UP and DOWN arrows," |
105
|
|
|
" select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"), |
106
|
|
|
choices=choices, |
107
|
|
|
), |
108
|
|
|
] |
109
|
1 |
|
return questions |
110
|
|
|
|
111
|
1 |
|
def _get_only_fail_rule(self, rules): |
112
|
1 |
|
return list(filter(lambda rule: rule['result'] == 'fail', rules)) |
113
|
|
|
|
114
|
1 |
|
def _get_wanted_rules(self): |
115
|
1 |
|
return [ |
116
|
|
|
x for x in self.xml_parser.used_rules if re.search( |
117
|
|
|
self.rule_name, x['id_rule'])] |
118
|
|
|
|
119
|
1 |
|
def _get_wanted_not_selected_rules(self): |
120
|
1 |
|
return [ |
121
|
|
|
x for x in self.xml_parser.notselected_rules if re.search( |
122
|
|
|
self.rule_name, x['id_rule'])] |
123
|
|
|
|
124
|
1 |
|
def search_rules_id(self): |
125
|
1 |
|
rules = self._get_wanted_rules() |
126
|
1 |
|
notselected_rules = self._get_wanted_not_selected_rules() |
127
|
1 |
|
if len(notselected_rules) and not rules: |
128
|
1 |
|
raise ValueError( |
129
|
|
|
('err- rule(s) "{}" was not selected, ' |
130
|
|
|
"so there are no results. The rule is" |
131
|
|
|
' "notselected" because it' |
132
|
|
|
" wasn't a part of the executed profile" |
133
|
|
|
" and therefore it wasn't evaluated " |
134
|
|
|
"during the scan.") |
135
|
|
|
.format(notselected_rules[0]['id_rule'])) |
136
|
1 |
|
elif not notselected_rules and not rules: |
137
|
1 |
|
raise ValueError('err- 404 rule not found!') |
138
|
|
|
else: |
139
|
1 |
|
return rules |
140
|
|
|
|
141
|
1 |
|
def open_web_browser(self, src): |
142
|
1 |
|
if not self.off_webbrowser: |
143
|
|
|
src = os.path.join(src, 'index.html') |
144
|
|
|
try: |
145
|
|
|
webbrowser.get('firefox').open_new_tab(src) |
146
|
|
|
except BaseException: |
147
|
|
|
webbrowser.open_new_tab(src) |
148
|
|
|
|
149
|
1 |
|
def create_dict_of_rule(self, rule_id): |
150
|
1 |
|
converter = Converter(self.xml_parser.get_oval_tree(rule_id)) |
151
|
1 |
|
return converter.to_JsTree_dict() |
152
|
|
|
|
153
|
1 |
|
def copy_interpreter(self, dst): |
154
|
1 |
|
src = self.get_src('tree_html_interpreter') |
155
|
1 |
|
os.mkdir(dst) |
156
|
1 |
|
for item in os.listdir(src): |
157
|
1 |
|
s = os.path.join(src, item) |
158
|
1 |
|
d = os.path.join(dst, item) |
159
|
1 |
|
if os.path.isdir(s): |
160
|
1 |
|
shutil.copytree(s, d) |
161
|
|
|
else: |
162
|
1 |
|
shutil.copy2(s, d) |
163
|
|
|
|
164
|
1 |
|
def get_src(self, src): |
165
|
1 |
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
166
|
1 |
|
FIXTURE_DIR = os.path.join(_dir, src) |
167
|
1 |
|
return str(FIXTURE_DIR) |
168
|
|
|
|
169
|
1 |
|
def save_dict(self, dict_, src): |
170
|
1 |
|
with open(os.path.join(src, 'data.js'), "w+") as data_file: |
171
|
1 |
|
data_file.write("var data_json =" + str(json.dumps( |
172
|
|
|
dict_, sort_keys=False, indent=4) + ";")) |
173
|
|
|
|
174
|
1 |
|
def get_save_src(self, rule): |
175
|
1 |
|
date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
176
|
1 |
|
if self.out is not None: |
177
|
1 |
|
if not os.path.isdir(self.out): |
178
|
1 |
|
os.mkdir(self.out) |
179
|
1 |
|
return os.path.join(self.out, 'graph-of-' + rule + date) |
180
|
|
|
return os.path.join( |
181
|
|
|
self.out, |
182
|
|
|
'graph-of-' + rule + date) |
183
|
1 |
|
return os.path.join( |
184
|
|
|
os.getcwd(), |
185
|
|
|
'graph-of-' + rule + date) |
186
|
|
|
|
187
|
1 |
|
def parse_arguments(self, args): |
188
|
1 |
|
self.prepare_parser() |
189
|
1 |
|
args = self.parser.parse_args(args) |
190
|
1 |
|
return args |
191
|
|
|
|
192
|
1 |
|
def prepare_parser(self): |
193
|
1 |
|
self.parser = argparse.ArgumentParser( |
194
|
|
|
description=self.MESSAGES.get('description')) |
195
|
1 |
|
self.parser.add_argument( |
196
|
|
|
'--all', |
197
|
|
|
action="store_true", |
198
|
|
|
default=False, |
199
|
|
|
help="Process all matched rules.") |
200
|
1 |
|
self.parser.add_argument( |
201
|
|
|
'--remove-pass-tests', |
202
|
|
|
action="store_true", |
203
|
|
|
default=False, |
204
|
|
|
help=( |
205
|
|
|
"Do not display passing tests for better orientation in" |
206
|
|
|
" graphs that contain a large amount of nodes.(Not implemented)")) |
207
|
1 |
|
self.parser.add_argument( |
208
|
|
|
'-o', |
209
|
|
|
'--output', |
210
|
|
|
action="store", |
211
|
|
|
default=None, |
212
|
|
|
help=self.MESSAGES.get('--output')) |
213
|
1 |
|
self.parser.add_argument( |
214
|
|
|
"source_filename", |
215
|
|
|
help=self.MESSAGES.get('source_filename')) |
216
|
1 |
|
self.parser.add_argument( |
217
|
|
|
"rule_id", help=( |
218
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
219
|
|
|
" a part of the ID or a regular expression can be used." |
220
|
|
|
" If brackets are used in the regular expression " |
221
|
|
|
"the regular expression must be quoted.")) |
222
|
|
|
|