2010-11-09 09:42:43 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
"""
|
2013-01-18 14:07:51 +00:00
|
|
|
Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
|
2010-11-09 09:42:43 +00:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
2010-11-23 21:00:42 +00:00
|
|
|
from lib.core.common import clearConsoleLine
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.common import dataToStdout
|
2010-12-26 23:50:16 +00:00
|
|
|
from lib.core.common import filterListValue
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.common import getFileItems
|
2011-01-28 16:36:09 +00:00
|
|
|
from lib.core.common import Backend
|
2011-06-18 15:47:19 +00:00
|
|
|
from lib.core.common import getPageWordSet
|
2012-02-24 13:07:20 +00:00
|
|
|
from lib.core.common import hashDBWrite
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.common import randomInt
|
2011-07-04 19:58:41 +00:00
|
|
|
from lib.core.common import randomStr
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.common import safeStringFormat
|
2011-03-29 21:54:15 +00:00
|
|
|
from lib.core.common import safeSQLIdentificatorNaming
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.data import conf
|
2010-11-11 20:37:25 +00:00
|
|
|
from lib.core.data import kb
|
2010-11-09 09:42:43 +00:00
|
|
|
from lib.core.data import logger
|
2010-12-26 11:15:02 +00:00
|
|
|
from lib.core.enums import DBMS
|
2011-12-28 13:50:03 +00:00
|
|
|
from lib.core.enums import HASHDB_KEYS
|
2012-12-06 14:14:19 +01:00
|
|
|
from lib.core.exception import SqlmapDataException
|
|
|
|
|
from lib.core.exception import SqlmapMissingMandatoryOptionException
|
2010-12-11 22:13:19 +00:00
|
|
|
from lib.core.settings import METADB_SUFFIX
|
2011-07-04 19:58:41 +00:00
|
|
|
from lib.core.settings import BRUTE_COLUMN_EXISTS_TEMPLATE
|
|
|
|
|
from lib.core.settings import BRUTE_TABLE_EXISTS_TEMPLATE
|
2011-06-07 09:50:00 +00:00
|
|
|
from lib.core.threads import getCurrentThreadData
|
|
|
|
|
from lib.core.threads import runThreads
|
2010-12-08 22:14:42 +00:00
|
|
|
from lib.request import inject
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2012-12-06 14:14:19 +01:00
|
|
|
def _addPageTextWords():
|
2011-06-18 12:30:26 +00:00
|
|
|
wordsList = []
|
2011-02-08 00:02:54 +00:00
|
|
|
|
2010-12-26 09:40:40 +00:00
|
|
|
infoMsg = "adding words used on web page to the check list"
|
2010-12-25 09:37:33 +00:00
|
|
|
logger.info(infoMsg)
|
2011-06-18 15:47:19 +00:00
|
|
|
pageWords = getPageWordSet(kb.originalPage)
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2010-12-25 09:37:33 +00:00
|
|
|
for word in pageWords:
|
2010-12-25 10:42:36 +00:00
|
|
|
word = word.lower()
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2011-06-18 12:30:26 +00:00
|
|
|
if len(word) > 2 and not word[0].isdigit() and word not in wordsList:
|
|
|
|
|
wordsList.append(word)
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2011-06-18 12:30:26 +00:00
|
|
|
return wordsList
|
|
|
|
|
|
|
|
|
|
def tableExists(tableFile, regex=None):
|
2011-07-04 19:58:41 +00:00
|
|
|
result = inject.checkBooleanExpression("%s" % safeStringFormat(BRUTE_TABLE_EXISTS_TEMPLATE, (randomInt(1), randomStr())))
|
|
|
|
|
if result:
|
|
|
|
|
errMsg = "can't use table existence check because of detected invalid results "
|
|
|
|
|
errMsg += "(most probably caused by inability of the used injection "
|
|
|
|
|
errMsg += "to distinguish errornous results)"
|
2013-01-03 23:20:55 +01:00
|
|
|
raise SqlmapDataException(errMsg)
|
2011-07-04 19:58:41 +00:00
|
|
|
|
2012-06-15 20:49:28 +00:00
|
|
|
tables = getFileItems(tableFile, lowercase=Backend.getIdentifiedDbms() in (DBMS.ACCESS,), unique=True)
|
2011-06-18 12:30:26 +00:00
|
|
|
|
|
|
|
|
infoMsg = "checking table existence using items from '%s'" % tableFile
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
2012-12-06 14:14:19 +01:00
|
|
|
tables.extend(_addPageTextWords())
|
2010-12-26 23:50:16 +00:00
|
|
|
tables = filterListValue(tables, regex)
|
2011-06-07 10:08:12 +00:00
|
|
|
|
|
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
threadData.shared.count = 0
|
|
|
|
|
threadData.shared.limit = len(tables)
|
2012-12-20 20:55:59 +01:00
|
|
|
threadData.shared.value = []
|
2011-06-07 10:08:12 +00:00
|
|
|
threadData.shared.unique = set()
|
2010-12-21 10:31:56 +00:00
|
|
|
|
2010-12-20 23:21:01 +00:00
|
|
|
def tableExistsThread():
|
2011-06-07 10:08:12 +00:00
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
|
|
|
|
|
while kb.threadContinue:
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.acquire()
|
2011-06-07 10:08:12 +00:00
|
|
|
if threadData.shared.count < threadData.shared.limit:
|
2011-06-16 14:27:44 +00:00
|
|
|
table = safeSQLIdentificatorNaming(tables[threadData.shared.count], True)
|
2011-06-07 10:08:12 +00:00
|
|
|
threadData.shared.count += 1
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.release()
|
2011-06-07 10:08:12 +00:00
|
|
|
else:
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.release()
|
2011-06-07 10:08:12 +00:00
|
|
|
break
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2011-05-30 23:15:29 +00:00
|
|
|
if conf.db and METADB_SUFFIX not in conf.db:
|
2011-02-09 17:07:02 +00:00
|
|
|
fullTableName = "%s%s%s" % (conf.db, '..' if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE) else '.', table)
|
2010-12-27 14:17:20 +00:00
|
|
|
else:
|
|
|
|
|
fullTableName = table
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2011-07-04 19:58:41 +00:00
|
|
|
result = inject.checkBooleanExpression("%s" % safeStringFormat(BRUTE_TABLE_EXISTS_TEMPLATE, (randomInt(1), fullTableName)))
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.io.acquire()
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2011-06-07 10:08:12 +00:00
|
|
|
if result and table.lower() not in threadData.shared.unique:
|
2012-12-20 20:55:59 +01:00
|
|
|
threadData.shared.value.append(table)
|
2011-06-07 10:08:12 +00:00
|
|
|
threadData.shared.unique.add(table.lower())
|
2011-04-10 21:52:08 +00:00
|
|
|
|
2010-12-20 23:21:01 +00:00
|
|
|
if conf.verbose in (1, 2):
|
|
|
|
|
clearConsoleLine(True)
|
2011-04-14 09:31:45 +00:00
|
|
|
infoMsg = "[%s] [INFO] retrieved: %s\r\n" % (time.strftime("%X"), table)
|
2010-12-20 23:21:01 +00:00
|
|
|
dataToStdout(infoMsg, True)
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2010-12-13 21:37:12 +00:00
|
|
|
if conf.verbose in (1, 2):
|
2013-01-09 16:10:26 +01:00
|
|
|
status = '%d/%d items (%d%%)' % (threadData.shared.count, threadData.shared.limit, round(100.0 * threadData.shared.count / threadData.shared.limit))
|
2011-01-16 00:15:30 +00:00
|
|
|
dataToStdout("\r[%s] [INFO] tried %s" % (time.strftime("%X"), status), True)
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.io.release()
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2010-12-20 23:21:01 +00:00
|
|
|
try:
|
2011-06-07 10:08:12 +00:00
|
|
|
runThreads(conf.threads, tableExistsThread, threadChoice=True)
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2010-12-20 23:21:01 +00:00
|
|
|
except KeyboardInterrupt:
|
2011-06-07 10:08:12 +00:00
|
|
|
warnMsg = "user aborted during table existence "
|
|
|
|
|
warnMsg += "check. sqlmap will display partial output"
|
2011-03-31 14:26:14 +00:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
2010-11-23 21:00:42 +00:00
|
|
|
clearConsoleLine(True)
|
2011-01-16 00:15:30 +00:00
|
|
|
dataToStdout("\n")
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2012-12-20 20:55:59 +01:00
|
|
|
if not threadData.shared.value:
|
2011-05-30 23:04:49 +00:00
|
|
|
warnMsg = "no table(s) found"
|
2010-11-09 09:42:43 +00:00
|
|
|
logger.warn(warnMsg)
|
2010-11-11 20:37:25 +00:00
|
|
|
else:
|
2012-12-20 20:55:59 +01:00
|
|
|
for item in threadData.shared.value:
|
2012-07-14 11:01:30 +02:00
|
|
|
if conf.db not in kb.data.cachedTables:
|
2010-11-11 20:37:25 +00:00
|
|
|
kb.data.cachedTables[conf.db] = [item]
|
|
|
|
|
else:
|
|
|
|
|
kb.data.cachedTables[conf.db].append(item)
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2013-01-10 16:09:28 +01:00
|
|
|
for _ in ((conf.db, item) for item in threadData.shared.value):
|
2011-12-28 13:50:03 +00:00
|
|
|
if _ not in kb.brute.tables:
|
|
|
|
|
kb.brute.tables.append(_)
|
|
|
|
|
|
2012-02-24 13:07:20 +00:00
|
|
|
hashDBWrite(HASHDB_KEYS.KB_BRUTE_TABLES, kb.brute.tables, True)
|
2011-12-28 13:50:03 +00:00
|
|
|
|
2010-11-11 20:37:25 +00:00
|
|
|
return kb.data.cachedTables
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2010-12-26 23:50:16 +00:00
|
|
|
def columnExists(columnFile, regex=None):
|
2010-11-09 16:15:55 +00:00
|
|
|
if not conf.tbl:
|
|
|
|
|
errMsg = "missing table parameter"
|
2013-01-03 23:20:55 +01:00
|
|
|
raise SqlmapMissingMandatoryOptionException(errMsg)
|
2010-11-09 16:15:55 +00:00
|
|
|
|
2011-07-04 19:58:41 +00:00
|
|
|
result = inject.checkBooleanExpression(safeStringFormat(BRUTE_COLUMN_EXISTS_TEMPLATE, (randomStr(), randomStr())))
|
|
|
|
|
if result:
|
|
|
|
|
errMsg = "can't use column existence check because of detected invalid results "
|
|
|
|
|
errMsg += "(most probably caused by inability of the used injection "
|
|
|
|
|
errMsg += "to distinguish errornous results)"
|
2013-01-03 23:20:55 +01:00
|
|
|
raise SqlmapDataException(errMsg)
|
2011-07-04 19:58:41 +00:00
|
|
|
|
2011-06-18 12:30:26 +00:00
|
|
|
infoMsg = "checking column existence using items from '%s'" % columnFile
|
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
|
2010-12-26 11:15:02 +00:00
|
|
|
columns = getFileItems(columnFile, unique=True)
|
2012-12-06 14:14:19 +01:00
|
|
|
columns.extend(_addPageTextWords())
|
2010-12-26 23:50:16 +00:00
|
|
|
columns = filterListValue(columns, regex)
|
|
|
|
|
|
2012-11-28 00:03:17 +01:00
|
|
|
table = safeSQLIdentificatorNaming(conf.tbl, True)
|
2011-05-30 23:15:29 +00:00
|
|
|
if conf.db and METADB_SUFFIX not in conf.db:
|
2012-11-28 00:03:17 +01:00
|
|
|
table = "%s.%s" % (safeSQLIdentificatorNaming(conf.db), table)
|
2010-11-11 22:26:36 +00:00
|
|
|
|
2010-12-24 20:17:53 +00:00
|
|
|
kb.threadContinue = True
|
2011-04-08 11:02:21 +00:00
|
|
|
kb.bruteMode = True
|
2010-12-24 20:17:53 +00:00
|
|
|
|
2011-06-07 09:50:00 +00:00
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
threadData.shared.count = 0
|
|
|
|
|
threadData.shared.limit = len(columns)
|
2012-12-20 20:55:59 +01:00
|
|
|
threadData.shared.value = []
|
2011-06-07 09:50:00 +00:00
|
|
|
|
2010-12-24 20:17:53 +00:00
|
|
|
def columnExistsThread():
|
2011-06-07 09:50:00 +00:00
|
|
|
threadData = getCurrentThreadData()
|
|
|
|
|
|
|
|
|
|
while kb.threadContinue:
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.acquire()
|
2011-06-07 09:50:00 +00:00
|
|
|
if threadData.shared.count < threadData.shared.limit:
|
|
|
|
|
column = safeSQLIdentificatorNaming(columns[threadData.shared.count])
|
|
|
|
|
threadData.shared.count += 1
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.release()
|
2011-06-07 09:50:00 +00:00
|
|
|
else:
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.count.release()
|
2011-06-07 09:50:00 +00:00
|
|
|
break
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2011-07-04 19:58:41 +00:00
|
|
|
result = inject.checkBooleanExpression(safeStringFormat(BRUTE_COLUMN_EXISTS_TEMPLATE, (column, table)))
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.io.acquire()
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2010-12-24 20:17:53 +00:00
|
|
|
if result:
|
2012-12-20 20:55:59 +01:00
|
|
|
threadData.shared.value.append(column)
|
2010-12-24 20:17:53 +00:00
|
|
|
|
|
|
|
|
if conf.verbose in (1, 2):
|
|
|
|
|
clearConsoleLine(True)
|
2011-04-14 09:31:45 +00:00
|
|
|
infoMsg = "[%s] [INFO] retrieved: %s\r\n" % (time.strftime("%X"), column)
|
2010-12-24 20:17:53 +00:00
|
|
|
dataToStdout(infoMsg, True)
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2010-12-13 21:37:12 +00:00
|
|
|
if conf.verbose in (1, 2):
|
2013-01-09 16:10:26 +01:00
|
|
|
status = '%d/%d items (%d%%)' % (threadData.shared.count, threadData.shared.limit, round(100.0 * threadData.shared.count / threadData.shared.limit))
|
2011-01-16 00:15:30 +00:00
|
|
|
dataToStdout("\r[%s] [INFO] tried %s" % (time.strftime("%X"), status), True)
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2011-12-28 16:27:17 +00:00
|
|
|
kb.locks.io.release()
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2010-12-24 20:17:53 +00:00
|
|
|
try:
|
2011-06-07 09:50:00 +00:00
|
|
|
runThreads(conf.threads, columnExistsThread, threadChoice=True)
|
2011-01-07 16:36:32 +00:00
|
|
|
|
2010-12-24 20:17:53 +00:00
|
|
|
except KeyboardInterrupt:
|
2011-06-07 09:50:00 +00:00
|
|
|
warnMsg = "user aborted during column existence "
|
|
|
|
|
warnMsg += "check. sqlmap will display partial output"
|
2011-03-31 14:26:14 +00:00
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
2010-11-23 21:00:42 +00:00
|
|
|
clearConsoleLine(True)
|
2011-01-16 00:15:30 +00:00
|
|
|
dataToStdout("\n")
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2012-12-20 20:55:59 +01:00
|
|
|
if not threadData.shared.value:
|
2011-05-30 23:04:49 +00:00
|
|
|
warnMsg = "no column(s) found"
|
2010-11-09 09:42:43 +00:00
|
|
|
logger.warn(warnMsg)
|
2010-11-11 20:37:25 +00:00
|
|
|
else:
|
|
|
|
|
columns = {}
|
2010-11-09 09:42:43 +00:00
|
|
|
|
2012-12-20 20:55:59 +01:00
|
|
|
for column in threadData.shared.value:
|
2011-01-18 11:42:36 +00:00
|
|
|
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s WHERE ROUND(%s)=ROUND(%s))", (column, table, column, column)))
|
2010-11-11 20:37:25 +00:00
|
|
|
|
|
|
|
|
if result:
|
|
|
|
|
columns[column] = 'numeric'
|
|
|
|
|
else:
|
|
|
|
|
columns[column] = 'non-numeric'
|
|
|
|
|
|
|
|
|
|
kb.data.cachedColumns[conf.db] = {conf.tbl: columns}
|
|
|
|
|
|
2011-12-28 13:50:03 +00:00
|
|
|
for _ in map(lambda x: (conf.db, conf.tbl, x[0], x[1]), columns.items()):
|
|
|
|
|
if _ not in kb.brute.columns:
|
|
|
|
|
kb.brute.columns.append(_)
|
|
|
|
|
|
2012-02-24 13:07:20 +00:00
|
|
|
hashDBWrite(HASHDB_KEYS.KB_BRUTE_COLUMNS, kb.brute.columns, True)
|
2011-12-28 13:50:03 +00:00
|
|
|
|
2010-11-11 20:37:25 +00:00
|
|
|
return kb.data.cachedColumns
|