sqlmap/lib/techniques/brute/use.py

122 lines
3.7 KiB
Python
Raw Normal View History

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
"""
import time
2010-11-24 00:00:42 +03:00
from lib.core.common import clearConsoleLine
2010-11-09 12:42:43 +03:00
from lib.core.common import dataToStdout
from lib.core.common import getFileItems
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
from lib.core.data import kb
2010-11-09 12:42:43 +03:00
from lib.core.data import logger
from lib.core.exception import sqlmapMissingMandatoryOptionException
from lib.core.settings import METADB_SUFFIX
2010-12-09 01:14:42 +03:00
from lib.request import inject
2010-11-09 12:42:43 +03:00
def tableExists(tableFile):
2010-11-09 19:53:33 +03:00
tables = getFileItems(tableFile)
2010-11-09 12:42:43 +03:00
retVal = []
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)
pushValue(conf.verbose)
conf.verbose = 0
count = 0
length = len(tables)
for table in tables:
if conf.db and not conf.db.endswith(METADB_SUFFIX):
2010-11-12 01:26:36 +03:00
table = "%s.%s" % (conf.db, table)
2010-12-09 01:38:26 +03:00
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %d FROM %s)", (randomInt(1), table)), expectingNone=True)
2010-11-09 12:42:43 +03:00
if result:
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
infoMsg = "\r[%s] [INFO] retrieved: %s\n" % (time.strftime("%X"), table)
2010-11-09 12:42:43 +03:00
dataToStdout(infoMsg, True)
retVal.append(table)
count += 1
status = '%d/%d items (%d%s)' % (count, length, round(100.0*count/length), '%')
dataToStdout("\r[%s] [INFO] tried: %s" % (time.strftime("%X"), status), True)
conf.verbose = popValue()
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
2010-11-09 12:42:43 +03:00
if not retVal:
warnMsg = "no table found"
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):
if not conf.tbl:
errMsg = "missing table parameter"
raise sqlmapMissingMandatoryOptionException, errMsg
2010-11-09 19:53:33 +03:00
columns = getFileItems(columnFile)
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)
pushValue(conf.verbose)
conf.verbose = 0
count = 0
length = len(columns)
2010-11-09 12:42:43 +03:00
for column in columns:
2010-12-09 01:38:26 +03:00
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s)", (column, table)), expectingNone=True)
2010-11-09 12:42:43 +03:00
if result:
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
infoMsg = "\r[%s] [INFO] retrieved: %s\n" % (time.strftime("%X"), column)
2010-11-09 12:42:43 +03:00
dataToStdout(infoMsg, True)
retVal.append(column)
count += 1
status = '%d/%d items (%d%s)' % (count, length, round(100.0*count/length), '%')
dataToStdout("\r[%s] [INFO] tried: %s" % (time.strftime("%X"), status), True)
conf.verbose = popValue()
2010-11-24 00:00:42 +03:00
clearConsoleLine(True)
2010-11-09 12:42:43 +03:00
if not retVal:
warnMsg = "no column found"
logger.warn(warnMsg)
else:
columns = {}
2010-11-09 12:42:43 +03:00
for column in retVal:
2010-12-09 01:38:26 +03:00
result = inject.checkBooleanExpression("%s" % safeStringFormat("EXISTS(SELECT %s FROM %s WHERE %s>0)", (column, table, column)), expectingNone=True)
if result:
columns[column] = 'numeric'
else:
columns[column] = 'non-numeric'
kb.data.cachedColumns[conf.db] = {conf.tbl: columns}
return kb.data.cachedColumns