-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.py
243 lines (232 loc) · 8.98 KB
/
search.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
This module controls the searching of nodes and templates.
"""
import re
import initialize
from processdb import process_json
from get_property import get_template_directory
from get_property import get_policy_directory
from get_property import get_home_directory
def search_node(argument_node,node_object):
"""
The function search_node will search through the list of nodes
from the user's query and match a single or multiple node(s).
"""
node_list = extract_nodes(node_object)
query = re.compile(argument_node)
search_result = list(filter(query.match,node_list))
return search_result
def extract_nodes(node_object):
"""
This function will extract all the nodes from the
database of node_objects so that it can run the search against
the list of node(s).
"""
node_list = []
index = 0
for node in node_object:
name = node_object[index]['name']
node_list.append(name)
index = index + 1
return node_list
def search_template(template_list,safe_push_list,match_node,node_template,node_object,auditcreeper,push_cfgs):
"""
This function will take the search results from the list of nodes
and run it against node_object to determine the hardware vendor and type
and compare with the node_template database to match. Once matched,
it will check to verify an existing template is available.
"""
search_result = []
index = 0
element = 0
for node in match_node:
for node_obj in node_object:
if node == node_obj['name']:
"""
index variable gets the position in the list and appends it to the global variable 'element'.
"""
index = node_object.index(node_obj)
initialize.element.append(index)
"""
This section will pull out all the templates belonging to the specific
hardware vendor, operating system and type from the template database.
"""
for node_temp in node_template:
if node_obj['hardware_vendor'] == node_temp['hardware_vendor'] and node_obj['opersys'] == node_temp['opersys'] and node_obj['type'] == node_temp['type']:
if auditcreeper:
template_node_list = []
for template_dir_name in node_temp['templates']:
template_name = list(template_dir_name)[0].split('/')[-1]
template_node_list.append(template_name)
safe_push = list(template_dir_name.values())[0]
safe_push_list.append(safe_push)
if 'disabled' in safe_push_list and push_cfgs:
run_time = 1
first_run = True
disabled_templates = disabled_safe_push_element(safe_push_list,template_node_list,node_obj)
for index in disabled_templates:
if first_run:
template_node_list.pop(index)
first_run = False
else:
template_node_list.pop(index - run_time)
run_time = run_time + 1
"""
If all templates are disabled, exit.
"""
if len(safe_push_list) and 'enabled' in safe_push_list and push_cfgs:
search_result.append("MATCH")
if len(template_node_list) > 0 and not push_cfgs:
search_result.append("MATCH")
if len(template_node_list) == 0 and push_cfgs:
exit()
template_list.append(template_node_list)
del safe_push_list[:]
else:
directory = get_template_directory(node_obj['hardware_vendor'],node_obj['opersys'],node_obj['type'])
file = directory + template_list[element]
template_index = 0
template_node_list = []
node_templates = node_temp['templates'].copy()
for template_path in node_temp['templates']:
template_name = list(template_path)[0].split('/')[-1]
template_node_list.append(template_name)
node_temp['templates'][template_index] = list(template_path)[0].replace('~','{}'.format(get_home_directory()))
safe_push = list(template_path.values())[0]
safe_push_list.append(safe_push)
template_index = template_index + 1
try:
template_index = template_node_list.index(template_list[element])
if safe_push_list[template_index] != 'enabled' and push_cfgs:
print('[x] {} ; {}'.format(node_obj['name']),template_node_list[template_index])
exit()
except Exception as error:
pass
if file in node_temp['templates']:
search_result.append("MATCH")
node_temp['templates'] = node_templates.copy()
else:
print('[x] No associating template {}'.format(template_list[element]) + ' for node {}'.format(node))
exit()
search_result.append("NO MATCH")
node_temp['templates'] = node_templates.copy()
del safe_push_list[:]
else:
continue
else:
continue
return search_result
def search_policy(policy_list,safe_push_list,match_node,node_policy,node_object,auditcreeper,push_acl):
"""
This function will take the search results from the list of nodes
and run it against node_object to determine the hardware vendor, operating system and type
and compare with the node_policy database to match. If a node is not
deemed as a firewall, it will not allow a policy push.
"""
search_result = []
index = 0
element = 0
for node in match_node:
for node_obj in node_object:
if node == node_obj['name']:
"""
index variable gets the position in the list and appends it to the global variable 'element'.
"""
index = node_object.index(node_obj)
initialize.element.append(index)
"""
This section will pull out all the policies belonging to the specific
hardware vendor, operating system and type from the policy database.
"""
if node_object[index]['type'] != 'firewall' or node_object[index]['type'] != 'vfirewall':
pass
else:
for node_pol in node_policy:
print(node,node_pol['name'])
if node == node_pol['name']:
# policy_index = node_policy.index(node_pol)
# initialize.element_policy.append(policy_index)
if auditcreeper:
policy_node_list = []
for policy_dir_name in node_pol['policy']:
policy_name = list(policy_dir_name)[0].split('/')[-1]
policy_node_list.append(policy_name)
safe_push = list(policy_dir_name.values())[0]
safe_push_list.append(safe_push)
if 'disabled' in safe_push_list and push_acl:
run_time = 1
first_run = True
disabled_policies = disabled_safe_push_element(safe_push_list,policy_node_list,node_obj)
for index in disabled_policies:
if first_run:
policy_node_list.pop(index)
first_run = False
else:
policy_node_list.pop(index - run_time)
run_time = run_time + 1
"""
If all policies are disabled.
"""
if len(policy_node_list) == 0:
exit()
policy_list.append(policy_node_list)
del safe_push_list[:]
else:
directory = get_policy_directory(node_obj['hardware_vendor'],node_obj['opersys'],node_obj['type'])
file = directory + policy_list[element]
policy_index = 0
policy_node_list = []
node_policy = node_pol['policy'].copy()
for policy_path in node_pol['policy']:
policy_name = list(policy_path)[0].split('/')[-1]
policy_node_list.append(policy_name)
node_pol['policy'][policy_index] = list(policy_path)[0].replace('~','{}'.format(get_home_directory()))
safe_push = list(policy_path.values())[0]
safe_push_list.append(safe_push)
policy_index = policy_index + 1
try:
policy_index = policy_node_list.index(policy_list[element])
if safe_push_list[policy_index] != 'enabled':
print('[x] {} ; {}'.format(node_obj['name']),policy_node_list[policy_index])
exit()
except Exception as error:
pass
if file in node_pol['policy']:
search_result.append("MATCH")
node_pol['policy'] = node_policy.copy()
else:
print('[x] No associating policy {}'.format(policy_list[element]) + ' for node {}'.format(node))
search_result.append("NO MATCH")
# node_pol['policy'] = node_policy.copy()
else:
continue
else:
continue
return search_result
def node_element(match_node,node_object):
"""
This function appends the position index of the match results (match_node) against
the overall node_objects. This function call is only needed when search_template
function is not used.
"""
index = 0
for node in match_node:
for node_obj in node_object:
if node == node_obj['name']:
index = node_object.index(node_obj)
initialize.element.append(index)
return None
def disabled_safe_push_element(safe_push_list,template_node_list,node_obj):
"""
This function appends the position index of the match results (match_node) against
the overall node_objects. This function call is only needed when search_template
function is not used.
"""
index = 0
disabled_templates = []
for element in safe_push_list:
if element == 'disabled':
print('[x] {} ; {}'.format(node_obj['name'],template_node_list[index]))
disabled_templates.append(index)
index = index + 1
return disabled_templates