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