-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportScans.py
More file actions
234 lines (197 loc) · 8.44 KB
/
ExportScans.py
File metadata and controls
234 lines (197 loc) · 8.44 KB
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
# Native Python libraries
import json
import base64
import os
import time
# Install using pip
import requests
import configparser
# Set API access information from config.ini, stored in the directory the app is launched from
config = configparser.ConfigParser()
config.read(f'.\config.ini')
apiaccesskey = config['Tenable']['apiaccesskey']
apisecretkey = config['Tenable']['apisecretkey']
# Put the name of the scan in the string below, then save and run the script.
scan_name_for_exports = ''
### Create folder for exported files to go into
def createExportFolder(scan_name):
if not os.path.exists("./Scans"):
os.mkdir("./Scans")
if not os.path.exists(f"./Scans/{scan_name}"):
os.mkdir(f"./Scans/{scan_name}")
### Gets the schedule_uuid to identify specific scans for exporting. Using this method grabs the latest scan results.
def GetScanIdByName(scan_name):
url = "https://cloud.tenable.com/scans"
headers = {'X-ApiKeys': f'accessKey={apiaccesskey}; secretKey={apisecretkey};', 'accept': 'application/json'}
sendinfo = requests.request("GET", url, headers=headers)
response = sendinfo.json()
for scan in response['scans']:
if scan_name == scan['name']:
scan_id = scan['schedule_uuid']
return scan_id
# Make call to Tenable, export Exec Summary PDF and download.
def ExportExecutiveSummaryPdfReport(scan_name):
schedule_uuid = GetScanIdByName(scan_name)
url = f"https://cloud.tenable.com/scans/{schedule_uuid}/export"
payload = json.dumps({'format':'pdf',
'chapters': 'vuln_hosts_summary'
})
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/json",
'content-type': "application/json"
}
sendinfo = requests.request("POST", url, data=payload, headers=headers)
response = sendinfo.json()
ExecutiveSummaryFileId = response['file']
return response['file']
### By Asset Report
def ExportByAssetPdfReport(scan_name):
schedule_uuid = GetScanIdByName(scan_name)
url = f"https://cloud.tenable.com/scans/{schedule_uuid}/export"
payload = json.dumps({'format':'pdf',
'chapters': 'vuln_by_host'
})
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/json",
'content-type': "application/json"
}
sendinfo = requests.request("POST", url, data=payload, headers=headers)
response = sendinfo.json()
ByAssetFileId = response['file']
return response['file']
### By Vulnerability Report
def ExportByPluginPdfReport(scan_name):
schedule_uuid = GetScanIdByName(scan_name)
url = f"https://cloud.tenable.com/scans/{schedule_uuid}/export"
payload = json.dumps({'format':'pdf',
'chapters': 'vuln_by_plugin'
})
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/json",
'content-type': "application/json"
}
sendinfo = requests.request("POST", url, data=payload, headers=headers)
response = sendinfo.json()
ByPluginFileId = response['file']
return response['file']
### CSV Report
def ExportCsvReport(scan_name):
schedule_uuid = GetScanIdByName(scan_name)
url = f"https://cloud.tenable.com/scans/{schedule_uuid}/export"
payload = json.dumps({'format':'csv'})
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/json",
'content-type': "application/json"
}
sendinfo = requests.request("POST", url, data=payload, headers=headers)
response = sendinfo.json()
CsvFileId = response['file']
return response['file']
### Checks status of requested export
def CheckExportStatus(scan_name, file_id):
scheduleUuid = GetScanIdByName(scan_name)
url = f"https://cloud.tenable.com/scans/{scheduleUuid}/export/{file_id}/status"
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey};',
'accept': "application/json",
'content-type': "application/json"
}
sendinfo = requests.request("GET", url, headers=headers)
response = sendinfo.json()
return response['status']
### Downloads the Executive Summary report
def GetExecutiveSummaryPdfExport(scan_name):
createExportFolder(scan_name)
ExecutiveSummaryFileID = ExportExecutiveSummaryPdfReport(scan_name)
scheduleUuid = GetScanIdByName(scan_name)
while True:
url = f"https://cloud.tenable.com/scans/{scheduleUuid}/export/{ExecutiveSummaryFileID}/download"
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/octet-stream",
}
CurrentExportStatus = CheckExportStatus(scan_name, ExecutiveSummaryFileID)
if CurrentExportStatus == 'ready':
sendInfo = requests.request("GET", url, headers=headers)
print("Downloading Executive Summary Report...")
newPdfReport = open(f"./Scans/{scan_name}/ExecutiveSummary.pdf", "w+b")
newPdfReport.write(sendInfo.content)
newPdfReport.close()
break
else:
time.sleep(5)
### Downloads the By Asset Export
def GetByAssetPdfExport(scan_name):
createExportFolder(scan_name)
ByAssetFileID = ExportByAssetPdfReport(scan_name)
scheduleUuid = GetScanIdByName(scan_name)
while True:
url = f"https://cloud.tenable.com/scans/{scheduleUuid}/export/{ByAssetFileID}/download"
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/octet-stream",
}
CurrentExportStatus = CheckExportStatus(scan_name, ByAssetFileID)
if CurrentExportStatus == 'ready':
sendInfo = requests.request("GET", url, headers=headers)
print("Downloading By Asset report...")
newPdfReport = open(f"./Scans/{scan_name}/By Asset Report.pdf", "w+b")
newPdfReport.write(sendInfo.content)
newPdfReport.close()
break
else:
time.sleep(5)
### Downloads the By Plugin Export
def GetByPluginPdfExport(scan_name):
createExportFolder(scan_name)
ByPluginFileId = ExportByPluginPdfReport(scan_name)
scheduleUuid = GetScanIdByName(scan_name)
while True:
url = f"https://cloud.tenable.com/scans/{scheduleUuid}/export/{ByPluginFileId}/download"
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/octet-stream",
}
CurrentExportStatus = CheckExportStatus(scan_name, ByPluginFileId)
if CurrentExportStatus == 'ready':
sendInfo = requests.request("GET", url, headers=headers)
print("Downloading By Vulnerability report...")
newPdfReport = open(f"./Scans/{scan_name}/By Vulnerability Report.pdf", "w+b")
newPdfReport.write(sendInfo.content)
newPdfReport.close()
break
else:
time.sleep(5)
### CSV Export
def GetCsvExport(scan_name):
createExportFolder(scan_name)
CsvFileId = ExportCsvReport(scan_name)
scheduleUuid = GetScanIdByName(scan_name)
while True:
url = f"https://cloud.tenable.com/scans/{scheduleUuid}/export/{CsvFileId}/download"
headers = {
'X-ApiKeys' : f'accessKey={apiaccesskey}; secretKey={apisecretkey}',
'accept': "application/octet-stream",
}
CurrentExportStatus = CheckExportStatus(scan_name, CsvFileId)
if CurrentExportStatus == 'ready':
sendInfo = requests.request("GET", url, headers=headers)
print("Downloading CSV Report...")
newPdfReport = open(f"./Scans/{scan_name}/CSV Report.csv", "w+b")
newPdfReport.write(sendInfo.content)
newPdfReport.close()
break
else:
time.sleep(5)
### Puts all the downloads together into one call function.
def GetAllScans(scan_name):
GetExecutiveSummaryPdfExport(scan_name)
GetByAssetPdfExport(scan_name)
GetByPluginPdfExport(scan_name)
GetCsvExport(scan_name)
### Calls the all downloads function above using the name at the top of the script.
GetAllScans(scan_name_for_exports)