Skip to content

Commit 5882904

Browse files
author
Brent Cook
committed
Land rapid7#6039, fix error handling in mssql_idf
2 parents c967b60 + e64804d commit 5882904

File tree

2 files changed

+33
-77
lines changed

2 files changed

+33
-77
lines changed

lib/msf/core/exploit/mssql.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,18 @@ def mssql_login(user='sa', pass='', db='')
385385
disconnect if self.sock
386386
connect
387387

388-
# Send a prelogin packet and check that encryption is not enabled
389-
if mssql_prelogin() != ENCRYPT_NOT_SUP
390-
print_error("Encryption is not supported")
388+
begin
389+
# Send a prelogin packet and check that encryption is not enabled
390+
if mssql_prelogin() != ENCRYPT_NOT_SUP
391+
print_error("Encryption is not supported")
392+
return false
393+
end
394+
rescue EOFError
395+
print_error("Probable server or network failure.")
391396
return false
392397
end
393398

399+
394400
if datastore['USE_WINDOWS_AUTHENT']
395401

396402
idx = 0
@@ -623,7 +629,12 @@ def mssql_login(user='sa', pass='', db='')
623629
# Packet header and total length including header
624630
pkt = "\x10\x01" + [pkt.length + 8].pack('n') + [0].pack('n') + [1].pack('C') + "\x00" + pkt
625631

626-
resp = mssql_send_recv(pkt)
632+
begin
633+
resp = mssql_send_recv(pkt)
634+
rescue EOFError
635+
print_error("Probable server or network failure.")
636+
return false
637+
end
627638

628639
end
629640

modules/auxiliary/admin/mssql/mssql_idf.rb

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
require 'msf/core'
1616

17-
1817
class Metasploit3 < Msf::Auxiliary
1918

2019
include Msf::Exploit::Remote::MSSQL
@@ -95,11 +94,20 @@ def run
9594
sql += "CLOSE table_cursor "
9695
sql += "DEALLOCATE table_cursor "
9796

97+
begin
98+
if mssql_login_datastore
99+
result = mssql_query(sql, false)
100+
column_data = result[:rows]
101+
else
102+
print_error('Login failed')
103+
return
104+
end
105+
rescue Rex::ConnectionRefused => e
106+
print_error("Connection failed: #{e}")
107+
return
108+
end
98109

99-
# Add error handling here
100-
result = mssql_query(sql, false) if mssql_login_datastore
101110
column_data = result[:rows]
102-
103111
widths = [0, 0, 0, 0, 0, 9]
104112
total_width = 0
105113

@@ -113,25 +121,24 @@ def run
113121
total_width += a
114122
}
115123

116-
print_line("")
124+
print_line
117125

118126
buffer = ""
119127
headings.each { |row|
120128
0.upto(5) { |col|
121129
buffer += row[col].ljust(widths[col] + 1)
122130
}
123131
print_line(buffer)
124-
print_line("")
132+
print_line
125133
buffer = ""
126134

127135
0.upto(5) { |col|
128136
buffer += print "=" * widths[col] + " "
129137
}
130138
print_line(buffer)
131-
print_line("")
139+
print_line
132140
}
133141

134-
table_data_sql = {}
135142
column_data.each { |row|
136143
count_sql = "SELECT COUNT(*) AS count FROM "
137144

@@ -153,73 +160,11 @@ def run
153160

154161
buffer += row_count.to_s
155162
print_line(buffer)
156-
print_line("")
157-
158-
# if row_count == 0
159-
# data_sql = nil
160-
# table_data_sql[full_table + "." + column_name] = nil
161-
# elsif row_count < 4
162-
# data_sql = "SELECT * from " + full_table
163-
# table_data_sql[full_table + "." + column_name] = data_sql
164-
# else
165-
# data_sql = "SELECT TOP 3 * from " + full_table
166-
#
167-
# # or this will get top, middle and last rows
168-
#
169-
# data_sql = "
170-
# with tmp as (select *,ROW_NUMBER() over (order by " + column_name + ") as rownumber from " + full_table + " )
171-
# select * from tmp where rownumber between 1 and 1;
172-
# with tmp as (select *,ROW_NUMBER() over (order by " + column_name + ") as rownumber from " + full_table + " )
173-
# select * from tmp where rownumber between " + (row_count / 2).to_s + " and " + (row_count / 2).to_s + ";
174-
# with tmp as (select *,ROW_NUMBER() over (order by " + column_name + ") as rownumber from " + full_table + " )
175-
# select * from tmp where rownumber between " + row_count.to_s + " and " + row_count.to_s + ";
176-
# "
177-
# table_data_sql[full_table + "." + column_name] = data_sql
178-
# end
163+
print_line
179164
}
180165

181-
print_line("")
182-
183-
# The code from this point on is for dumping out some sample data however the MSSQL parser isn't working
184-
# correctly so the output is messed up. I'll finish implementing this once the bug is fixed.
185-
186-
# print_line("")
187-
# print_with_underline("Sample Data")
188-
# print_line("")
189-
# table_data_sql.each_pair { |table, sql|
190-
# if !sql.nil?
191-
# print_with_underline table
192-
# result = mssql_query(sql, true) if mssql_login_datastore
193-
# #print_line result.inspect
194-
# result[:colnames].each { |row|
195-
# print row.ljust(20)
196-
# }
197-
# end
198-
# }
199-
#
200-
# if !data_sql.nil?
201-
# result = mssql_query(data_sql, false) if mssql_login_datastore
202-
# # print_line "INSPECT"
203-
# # print_line result.keys.inspect
204-
# # print_line result[:colnames].inspect
205-
# result[:colnames].each { |row|
206-
# print row.ljust(20)
207-
# }
208-
# print_line("")
209-
# result[:colnames].each { |row|
210-
# print "=" * 20 + " "
211-
# }
212-
# print_line("")
213-
#
214-
# if !result[:rows].nil?
215-
## print_line data_sql
216-
# result[:rows].each { |acol|
217-
# acol.each { |aval|
218-
# # print_line aval
219-
# }
220-
# }
221-
# end
222-
# end
166+
print_line
223167
disconnect
224168
end
169+
225170
end

0 commit comments

Comments
 (0)