-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsecdump.py
executable file
·354 lines (282 loc) · 10.4 KB
/
secdump.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# secdump - A utility to dump PAN-OS security rulebases into comma-delimited output
__author__ = "Robert Hagen (@stealthllama)"
__copyright__ = "Copyright 2018, Palo Alto Networks"
__version__ = "0.1"
__license__ = "GPL"
__status__ = "Development"
from pan.xapi import *
import xml.etree.ElementTree as eT
import argparse
import getpass
def open_file(filename):
try:
outfilehandle = open(filename, 'w')
return outfilehandle
except IOError:
print("Error: Cannot open file %s" % filename)
def make_parser():
# Parse the arguments
parser = argparse.ArgumentParser(description="Export security rules from a Palo Alto Networks firewall")
parser.add_argument("-u", "--username", help="administrator username")
parser.add_argument("-p", "--password", help="administrator password", default='')
parser.add_argument("-f", "--firewall", help="firewall address")
parser.add_argument("-t", "--tag", help="firewall tag from the .panrc file", default='')
parser.add_argument("-o", "--outfile", help="output file", default='')
args = parser.parse_args()
if args.password == '':
args.password = getpass.getpass()
return args
def get_local_tree(thisconn):
rulebase_xpath = \
"/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/rulebase/security/rules"
thisconn.get(xpath=rulebase_xpath)
tree = eT.fromstring(thisconn.xml_result())
return tree
def get_shared_tree(thisconn):
thisconn.op(cmd="<show><config><pushed-shared-policy></pushed-shared-policy></config></show>")
tree = eT.fromstring(thisconn.xml_result())
if tree is not unicode:
prerules = tree.find('panorama/pre-rulebase/security/rules')
postrules = tree.find('panorama/post-rulebase/security/rules')
return prerules, postrules
else:
return tree
def get_predefined_tree(thisconn):
rulebase_xpath = "/config/predefined/default-security-rules"
thisconn.get(xpath=rulebase_xpath)
tree = eT.fromstring(thisconn.xml_result())
return tree
def write_security_header(thisfile):
thisfile.write(',Name,Tags,Type,Source Zone,Source Address,Source User,Source HIP Profile,Destination Zone,Destination Address,Application,Service,URL Category,Action,Profile,Options,Description\n')
def format_members(thislist):
outlist = ";".join(str(x) for x in thislist)
return outlist
def write_security_rule(rule, f, rulecount, t):
#
# Process the rule
#
# Is the rule disabled?
rule_state = rule.find('disabled')
if rule_state is None:
status = ''
else:
status = '[Disabled] '
# Get the rule name
rule_name = rule.get('name')
# Get the tag members
tag = []
for tag_iter in rule.iterfind('tag/member'):
tag.append(tag_iter.text)
# Get the from_zone members
from_zone = []
for from_iter in rule.iterfind('from/member'):
from_zone.append(from_iter.text)
# Get the to_zone members
to_zone = []
for to_iter in rule.iterfind('to/member'):
to_zone.append(to_iter.text)
# Get the source address members
source = []
for source_iter in rule.iterfind('source/member'):
source.append(source_iter.text)
# Get the destination address members
destination = []
for dest_iter in rule.iterfind('destination/member'):
destination.append(dest_iter.text)
# Get the source user members
user = []
for user_iter in rule.iterfind('source-user/member'):
user.append(user_iter.text)
# Get the HIP profile members
hip = []
for hip_iter in rule.iterfind('hip-profiles/member'):
hip.append(hip_iter.text)
# Get the URL category members
category = []
for category_iter in rule.iterfind('category/member'):
category.append(category_iter.text)
# Get the application members
application = []
for application_iter in rule.iterfind('application/member'):
application.append(application_iter.text)
# Get the service members
service = []
for service_iter in rule.iterfind('service/member'):
service.append(service_iter.text)
# Get the action
action = rule.find('action')
# Get the log setting
log_setting = rule.find('log-setting')
# Get the description
description = rule.find('description')
# Get the profiles or profile group
av_profile = []
vuln_profile = []
spyware_profile = []
url_profile = []
data_profile = []
file_profile = []
wildfire_profile = []
profile_group = []
if rule.find('profile-setting/group'):
profile_group = rule.find('profile-setting/group/member')
elif rule.find('profile-setting/profiles'):
av_profile = rule.find('profile-setting/profiles/virus/member')
vuln_profile = rule.find('profile-setting/profiles/vulnerability/member')
spyware_profile = rule.find('profile-setting/profiles/spyware/member')
url_profile = rule.find('profile-setting/profiles/url-filtering/member')
data_profile = rule.find('profile-setting/profiles/data-filtering/member')
file_profile = rule.find('profile-setting/profiles/file-blocking/member')
wildfire_profile = rule.find('profile-setting/profiles/wildfire-analysis/member')
#
# Let's write the rule
#
# Write the rule count
f.write(str(rulecount) + ',')
# Write the rule name
f.write(status + rule_name + ',')
# Write the tag members (if defined)
if len(tag) == 0:
f.write(status + 'none,')
else:
f.write(status + format_members(tag) + ',')
# Write the rule type
f.write(status + t + ',')
# Write the from_zone members
if t != 'default':
f.write(status + format_members(from_zone) + ',')
else:
f.write(status + 'any' + ',')
# Write the source members
if t != 'default':
f.write(status + format_members(source) + ',')
else:
f.write(status + 'any' + ',')
# Write the user members
if t != 'default':
f.write(status + format_members(user) + ',')
else:
f.write(status + 'any' + ',')
# Write the HIP profile members
if t != 'default':
f.write(status + format_members(hip) + ',')
else:
f.write(status + 'any' + ',')
# Write the to_zone members
if t != 'default':
f.write(status + format_members(to_zone) + ',')
elif rule_name == 'intrazone-default':
f.write(status + '(intrazone)' + ',')
else:
f.write(status + 'any' + ',')
# Write the destination members
if t != 'default':
f.write(status + format_members(destination) + ',')
else:
f.write(status + 'any' + ',')
# Write the application members
if t != 'default':
f.write(status + format_members(application) + ',')
else:
f.write(status + 'any' + ',')
# Write the service members
if t != 'default':
f.write(status + format_members(service) + ',')
else:
f.write(status + 'any' + ',')
# Write the category members
if t != 'default':
f.write(status + format_members(category) + ',')
else:
f.write(status + 'any' + ',')
# Write the action
f.write(status + action.text + ',')
# Write the profile or group
if rule.find('profile-setting/group'):
f.write(status + profile_group.text)
elif rule.find('profile-setting/profiles'):
profile_list = []
if av_profile is not None:
profile_list.append('Antivirus: ' + av_profile.text)
if vuln_profile is not None:
profile_list.append('Anti-Spyware: ' + vuln_profile.text)
if spyware_profile is not None:
profile_list.append('Vulnerability Protection: ' + spyware_profile.text)
if url_profile is not None:
profile_list.append('URL Filtering: ' + url_profile.text)
if data_profile is not None:
profile_list.append('Data Filtering: ' + data_profile.text)
if file_profile is not None:
profile_list.append('File Blocking: ' + file_profile.text)
if wildfire_profile is not None:
profile_list.append('WildFire Analysis: ' + wildfire_profile.text)
f.write(status + format_members(profile_list))
else:
f.write('none')
f.write(',')
# Write the log forwarding profile (if defined)
if log_setting is None:
f.write(status + 'none,')
else:
f.write(status + log_setting.text + ',')
# Write the description (if defined)
if description is None:
f.write(status + 'none,')
else:
f.write(status + description.text)
# Finish it!
f.write('\n')
def main():
# Grab the args
myargs = make_parser()
# Open a firewall API connection
if myargs.tag:
# Use the .panrc API key
myconn = PanXapi(tag=myargs.tag)
else:
# Generate the API key
myconn = PanXapi(api_username=myargs.username, api_password=myargs.password, hostname=myargs.firewall)
# Open the output file
if myargs.outfile:
outfile = open_file(myargs.outfile)
else:
outfile = sys.stdout
# Grab the local rulebase XML tree
localtree = get_local_tree(myconn)
# Grab the shared rulebase XML tree
sharedtree = get_shared_tree(myconn)
# Grab the predfined rulebase XML tree
predefinedtree = get_predefined_tree(myconn)
# Write the HTML table
write_security_header(outfile)
# Process all the security rules
count = 1
rule_type = ''
# Process the pre-rules rules
if sharedtree is not None and sharedtree[0]:
for prerule in sharedtree[0].iter('entry'):
rule_type='pre'
write_security_rule(prerule, outfile, count, rule_type)
count += 1
# Process the local security rules
for rule in localtree.iter('entry'):
rule_type='local'
write_security_rule(rule, outfile, count, rule_type)
count += 1
# Process the post-rules
if sharedtree is not None and sharedtree[1]:
for postrule in sharedtree[1].iter('entry'):
rule_type='post'
write_security_rule(postrule, outfile, count, rule_type)
count += 1
# Process the predefined rules
for predefinedrule in predefinedtree.iter('entry'):
rule_type='default'
write_security_rule(predefinedrule, outfile, count, rule_type)
count += 1
# Close the output file
if outfile is not sys.stdout:
outfile.close()
if __name__ == '__main__':
main()