2010-11-09 12:42:43 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
|
|
|
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
|
2010-12-27 17:17:20 +03:00
|
|
|
from lib.core.common import dataToSessionFile
|
2010-11-09 12:42:43 +03:00
|
|
|
from lib.core.common import dataToStdout
|
2010-12-27 02:50:16 +03:00
|
|
|
from lib.core.common import filterListValue
|
2010-11-09 12:42:43 +03:00
|
|
|
from lib.core.common import getFileItems
|
2011-01-20 02:06:15 +03:00
|
|
|
from lib.core.common import backend
|
2010-12-25 12:37:33 +03:00
|
|
|
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
|
|
|
|
from lib.core.common import safeStringFormat
|
|
|
|
from lib.core.data import conf
|
2010-11-11 23:37:25 +03:00
|
|
|
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
|
2010-11-09 19:15:55 +03:00
|
|
|
from lib.core.exception import sqlmapMissingMandatoryOptionException
|
2010-12-25 12:37:33 +03:00
|
|
|
from lib.core.exception import sqlmapThreadException
|
2010-12-12 01:13:19 +03:00
|
|
|
from lib.core.settings import METADB_SUFFIX
|
2010-12-27 17:17:20 +03:00
|
|
|
from lib.core.session import safeFormatString
|
2010-12-09 01:14:42 +03:00
|
|
|
from lib.request import inject
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-27 02:50:16 +03:00
|
|
|
def tableExists(tableFile, regex=None):
|
2011-01-20 02:06:15 +03:00
|
|
|
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)
|
2010-12-25 12:37:33 +03:00
|
|
|
|
2010-12-26 12:40:40 +03:00
|
|
|
infoMsg = "adding words used on web page to the check list"
|
2010-12-25 12:37:33 +03:00
|
|
|
logger.info(infoMsg)
|
|
|
|
pageWords = getPageTextWordsSet(kb.originalPage)
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-25 12:37:33 +03:00
|
|
|
for word in pageWords:
|
2010-12-25 13:42:36 +03:00
|
|
|
word = word.lower()
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-27 02:50:16 +03:00
|
|
|
if len(word) > 2 and not word[0].isdigit() and word not in tables:
|
2010-12-25 13:42:36 +03:00
|
|
|
tables.append(word)
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-27 02:50:16 +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 = []
|
|
|
|
tbllock = threading.Lock()
|
|
|
|
iolock = threading.Lock()
|
|
|
|
kb.threadContinue = True
|
2011-01-07 19:47:46 +03:00
|
|
|
kb.suppressSession = True
|
2010-12-21 13:31:56 +03:00
|
|
|
|
2010-12-21 02:21:01 +03:00
|
|
|
def tableExistsThread():
|
|
|
|
while count[0] < length and kb.threadContinue:
|
|
|
|
tbllock.acquire()
|
|
|
|
table = tables[count[0]]
|
|
|
|
count[0] += 1
|
|
|
|
tbllock.release()
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-21 02:21:01 +03:00
|
|
|
if conf.db and not conf.db.endswith(METADB_SUFFIX):
|
2010-12-27 17:17:20 +03:00
|
|
|
fullTableName = "%s.%s" % (conf.db, table)
|
|
|
|
else:
|
|
|
|
fullTableName = table
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-27 17:17:20 +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
|
|
|
|
2010-12-21 02:21:01 +03:00
|
|
|
if result:
|
|
|
|
retVal.append(table)
|
|
|
|
|
2010-12-27 17:17:20 +03:00
|
|
|
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 = "\r[%s] [INFO] retrieved: %s\n" % (time.strftime("%X"), table)
|
|
|
|
dataToStdout(infoMsg, True)
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-14 00:37:12 +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-14 00:37:12 +03:00
|
|
|
|
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:
|
|
|
|
warnMsg = "running in a single-thread mode. this could take a while."
|
|
|
|
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
|
2010-12-21 17:21:24 +03:00
|
|
|
kb.threadException = True
|
|
|
|
|
|
|
|
print
|
|
|
|
logger.debug("waiting for threads to finish")
|
|
|
|
|
|
|
|
try:
|
|
|
|
while (threading.activeCount() > 1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise sqlmapThreadException, "user aborted"
|
2010-12-21 02:21:01 +03:00
|
|
|
finally:
|
2010-12-26 05:19:10 +03:00
|
|
|
kb.threadContinue = True
|
|
|
|
kb.threadException = False
|
2011-01-07 19:47:46 +03:00
|
|
|
kb.suppressSession = 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:
|
|
|
|
warnMsg = "no table found"
|
|
|
|
logger.warn(warnMsg)
|
2010-11-11 23:37:25 +03:00
|
|
|
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
|
|
|
|
2010-11-11 23:37:25 +03:00
|
|
|
return kb.data.cachedTables
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-27 02:50:16 +03:00
|
|
|
def columnExists(columnFile, regex=None):
|
2010-11-09 19:15:55 +03:00
|
|
|
if not conf.tbl:
|
|
|
|
errMsg = "missing table parameter"
|
|
|
|
raise sqlmapMissingMandatoryOptionException, errMsg
|
|
|
|
|
2010-12-26 14:15:02 +03:00
|
|
|
columns = getFileItems(columnFile, unique=True)
|
2010-12-27 02:50:16 +03:00
|
|
|
columns = filterListValue(columns, regex)
|
|
|
|
|
2010-12-12 01:13:19 +03:00
|
|
|
if conf.db and not conf.db.endswith(METADB_SUFFIX):
|
2010-11-12 01:26:36 +03:00
|
|
|
table = "%s.%s" % (conf.db, conf.tbl)
|
|
|
|
else:
|
|
|
|
table = conf.tbl
|
|
|
|
|
2010-11-09 12:42:43 +03:00
|
|
|
retVal = []
|
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)
|
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
count = [0]
|
2010-11-09 19:15:55 +03:00
|
|
|
length = len(columns)
|
2010-12-24 23:17:53 +03:00
|
|
|
threads = []
|
|
|
|
collock = threading.Lock()
|
|
|
|
iolock = threading.Lock()
|
|
|
|
kb.threadContinue = True
|
2011-01-07 19:47:46 +03:00
|
|
|
kb.suppressSession = True
|
2010-12-24 23:17:53 +03:00
|
|
|
|
|
|
|
def columnExistsThread():
|
|
|
|
while count[0] < length and kb.threadContinue:
|
|
|
|
collock.acquire()
|
|
|
|
column = columns[count[0]]
|
|
|
|
count[0] += 1
|
|
|
|
collock.release()
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s)", (column, table)))
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
iolock.acquire()
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
if result:
|
|
|
|
retVal.append(column)
|
|
|
|
|
|
|
|
if conf.verbose in (1, 2):
|
|
|
|
clearConsoleLine(True)
|
|
|
|
infoMsg = "\r[%s] [INFO] retrieved: %s\n" % (time.strftime("%X"), column)
|
|
|
|
dataToStdout(infoMsg, True)
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-14 00:37:12 +03:00
|
|
|
if conf.verbose in (1, 2):
|
2010-12-24 23:17:53 +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-24 23:17:53 +03:00
|
|
|
iolock.release()
|
2010-12-14 00:37:12 +03:00
|
|
|
|
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:
|
|
|
|
warnMsg = "running in a single-thread mode. this could take a while."
|
|
|
|
logger.warn(warnMsg)
|
2010-12-26 12:40:40 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
# Start the threads
|
|
|
|
for numThread in range(conf.threads):
|
|
|
|
thread = threading.Thread(target=columnExistsThread, name=str(numThread))
|
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
# And wait for them to all finish
|
|
|
|
try:
|
|
|
|
alive = True
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-24 23:17:53 +03:00
|
|
|
while alive:
|
|
|
|
alive = False
|
2011-01-07 19:36:32 +03:00
|
|
|
|
2010-12-24 23:17:53 +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")
|
|
|
|
|
|
|
|
try:
|
|
|
|
while (threading.activeCount() > 1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise sqlmapThreadException, "user aborted"
|
|
|
|
finally:
|
2010-12-26 05:19:10 +03:00
|
|
|
kb.threadContinue = True
|
|
|
|
kb.threadException = False
|
2011-01-07 19:47:46 +03:00
|
|
|
kb.suppressSession = 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:
|
|
|
|
warnMsg = "no column found"
|
|
|
|
logger.warn(warnMsg)
|
2010-11-11 23:37:25 +03:00
|
|
|
else:
|
|
|
|
columns = {}
|
2010-11-09 12:42:43 +03:00
|
|
|
|
2010-11-11 23:37:25 +03:00
|
|
|
for column in retVal:
|
2011-01-18 14:42:36 +03:00
|
|
|
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s WHERE ROUND(%s)=ROUND(%s))", (column, table, column, column)))
|
2010-11-11 23:37:25 +03:00
|
|
|
|
|
|
|
if result:
|
|
|
|
columns[column] = 'numeric'
|
|
|
|
else:
|
|
|
|
columns[column] = 'non-numeric'
|
|
|
|
|
2010-12-27 17:17:20 +03:00
|
|
|
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])))
|
|
|
|
|
2010-11-11 23:37:25 +03:00
|
|
|
kb.data.cachedColumns[conf.db] = {conf.tbl: columns}
|
|
|
|
|
|
|
|
return kb.data.cachedColumns
|