sqlmap/lib/core/dump.py

448 lines
14 KiB
Python
Raw Normal View History

2008-10-15 19:38:22 +04:00
#!/usr/bin/env python
"""
2008-10-15 19:56:32 +04:00
$Id$
2008-10-15 19:38:22 +04:00
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
2008-10-15 19:38:22 +04:00
"""
import codecs
import re
import os
2008-10-15 19:38:22 +04:00
2011-02-21 01:41:42 +03:00
from lib.core.common import Backend
2008-10-15 19:38:22 +04:00
from lib.core.common import dataToDumpFile
from lib.core.common import dataToStdout
2010-06-02 16:31:36 +04:00
from lib.core.common import getUnicode
from lib.core.common import normalizeUnicode
from lib.core.common import openFile
2010-10-21 13:51:07 +04:00
from lib.core.common import restoreDumpMarkedChars
2008-10-15 19:38:22 +04:00
from lib.core.data import conf
2010-09-24 17:19:35 +04:00
from lib.core.data import kb
2008-10-15 19:38:22 +04:00
from lib.core.data import logger
2011-02-21 01:41:42 +03:00
from lib.core.enums import DBMS
2010-09-24 17:19:35 +04:00
from lib.core.replication import Replication
2011-01-30 14:36:03 +03:00
from lib.core.settings import UNICODE_ENCODING
2008-10-15 19:38:22 +04:00
class Dump:
"""
This class defines methods used to parse and output the results
of SQL injection actions
"""
def __init__(self):
self.__outputFile = None
self.__outputFP = None
def __write(self, data, n=True):
2010-10-15 14:28:06 +04:00
text = "%s%s" % (data, "\n" if n else " ")
dataToStdout(text)
2010-10-15 14:28:06 +04:00
self.__outputFP.write(text)
2008-10-15 19:38:22 +04:00
self.__outputFP.flush()
conf.loggedToOut = True
2010-10-21 13:51:07 +04:00
def __formatString(self, inpStr):
return restoreDumpMarkedChars(getUnicode(inpStr))
2008-10-15 19:38:22 +04:00
def setOutputFile(self):
self.__outputFile = "%s%slog" % (conf.outputPath, os.sep)
2011-01-30 14:36:03 +03:00
self.__outputFP = codecs.open(self.__outputFile, "ab", UNICODE_ENCODING)
2010-09-26 18:56:55 +04:00
def getOutputFile(self):
return self.__outputFile
def string(self, header, data, sort=True):
2008-10-15 19:38:22 +04:00
if isinstance(data, (list, tuple, set)):
self.lister(header, data, sort)
2008-10-15 19:38:22 +04:00
return
2010-06-02 16:31:36 +04:00
data = getUnicode(data)
2008-10-15 19:38:22 +04:00
if data:
data = self.__formatString(data)
2008-10-15 19:38:22 +04:00
if "\n" in data:
self.__write("%s:\n---\n%s\n---\n" % (header, data))
2008-10-15 19:38:22 +04:00
else:
self.__write("%s: '%s'\n" % (header, data))
else:
self.__write("%s:\tNone\n" % header)
2010-09-24 17:19:35 +04:00
def lister(self, header, elements, sort=True):
if elements:
self.__write("%s [%d]:" % (header, len(elements)))
2008-10-15 19:38:22 +04:00
if sort:
try:
elements = set(elements)
elements = list(elements)
2011-03-18 19:52:46 +03:00
elements.sort(key=lambda x: x.lower() if isinstance(x, basestring) else x)
except:
pass
2008-10-15 19:38:22 +04:00
for element in elements:
if isinstance(element, basestring):
2008-10-15 19:38:22 +04:00
self.__write("[*] %s" % element)
elif isinstance(element, (list, tuple, set)):
2010-06-02 16:31:36 +04:00
self.__write("[*] " + ", ".join(getUnicode(e) for e in element))
2008-10-15 19:38:22 +04:00
if elements:
self.__write("")
def technic(self, header, data):
self.string(header, data)
def banner(self,data):
self.string("banner", data)
def currentUser(self,data):
self.string("current user", data)
def currentDb(self,data):
2011-02-21 01:41:42 +03:00
if Backend.getIdentifiedDbms() in (DBMS.MAXDB, DBMS.ORACLE):
self.string("current database (no practical usage on %s)" % Backend.getIdentifiedDbms(), data)
else:
self.string("current database", data)
def dba(self,data):
self.string("current user is DBA", data)
def users(self,users):
self.lister("database management system users", users)
2008-10-15 19:38:22 +04:00
def userSettings(self, header, userSettings, subHeader):
self.__areAdmins = set()
if userSettings:
self.__write("%s:" % header)
2008-10-15 19:38:22 +04:00
if isinstance(userSettings, (tuple, list, set)):
self.__areAdmins = userSettings[1]
userSettings = userSettings[0]
users = userSettings.keys()
2011-03-18 19:52:46 +03:00
users.sort(key=lambda x: x.lower() if isinstance(x, basestring) else x)
2008-10-15 19:38:22 +04:00
for user in users:
settings = userSettings[user]
if settings is None:
stringSettings = ""
else:
stringSettings = " [%d]:" % len(settings)
2008-10-15 19:38:22 +04:00
if user in self.__areAdmins:
self.__write("[*] %s (administrator)%s" % (user, stringSettings))
2008-10-15 19:38:22 +04:00
else:
self.__write("[*] %s%s" % (user, stringSettings))
2008-10-15 19:38:22 +04:00
if settings:
settings.sort()
2008-10-15 19:38:22 +04:00
for setting in settings:
self.__write(" %s: %s" % (subHeader, setting))
2008-10-15 19:38:22 +04:00
print
def dbs(self,dbs):
self.lister("available databases", dbs)
2008-10-15 19:38:22 +04:00
def dbTables(self, dbTables):
if isinstance(dbTables, dict) and len(dbTables) > 0:
2010-09-30 16:35:45 +04:00
maxlength = 0
for tables in dbTables.values():
for table in tables:
if isinstance(table, (list, tuple, set)):
table = table[0]
maxlength = max(maxlength, len(normalizeUnicode(table)))
2010-09-30 16:35:45 +04:00
lines = "-" * (int(maxlength) + 2)
for db, tables in dbTables.items():
tables.sort()
2010-09-30 16:35:45 +04:00
self.__write("Database: %s" % db)
if len(tables) == 1:
self.__write("[1 table]")
else:
self.__write("[%d tables]" % len(tables))
self.__write("+%s+" % lines)
for table in tables:
if isinstance(table, (list, tuple, set)):
table = table[0]
blank = " " * (maxlength - len(normalizeUnicode(table)))
2010-09-30 16:35:45 +04:00
self.__write("| %s%s |" % (table, blank))
self.__write("+%s+\n" % lines)
else:
self.string("tables", dbTables)
2008-10-15 19:38:22 +04:00
def dbTableColumns(self, tableColumns):
for db, tables in tableColumns.items():
if not db:
db = "All"
2008-10-15 19:38:22 +04:00
for table, columns in tables.items():
maxlength1 = 0
maxlength2 = 0
2008-10-15 19:38:22 +04:00
colType = None
colList = columns.keys()
2011-03-18 19:52:46 +03:00
colList.sort(key=lambda x: x.lower() if isinstance(x, basestring) else x)
2008-10-15 19:38:22 +04:00
for column in colList:
colType = columns[column]
maxlength1 = max(maxlength1, len(column))
2008-10-15 19:38:22 +04:00
if colType is not None:
maxlength2 = max(maxlength2, len(colType))
maxlength1 = max(maxlength1, len("COLUMN"))
lines1 = "-" * (int(maxlength1) + 2)
if colType is not None:
maxlength2 = max(maxlength2, len("TYPE"))
lines2 = "-" * (int(maxlength2) + 2)
self.__write("Database: %s\nTable: %s" % (db, table))
if len(columns) == 1:
self.__write("[1 column]")
else:
self.__write("[%d columns]" % len(columns))
2008-10-15 19:38:22 +04:00
if colType is not None:
self.__write("+%s+%s+" % (lines1, lines2))
else:
self.__write("+%s+" % lines1)
blank1 = " " * (maxlength1 - len("COLUMN"))
if colType is not None:
blank2 = " " * (maxlength2 - len("TYPE"))
2008-10-15 19:38:22 +04:00
if colType is not None:
self.__write("| Column%s | Type%s |" % (blank1, blank2))
self.__write("+%s+%s+" % (lines1, lines2))
else:
self.__write("| Column%s |" % blank1)
self.__write("+%s+" % lines1)
for column in colList:
colType = columns[column]
blank1 = " " * (maxlength1 - len(column))
if colType is not None:
blank2 = " " * (maxlength2 - len(colType))
self.__write("| %s%s | %s%s |" % (column, blank1, colType, blank2))
else:
self.__write("| %s%s |" % (column, blank1))
if colType is not None:
self.__write("+%s+%s+\n" % (lines1, lines2))
else:
self.__write("+%s+\n" % lines1)
2008-10-15 19:38:22 +04:00
def dbTableValues(self, tableValues):
2010-09-24 17:34:46 +04:00
replication = None
rtable = None
if tableValues is None:
return
2008-10-15 19:38:22 +04:00
db = tableValues["__infos__"]["db"]
if not db:
db = "All"
table = tableValues["__infos__"]["table"]
2010-09-24 17:34:46 +04:00
if conf.replicate:
2010-09-24 18:34:05 +04:00
replication = Replication("%s%s%s.sqlite3" % (conf.dumpPath, os.sep, db))
2010-09-24 17:34:46 +04:00
elif not conf.multipleTargets:
2008-10-15 19:38:22 +04:00
dumpDbPath = "%s%s%s" % (conf.dumpPath, os.sep, db)
if not os.path.isdir(dumpDbPath):
os.makedirs(dumpDbPath, 0755)
dumpFileName = "%s%s%s.csv" % (dumpDbPath, os.sep, table)
dumpFP = openFile(dumpFileName, "wb")
2008-10-15 19:38:22 +04:00
2010-09-24 17:19:35 +04:00
count = int(tableValues["__infos__"]["count"])
separator = str()
field = 1
fields = len(tableValues) - 1
2008-10-15 19:38:22 +04:00
columns = tableValues.keys()
2011-03-18 19:52:46 +03:00
columns.sort(key=lambda x: x.lower() if isinstance(x, basestring) else x)
2008-10-15 19:38:22 +04:00
for column in columns:
if column != "__infos__":
info = tableValues[column]
lines = "-" * (int(info["length"]) + 2)
2008-10-15 19:38:22 +04:00
separator += "+%s" % lines
separator += "+"
self.__write("Database: %s\nTable: %s" % (db, table))
2010-09-24 17:19:35 +04:00
if conf.replicate:
2010-09-24 18:34:05 +04:00
cols = []
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
for column in columns:
if column != "__infos__":
colType = Replication.INTEGER
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
for value in tableValues[column]['values']:
try:
if re.search("^[\ *]*$", value): #NULL
continue
2010-11-04 15:21:06 +03:00
_ = int(value)
2010-09-24 18:34:05 +04:00
except ValueError:
colType = None
break
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
if colType is None:
colType = Replication.REAL
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
for value in tableValues[column]['values']:
try:
if re.search("^[\ *]*$", value): #NULL
continue
2010-11-04 15:21:06 +03:00
_ = float(value)
2010-09-24 18:34:05 +04:00
except ValueError:
colType = None
break
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
cols.append((column, colType if colType else Replication.TEXT))
2010-11-04 15:21:06 +03:00
2010-09-24 18:34:05 +04:00
rtable = replication.createTable(table, cols)
2010-09-24 17:19:35 +04:00
2008-10-15 19:38:22 +04:00
if count == 1:
self.__write("[1 entry]")
else:
self.__write("[%d entries]" % count)
self.__write(separator)
for column in columns:
if column != "__infos__":
info = tableValues[column]
2008-10-15 19:38:22 +04:00
maxlength = int(info["length"])
blank = " " * (maxlength - len(column))
2008-10-15 19:38:22 +04:00
self.__write("| %s%s" % (column, blank), n=False)
2010-09-24 17:34:46 +04:00
if not conf.replicate:
if not conf.multipleTargets and field == fields:
dataToDumpFile(dumpFP, "%s" % column)
elif not conf.multipleTargets:
dataToDumpFile(dumpFP, "%s," % column)
2008-10-15 19:38:22 +04:00
field += 1
self.__write("|\n%s" % separator)
2010-09-24 17:34:46 +04:00
if not conf.multipleTargets and not conf.replicate:
2008-10-15 19:38:22 +04:00
dataToDumpFile(dumpFP, "\n")
for i in range(count):
field = 1
2010-09-24 17:19:35 +04:00
values = []
2008-10-15 19:38:22 +04:00
for column in columns:
if column != "__infos__":
info = tableValues[column]
if len(info["values"]) <= i:
continue
2010-06-02 16:31:36 +04:00
value = getUnicode(info["values"][i])
if re.search("^[\ *]*$", value):
2008-10-15 19:38:22 +04:00
value = "NULL"
2010-09-24 17:19:35 +04:00
values.append(value)
2008-10-15 19:38:22 +04:00
maxlength = int(info["length"])
blank = " " * (maxlength - len(value))
2008-10-15 19:38:22 +04:00
self.__write("| %s%s" % (value, blank), n=False)
2010-09-24 17:34:46 +04:00
if not conf.replicate:
if not conf.multipleTargets and field == fields:
dataToDumpFile(dumpFP, "\"%s\"" % value)
elif not conf.multipleTargets:
dataToDumpFile(dumpFP, "\"%s\"," % value)
2008-10-15 19:38:22 +04:00
field += 1
2010-09-24 17:19:35 +04:00
if conf.replicate:
rtable.insert(values)
2008-10-15 19:38:22 +04:00
self.__write("|")
2010-09-24 17:34:46 +04:00
if not conf.multipleTargets and not conf.replicate:
2008-10-15 19:38:22 +04:00
dataToDumpFile(dumpFP, "\n")
self.__write("%s\n" % separator)
2010-09-24 17:34:46 +04:00
if conf.replicate:
logger.info("Table '%s.%s' dumped to sqlite3 file '%s'" % (db, table, replication.dbpath))
elif not conf.multipleTargets:
2008-10-15 19:38:22 +04:00
dataToDumpFile(dumpFP, "\n")
dumpFP.close()
logger.info("Table '%s.%s' dumped to CSV file '%s'" % (db, table, dumpFileName))
def dbColumns(self, dbColumns, colConsider, dbs):
for column in dbColumns.keys():
if colConsider == "1":
colConsiderStr = "s like '" + column + "' were"
else:
colConsiderStr = " '%s' was" % column
msg = "Column%s found in the " % colConsiderStr
msg += "following databases:"
self.__write(msg)
printDbs = {}
for db, tblData in dbs.items():
for tbl, colData in tblData.items():
for col, dataType in colData.items():
if column.lower() in col.lower():
if db in printDbs:
if tbl in printDbs[db]:
printDbs[db][tbl][col] = dataType
else:
printDbs[db][tbl] = { col: dataType }
else:
printDbs[db] = {}
printDbs[db][tbl] = { col: dataType }
continue
self.dbTableColumns(printDbs)
def query(self, query, queryRes):
self.string(query, queryRes)
def rFile(self,filePath,fileData):
self.string("%s file saved to" % filePath,fileData,sort=False)
def registerValue(self,registerData):
self.string("Registry key value data", registerData,sort=False)
2008-10-15 19:38:22 +04:00
# object to manage how to print the retrieved queries output to
# standard output and sessions file
dumper = Dump()