-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPaAPI.py
526 lines (448 loc) · 23.5 KB
/
PaAPI.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
from Rules import *
from Reports import *
import urllib
import httplib
from time import sleep
class PaAPI:
'''
Class for communicating with the PaloALto
Authors:
David Rice [email protected]
Last Updated:
01/29/2015
'''
##### Global Variables #####
apiKey = ""
baseURL = ""
rules = []
report = []
##### Public Methods #####
'''
Constructor: Will setup the connection to the PaloAlto
Constructor args:
apiKeyFile => file name containing the PaloAlto API configs (string)
'''
def __init__(self, apiKeyFile):
self.__importConfigFile(apiKeyFile)
self.__loadFireWallRules()
### Methods for establishing a connection with the PaloAlto ###
# This method will import the PaloAlto API key so the XMLAPI can be used
def __importConfigFile (self, apiKeyFile):
myFile = open(apiKeyFile, 'r')
for line in myFile:
if line.startswith("baseurl"):
self.baseURL = line.split("=")[1].rstrip()
if not self.baseURL.endswith("/"):
self.baseURL = self.baseURL + "/"
elif line.startswith("apikey"):
self.apiKey = line.split("=")[1].rstrip()# + "=="
### Methods for importing and instantiating pre-existing FireWall Rules from the PaloAlto as Rule objects ###
# getFireWallRulesALL: will return a dictionary of all of the current PaloAlto firewall rules
def __loadFireWallRules(self):
root = self.__getFireWallRulesRoot()
self.rules = []
for child in root.iter('entry'):
self.rules.append(
Rules(
self.__getRuleName(child),
self.__getRuleFrom(child),
self.__getRuleTo(child),
self.__getRuleSource(child),
self.__getRuleDestination(child),
self.__getRuleService(child),
self.__getRuleApplication(child),
self.__getRuleAction(child),
self.__getRuleSourceUser(child),
self.__getRuleDisableServerResponse(child),
self.__getRuleNegateSource(child),
self.__getRuleNegateDestination(child),
self.__getRuleDisabled(child),
self.__getRuleGroup(child),
self.__getRuleHipProfile(child),
self.__getRuleLogStart(child),
self.__getRuleLogEnd(child),
self.__getRuleDescription(child),
)
)
'''
getFireWallRules: will return a list of all of the Rule objects
'''
def getFireWallRules(self):
return self.rules
'''
getFireWallRule: will return a Rule objects based on the rules name
getFireWallRule args:
ruleName => name of the rule (string)
'''
def getFireWallRule(self, ruleName):
for rule in self.rules:
if rule.name == ruleName:
return rule
raise ValueError("Object does not exist")
# This method will return the root of the PaloALto firewall rules for parsing the XML file provided by __getFireWallRulesXML
def __getFireWallRulesRoot(self):
paRules = self.__getFireWallRulesXML()
import xml.etree.ElementTree as ET
return ET.fromstring(paRules)
# This method will return the PaloALto firewall rules in XML format
def __getFireWallRulesXML (self):
paRules = self.__readWebPage(self.baseURL + "api/?type=config&action=show&key=" + self.apiKey + "&xpath=/config/devices/entry/vsys/entry/rulebase/security/rules")
return paRules
'''
createFireWallRule: will create and return a Rule object
createFireWallRule args:
name => name of the rule (string)
memFrom => from members of the rule (list of strings => zones)
memTo => to members of the rule (list of strings => zones)
src => sources of the rule (list of strings)
dst => destination of the rule (list of strings)
srv => services of the rule (list of strings)
app => applications of the rule (list of strings)
act => action of the rule (string => 'allow' or 'deny')
srcUsr => source users of the rule (list of strings)
disRsp => disable server response of the rule (string => 'yes' or 'no')
negSrc => negate sources of the rule (string => 'yes' or 'no')
negDst => negate destinations of the rule (string => 'yes' or 'no')
disable => disable the rule (string => 'yes' or 'no')
group => groups of the rule (list of strings)
hipProf => hip-profiles of the rule (list of strings)
logStart => start the log of the rule (string => 'yes' or 'no')
logEnd => end the log of the rule (string => 'yes' or 'no')
desc => description of the rule (string)
'''
def createFireWallRule(self, name, memFrom, memTo, src, dst, srv, app, act, srcUsr, disRsp, negSrc, negDst, disable, group, hipProf, logStart, logEnd, desc):
newRule = Rules(name, memFrom, memTo, src, dst, srv, app, act, srcUsr, disRsp, negSrc, negDst, disable, group, hipProf, logStart, logEnd, desc)
self.rules.append(newRule)
return newRule
### Methods for Deleting FireWall Rules from the PaloAlto using Rule objects ###
'''
deleteFireWallRule: will create and submit the URL for deleting a PaloAlto FireWall Rule
deleteFireWallRule args:
rule => rule you want to delete (rule object)
'''
def deleteFireWallRule(self, rule):
url = self.baseURL + "api/?type=config&action=delete&key=" + self.apiKey
url = url + "&xpath=/config/devices/entry/vsys/entry/rulebase/security/rules/entry[@name='"+ rule.getRuleName() + "']"
paResponse = self.__readWebPage(url)
paRoot = self.__getWriteResponseRoot(paResponse)
for subRoot in paRoot.iter('response'):
for msg in subRoot:
if msg.text == "command succeeded":
return msg.text
else:
raise ValueError(msg.text)
### Methods for committing changes to the PaloAlto ###
'''
commitFireWallConfiguration: will create and submit the URL for committing changes to the PaloAlto
'''
def commitFireWallConfiguration(self):
url = self.baseURL + "api/?type=commit&key=" + self.apiKey + "&cmd=<commit></commit>"
paResponse = self.__readWebPage(url)
paRoot = self.__getWriteResponseRoot(paResponse)
# Parsing the XML Response to confirm whether the commit took place or not
for msg in paRoot.iter('response'):
if msg[0].text and msg[0].text == "There are no changes to commit.":
return msg[0].text
elif msg[0][0].text and "Another commit" in msg[0][0].text:
print(".")
sleep(5)
self.commitFireWallConfiguration()
elif msg[0][0][0].text and"Commit job" in msg[0][0][0].text:
return msg[0][0][0].text
else:
raise ValueError(msg[0].text)
### Methods for writing a FireWall Rule to the PaloAlto ###
'''
writeFireWallRule: will create and submit the URL for writing a PaloAlto FireWall Rule
writeFireWallRule args:
rule => rule you want to write (rule object)
'''
def writeFireWallRule(self, rule):
url = self.__buildFireWallRulesXML(rule)
paResponse = self.__readWebPage(url)
paRoot = self.__getWriteResponseRoot(paResponse)
# Parsing the XML Response to confirm whether the commit took place or not
for subRoot in paRoot.iter('response'):
for msg in subRoot:
if msg.text == "command succeeded":
return msg.text
else:
raise ValueError(msg[0].text)
return paRoot
# This method will create XML to be used to generate a PaloALto firewall rule
def __buildFireWallRulesXML (self, rule):
url = self.baseURL + "api/?type=config&action=set&key=" + self.apiKey
url = url + "&xpath=/config/devices/entry/vsys/entry/rulebase/security/rules/entry" + rule.genRuleNameXML()
url = url + "&element="
url = url + rule.genRuleFromMembersXML()
url = url + rule.genRuleToMembersXML()
url = url + rule.genRuleSourceXML()
url = url + rule.genRuleDestinationXML()
url = url + rule.genRuleServiceXML()
url = url + rule.genRuleApplicationXML()
url = url + rule.genRuleActionXML()
url = url + rule.genRuleSourceUserXML()
url = url + rule.genRuleDisableServerResponseXML()
url = url + rule.genRuleNegateSourceXML()
url = url + rule.genRuleNegateDestinationXML()
url = url + rule.genRuleDisabledXML()
url = url + rule.genRuleGroupsXML()
url = url + rule.genRuleHipProfilesXML()
url = url + rule.genRuleLogStartXML()
url = url + rule.genRuleLogEndXML()
url = url + rule.genRuleDescriptionXML()
return url
### Methods for reading WebPages ###
# This method will return the html of a provided url
def __readWebPage(self, queryPage):
try:
response = urllib.urlopen(queryPage)
html = response.read()
return html
except httplib.BadStatusLine:
pass
def __getWriteResponseRoot (self, resp):
import xml.etree.ElementTree as ET
return ET.fromstring(resp)
### Parse XML Methods ###
# Return the name of a Rule
def __getRuleName(self, root):
return root.get('name')
# This method will return the 'from' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleFrom(self, root):
retAttr = []
for attrs in root.iter('from'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'to' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleTo(self, root):
retAttr = []
for attrs in root.iter('to'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'source' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleSource(self, root):
retAttr = []
for attrs in root.iter('source'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'destination' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleDestination(self, root):
retAttr = []
for attrs in root.iter('destination'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'service' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleService(self, root):
retAttr = []
for attrs in root.iter('service'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'application' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleApplication(self, root):
retAttr = []
for attrs in root.iter('application'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'action' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleAction(self, root):
retAttr = ""
for attr in root.iter('action'):
retAttr = attr.text
return retAttr
# This method will return the 'source-user' attribute from the PaloAlto API when provided correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleSourceUser(self, root):
retAttr = []
for attrs in root.iter('source-user'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'disable-server-response-inspection' attribute from the PaloAlto when provided correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleDisableServerResponse(self, root):
retAttr = ""
for attr in root.iter('option'):
retAttr = attr[0].text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'negate-source' attribute from the PaloAlto when provided correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleNegateSource(self, root):
retAttr = ""
for attr in root.iter('negate-source'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'negate-destination' attribute from the PaloAlto when provided correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleNegateDestination(self, root):
retAttr = ""
for attr in root.iter('negate-destination'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'disabled' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleDisabled(self, root):
retAttr = ""
for attr in root.iter('disabled'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'group' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleGroup(self, root):
retAttr = []
for attrs in root.iter('profile-setting'):
for attr in attrs[0]:
retAttr.append(attr.text)
return retAttr
# This method will return the 'hip-profiles' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleHipProfile(self, root):
retAttr = []
for attrs in root.iter('hip-profiles'):
for attr in attrs:
retAttr.append(attr.text)
return retAttr
# This method will return the 'log-start' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleLogStart(self, root):
retAttr = ""
for attr in root.iter('log-start'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'log-end' attribute from the PaloAlto API when provided the correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleLogEnd(self, root):
retAttr = ""
for attr in root.iter('log-end'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return "no"
# This method will return the 'description' attribute from the PaloAlto when provided correct root (see getFireWallRule/getFireWallRulesALL as examples)
def __getRuleDescription(self, root):
retAttr = ""
for attr in root.iter('description'):
retAttr = attr.text
if retAttr:
return retAttr
else:
return ""
### Methods for getting reports from the PaloAlto ###
def getReport(self, reportName):
self.__loadReport(reportName)
return self.report
def __loadReport(self, reportName):
root = self.__getReportRoot(reportName)
self.report = []
reportAttrs = []
reportAttrFields = {}
for child in root.iter('entry'):
for subchild in child:
reportAttrFields[subchild.tag] = subchild.text
reportAttrs.append(reportAttrFields)
Reports(
reportName,
reportAttrs
)
# This method will return the root of the PaloALto report for parsing the XML file provided by getReportXML
def __getReportRoot(self, reportName):
paReport = self.__getReportXML(reportName)
import xml.etree.ElementTree as ET
return ET.fromstring(paReport)
def __getReportXML(self, reportName):
reports = [
"bandwidth-trend", "botnet", "hruser-top-applications", "hruser-top-threats", "hruser-top-url-categories",
"risk-trend", "risky-users", "spyware-infected-hosts", "threat-trend", "top-application-categories",
"top-applications", "top-attackers", "top-attackers-by-countries", "top-attacks", "top-blocked-url-categories",
"top-blocked-url-user-behavior", "top-blocked-url-users", "top-blocked-websites", "top-connections",
"top-denied-applications", "top-denied-destinations", "top-denied-sources", "top-destination-countries",
"top-destinations", "top-egress-interfaces", "top-egress-zones", "top-http-applications", "top-ingress-interfaces",
"top-ingress-zones", "top-rules", "top-source-countries", "top-sources", "top-spyware-threats",
"top-technology-categories", "top-url-categories", "top-url-user-behavior", "top-url-users", "top-users",
"top-victims", "top-victims-by-countries", "top-viruses", "top-vulnerabilities", "top-websites",
"unknown-tcp-connections", "unknown-udp-connections", "wildfire-file-digests"
]
if reportName in reports:
url = self.baseURL + "api/?type=report&reporttype=predefined&reportname=" + reportName + "&key=" + self.apiKey
paReport = self.__readWebPage(url)
return paReport
else:
repList = ""
for rep in reports:
repList = repList + rep + " "
raise ValueError("Report name '" + reportName + "' does not exist. You must use one of these reports:\n" + repList)
def getDynamicReport(self, reportName, period, topN):
self.__loadDynamicReport(reportName, period, topN)
return self.report
def __loadDynamicReport(self, reportName, period, topN):
root = self.__getDynamicReportRoot(reportName, period, topN)
self.report = []
reportAttrs = []
reportAttrFields = {}
for child in root.iter('entry'):
for subchild in child:
reportAttrFields[subchild.tag] = subchild.text
reportAttrs.append(reportAttrFields)
Reports(
reportName,
reportAttrs
)
# This method will return the root of the PaloALto report for parsing the XML file provided by getReportXML
def __getDynamicReportRoot(self, reportName, period, topN):
paReport = self.__getDynamicReportXML(reportName, period, topN)
import xml.etree.ElementTree as ET
return ET.fromstring(paReport)
def __getDynamicReportXML(self, reportName, period, topN):
reports = [
#"custom-dynamic-report",
"acc-summary", "top-app-summary", "top-application-categories-summary", "top-application-risk-summary",
"top-application-subcategories-summary", "top-application-tech-summary", "top-applications-summary", "top-applications-trsum",
"top-attacker-countries-summary", "top-attackers-summary", "top-attacks-acc", "top-blocked-url-categories-summary", "top-blocked-url-summary",
"top-blocked-url-user-behavior-summary", "top-data-dst-countries-summary", "top-data-dst-summary", "top-data-egress-zones-summary",
"top-data-filename-summary", "top-data-filetype-summary", "top-data-ingress-zones-summary", "top-data-src-countries-summary",
"top-data-src-summary", "top-data-type-summary", "top-dst-countries-summary", "top-dst-summary", "top-egress-zones-summary",
"top-hip-objects-details", "top-hip-objects-summary", "top-hip-profiles-details", "top-hip-profiles-summary", "top-hip-report-links",
"top-hr-applications-summary", "top-ingress-zones-summary", "top-rule-summary", "top-spyware-download-summary", "top-spyware-phonehome-summary",
"top-spyware-threats-summary", "top-src-countries-summary", "top-src-summary", "top-threat-egress-zones-summary", "top-threat-ingress-zones-summary",
"top-threats-type-summary", "top-url-categories-summary", "top-url-summary", "top-url-user-behavior-summary", "top-victim-countries-summary",
"top-victims-summary", "top-viruses-summary", "top-vulnerabilities-summary"
]
periods = [
"last-60-seconds", "last-15-minutes", "last-hour", "last-12-hrs", "last-24-hrs", "last-calendar-day", "last-7-days", "last-7-calendar-days",
"last-calendar-week", "last-30-days"
]
if reportName in reports:
url = self.baseURL + "api/?type=report&reporttype=dynamic&reportname=" + reportName
if period in periods:
url = url + "&period=" + period
else:
if period:
perList = ""
for per in periods:
perList = perList + per + " "
raise ValueError("The period value '" + period + "' does not exist, use one of the following:\n" + perList)
if topN and topN.isdigit():
url = url + "&topn=" + topN
elif topN and not topN.isdigit():
raise ValueError("The topN value must be an integer")
url = url + "&key=" + self.apiKey
paReport = self.__readWebPage(url)
return paReport
else:
repList = ""
for rep in reports:
repList = repList + rep + " "
raise ValueError("Report name '" + reportName + "' does not exist. You must use one of these reports:\n" + repList)