-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogPullAPI
126 lines (96 loc) · 3.95 KB
/
logPullAPI
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
#!/usr/bin/python
# Program to pull logs from firewall via API.
# First task allows for traffic pulls.
# Next add-on will allow for user to specify what type of log they want.
# Usage: logPullAPI.py [-h] [-f FIREWALL] [-u USERNAME] [-p PASSWORD]
# or run "./logPullAPI.py" from the command line and answer the prompts or any
# combination of the two.
#
# Note: If the password has a special character in it, it may need to be
# delimited with "\" when typing in
#
import ssl
import string
import getpass
import subprocess
import argparse
import urllib
import urllib2
from xml.dom import minidom
# Handler for the command line arguments, if used.
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--firewall", help="Name or IP address of the firewall")
parser.add_argument("-u", "--username", help="User login")
parser.add_argument("-p", "--password", help="Login password")
parser.add_argument("-l", "--logtype", help="Specify the log-type to capture. Options are: traffic\nthreat\nconfig\nsystem\nwildfire\nurl\ndata")
parser.add_argument("-a", "--logAmount", help="Specify the amount of logs to pull, 21-5000(max supported)")
args = parser.parse_args()
# Gather the user defined variables, either from the command-line options,
# or if they are not provided, from a user prompt
if args.firewall:
firewall = args.firewall
else:
firewall = raw_input("Enter the name or IP of the firewall: ")
if args.username:
user = args.username
else:
user = raw_input("Enter the user login: ")
if args.logtype:
logtype = args.logtype
else:
logtype = raw_input("Specify the log-type to capture. Options are:\n traffic\nthreat\nconfig\nsystem\nwildfire\nurl\ndata:\n ")
if args.logAmount:
logAmount = args.logAmount
else:
logAmount = raw_input("Specify the amount of logs to pull, 21-5000(max supported). The default is 20 logs: ")
if args.password:
pw = args.password
else:
pw = getpass.getpass()
############################################################################
# Ignore everything above this line
############################################################################
############################################################################
#Primary Code for Generating API Key and Sending API Requests
############################################################################
def send_api_request(url, values):
# Function to send the api request to the firewall and return the
# parsed response.
context = ssl._create_unverified_context()
data = urllib.urlencode(values)
request = urllib2.Request(url, data)
response = urllib2.urlopen(request, context=context).read()
print response
return minidom.parseString(response)
def get_api_key(hostname, username, password):
# Function to generate a key using the user defined login credentials
url = 'https://' + hostname + '/api'
values = {'type': 'keygen', 'user': username, 'password': password}
parsedKey = send_api_request(url, values)
nodes = parsedKey.getElementsByTagName('key')
key = nodes[0].firstChild.nodeValue
return key
############################################################################
# Code for Retrieving Logs
############################################################################
def main():
key = get_api_key(firewall, user, pw)
url = 'https://' + firewall + '/api'
print key
#print url
return key
def retrieveLogs():
key = get_api_key(firewall, user, pw)
url = 'https://' + firewall + '/api'
values = {'type': 'log', 'log-type': logtype, 'key': key, 'nlogs': logAmount}
jobCreated = send_api_request(url, values)
jobNumber = jobCreated.getElementsByTagName('job')
job = jobNumber[0].firstChild.nodeValue
print jobCreated
print job
newURL = 'https://' + firewall + '/api'
newValues = {'type': 'log', 'log-type': logtype, 'action': 'get', 'job-id': job, 'key': key}
retrieveJob = send_api_request(newURL, newValues)
print retrieveJob
retrieveLogs()
main()