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
|
|
|
|
2010-10-14 18:41:14 +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 logging
|
2010-05-21 16:09:31 +04:00
|
|
|
import os
|
2009-06-11 19:01:48 +04:00
|
|
|
import subprocess
|
2008-10-15 19:38:22 +04:00
|
|
|
import sys
|
|
|
|
|
2011-01-15 18:14:22 +03:00
|
|
|
from lib.core.enums import DBMS
|
2011-01-27 19:55:58 +03:00
|
|
|
from lib.core.enums import PLACE
|
2010-10-19 12:55:14 +04:00
|
|
|
from lib.core.revision import getRevisionNumber
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
# sqlmap version and site
|
2010-03-15 14:04:57 +03:00
|
|
|
VERSION = "0.9-dev"
|
2010-10-19 12:55:14 +04:00
|
|
|
REVISION = getRevisionNumber()
|
2010-03-03 19:19:17 +03:00
|
|
|
VERSION_STRING = "sqlmap/%s" % VERSION
|
|
|
|
DESCRIPTION = "automatic SQL injection and database takeover tool"
|
2008-10-15 19:38:22 +04:00
|
|
|
SITE = "http://sqlmap.sourceforge.net"
|
|
|
|
|
2010-12-18 12:51:34 +03:00
|
|
|
# minimum distance of ratio from kb.matchRatio to result in True
|
2010-11-10 01:49:31 +03:00
|
|
|
DIFF_TOLERANCE = 0.05
|
2010-11-10 02:35:37 +03:00
|
|
|
CONSTANT_RATIO = 0.9
|
2010-11-10 01:32:05 +03:00
|
|
|
|
2011-01-03 11:32:06 +03:00
|
|
|
# lower and upper values for match ratio in case of stable page
|
|
|
|
LOWER_RATIO_BOUND = 0.02
|
|
|
|
UPPER_RATIO_BOUND = 0.98
|
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
# sqlmap logger
|
2010-11-08 01:34:29 +03:00
|
|
|
logging.addLevelName(9, "PAYLOAD")
|
|
|
|
logging.addLevelName(8, "TRAFFIC OUT")
|
|
|
|
logging.addLevelName(7, "TRAFFIC IN")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
LOGGER = logging.getLogger("sqlmapLog")
|
|
|
|
LOGGER_HANDLER = logging.StreamHandler(sys.stdout)
|
|
|
|
FORMATTER = logging.Formatter("[%(asctime)s] [%(levelname)s] %(message)s", "%H:%M:%S")
|
|
|
|
|
|
|
|
LOGGER_HANDLER.setFormatter(FORMATTER)
|
|
|
|
LOGGER.addHandler(LOGGER_HANDLER)
|
|
|
|
LOGGER.setLevel(logging.WARN)
|
|
|
|
|
2010-10-21 13:51:07 +04:00
|
|
|
# dump markers
|
|
|
|
DUMP_NEWLINE_MARKER = "__NEWLINE__"
|
2010-11-16 18:11:03 +03:00
|
|
|
DUMP_CR_MARKER = "__CARRIAGE_RETURN__"
|
2010-10-21 13:51:07 +04:00
|
|
|
DUMP_DEL_MARKER = "__DEL__"
|
|
|
|
DUMP_TAB_MARKER = "__TAB__"
|
|
|
|
DUMP_START_MARKER = "__START__"
|
|
|
|
DUMP_STOP_MARKER = "__STOP__"
|
|
|
|
|
2011-02-04 15:43:18 +03:00
|
|
|
URI_QUESTION_MARKER = "__QUESTION_MARK__"
|
|
|
|
|
2010-12-10 14:32:46 +03:00
|
|
|
PAYLOAD_DELIMITER = "\x00"
|
|
|
|
CHAR_INFERENCE_MARK = "%c"
|
2011-01-05 13:25:07 +03:00
|
|
|
NON_CONTROL_CHAR_REGEX = r'[^\x00-\x1f]'
|
2010-12-11 13:52:04 +03:00
|
|
|
|
2011-02-15 03:28:27 +03:00
|
|
|
# dumping characters used in GROUP_CONCAT MySQL technique
|
|
|
|
CONCAT_ROW_DELIMITER = ','
|
|
|
|
CONCAT_VALUE_DELIMITER = '|'
|
|
|
|
|
2010-12-21 18:13:13 +03:00
|
|
|
# coefficient used for a time-based query delay checking (must be >= 7)
|
2011-01-16 20:52:42 +03:00
|
|
|
TIME_STDEV_COEFF = 10
|
|
|
|
|
2011-02-02 14:22:35 +03:00
|
|
|
# coefficient used for a union-based number of columns checking (must be >= 7)
|
|
|
|
UNION_STDEV_COEFF = 7
|
|
|
|
|
2011-01-16 20:52:42 +03:00
|
|
|
# length of queue for candidates for time delay adjustment
|
|
|
|
TIME_DELAY_CANDIDATES = 3
|
|
|
|
|
|
|
|
# default time delay in seconds
|
|
|
|
TIME_DEFAULT_DELAY = 5
|
2010-12-18 00:29:09 +03:00
|
|
|
|
2011-02-22 16:00:58 +03:00
|
|
|
# HTTP timeout in silent mode
|
|
|
|
HTTP_SILENT_TIMEOUT = 3
|
|
|
|
|
2010-12-21 18:26:23 +03:00
|
|
|
# maximum number of techniques used in inject.py/getValue() per one value
|
|
|
|
MAX_TECHNIQUES_PER_VALUE = 2
|
2010-12-21 18:24:14 +03:00
|
|
|
|
2010-12-12 01:13:19 +03:00
|
|
|
# suffix used for naming meta databases in DBMS(es) without explicit database name
|
2011-01-16 20:52:42 +03:00
|
|
|
METADB_SUFFIX = "_masterdb"
|
2010-12-12 01:13:19 +03:00
|
|
|
|
2010-12-11 13:52:04 +03:00
|
|
|
# minimum time response set needed for time-comparison based on standard deviation
|
2011-02-07 15:34:23 +03:00
|
|
|
MIN_TIME_RESPONSES = 10
|
2010-12-08 15:49:26 +03:00
|
|
|
|
2011-02-02 16:03:24 +03:00
|
|
|
# minimum comparison ratio set needed for searching valid union column number based on standard deviation
|
|
|
|
MIN_UNION_RESPONSES = 5
|
|
|
|
|
2010-12-11 13:52:04 +03:00
|
|
|
# after these number of blanks at the end inference should stop (just in case)
|
2011-01-20 02:16:22 +03:00
|
|
|
INFERENCE_BLANK_BREAK = 15
|
2010-12-11 13:52:04 +03:00
|
|
|
|
2011-01-17 13:15:19 +03:00
|
|
|
# use this replacement character for cases when inference is not able to retrieve the proper character value
|
|
|
|
INFERENCE_UNKNOWN_CHAR = '?'
|
|
|
|
|
2011-01-31 18:00:41 +03:00
|
|
|
# character used for operation "greater" in inference
|
|
|
|
INFERENCE_GREATER_CHAR = ">"
|
|
|
|
|
|
|
|
# character used for operation "equals" in inference
|
|
|
|
INFERENCE_EQUALS_CHAR = "="
|
|
|
|
|
2011-01-31 19:07:23 +03:00
|
|
|
# character used for operation "not-equals" in inference
|
|
|
|
INFERENCE_NOT_EQUALS_CHAR = "!="
|
|
|
|
|
2010-12-21 18:13:13 +03:00
|
|
|
# string used for representation of unknown dbms version
|
|
|
|
UNKNOWN_DBMS_VERSION = "Unknown"
|
|
|
|
|
2010-12-24 14:06:57 +03:00
|
|
|
# dynamicity mark length used in dynamicity removal engine
|
|
|
|
DYNAMICITY_MARK_LENGTH = 32
|
|
|
|
|
2010-12-27 13:56:28 +03:00
|
|
|
# dummy user prefix used in dictionary attack
|
2011-01-17 13:23:37 +03:00
|
|
|
DUMMY_USER_PREFIX = "__dummy__"
|
|
|
|
|
|
|
|
# Reference: http://en.wikipedia.org/wiki/ISO/IEC_8859-1
|
|
|
|
DEFAULT_PAGE_ENCODING = "iso-8859-1"
|
2010-12-27 13:56:28 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
# System variables
|
2009-06-11 19:01:48 +04:00
|
|
|
IS_WIN = subprocess.mswindows
|
2010-05-21 16:09:31 +04:00
|
|
|
# The name of the operating system dependent module imported. The following
|
|
|
|
# names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce',
|
|
|
|
# 'java', 'riscos'
|
|
|
|
PLATFORM = os.name
|
2009-04-22 15:48:07 +04:00
|
|
|
PYVERSION = sys.version.split()[0]
|
|
|
|
|
2010-03-18 20:20:54 +03:00
|
|
|
# Database management system specific variables
|
2010-08-30 17:29:19 +04:00
|
|
|
MSSQL_SYSTEM_DBS = ( "Northwind", "model", "msdb", "pubs", "tempdb" )
|
|
|
|
MYSQL_SYSTEM_DBS = ( "information_schema", "mysql" ) # Before MySQL 5.0 only "mysql"
|
|
|
|
PGSQL_SYSTEM_DBS = ( "information_schema", "pg_catalog", "pg_toast" )
|
|
|
|
ORACLE_SYSTEM_DBS = ( "SYSTEM", "SYSAUX" ) # These are TABLESPACE_NAME
|
2010-03-18 20:20:54 +03:00
|
|
|
SQLITE_SYSTEM_DBS = ( "sqlite_master", "sqlite_temp_master" )
|
|
|
|
ACCESS_SYSTEM_DBS = ( "MSysAccessObjects", "MSysACEs", "MSysObjects", "MSysQueries", "MSysRelationships", "MSysAccessStorage",\
|
|
|
|
"MSysAccessXML", "MSysModules", "MSysModules2" )
|
|
|
|
FIREBIRD_SYSTEM_DBS = ( "RDB$BACKUP_HISTORY", "RDB$CHARACTER_SETS", "RDB$CHECK_CONSTRAINTS", "RDB$COLLATIONS", "RDB$DATABASE",\
|
|
|
|
"RDB$DEPENDENCIES", "RDB$EXCEPTIONS", "RDB$FIELDS", "RDB$FIELD_DIMENSIONS", " RDB$FILES", "RDB$FILTERS",\
|
|
|
|
"RDB$FORMATS", "RDB$FUNCTIONS", "RDB$FUNCTION_ARGUMENTS", "RDB$GENERATORS", "RDB$INDEX_SEGMENTS", "RDB$INDICES",\
|
|
|
|
"RDB$LOG_FILES", "RDB$PAGES", "RDB$PROCEDURES", "RDB$PROCEDURE_PARAMETERS", "RDB$REF_CONSTRAINTS", "RDB$RELATIONS",\
|
|
|
|
"RDB$RELATION_CONSTRAINTS", "RDB$RELATION_FIELDS", "RDB$ROLES", "RDB$SECURITY_CLASSES", "RDB$TRANSACTIONS", "RDB$TRIGGERS",\
|
|
|
|
"RDB$TRIGGER_MESSAGES", "RDB$TYPES", "RDB$USER_PRIVILEGES", "RDB$VIEW_RELATIONS" )
|
2010-08-30 17:29:19 +04:00
|
|
|
MAXDB_SYSTEM_DBS = ( "SYSINFO", "DOMAIN" )
|
2010-10-12 23:05:12 +04:00
|
|
|
SYBASE_SYSTEM_DBS = ( "master", "model", "sybsystemdb", "sybsystemprocs" )
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-08-30 17:29:19 +04:00
|
|
|
MSSQL_ALIASES = [ "microsoft sql server", "mssqlserver", "mssql", "ms" ]
|
|
|
|
MYSQL_ALIASES = [ "mysql", "my" ]
|
|
|
|
PGSQL_ALIASES = [ "postgresql", "postgres", "pgsql", "psql", "pg" ]
|
|
|
|
ORACLE_ALIASES = [ "oracle", "orcl", "ora", "or" ]
|
|
|
|
SQLITE_ALIASES = [ "sqlite", "sqlite3" ]
|
|
|
|
ACCESS_ALIASES = [ "access", "jet", "microsoft access", "msaccess" ]
|
|
|
|
FIREBIRD_ALIASES = [ "firebird", "mozilla firebird", "interbase", "ibase", "fb" ]
|
|
|
|
MAXDB_ALIASES = [ "maxdb", "sap maxdb", "sap db" ]
|
2010-10-12 23:05:12 +04:00
|
|
|
SYBASE_ALIASES = [ "sybase", "sybase sql server" ]
|
2008-11-12 03:36:50 +03:00
|
|
|
|
2010-10-12 23:05:12 +04:00
|
|
|
SUPPORTED_DBMS = MSSQL_ALIASES + MYSQL_ALIASES + PGSQL_ALIASES + ORACLE_ALIASES + SQLITE_ALIASES + ACCESS_ALIASES + FIREBIRD_ALIASES + MAXDB_ALIASES + SYBASE_ALIASES
|
2010-08-30 17:29:19 +04:00
|
|
|
SUPPORTED_OS = ( "linux", "windows" )
|
2009-02-09 13:28:03 +03:00
|
|
|
|
2011-02-14 00:58:48 +03:00
|
|
|
REFERER_ALIASES = ( "ref", "referer", "referrer" )
|
|
|
|
USER_AGENT_ALIASES = ( "ua", "useragent", "user-agent" )
|
|
|
|
|
2011-01-19 02:02:11 +03:00
|
|
|
FROM_TABLE = {
|
|
|
|
DBMS.ORACLE: " FROM DUAL",
|
|
|
|
DBMS.ACCESS: " FROM MSysObjects",
|
|
|
|
DBMS.FIREBIRD: " FROM RDB$DATABASE",
|
|
|
|
DBMS.MAXDB: " FROM VERSIONS"
|
|
|
|
}
|
2011-01-15 18:14:22 +03:00
|
|
|
|
2010-08-30 17:29:19 +04:00
|
|
|
SQL_STATEMENTS = {
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL SELECT statement": (
|
2008-12-20 16:21:47 +03:00
|
|
|
"select ",
|
2009-02-09 13:28:03 +03:00
|
|
|
"show ",
|
2009-02-03 02:44:19 +03:00
|
|
|
" top ",
|
2010-01-05 19:15:31 +03:00
|
|
|
" distinct ",
|
2008-12-20 16:21:47 +03:00
|
|
|
" from ",
|
2008-12-23 01:48:44 +03:00
|
|
|
" from dual",
|
2008-12-20 16:21:47 +03:00
|
|
|
" where ",
|
|
|
|
" group by ",
|
|
|
|
" order by ",
|
|
|
|
" having ",
|
|
|
|
" limit ",
|
|
|
|
" offset ",
|
|
|
|
" union all ",
|
2009-01-20 00:27:51 +03:00
|
|
|
" rownum as ",
|
|
|
|
"(case ", ),
|
2008-12-19 23:09:46 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL data definition": (
|
2008-12-20 16:21:47 +03:00
|
|
|
"create ",
|
2009-04-22 15:48:07 +04:00
|
|
|
"declare ",
|
2008-12-20 16:21:47 +03:00
|
|
|
"drop ",
|
|
|
|
"truncate ",
|
|
|
|
"alter ", ),
|
2008-12-19 23:09:46 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL data manipulation": (
|
2008-12-20 16:21:47 +03:00
|
|
|
"insert ",
|
|
|
|
"update ",
|
|
|
|
"delete ",
|
|
|
|
"merge ", ),
|
2008-12-19 23:09:46 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL data control": (
|
2008-12-20 16:21:47 +03:00
|
|
|
"grant ", ),
|
2008-12-19 23:09:46 +03:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL data execution": (
|
2010-04-23 20:34:20 +04:00
|
|
|
" exec ",
|
2009-01-10 17:39:27 +03:00
|
|
|
"execute ", ),
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
"SQL transaction": (
|
2008-12-20 16:21:47 +03:00
|
|
|
"start transaction ",
|
|
|
|
"begin work ",
|
|
|
|
"begin transaction ",
|
|
|
|
"commit ",
|
|
|
|
"rollback ", ),
|
2009-04-22 15:48:07 +04:00
|
|
|
}
|
2010-12-25 13:16:20 +03:00
|
|
|
|
|
|
|
ERROR_PARSING_REGEXES = (
|
2011-01-07 20:10:58 +03:00
|
|
|
r"<b>[^<]*(fatal|error|warning|exception)[^<]*</b>:?\s*(?P<result>.+?)<br\s*/?\s*>",
|
|
|
|
r"<li>Error Type:<br>(?P<result>.+?)</li>",
|
|
|
|
r"error '[0-9a-f]{8}'((<[^>]+>)|\s)+(?P<result>[^<>]+)"
|
2010-12-25 13:16:20 +03:00
|
|
|
)
|
2011-01-04 18:49:20 +03:00
|
|
|
|
2011-01-16 22:00:19 +03:00
|
|
|
META_CHARSET_REGEX = r'<meta http-equiv="?content-type"?[^>]+charset=(?P<result>[^">]+)'
|
2011-01-15 18:56:11 +03:00
|
|
|
|
2011-01-17 12:28:25 +03:00
|
|
|
# Reference: http://www.cs.ru.nl/bachelorscripties/2010/Martin_Devillers___0437999___Analyzing_password_strength.pdf
|
|
|
|
COMMON_PASSWORD_SUFFIXES = ["1", "123", "2", "12", "3", "13", "7", "11", "5", "22", "23", "01", "4", "07", "21", "14", "10", "06", "08", "8", "15", "69", "16", "6", "18"]
|
|
|
|
|
2011-01-15 18:56:11 +03:00
|
|
|
# Reference: http://www.the-interweb.com/serendipity/index.php?/archives/94-A-brief-analysis-of-40,000-leaked-MySpace-passwords.html
|
2011-01-17 12:28:25 +03:00
|
|
|
COMMON_PASSWORD_SUFFIXES += ["!", ".", "*", "!!", "?", ";", "..", "!!!", ",", "@"]
|
2011-01-20 19:07:08 +03:00
|
|
|
|
|
|
|
# Splitter used between requests in WebScarab log files
|
|
|
|
WEBSCARAB_SPLITTER = "### Conversation"
|
|
|
|
|
|
|
|
# Splitter used between requests in BURP log files
|
|
|
|
BURP_SPLITTER = "======================================================"
|
2011-01-27 19:55:58 +03:00
|
|
|
|
2011-01-30 14:36:03 +03:00
|
|
|
# Encoding used for Unicode data
|
|
|
|
UNICODE_ENCODING = "utf8"
|
2011-01-31 15:41:39 +03:00
|
|
|
|
|
|
|
# Reference: http://www.w3.org/Protocols/HTTP/Object_Headers.html#uri
|
|
|
|
URI_HTTP_HEADER = "URI"
|
2011-01-31 23:36:01 +03:00
|
|
|
|
2011-02-10 22:51:34 +03:00
|
|
|
# Default Tor proxy settings
|
|
|
|
DEFAULT_TOR_PROXY = "http://127.0.0.1:8118"
|
|
|
|
|
2011-01-31 23:36:01 +03:00
|
|
|
# Uri format which could be injectable (e.g. www.site.com/id82)
|
|
|
|
URI_INJECTABLE_REGEX = r".*/([^\.*?]+)\Z"
|
2011-02-02 13:10:28 +03:00
|
|
|
|
2011-02-02 17:25:16 +03:00
|
|
|
# Regex used for masking sensitive data
|
|
|
|
SENSITIVE_DATA_REGEX = "\s(?P<result>[^\s]*%s[^\s]*)\s"
|
|
|
|
|
2011-02-02 13:10:28 +03:00
|
|
|
# Maximum number of threads (avoiding connection issues and/or DoS)
|
|
|
|
MAX_NUMBER_OF_THREADS = 10
|
2011-02-03 19:59:49 +03:00
|
|
|
|
|
|
|
# Minimum range between minimum and maximum of statistical set
|
|
|
|
MIN_STATISTICAL_RANGE = 0.01
|
2011-02-04 02:25:56 +03:00
|
|
|
|
|
|
|
# Minimum value for comparison ratio
|
|
|
|
MIN_RATIO = 0.0
|
|
|
|
|
|
|
|
# Maximum value for comparison ratio
|
|
|
|
MAX_RATIO = 1.0
|
2011-02-04 15:25:14 +03:00
|
|
|
|
|
|
|
# Character used for marking injectable position inside URI
|
|
|
|
URI_INJECTION_MARK_CHAR = '*'
|
2011-02-04 20:40:55 +03:00
|
|
|
|
|
|
|
# Maximum length used for retrieving data over MySQL error based payload due to "known" problems with longer result strings
|
2011-02-08 19:23:33 +03:00
|
|
|
MYSQL_ERROR_CHUNK_LENGTH = 50
|
2011-02-07 01:32:44 +03:00
|
|
|
|
2011-02-07 03:33:54 +03:00
|
|
|
# Do not unescape the injected statement if it contains any of the following SQL words
|
2011-02-09 14:20:03 +03:00
|
|
|
EXCLUDE_UNESCAPE = ("WAITFOR DELAY ", " INTO DUMPFILE ", " INTO OUTFILE ", "CREATE ", "BULK ", "EXEC ", "RECONFIGURE ", "DECLARE ", CHAR_INFERENCE_MARK)
|
2011-02-24 19:52:46 +03:00
|
|
|
|
|
|
|
# Mark used for replacement of reflected values
|
|
|
|
REFLECTED_VALUE_MARKER = '__REFLECTED_VALUE__'
|
2011-02-27 20:43:41 +03:00
|
|
|
|
|
|
|
# Regular expression used for marking non-alphanum characters
|
2011-02-27 20:58:01 +03:00
|
|
|
REFLECTED_NON_ALPHA_NUM_REGEX = r'[^<>\\r\\n]+?'
|