2009-04-22 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
This file is part of the sqlmap project, http://sqlmap.sourceforge.net.
|
|
|
|
|
|
|
|
Copyright (c) 2007-2009 Bernardo Damele A. G. <bernardo.damele@gmail.com>
|
|
|
|
Copyright (c) 2006 Daniele Bellucci <daniele.bellucci@gmail.com>
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
from lib.core.common import readInput
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.dump import dumper
|
2009-04-28 03:05:11 +04:00
|
|
|
from lib.core.exception import sqlmapUnsupportedFeatureException
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.shell import autoCompletion
|
|
|
|
from lib.takeover.udf import UDF
|
2010-01-14 17:03:16 +03:00
|
|
|
from lib.takeover.web import Web
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.takeover.xp_cmdshell import xp_cmdshell
|
|
|
|
|
2010-01-14 17:03:16 +03:00
|
|
|
class Abstraction(Web, UDF, xp_cmdshell):
|
2009-04-22 15:48:07 +04:00
|
|
|
"""
|
|
|
|
This class defines an abstraction layer for OS takeover functionalities
|
|
|
|
to UDF / xp_cmdshell objects
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.envInitialized = False
|
|
|
|
|
|
|
|
UDF.__init__(self)
|
2010-01-14 17:03:16 +03:00
|
|
|
Web.__init__(self)
|
2009-04-22 15:48:07 +04:00
|
|
|
xp_cmdshell.__init__(self)
|
|
|
|
|
|
|
|
def execCmd(self, cmd, silent=False, forgeCmd=False):
|
2010-01-14 17:03:16 +03:00
|
|
|
if self.webBackdoorUrl and not kb.stackedTest:
|
2010-01-14 17:33:08 +03:00
|
|
|
self.webBackdoorRunCmd(cmd)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
elif kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2009-09-26 03:03:45 +04:00
|
|
|
self.udfExecCmd(cmd, silent=silent)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
self.xpCmdshellExecCmd(cmd, silent, forgeCmd)
|
|
|
|
|
|
|
|
else:
|
|
|
|
errMsg = "Feature not yet implemented for the back-end DBMS"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def evalCmd(self, cmd, first=None, last=None):
|
2010-01-14 17:33:08 +03:00
|
|
|
if self.webBackdoorUrl and not kb.stackedTest:
|
|
|
|
return self.webBackdoorRunCmd(cmd)
|
|
|
|
|
|
|
|
elif kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2009-09-26 03:03:45 +04:00
|
|
|
return self.udfEvalCmd(cmd, first, last)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
2009-09-26 03:03:45 +04:00
|
|
|
return self.xpCmdshellEvalCmd(cmd, first, last)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
else:
|
|
|
|
errMsg = "Feature not yet implemented for the back-end DBMS"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
|
|
|
def runCmd(self, cmd):
|
|
|
|
getOutput = None
|
|
|
|
|
|
|
|
message = "do you want to retrieve the command standard "
|
|
|
|
message += "output? [Y/n] "
|
|
|
|
getOutput = readInput(message, default="Y")
|
|
|
|
|
|
|
|
if not getOutput or getOutput in ("y", "Y"):
|
|
|
|
output = self.evalCmd(cmd)
|
|
|
|
|
|
|
|
if output:
|
|
|
|
dumper.string("command standard output", output)
|
|
|
|
else:
|
|
|
|
print "No output"
|
|
|
|
else:
|
|
|
|
self.execCmd(cmd, forgeCmd=True)
|
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
def shell(self):
|
|
|
|
if self.webBackdoorUrl and not kb.stackedTest:
|
|
|
|
infoMsg = "calling OS shell. To quit type "
|
|
|
|
infoMsg += "'x' or 'q' and press ENTER"
|
2009-04-22 15:48:07 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
else:
|
2010-01-14 17:33:08 +03:00
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
|
|
|
infoMsg = "going to use injected sys_eval and sys_exec "
|
|
|
|
infoMsg += "user-defined functions for operating system "
|
|
|
|
infoMsg += "command execution"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
infoMsg = "going to use xp_cmdshell extended procedure for "
|
|
|
|
infoMsg += "operating system command execution"
|
|
|
|
logger.info(infoMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
else:
|
|
|
|
errMsg = "feature not yet implemented for the back-end DBMS"
|
|
|
|
raise sqlmapUnsupportedFeatureException, errMsg
|
|
|
|
|
|
|
|
infoMsg = "calling %s OS shell. To quit type " % kb.os or "Windows"
|
|
|
|
infoMsg += "'x' or 'q' and press ENTER"
|
|
|
|
logger.info(infoMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
|
|
|
autoCompletion(osShell=True)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
command = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
command = raw_input("os-shell> ")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print
|
|
|
|
errMsg = "user aborted"
|
|
|
|
logger.error(errMsg)
|
|
|
|
except EOFError:
|
|
|
|
print
|
|
|
|
errMsg = "exit"
|
|
|
|
logger.error(errMsg)
|
|
|
|
break
|
|
|
|
|
|
|
|
if not command:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if command.lower() in ( "x", "q", "exit", "quit" ):
|
|
|
|
break
|
|
|
|
|
|
|
|
self.runCmd(command)
|
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
def initEnv(self, mandatory=True, detailed=False, web=False):
|
2010-01-02 05:02:12 +03:00
|
|
|
if self.envInitialized:
|
2009-04-22 15:48:07 +04:00
|
|
|
return
|
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
if web:
|
|
|
|
self.webInit()
|
|
|
|
else:
|
|
|
|
self.checkDbmsOs(detailed)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
if mandatory and not self.isDba():
|
|
|
|
warnMsg = "the functionality requested might not work because "
|
|
|
|
warnMsg += "the session user is not a database administrator"
|
|
|
|
logger.warn(warnMsg)
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2010-01-14 18:11:32 +03:00
|
|
|
if kb.dbms in ( "MySQL", "PostgreSQL" ):
|
2010-02-12 01:57:50 +03:00
|
|
|
self.udfInjectSys()
|
2010-01-14 18:11:32 +03:00
|
|
|
elif kb.dbms == "Microsoft SQL Server":
|
|
|
|
if mandatory:
|
|
|
|
self.xpCmdshellInit()
|
|
|
|
else:
|
|
|
|
errMsg = "feature not yet implemented for the back-end DBMS"
|
|
|
|
raise sqlmapUnsupportedFeatureException(errMsg)
|
2010-01-04 18:02:56 +03:00
|
|
|
|
|
|
|
self.envInitialized = True
|