forked from eelsivart/nessus-report-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nessus6-report-downloader.rb
executable file
·364 lines (313 loc) · 10.5 KB
/
nessus6-report-downloader.rb
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
#!/usr/bin/env ruby
#################################################################################################
#
# Original script can be found here: https://github.com/eelsivart/nessus-report-downloader
#
# Script was extended for usage with command line options and to automaticly download files
# from a given number of the last days (now-x).
# This is needed for the usage of the script with cronjobs.
#
#################################################################################################
require 'net/http'
require 'fileutils'
require 'io/console'
require 'date'
require 'json'
require 'openssl'
require 'optparse'
require 'date'
# This method will download the specified file type from specified reports
def report_download(http, headers, reports, reports_to_dl, filetypes_to_dl, chapters_to_dl, rpath, db_export_pw)
begin
puts "\nDownloading report(s). Please wait..."
# if all reports are selected
if reports_to_dl[0].eql?("all")
reports_to_dl.clear
# re-init array with all the scan ids
reports["scans"].each do |scan|
reports_to_dl.push(scan["id"].to_s)
end
end
# iterate through all the indexes and download the reports
reports_to_dl.each do |rep|
rep = rep.strip
filetypes_to_dl.each do |ft|
# export report
puts "\n[+] Exporting scan report, scan id: " + rep + ", type: " + ft
path = "/scans/" + rep + "/export"
data = {'format' => ft, 'chapters' => chapters_to_dl, 'password' => db_export_pw}
resp = http.post(path, data.to_json, headers)
fileid = JSON.parse(resp.body)
# check export status
status_path = "/scans/" + rep + "/export/" + fileid["file"].to_s + "/status"
loop do
sleep(5)
puts "[+] Checking export status..."
status_resp = http.get(status_path, headers)
status_result = JSON.parse(status_resp.body)
break if status_result["status"] == "ready"
puts "[-] Export not ready yet, checking again in 5 secs."
end
# download report
puts "[+] Report ready for download..."
dl_path = "/scans/" + rep + "/export/" + fileid["file"].to_s + "/download"
dl_resp = http.get(dl_path, headers)
# create final path/filename and write to file
fname_temp = dl_resp.response["Content-Disposition"].split('"')
fname = "#{rpath}/#{fname_temp[1]}"
# write file
open(fname, 'w') { |f|
f.puts dl_resp.body
}
puts "[+] Downloading report to: #{fname}"
end
end
rescue StandardError => download_report_error
puts "\n\nError downloading report: #{download_report_error}\n\n"
exit
end
end
# This method will return a list of all the reports on the server
def get_report_list(http, headers)
begin
# Try and do stuff
path = "/scans"
resp = http.get(path, headers)
#puts "Number of reports found: #{reports.count}\n\n"
results = JSON.parse(resp.body)
printf("%-7s %-50s %-30s %-15s\n", "Scan ID", "Name", "Last Modified", "Status")
printf("%-7s %-50s %-30s %-15s\n", "-------", "----", "-------------", "------")
# print out all the reports
results["scans"].each do |scan|
printf("%-7s %-50s %-30s %-15s\n", scan["id"], scan["name"], DateTime.strptime(scan["last_modification_date"].to_s,'%s').strftime('%b %e, %Y %H:%M %Z'), scan["status"])
end
return results
rescue StandardError => get_scanlist_error
puts "\n\nError getting scan list: #{get_scanlist_error}\n\n"
exit
end
end
# return a list of all reports of the last x days
def get_report_list_lastdays(http, headers, days)
begin
# Try and do stuff
path = "/scans"
d=days.to_i
now = Date.today
sometimes = now - d
puts sometimes
t=sometimes.to_time.to_i
data = "?last_modification_date="+t.to_s
resp = http.get(path+data, headers)
#puts "Number of reports found: #{reports.count}\n\n"
results = JSON.parse(resp.body)
printf("%-7s %-50s %-30s %-15s\n", "Scan ID", "Name", "Last Modified", "Status")
printf("%-7s %-50s %-30s %-15s\n", "-------", "----", "-------------", "------")
# print out all the reports
results["scans"].each do |scan|
printf("%-7s %-50s %-30s %-15s\n", scan["id"], scan["name"], DateTime.strptime(scan["last_modification_date"].to_s,'%s').strftime('%b %e, %Y %H:%M %Z'), scan["status"])
end
return results
rescue StandardError => get_scanlist_error
puts "\n\nError getting scan list: #{get_scanlist_error}\n\n"
exit
end
end
# This method will make the initial login request and set the token value to use for subsequent requests
def get_token(http, username, password)
begin
path = "/session"
data = {'username' => username, 'password' => password}
resp = http.post(path, data.to_json, 'Content-Type' => 'application/json')
token = JSON.parse(resp.body)
headers = {
"User-Agent" => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0',
"X-Cookie" => 'token=' + token["token"],
"Accept" => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
"Accept-Language" => 'en-us,en;q=0.5',
"Accept-Encoding" => 'text/html;charset=UTF-8',
"Cache-Control" => 'max-age=0',
"Content-Type" => 'application/json'
}
return headers
rescue StandardError => get_token_error
puts "\n\nError logging in/getting token: #{get_token_error}\n\n"
exit
end
end
### MAIN ###
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: nessus6-report-downloader.rb [options]"
opts.on('-s', '--server SERVER', 'server ip') { |v| options[:server] = v }
opts.on('-p', '--port PORT', 'server port') { |v| options[:port] = v }
opts.on('-u', '--user username', 'login username') { |v| options[:user] = v }
opts.on('-w', '--pass password', 'login password') { |v| options[:pass] = v }
opts.on('-t', '--type report-type', 'report type') { |v| options[:type] = v }
opts.on('-l', '--path localpath', 'local file path for saving the reports') { |v| options[:path] = v }
opts.on('-d', '--days DAYS', 'automatically downloads reports (nessus format) from the last x days, 0=today 1=yesterday and so on') { |v| options[:days] = v }
end.parse!
puts "\nNessus 6 Report Downloader 1.0"
# Collect server info
if options[:server] == nil
print "\nEnter the Nessus Server IP: "
nserver = gets.chomp.to_s
else
nserver = options[:server].chomp.to_s
puts nserver
end
if options[:port] == nil
print "Enter the Nessus Server Port [8834]: "
nserverport = gets.chomp.to_s
if nserverport.eql?("")
nserverport = "8834"
end
else
nserverport = options[:port].chomp.to_s
puts nserverport
end
# https object
http = Net::HTTP.new(nserver, nserverport)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# Collect user/pass info
if options[:user] == nil
print "Enter your Nessus Username: "
username = gets.chomp.to_s
else
username = options[:user].chomp.to_s
puts username
end
if options[:pass] == nil
print "Enter your Nessus Password (will not echo): "
password = STDIN.noecho(&:gets).chomp.to_s
else
password = options[:pass].chomp.to_s
puts password
end
# login and get token cookie
headers = get_token(http, username, password)
reports=""
reports_to_dl=""
# get list of reports
if options[:days] == nil
puts "\n\nGetting report list..."
reports = get_report_list(http, headers)
print "Enter the report(s) your want to download (comma separate list) or 'all': "
reports_to_dl = (gets.chomp.to_s).split(",")
else
puts "\n\nGetting report list..."
reports = get_report_list_lastdays(http, headers, options[:days])
#print "Enter the report(s) your want to download (comma separate list) or 'all': "
reports_to_dl = "all".split(",")
end
if reports_to_dl.count == 0
puts "\nError! You need to choose at least one report!\n\n"
exit
end
if options[:days] == nil
# select file types to download
puts "\nChoose File Type(s) to Download: "
puts "[0] Nessus (No chapter selection)"
puts "[1] HTML"
puts "[2] PDF"
puts "[3] CSV (No chapter selection)"
puts "[4] DB (No chapter selection)"
print "Enter the file type(s) you want to download (comma separate list) or 'all': "
filetypes_to_dl = (gets.chomp.to_s).split(",")
if filetypes_to_dl.count == 0
puts "\nError! You need to choose at least one file type!\n\n"
exit
end
# see which file types to download
formats = []
cSelect = false
dbSelect = false
filetypes_to_dl.each do |ft|
case ft.strip
when "all"
formats.push("nessus")
formats.push("html")
formats.push("pdf")
formats.push("csv")
formats.push("db")
cSelect = true
dbSelect = true
when "0"
formats.push("nessus")
when "1"
formats.push("html")
cSelect = true
when "2"
formats.push("pdf")
cSelect = true
when "3"
formats.push("csv")
when "4"
formats.push("db")
dbSelect = true
end
end
# enter password used to encrypt db exports (required)
db_export_pw = ""
if dbSelect
print "\nEnter a Password to encrypt the DB export (will not echo): "
db_export_pw = STDIN.noecho(&:gets).chomp.to_s
print "\n"
end
# select chapters to include, only show if html or pdf is in file type selection
chapters = ""
if cSelect
puts "\nChoose Chapter(s) to Include: "
puts "[0] Vulnerabilities By Plugin"
puts "[1] Vulnerabilities By Host"
puts "[2] Hosts Summary (Executive)"
puts "[3] Suggested Remediations"
puts "[4] Compliance Check (Executive)"
puts "[5] Compliance Check"
print "Enter the chapter(s) you want to include (comma separate list) or 'all': "
chapters_to_dl = (gets.chomp.to_s).split(",")
if chapters_to_dl.count == 0
puts "\nError! You need to choose at least one chapter!\n\n"
exit
end
# see which chapters to download
chapters_to_dl.each do |chap|
case chap.strip
when "all"
chapters << "vuln_hosts_summary;vuln_by_plugin;vuln_by_host;remediations;compliance_exec;compliance;"
when "0"
chapters << "vuln_by_plugin;"
when "1"
chapters << "vuln_by_host;"
when "2"
chapters << "vuln_hosts_summary;"
when "3"
chapters << "remediations;"
when "4"
chapters << "compliance_exec;"
when "5"
chapters << "compliance;"
end
end
end
# create report folder
print "\nPath to save reports to (without trailing slash): "
rpath = gets.chomp.to_s
unless File.directory?(rpath)
FileUtils.mkdir_p(rpath)
end
# run report download
if formats.count > 0
report_download(http, headers, reports, reports_to_dl, formats, chapters, rpath, db_export_pw)
end
puts "\nReport Download Completed!\n\n"
else
formats = []
cSelect = false
dbSelect = false
formats.push("nessus")
rpath = options[:path]
report_download(http, headers, reports, reports_to_dl, formats, chapters, rpath, db_export_pw)
puts "\nReport Download Completed!\n\n"
end