-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nessus-API-Report-Exporter.py
197 lines (138 loc) · 4.6 KB
/
Nessus-API-Report-Exporter.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
#!/usr/bin/env python
import requests
import json
import time
import pandas as pd
import warnings
import datetime
import re
warnings.filterwarnings("ignore") # To ingore SSL error if it accure
def download_file(url):
local_filename = d_fn+"_"+today+"."+report_format
# NOTE the stream=True parameter below
with requests.get(url,headers=headers, data=payload, verify=False, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=None):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
if chunk:
f.write(chunk)
return local_filename
def Convert(string):
li = list(string.split(","))
return li
print ("Enter Nessus IP address and port (example: 10.0.0.1:8834)")
pat = re.compile("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):[0-9][0-9]?[0-9]?[0-9]?[0-9]?$")
while True:
s_address = str(input()) # Server IP
test = pat.match(s_address)
if test:
break
else:
print ("Incorrect ip address")
while True:
print ("Input Access key:")
a_key = str(input())
print ("Input Secret key:")
s_key = str(input())
api_key = "accessKey="+a_key+"; "+"secretKey="+s_key+";"
url = "https://"+s_address
payload={}
headers = {
'X-ApiKeys': api_key
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
if str(response) == "<Response [200]>":
print ("Connected to "+s_address)
break
else:
print ("WARNING! Incorrect API keys. Try again.")
url = "https://"+s_address+"/scans"
srv_url = url
payload={}
headers = {
'X-ApiKeys': api_key
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print (response.text)
y = json.loads(response.text)
folders = y["folders"]
scans = y["scans"]
list_folders = pd.DataFrame(folders)
print(list_folders[["id","name"]].to_string(index = False))
scans_table = pd.DataFrame(scans)
print ("--"*10)
print('Enter folder id: ')
f_n = int(input())
print ("--"*10)
list_scans = scans_table[scans_table.folder_id==f_n]
print(list_scans[["id","name"]].to_string(index = False))
print ("--"*10)
print('Enter scan id (You can enter multiple scans. Example: 105,240,196).')
sn_list = Convert(input())
print ("Choose the report format (1-2):\n 1. CSV \n 2. Nessus")
allowed_types = ["1","2"]
while True:
f_t = str(input())
if f_t in allowed_types:
break
else:
print ("Choose the correct number: ")
if f_t == "1":
report_format = "csv"
if f_t == "2":
report_format = "nessus"
#if f_t == "3":
# report_format = "pdf"
#if f_t == "4":
# report_format = "html"
for s_n in sn_list:
url = srv_url+"/"+s_n+"/export"
payload = json.dumps({
"format": report_format
#"filter.0.filter": "severity",
# "filter.0.quality": "eq",
#"filter.0.value": f_sev
})
headers = {
'Content-Type': 'application/json',
'X-ApiKeys': api_key,
}
response_export = requests.request("POST", url, headers=headers, data=payload, verify=False)
print(response_export.text)
tf_json = json.loads(response_export.text)
timeout=0
print ("--"*10)
print ("Wait untill the report will be prepared")
while True: # Ждём готовность отчёта
token=(tf_json["token"])
file=(tf_json["file"])
url = srv_url+"/"+s_n+"/export/"+str(file)+"/status"
payload={}
headers = {
'X-ApiKeys': api_key
}
response_status = requests.request("GET", url, headers=headers, data=payload, verify=False)
print (response_status.text)
if ((response_status.text == '{"status":"ready"}') or (timeout == 5000000)):
break
else:
time.sleep(1)
timeout = timeout+1
print ("--"*10)
print("Checking export status: ",response_status.text)
url = srv_url+"/"+s_n+"/export/"+str(file)+"/download"
payload={
}
headers = {
'X-ApiKeys': api_key
}
#response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
d_fn = scans_table[scans_table.id==int(s_n)][["name"]].to_string(index = False,header=None)
today = str(datetime.datetime.now().strftime("%d-%m-%Y"))
download_file(url)
print ("--"*10)
print ("File saved as: ", d_fn+"_"+today+"."+report_format)
print ("---DONE!---")