Skip to content

Commit 7b5da6f

Browse files
committed
Land rapid7#5241, sqlmap parsing fixes
2 parents 4aa8344 + eb84c8b commit 7b5da6f

File tree

5 files changed

+90
-80
lines changed

5 files changed

+90
-80
lines changed

lib/nessus/nessus-xmlrpc.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ def scan_export_status(scan_id, file_id)
181181
request = Net::HTTP::Get.new("/scans/#{scan_id}/export/#{file_id}/status")
182182
request.add_field("X-Cookie", @token)
183183
res = @connection.request(request)
184-
return res.code, JSON.parse(res.body)
184+
if res.code == "200"
185+
return "ready"
186+
else
187+
res = JSON.parse(res.body)
188+
return res
189+
end
185190
end
186191

187192
def policy_delete(policy_id)

lib/sqlmap/sqlmap_manager.rb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,57 @@ def initialize(session)
88

99
def new_task
1010
res = @session.get('/task/new')
11-
return JSON.parse(res.body)
11+
parse_response(res)
1212
end
1313

1414
def delete_task(task_id)
1515
res = @session.get('/task/' + task_id + '/delete')
16-
return JSON.parse(res.body)
16+
parse_response(res)
1717
end
1818

1919
def set_option(task_id, key, value)
2020
post = { key => value }
2121
res = @session.post('/option/' + task_id + '/set', nil, post.to_json, {'ctype' => 'application/json'})
22-
return JSON.parse(res.body)
22+
parse_response(res)
2323
end
2424

2525
def get_options(task_id)
2626
res = @session.get('/option/' + task_id + '/list')
27-
return JSON.parse(res.body)
27+
parse_response(res)
2828
end
2929

3030
def start_task(task_id, options = {})
3131
res = @session.post('/scan/' + task_id + '/start' , nil, options.to_json, {'ctype' => 'application/json'})
32-
return JSON.parse(res.body)
32+
parse_response(res)
33+
3334
end
3435

3536
def get_task_status(task_id)
3637
res = @session.get('/scan/' + task_id + '/status')
37-
return JSON.parse(res.body)
38+
parse_response(res)
3839
end
3940

4041
def get_task_log(task_id)
4142
res = @session.get('/scan/' + task_id + '/log')
42-
return JSON.parse(res.body)
43+
parse_response(res)
4344
end
4445

4546
def get_task_data(task_id)
4647
res = @session.get('/scan/' + task_id + '/data')
47-
return JSON.parse(res.body)
48+
parse_response(res)
49+
end
50+
51+
private
52+
def parse_response(res)
53+
json = {}
54+
if res && res.body
55+
begin
56+
json = JSON.parse(res.body)
57+
rescue JSON::ParserError
58+
end
59+
end
60+
61+
json
4862
end
4963
end
5064
end

lib/sqlmap/sqlmap_session.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Sqlmap
22
class Session
3-
def initialize(host, port = 8775)
3+
def initialize(host, port)
44
@host = host
55
@port = port
66
end
@@ -13,9 +13,13 @@ def get(uri, headers = nil, params = nil)
1313

1414
args['headers'] = headers if headers
1515
args['vars_get'] = params if params
16-
res = c.request_cgi(args)
17-
res = c.send_recv(res)
18-
return res
16+
begin
17+
res = c.request_cgi(args)
18+
res = c.send_recv(res)
19+
return res
20+
rescue Rex::ConnectionRefused
21+
return
22+
end
1923
end
2024

2125
def post(uri, headers = nil, data = nil, originator_args = nil)
@@ -26,12 +30,15 @@ def post(uri, headers = nil, data = nil, originator_args = nil)
2630
}
2731

2832
args.merge!(originator_args) if originator_args
29-
3033
args['headers'] = headers if headers
3134
args['data'] = data if data
32-
res = c.request_cgi(args)
33-
res = c.send_recv(res)
34-
return res
35+
begin
36+
res = c.request_cgi(args)
37+
res = c.send_recv(res)
38+
return res
39+
rescue Rex::ConnectionRefused
40+
return
41+
end
3542
end
3643
end
3744
end

plugins/nessus.rb

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
module Msf
66

7+
PLUGIN_NAME = 'Nessus'
8+
PLUGIN_DESCRIPTION = 'Nessus Bridge for Metasploit'
9+
710
class Plugin::Nessus < Msf::Plugin
811

912
def name
10-
"Nessus"
11-
end
12-
13-
def desc
14-
"Nessus Bridge for Metasploit"
13+
PLUGIN_NAME
1514
end
1615

1716
def desc
@@ -22,7 +21,7 @@ class ConsoleCommandDispatcher
2221
include Msf::Ui::Console::CommandDispatcher
2322

2423
def name
25-
"Nessus"
24+
PLUGIN_NAME
2625
end
2726

2827
def xindex
@@ -455,7 +454,7 @@ def cmd_nessus_template_list(*args)
455454
print_status("Returns a list of information about the scan or policy templates..")
456455
return
457456
end
458-
if type.downcase.in?(['scan', 'policy'])
457+
if type.in?(['scan', 'policy'])
459458
list=@n.list_template(type)
460459
else
461460
print_error("Only scan and policy are valid templates")
@@ -1188,7 +1187,7 @@ def cmd_nessus_scan_details(*args)
11881187
when 2
11891188
scan_id = args[0]
11901189
category = args[1]
1191-
if category.downcase.in?(['info', 'hosts', 'vulnerabilities', 'history'])
1190+
if category.in?(['info', 'hosts', 'vulnerabilities', 'history'])
11921191
category = args[1]
11931192
else
11941193
print_error("Invalid category. The available categories are info, hosts, vulnerabilities, and history")
@@ -1265,27 +1264,23 @@ def cmd_nessus_scan_export(*args)
12651264
case args.length
12661265
when 2
12671266
scan_id = args[0]
1268-
format = args[1]
1267+
format = args[1].downcase
12691268
else
12701269
print_status("Usage: ")
12711270
print_status("nessus_scan_export <scan ID> <export format>")
12721271
print_status("The available export formats are Nessus, HTML, PDF, CSV, or DB")
12731272
print_status("Use nessus_scan_list to list all available scans with their corresponding scan IDs")
12741273
return
12751274
end
1276-
if format.downcase.in?(['nessus','html','pdf','csv','db'])
1275+
if format.in?(['nessus','html','pdf','csv','db'])
12771276
export = @n.scan_export(scan_id, format)
12781277
if export["file"]
12791278
file_id = export["file"]
12801279
print_good("The export file ID for scan ID #{scan_id} is #{file_id}")
12811280
print_status("Checking export status...")
1282-
code, body = @n.scan_export_status(scan_id, file_id)
1283-
if code == "200"
1284-
if body =~ /ready/
1285-
print_good("The status of scan ID #{scan_id} export is ready")
1286-
else
1287-
print_status("Scan result not ready for download. Please check again after a few seconds")
1288-
end
1281+
status = @n.scan_export_status(scan_id, file_id)
1282+
if status == "ready"
1283+
print_good("The status of scan ID #{scan_id} export is ready")
12891284
else
12901285
print_error("There was some problem in exporting the scan. The error message is #{status}")
12911286
end
@@ -1310,30 +1305,16 @@ def cmd_nessus_scan_export_status(*args)
13101305
when 2
13111306
scan_id = args[0]
13121307
file_id = args[1]
1313-
check_export_status(scan_id, file_id)
1314-
else
1315-
print_status("Usage: ")
1316-
print_status("nessus_scan_export_status <scan ID> <file ID>")
1317-
print_status("Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID")
1318-
end
1319-
end
1320-
1321-
def check_export_status(scan_id, file_id, attempt = 0)
1322-
code, body = @n.scan_export_status(scan_id, file_id)
1323-
if code == "200"
1324-
if body.to_s =~ /ready/
1308+
status = @n.scan_export_status(scan_id, file_id)
1309+
if status == "ready"
13251310
print_status("The status of scan ID #{scan_id} export is ready")
13261311
else
1327-
if attempt < 3
1328-
print_status("Scan result not ready for download. Checking again...")
1329-
select(nil, nil, nil, 1)
1330-
attempt = attempt + 1
1331-
print_error("Current value of attempt is #{attempt}")
1332-
check_export_status(scan_id, file_id, attempt)
1333-
end
1312+
print_error("There was some problem in exporting the scan. The error message is #{status}")
13341313
end
13351314
else
1336-
print_error("There was some problem in exporting the scan. The error message is #{body}")
1315+
print_status("Usage: ")
1316+
print_status("nessus_scan_export_status <scan ID> <file ID>")
1317+
print_status("Use nessus_scan_export <scan ID> <format> to export a scan and get its file ID")
13371318
end
13381319
end
13391320

@@ -1691,7 +1672,7 @@ def nessus_verify_db
16911672
def initialize(framework, opts)
16921673
super
16931674
add_console_dispatcher(ConsoleCommandDispatcher)
1694-
print_status("Nessus Bridge for Metasploit")
1675+
print_status(PLUGIN_DESCRIPTION)
16951676
print_status("Type %bldnessus_help%clr for a command listing")
16961677
end
16971678

0 commit comments

Comments
 (0)