PageRenderTime 63ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/common.py

https://github.com/filippocld/sqlmap
Python | 3293 lines | 3232 code | 28 blank | 33 comment | 44 complexity | f51f1d2fcb14a527696acb64962097c9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. #!/usr/bin/env python
  2. """
  3. Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
  4. See the file 'doc/COPYING' for copying permission
  5. """
  6. import codecs
  7. import contextlib
  8. import cookielib
  9. import copy
  10. import ctypes
  11. import httplib
  12. import inspect
  13. import logging
  14. import ntpath
  15. import os
  16. import posixpath
  17. import random
  18. import re
  19. import socket
  20. import string
  21. import struct
  22. import sys
  23. import time
  24. import urllib
  25. import urlparse
  26. import unicodedata
  27. from ConfigParser import DEFAULTSECT
  28. from ConfigParser import RawConfigParser
  29. from StringIO import StringIO
  30. from difflib import SequenceMatcher
  31. from math import sqrt
  32. from optparse import OptionValueError
  33. from subprocess import PIPE
  34. from subprocess import Popen as execute
  35. from xml.dom import minidom
  36. from xml.sax import parse
  37. from extra.safe2bin.safe2bin import safecharencode
  38. from lib.core.bigarray import BigArray
  39. from lib.core.data import conf
  40. from lib.core.data import kb
  41. from lib.core.data import logger
  42. from lib.core.data import paths
  43. from lib.core.convert import base64pickle
  44. from lib.core.convert import base64unpickle
  45. from lib.core.convert import hexdecode
  46. from lib.core.convert import htmlunescape
  47. from lib.core.convert import stdoutencode
  48. from lib.core.convert import unicodeencode
  49. from lib.core.convert import utf8encode
  50. from lib.core.decorators import cachedmethod
  51. from lib.core.dicts import DBMS_DICT
  52. from lib.core.dicts import DEPRECATED_HINTS
  53. from lib.core.dicts import SQL_STATEMENTS
  54. from lib.core.enums import ADJUST_TIME_DELAY
  55. from lib.core.enums import CHARSET_TYPE
  56. from lib.core.enums import DBMS
  57. from lib.core.enums import EXPECTED
  58. from lib.core.enums import HEURISTIC_TEST
  59. from lib.core.enums import HTTPHEADER
  60. from lib.core.enums import HTTPMETHOD
  61. from lib.core.enums import OS
  62. from lib.core.enums import PLACE
  63. from lib.core.enums import PAYLOAD
  64. from lib.core.enums import REFLECTIVE_COUNTER
  65. from lib.core.enums import SORT_ORDER
  66. from lib.core.exception import SqlmapDataException
  67. from lib.core.exception import SqlmapFilePathException
  68. from lib.core.exception import SqlmapGenericException
  69. from lib.core.exception import SqlmapNoneDataException
  70. from lib.core.exception import SqlmapMissingDependence
  71. from lib.core.exception import SqlmapSilentQuitException
  72. from lib.core.exception import SqlmapSyntaxException
  73. from lib.core.exception import SqlmapUserQuitException
  74. from lib.core.log import LOGGER_HANDLER
  75. from lib.core.optiondict import optDict
  76. from lib.core.settings import BOLD_PATTERNS
  77. from lib.core.settings import CUSTOM_INJECTION_MARK_CHAR
  78. from lib.core.settings import DBMS_DIRECTORY_DICT
  79. from lib.core.settings import DEFAULT_COOKIE_DELIMITER
  80. from lib.core.settings import DEFAULT_GET_POST_DELIMITER
  81. from lib.core.settings import DEFAULT_MSSQL_SCHEMA
  82. from lib.core.settings import DEPRECATED_OPTIONS
  83. from lib.core.settings import DESCRIPTION
  84. from lib.core.settings import DUMMY_SQL_INJECTION_CHARS
  85. from lib.core.settings import DUMMY_USER_INJECTION
  86. from lib.core.settings import DYNAMICITY_MARK_LENGTH
  87. from lib.core.settings import ERROR_PARSING_REGEXES
  88. from lib.core.settings import FORM_SEARCH_REGEX
  89. from lib.core.settings import GENERIC_DOC_ROOT_DIRECTORY_NAMES
  90. from lib.core.settings import HASHDB_MILESTONE_VALUE
  91. from lib.core.settings import HOST_ALIASES
  92. from lib.core.settings import INFERENCE_UNKNOWN_CHAR
  93. from lib.core.settings import ISSUES_PAGE
  94. from lib.core.settings import IS_WIN
  95. from lib.core.settings import LARGE_OUTPUT_THRESHOLD
  96. from lib.core.settings import MIN_TIME_RESPONSES
  97. from lib.core.settings import ML
  98. from lib.core.settings import NULL
  99. from lib.core.settings import PARAMETER_AMP_MARKER
  100. from lib.core.settings import PARAMETER_SEMICOLON_MARKER
  101. from lib.core.settings import PARTIAL_VALUE_MARKER
  102. from lib.core.settings import PAYLOAD_DELIMITER
  103. from lib.core.settings import PLATFORM
  104. from lib.core.settings import PRINTABLE_CHAR_REGEX
  105. from lib.core.settings import PYVERSION
  106. from lib.core.settings import REFERER_ALIASES
  107. from lib.core.settings import REFLECTED_BORDER_REGEX
  108. from lib.core.settings import REFLECTED_MAX_REGEX_PARTS
  109. from lib.core.settings import REFLECTED_REPLACEMENT_REGEX
  110. from lib.core.settings import REFLECTED_VALUE_MARKER
  111. from lib.core.settings import REFLECTIVE_MISS_THRESHOLD
  112. from lib.core.settings import REVISION
  113. from lib.core.settings import SENSITIVE_DATA_REGEX
  114. from lib.core.settings import SITE
  115. from lib.core.settings import SUPPORTED_DBMS
  116. from lib.core.settings import TEXT_TAG_REGEX
  117. from lib.core.settings import TIME_STDEV_COEFF
  118. from lib.core.settings import UNICODE_ENCODING
  119. from lib.core.settings import UNKNOWN_DBMS_VERSION
  120. from lib.core.settings import URI_QUESTION_MARKER
  121. from lib.core.settings import URLENCODE_CHAR_LIMIT
  122. from lib.core.settings import URLENCODE_FAILSAFE_CHARS
  123. from lib.core.settings import USER_AGENT_ALIASES
  124. from lib.core.settings import VERSION
  125. from lib.core.settings import VERSION_STRING
  126. from lib.core.threads import getCurrentThreadData
  127. from thirdparty.clientform.clientform import ParseResponse
  128. from thirdparty.clientform.clientform import ParseError
  129. from thirdparty.magic import magic
  130. from thirdparty.odict.odict import OrderedDict
  131. from thirdparty.termcolor.termcolor import colored
  132. class UnicodeRawConfigParser(RawConfigParser):
  133. """
  134. RawConfigParser with unicode writing support
  135. """
  136. def write(self, fp):
  137. """
  138. Write an .ini-format representation of the configuration state.
  139. """
  140. if self._defaults:
  141. fp.write("[%s]\n" % DEFAULTSECT)
  142. for (key, value) in self._defaults.items():
  143. fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
  144. fp.write("\n")
  145. for section in self._sections:
  146. fp.write("[%s]\n" % section)
  147. for (key, value) in self._sections[section].items():
  148. if key != "__name__":
  149. if value is None:
  150. fp.write("%s\n" % (key))
  151. else:
  152. fp.write("%s = %s\n" % (key, getUnicode(value, UNICODE_ENCODING).replace('\n', '\n\t')))
  153. fp.write("\n")
  154. class Format(object):
  155. @staticmethod
  156. def humanize(values, chain=" or "):
  157. return chain.join(values)
  158. # Get methods
  159. @staticmethod
  160. def getDbms(versions=None):
  161. """
  162. Format the back-end DBMS fingerprint value and return its
  163. values formatted as a human readable string.
  164. @return: detected back-end DBMS based upon fingerprint techniques.
  165. @rtype: C{str}
  166. """
  167. if versions is None and Backend.getVersionList():
  168. versions = Backend.getVersionList()
  169. return Backend.getDbms() if versions is None else "%s %s" % (Backend.getDbms(), " and ".join(v for v in versions))
  170. @staticmethod
  171. def getErrorParsedDBMSes():
  172. """
  173. Parses the knowledge base htmlFp list and return its values
  174. formatted as a human readable string.
  175. @return: list of possible back-end DBMS based upon error messages
  176. parsing.
  177. @rtype: C{str}
  178. """
  179. htmlParsed = None
  180. if len(kb.htmlFp) == 0 or kb.heuristicTest != HEURISTIC_TEST.POSITIVE:
  181. pass
  182. elif len(kb.htmlFp) == 1:
  183. htmlParsed = kb.htmlFp[0]
  184. elif len(kb.htmlFp) > 1:
  185. htmlParsed = " or ".join(kb.htmlFp)
  186. return htmlParsed
  187. @staticmethod
  188. def getOs(target, info):
  189. """
  190. Formats the back-end operating system fingerprint value
  191. and return its values formatted as a human readable string.
  192. Example of info (kb.headersFp) dictionary:
  193. {
  194. 'distrib': set(['Ubuntu']),
  195. 'type': set(['Linux']),
  196. 'technology': set(['PHP 5.2.6', 'Apache 2.2.9']),
  197. 'release': set(['8.10'])
  198. }
  199. Example of info (kb.bannerFp) dictionary:
  200. {
  201. 'sp': set(['Service Pack 4']),
  202. 'dbmsVersion': '8.00.194',
  203. 'dbmsServicePack': '0',
  204. 'distrib': set(['2000']),
  205. 'dbmsRelease': '2000',
  206. 'type': set(['Windows'])
  207. }
  208. @return: detected back-end operating system based upon fingerprint
  209. techniques.
  210. @rtype: C{str}
  211. """
  212. infoStr = ""
  213. if info and "type" in info:
  214. infoStr += "%s operating system: %s" % (target, Format.humanize(info["type"]))
  215. if "distrib" in info:
  216. infoStr += " %s" % Format.humanize(info["distrib"])
  217. if "release" in info:
  218. infoStr += " %s" % Format.humanize(info["release"])
  219. if "sp" in info:
  220. infoStr += " %s" % Format.humanize(info["sp"])
  221. if "codename" in info:
  222. infoStr += " (%s)" % Format.humanize(info["codename"])
  223. if "technology" in info:
  224. infoStr += "\nweb application technology: %s" % Format.humanize(info["technology"], ", ")
  225. return infoStr.lstrip()
  226. class Backend:
  227. # Set methods
  228. @staticmethod
  229. def setDbms(dbms):
  230. dbms = aliasToDbmsEnum(dbms)
  231. if dbms is None:
  232. return None
  233. # Little precaution, in theory this condition should always be false
  234. elif kb.dbms is not None and kb.dbms != dbms:
  235. msg = "sqlmap previously fingerprinted back-end DBMS "
  236. msg += "%s. However now it has been fingerprinted " % kb.dbms
  237. msg += "to be %s. " % dbms
  238. msg += "Please, specify which DBMS is "
  239. msg += "correct [%s (default)/%s] " % (kb.dbms, dbms)
  240. while True:
  241. _ = readInput(msg, default=kb.dbms)
  242. if aliasToDbmsEnum(_) == kb.dbms:
  243. break
  244. elif aliasToDbmsEnum(_) == dbms:
  245. kb.dbms = aliasToDbmsEnum(_)
  246. break
  247. else:
  248. warnMsg = "invalid value"
  249. logger.warn(warnMsg)
  250. elif kb.dbms is None:
  251. kb.dbms = aliasToDbmsEnum(dbms)
  252. return kb.dbms
  253. @staticmethod
  254. def setVersion(version):
  255. if isinstance(version, basestring):
  256. kb.dbmsVersion = [version]
  257. return kb.dbmsVersion
  258. @staticmethod
  259. def setVersionList(versionsList):
  260. if isinstance(versionsList, list):
  261. kb.dbmsVersion = versionsList
  262. elif isinstance(versionsList, basestring):
  263. Backend.setVersion(versionsList)
  264. else:
  265. logger.error("invalid format of versionsList")
  266. @staticmethod
  267. def forceDbms(dbms, sticky=False):
  268. if not kb.stickyDBMS:
  269. kb.forcedDbms = aliasToDbmsEnum(dbms)
  270. kb.stickyDBMS = sticky
  271. @staticmethod
  272. def flushForcedDbms(force=False):
  273. if not kb.stickyDBMS or force:
  274. kb.forcedDbms = None
  275. kb.stickyDBMS = False
  276. @staticmethod
  277. def setOs(os):
  278. if os is None:
  279. return None
  280. # Little precaution, in theory this condition should always be false
  281. elif kb.os is not None and isinstance(os, basestring) and kb.os.lower() != os.lower():
  282. msg = "sqlmap previously fingerprinted back-end DBMS "
  283. msg += "operating system %s. However now it has " % kb.os
  284. msg += "been fingerprinted to be %s. " % os
  285. msg += "Please, specify which OS is "
  286. msg += "correct [%s (default)/%s] " % (kb.os, os)
  287. while True:
  288. _ = readInput(msg, default=kb.os)
  289. if _ == kb.os:
  290. break
  291. elif _ == os:
  292. kb.os = _.capitalize()
  293. break
  294. else:
  295. warnMsg = "invalid value"
  296. logger.warn(warnMsg)
  297. elif kb.os is None and isinstance(os, basestring):
  298. kb.os = os.capitalize()
  299. return kb.os
  300. @staticmethod
  301. def setOsVersion(version):
  302. if version is None:
  303. return None
  304. elif kb.osVersion is None and isinstance(version, basestring):
  305. kb.osVersion = version
  306. @staticmethod
  307. def setOsServicePack(sp):
  308. if sp is None:
  309. return None
  310. elif kb.osSP is None and isinstance(sp, int):
  311. kb.osSP = sp
  312. @staticmethod
  313. def setArch():
  314. msg = "what is the back-end database management system architecture?"
  315. msg += "\n[1] 32-bit (default)"
  316. msg += "\n[2] 64-bit"
  317. while True:
  318. _ = readInput(msg, default='1')
  319. if isinstance(_, basestring) and _.isdigit() and int(_) in (1, 2):
  320. kb.arch = 32 if int(_) == 1 else 64
  321. break
  322. else:
  323. warnMsg = "invalid value. Valid values are 1 and 2"
  324. logger.warn(warnMsg)
  325. return kb.arch
  326. # Get methods
  327. @staticmethod
  328. def getForcedDbms():
  329. return aliasToDbmsEnum(kb.get("forcedDbms"))
  330. @staticmethod
  331. def getDbms():
  332. return aliasToDbmsEnum(kb.get("dbms"))
  333. @staticmethod
  334. def getErrorParsedDBMSes():
  335. """
  336. Returns array with parsed DBMS names till now
  337. This functions is called to:
  338. 1. Sort the tests, getSortedInjectionTests() - detection phase.
  339. 2. Ask user whether or not skip specific DBMS tests in detection phase,
  340. lib/controller/checks.py - detection phase.
  341. 3. Sort the fingerprint of the DBMS, lib/controller/handler.py -
  342. fingerprint phase.
  343. """
  344. return kb.htmlFp if kb.get("heuristicTest") == HEURISTIC_TEST.POSITIVE else []
  345. @staticmethod
  346. def getIdentifiedDbms():
  347. dbms = None
  348. if not kb:
  349. pass
  350. elif Backend.getForcedDbms() is not None:
  351. dbms = Backend.getForcedDbms()
  352. elif Backend.getDbms() is not None:
  353. dbms = kb.dbms
  354. elif conf.get("dbms"):
  355. dbms = conf.dbms
  356. elif Backend.getErrorParsedDBMSes():
  357. dbms = unArrayizeValue(Backend.getErrorParsedDBMSes())
  358. elif kb.get("injection") and kb.injection.dbms:
  359. dbms = unArrayizeValue(kb.injection.dbms)
  360. return aliasToDbmsEnum(dbms)
  361. @staticmethod
  362. def getVersion():
  363. if len(kb.dbmsVersion) > 0:
  364. return kb.dbmsVersion[0]
  365. else:
  366. return None
  367. @staticmethod
  368. def getVersionList():
  369. if len(kb.dbmsVersion) > 0:
  370. return kb.dbmsVersion
  371. else:
  372. return None
  373. @staticmethod
  374. def getOs():
  375. return kb.os
  376. @staticmethod
  377. def getOsVersion():
  378. return kb.osVersion
  379. @staticmethod
  380. def getOsServicePack():
  381. return kb.osSP
  382. @staticmethod
  383. def getArch():
  384. if kb.arch is None:
  385. Backend.setArch()
  386. return kb.arch
  387. # Comparison methods
  388. @staticmethod
  389. def isDbms(dbms):
  390. if Backend.getDbms() is not None:
  391. return Backend.getDbms() == aliasToDbmsEnum(dbms)
  392. else:
  393. return Backend.getIdentifiedDbms() == aliasToDbmsEnum(dbms)
  394. @staticmethod
  395. def isDbmsWithin(aliases):
  396. return Backend.getDbms() is not None and Backend.getDbms().lower() in aliases
  397. @staticmethod
  398. def isVersion(version):
  399. return Backend.getVersion() is not None and Backend.getVersion() == version
  400. @staticmethod
  401. def isVersionWithin(versionList):
  402. if Backend.getVersionList() is None:
  403. return False
  404. for _ in Backend.getVersionList():
  405. if _ != UNKNOWN_DBMS_VERSION and _ in versionList:
  406. return True
  407. return False
  408. @staticmethod
  409. def isVersionGreaterOrEqualThan(version):
  410. return Backend.getVersion() is not None and str(Backend.getVersion()) >= str(version)
  411. @staticmethod
  412. def isOs(os):
  413. return Backend.getOs() is not None and Backend.getOs().lower() == os.lower()
  414. def paramToDict(place, parameters=None):
  415. """
  416. Split the parameters into names and values, check if these parameters
  417. are within the testable parameters and return in a dictionary.
  418. """
  419. testableParameters = OrderedDict()
  420. if place in conf.parameters and not parameters:
  421. parameters = conf.parameters[place]
  422. parameters = parameters.replace(", ", ",")
  423. parameters = re.sub(r"&(\w{1,4});", r"%s\g<1>%s" % (PARAMETER_AMP_MARKER, PARAMETER_SEMICOLON_MARKER), parameters)
  424. splitParams = parameters.split(conf.pDel or (DEFAULT_COOKIE_DELIMITER if place == PLACE.COOKIE else DEFAULT_GET_POST_DELIMITER))
  425. for element in splitParams:
  426. element = re.sub(r"%s(.+?)%s" % (PARAMETER_AMP_MARKER, PARAMETER_SEMICOLON_MARKER), r"&\g<1>;", element)
  427. elem = element.split("=")
  428. if len(elem) >= 2:
  429. parameter = elem[0].replace(" ", "")
  430. condition = not conf.testParameter
  431. condition |= parameter in conf.testParameter
  432. if condition:
  433. testableParameters[parameter] = "=".join(elem[1:])
  434. if not conf.multipleTargets:
  435. _ = urldecode(testableParameters[parameter], convall=True)
  436. if _.strip(DUMMY_SQL_INJECTION_CHARS) != _\
  437. or re.search(r'\A9{3,}', _) or re.search(DUMMY_USER_INJECTION, _):
  438. warnMsg = "it appears that you have provided tainted parameter values "
  439. warnMsg += "('%s') with most probably leftover " % element
  440. warnMsg += "chars/statements from manual SQL injection test(s). "
  441. warnMsg += "Please, always use only valid parameter values "
  442. warnMsg += "so sqlmap could be able to properly run "
  443. logger.warn(warnMsg)
  444. message = "Are you sure you want to continue? [y/N] "
  445. test = readInput(message, default="N")
  446. if test[0] not in ("y", "Y"):
  447. raise SqlmapSilentQuitException
  448. if conf.testParameter and not testableParameters:
  449. paramStr = ", ".join(test for test in conf.testParameter)
  450. if len(conf.testParameter) > 1:
  451. warnMsg = "provided parameters '%s' " % paramStr
  452. warnMsg += "are not inside the %s" % place
  453. logger.warn(warnMsg)
  454. else:
  455. parameter = conf.testParameter[0]
  456. if not intersect(USER_AGENT_ALIASES + REFERER_ALIASES + HOST_ALIASES, parameter, True):
  457. warnMsg = "provided parameter '%s' " % paramStr
  458. warnMsg += "is not inside the %s" % place
  459. logger.warn(warnMsg)
  460. elif len(conf.testParameter) != len(testableParameters.keys()):
  461. for parameter in conf.testParameter:
  462. if parameter not in testableParameters:
  463. warnMsg = "provided parameter '%s' " % parameter
  464. warnMsg += "is not inside the %s" % place
  465. logger.warn(warnMsg)
  466. return testableParameters
  467. def getDocRoot():
  468. docRoot = None
  469. pagePath = directoryPath(conf.path)
  470. defaultDocRoot = ("C:/xampp/htdocs/", "C:/Inetpub/wwwroot/") if Backend.isOs(OS.WINDOWS) else ("/var/www/",)
  471. if kb.absFilePaths:
  472. for absFilePath in kb.absFilePaths:
  473. if docRoot:
  474. break
  475. if directoryPath(absFilePath) == '/':
  476. continue
  477. absFilePath = normalizePath(absFilePath)
  478. windowsDriveLetter = None
  479. if isWindowsDriveLetterPath(absFilePath):
  480. windowsDriveLetter, absFilePath = absFilePath[:2], absFilePath[2:]
  481. absFilePath = ntToPosixSlashes(posixToNtSlashes(absFilePath))
  482. if any("/%s/" % _ in absFilePath for _ in GENERIC_DOC_ROOT_DIRECTORY_NAMES):
  483. for _ in GENERIC_DOC_ROOT_DIRECTORY_NAMES:
  484. _ = "/%s/" % _
  485. if _ in absFilePath:
  486. docRoot = "%s%s" % (absFilePath.split(_)[0], _)
  487. break
  488. elif pagePath in absFilePath:
  489. docRoot = absFilePath.split(pagePath)[0]
  490. if windowsDriveLetter:
  491. docRoot = "%s/%s" % (windowsDriveLetter, ntToPosixSlashes(docRoot))
  492. docRoot = normalizePath(docRoot)
  493. if docRoot:
  494. infoMsg = "retrieved the web server document root: '%s'" % docRoot
  495. logger.info(infoMsg)
  496. else:
  497. warnMsg = "unable to retrieve the web server document root"
  498. logger.warn(warnMsg)
  499. message = "please provide the web server document root "
  500. message += "[%s]: " % ",".join(root for root in defaultDocRoot)
  501. inputDocRoot = readInput(message, default=defaultDocRoot)
  502. if inputDocRoot:
  503. if isinstance(inputDocRoot, basestring):
  504. docRoot = inputDocRoot.split(',')
  505. else:
  506. docRoot = inputDocRoot
  507. else:
  508. docRoot = defaultDocRoot
  509. return docRoot
  510. def getDirs():
  511. directories = set("/")
  512. if kb.absFilePaths:
  513. infoMsg = "retrieved web server full paths: "
  514. infoMsg += "'%s'" % ", ".join(ntToPosixSlashes(path) for path in kb.absFilePaths)
  515. logger.info(infoMsg)
  516. for absFilePath in kb.absFilePaths:
  517. if absFilePath:
  518. directory = directoryPath(absFilePath)
  519. directory = ntToPosixSlashes(directory)
  520. directories.add(directory)
  521. else:
  522. warnMsg = "unable to retrieve any web server path"
  523. logger.warn(warnMsg)
  524. webDir = extractRegexResult(r"//[^/]+?/(?P<result>.*)/.", conf.url)
  525. if webDir:
  526. directories.add(webDir)
  527. message = "please provide any additional web server full path to try "
  528. message += "to upload the agent [Enter for None]: "
  529. inputDirs = readInput(message)
  530. if inputDirs:
  531. inputDirs = inputDirs.replace(", ", ",")
  532. inputDirs = inputDirs.split(",")
  533. for inputDir in inputDirs:
  534. if inputDir:
  535. directories.add(inputDir)
  536. return list(directories)
  537. def filePathToString(filePath):
  538. strRepl = filePath.replace("/", "_").replace("\\", "_")
  539. strRepl = strRepl.replace(" ", "_").replace(":", "_")
  540. return strRepl
  541. def singleTimeDebugMessage(message):
  542. singleTimeLogMessage(message, logging.DEBUG)
  543. def singleTimeWarnMessage(message):
  544. singleTimeLogMessage(message, logging.WARN)
  545. def singleTimeLogMessage(message, level=logging.INFO, flag=None):
  546. if flag is None:
  547. flag = hash(message)
  548. if flag not in kb.singleLogFlags:
  549. kb.singleLogFlags.add(flag)
  550. logger.log(level, message)
  551. def boldifyMessage(message):
  552. retVal = message
  553. if any(_ in message for _ in BOLD_PATTERNS):
  554. retVal = setColor(message, True)
  555. return retVal
  556. def setColor(message, bold=False):
  557. retVal = message
  558. level = extractRegexResult(r"\[(?P<result>[A-Z ]+)\]", message) or kb.get("stickyLevel")
  559. if message and getattr(LOGGER_HANDLER, "is_tty", False): # colorizing handler
  560. if bold:
  561. retVal = colored(message, color=None, on_color=None, attrs=("bold",))
  562. elif level:
  563. _ = LOGGER_HANDLER.level_map.get(logging.getLevelName(level))
  564. if _:
  565. background, foreground, bold = _
  566. retVal = colored(message, color=foreground, on_color="on_%s" % background if background else None, attrs=("bold",) if bold else None)
  567. kb.stickyLevel = level if message and message[-1] != "\n" else None
  568. return retVal
  569. def dataToStdout(data, forceOutput=False, bold=False):
  570. """
  571. Writes text to the stdout (console) stream
  572. """
  573. message = ""
  574. if not kb.get("threadException"):
  575. if forceOutput or not getCurrentThreadData().disableStdOut:
  576. if kb.get("multiThreadMode"):
  577. logging._acquireLock()
  578. message = stdoutencode(data)
  579. sys.stdout.write(setColor(message, bold))
  580. try:
  581. sys.stdout.flush()
  582. except IOError:
  583. pass
  584. if kb.get("multiThreadMode"):
  585. logging._releaseLock()
  586. kb.prependFlag = len(data) == 1 and data not in ('\n', '\r') or len(data) > 2 and data[0] == '\r' and data[-1] != '\n'
  587. def dataToTrafficFile(data):
  588. if not conf.trafficFile:
  589. return
  590. conf.trafficFP.write(data)
  591. conf.trafficFP.flush()
  592. def dataToDumpFile(dumpFile, data):
  593. dumpFile.write(data)
  594. dumpFile.flush()
  595. def dataToOutFile(filename, data):
  596. if not data:
  597. return "No data retrieved"
  598. retVal = "%s%s%s" % (conf.filePath, os.sep, filePathToString(filename))
  599. with codecs.open(retVal, "wb") as f:
  600. f.write(data)
  601. return retVal
  602. def strToHex(value):
  603. """
  604. Converts string value to it's hexadecimal representation
  605. """
  606. return (value if not isinstance(value, unicode) else value.encode(UNICODE_ENCODING)).encode("hex").upper()
  607. def readInput(message, default=None, checkBatch=True):
  608. """
  609. Reads input from terminal
  610. """
  611. retVal = None
  612. kb.stickyLevel = None
  613. if "\n" in message:
  614. message += "%s> " % ("\n" if message.count("\n") > 1 else "")
  615. elif message[-1] == ']':
  616. message += " "
  617. if conf.answers:
  618. for item in conf.answers.split(','):
  619. question = item.split('=')[0].strip()
  620. answer = item.split('=')[1] if len(item.split('=')) > 1 else None
  621. if answer and question.lower() in message.lower():
  622. retVal = getUnicode(answer, UNICODE_ENCODING)
  623. infoMsg = "%s%s" % (getUnicode(message), retVal)
  624. logger.info(infoMsg)
  625. debugMsg = "used the given answer"
  626. logger.debug(debugMsg)
  627. break
  628. if retVal is None:
  629. if checkBatch and conf.batch:
  630. if isListLike(default):
  631. options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default)
  632. elif default:
  633. options = getUnicode(default, UNICODE_ENCODING)
  634. else:
  635. options = unicode()
  636. infoMsg = "%s%s" % (getUnicode(message), options)
  637. logger.info(infoMsg)
  638. debugMsg = "used the default behaviour, running in batch mode"
  639. logger.debug(debugMsg)
  640. retVal = default
  641. else:
  642. logging._acquireLock()
  643. dataToStdout("\r%s" % message, forceOutput=True, bold=True)
  644. kb.prependFlag = False
  645. try:
  646. retVal = raw_input() or default
  647. retVal = getUnicode(retVal, system=True) if retVal else retVal
  648. except:
  649. time.sleep(0.05) # Reference: http://www.gossamer-threads.com/lists/python/python/781893
  650. kb.prependFlag = True
  651. raise SqlmapUserQuitException
  652. finally:
  653. logging._releaseLock()
  654. return retVal
  655. def randomRange(start=0, stop=1000):
  656. """
  657. Returns random integer value in given range
  658. """
  659. return int(random.randint(start, stop))
  660. def randomInt(length=4):
  661. """
  662. Returns random integer value with provided number of digits
  663. """
  664. return int("".join(random.choice(string.digits if i!=0 else string.digits.replace('0', '')) for i in xrange(0, length)))
  665. def randomStr(length=4, lowercase=False, alphabet=None):
  666. """
  667. Returns random string value with provided number of characters
  668. """
  669. if alphabet:
  670. retVal = "".join(random.choice(alphabet) for _ in xrange(0, length))
  671. elif lowercase:
  672. retVal = "".join(random.choice(string.lowercase) for _ in xrange(0, length))
  673. else:
  674. retVal = "".join(random.choice(string.letters) for _ in xrange(0, length))
  675. return retVal
  676. def sanitizeStr(value):
  677. """
  678. Sanitizes string value in respect to newline and line-feed characters
  679. """
  680. return getUnicode(value).replace("\n", " ").replace("\r", "")
  681. def checkFile(filename):
  682. """
  683. Checks for file existence
  684. """
  685. if not os.path.isfile(filename):
  686. raise SqlmapFilePathException, "unable to read file '%s'" % filename
  687. def banner():
  688. """
  689. This function prints sqlmap banner with its version
  690. """
  691. _ = """\n %s - %s\n %s\n\n""" % (VERSION_STRING, DESCRIPTION, SITE)
  692. dataToStdout(_, forceOutput=True)
  693. def parsePasswordHash(password):
  694. blank = " " * 8
  695. if not password or password == " ":
  696. password = NULL
  697. if Backend.isDbms(DBMS.MSSQL) and password != NULL and isHexEncodedString(password):
  698. hexPassword = password
  699. password = "%s\n" % hexPassword
  700. password += "%sheader: %s\n" % (blank, hexPassword[:6])
  701. password += "%ssalt: %s\n" % (blank, hexPassword[6:14])
  702. password += "%smixedcase: %s\n" % (blank, hexPassword[14:54])
  703. if not Backend.isVersionWithin(("2005", "2008")):
  704. password += "%suppercase: %s" % (blank, hexPassword[54:])
  705. return password
  706. def cleanQuery(query):
  707. retVal = query
  708. for sqlStatements in SQL_STATEMENTS.values():
  709. for sqlStatement in sqlStatements:
  710. sqlStatementEsc = sqlStatement.replace("(", "\\(")
  711. queryMatch = re.search("(%s)" % sqlStatementEsc, query, re.I)
  712. if queryMatch and "sys_exec" not in query:
  713. retVal = retVal.replace(queryMatch.group(1), sqlStatement.upper())
  714. return retVal
  715. def setPaths():
  716. """
  717. Sets absolute paths for project directories and files
  718. """
  719. # sqlmap paths
  720. paths.SQLMAP_EXTRAS_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "extra")
  721. paths.SQLMAP_PROCS_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "procs")
  722. paths.SQLMAP_SHELL_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "shell")
  723. paths.SQLMAP_TAMPER_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "tamper")
  724. paths.SQLMAP_TXT_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "txt")
  725. paths.SQLMAP_UDF_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "udf")
  726. paths.SQLMAP_XML_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "xml")
  727. paths.SQLMAP_XML_BANNER_PATH = os.path.join(paths.SQLMAP_XML_PATH, "banner")
  728. paths.SQLMAP_OUTPUT_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "output")
  729. paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
  730. paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")
  731. paths.SQLMAP_SEXEC_PATH = os.path.join(paths.SQLMAP_EXTRAS_PATH, "shellcodeexec")
  732. # sqlmap files
  733. paths.SQLMAP_HISTORY = os.path.join(paths.SQLMAP_ROOT_PATH, ".sqlmap_history")
  734. paths.SQLMAP_CONFIG = os.path.join(paths.SQLMAP_ROOT_PATH, "sqlmap-%s.conf" % randomStr())
  735. paths.COMMON_COLUMNS = os.path.join(paths.SQLMAP_TXT_PATH, "common-columns.txt")
  736. paths.COMMON_TABLES = os.path.join(paths.SQLMAP_TXT_PATH, "common-tables.txt")
  737. paths.COMMON_OUTPUTS = os.path.join(paths.SQLMAP_TXT_PATH, 'common-outputs.txt')
  738. paths.SQL_KEYWORDS = os.path.join(paths.SQLMAP_TXT_PATH, "keywords.txt")
  739. paths.SMALL_DICT = os.path.join(paths.SQLMAP_TXT_PATH, "smalldict.txt")
  740. paths.USER_AGENTS = os.path.join(paths.SQLMAP_TXT_PATH, "user-agents.txt")
  741. paths.WORDLIST = os.path.join(paths.SQLMAP_TXT_PATH, "wordlist.zip")
  742. paths.PHPIDS_RULES_XML = os.path.join(paths.SQLMAP_XML_PATH, "phpids_rules.xml")
  743. paths.ERRORS_XML = os.path.join(paths.SQLMAP_XML_PATH, "errors.xml")
  744. paths.PAYLOADS_XML = os.path.join(paths.SQLMAP_XML_PATH, "payloads.xml")
  745. paths.INJECTIONS_XML = os.path.join(paths.SQLMAP_XML_PATH, "injections.xml")
  746. paths.LIVE_TESTS_XML = os.path.join(paths.SQLMAP_XML_PATH, "livetests.xml")
  747. paths.QUERIES_XML = os.path.join(paths.SQLMAP_XML_PATH, "queries.xml")
  748. paths.GENERIC_XML = os.path.join(paths.SQLMAP_XML_BANNER_PATH, "generic.xml")
  749. paths.MSSQL_XML = os.path.join(paths.SQLMAP_XML_BANNER_PATH, "mssql.xml")
  750. paths.MYSQL_XML = os.path.join(paths.SQLMAP_XML_BANNER_PATH, "mysql.xml")
  751. paths.ORACLE_XML = os.path.join(paths.SQLMAP_XML_BANNER_PATH, "oracle.xml")
  752. paths.PGSQL_XML = os.path.join(paths.SQLMAP_XML_BANNER_PATH, "postgresql.xml")
  753. def weAreFrozen():
  754. """
  755. Returns whether we are frozen via py2exe.
  756. This will affect how we find out where we are located.
  757. Reference: http://www.py2exe.org/index.cgi/WhereAmI
  758. """
  759. return hasattr(sys, "frozen")
  760. def parseTargetDirect():
  761. """
  762. Parse target dbms and set some attributes into the configuration singleton.
  763. """
  764. if not conf.direct:
  765. return
  766. details = None
  767. remote = False
  768. for dbms in SUPPORTED_DBMS:
  769. details = re.search("^(?P<dbms>%s)://(?P<credentials>(?P<user>.+?)\:(?P<pass>.*)\@)?(?P<remote>(?P<hostname>.+?)\:(?P<port>[\d]+)\/)?(?P<db>[\w\d\ \:\.\_\-\/\\\\]+?)$" % dbms, conf.direct, re.I)
  770. if details:
  771. conf.dbms = details.group('dbms')
  772. if details.group('credentials'):
  773. conf.dbmsUser = details.group('user')
  774. conf.dbmsPass = details.group('pass')
  775. else:
  776. conf.dbmsUser = unicode()
  777. conf.dbmsPass = unicode()
  778. if not conf.dbmsPass:
  779. conf.dbmsPass = None
  780. if details.group('remote'):
  781. remote = True
  782. conf.hostname = details.group('hostname')
  783. conf.port = int(details.group('port'))
  784. else:
  785. conf.hostname = "localhost"
  786. conf.port = 0
  787. conf.dbmsDb = details.group('db')
  788. conf.parameters[None] = "direct connection"
  789. break
  790. if not details:
  791. errMsg = "invalid target details, valid syntax is for instance "
  792. errMsg += "'mysql://USER:PASSWORD@DBMS_IP:DBMS_PORT/DATABASE_NAME' "
  793. errMsg += "or 'access://DATABASE_FILEPATH'"
  794. raise SqlmapSyntaxException, errMsg
  795. for dbmsName, data in DBMS_DICT.items():
  796. if conf.dbms in data[0]:
  797. try:
  798. if dbmsName in (DBMS.ACCESS, DBMS.SQLITE, DBMS.FIREBIRD):
  799. if remote:
  800. warnMsg = "direct connection over the network for "
  801. warnMsg += "%s DBMS is not supported" % dbmsName
  802. logger.warn(warnMsg)
  803. conf.hostname = "localhost"
  804. conf.port = 0
  805. elif not remote:
  806. errMsg = "missing remote connection details"
  807. raise SqlmapSyntaxException, errMsg
  808. if dbmsName in (DBMS.MSSQL, DBMS.SYBASE):
  809. import _mssql
  810. import pymssql
  811. if not hasattr(pymssql, "__version__") or pymssql.__version__ < "1.0.2":
  812. errMsg = "'%s' third-party library must be " % data[1]
  813. errMsg += "version >= 1.0.2 to work properly. "
  814. errMsg += "Download from '%s'" % data[2]
  815. raise SqlmapMissingDependence, errMsg
  816. elif dbmsName == DBMS.MYSQL:
  817. import pymysql
  818. elif dbmsName == DBMS.PGSQL:
  819. import psycopg2
  820. elif dbmsName == DBMS.ORACLE:
  821. import cx_Oracle
  822. elif dbmsName == DBMS.SQLITE:
  823. import sqlite3
  824. elif dbmsName == DBMS.ACCESS:
  825. import pyodbc
  826. elif dbmsName == DBMS.FIREBIRD:
  827. import kinterbasdb
  828. except ImportError:
  829. errMsg = "sqlmap requires '%s' third-party library " % data[1]
  830. errMsg += "in order to directly connect to the database "
  831. errMsg += "%s. Download from '%s'" % (dbmsName, data[2])
  832. raise SqlmapMissingDependence, errMsg
  833. def parseTargetUrl():
  834. """
  835. Parse target url and set some attributes into the configuration singleton.
  836. """
  837. if not conf.url:
  838. return
  839. originalUrl = conf.url
  840. if re.search("\[.+\]", conf.url) and not socket.has_ipv6:
  841. errMsg = "IPv6 addressing is not supported "
  842. errMsg += "on this platform"
  843. raise SqlmapGenericException, errMsg
  844. if not re.search("^http[s]*://", conf.url, re.I):
  845. if ":443/" in conf.url:
  846. conf.url = "https://" + conf.url
  847. else:
  848. conf.url = "http://" + conf.url
  849. if CUSTOM_INJECTION_MARK_CHAR in conf.url:
  850. conf.url = conf.url.replace('?', URI_QUESTION_MARKER)
  851. urlSplit = urlparse.urlsplit(conf.url)
  852. hostnamePort = urlSplit[1].split(":") if not re.search("\[.+\]", urlSplit[1]) else filter(None, (re.search("\[.+\]", urlSplit[1]).group(0), re.search("\](:(?P<port>\d+))?", urlSplit[1]).group("port")))
  853. conf.scheme = urlSplit[0].strip().lower() if not conf.forceSSL else "https"
  854. conf.path = urlSplit[2].strip()
  855. conf.hostname = hostnamePort[0].strip()
  856. conf.ipv6 = conf.hostname != conf.hostname.strip("[]")
  857. conf.hostname = conf.hostname.strip("[]")
  858. try:
  859. _ = conf.hostname.encode("idna")
  860. except UnicodeError:
  861. _ = None
  862. if any((_ is None, re.search(r'\s', conf.hostname), '..' in conf.hostname, conf.hostname.startswith('.'))):
  863. errMsg = "invalid target url"
  864. raise SqlmapSyntaxException, errMsg
  865. if len(hostnamePort) == 2:
  866. try:
  867. conf.port = int(hostnamePort[1])
  868. except:
  869. errMsg = "invalid target url"
  870. raise SqlmapSyntaxException, errMsg
  871. elif conf.scheme == "https":
  872. conf.port = 443
  873. else:
  874. conf.port = 80
  875. if urlSplit[3]:
  876. conf.parameters[PLACE.GET] = urldecode(urlSplit[3]) if urlSplit[3] and urlencode(DEFAULT_GET_POST_DELIMITER, None) not in urlSplit[3] else urlSplit[3]
  877. conf.url = getUnicode("%s://%s:%d%s" % (conf.scheme, ("[%s]" % conf.hostname) if conf.ipv6 else conf.hostname, conf.port, conf.path))
  878. conf.url = conf.url.replace(URI_QUESTION_MARKER, '?')
  879. if not conf.referer and intersect(REFERER_ALIASES, conf.testParameter, True):
  880. debugMsg = "setting the HTTP Referer header to the target url"
  881. logger.debug(debugMsg)
  882. conf.httpHeaders = filter(lambda (key, value): key != HTTPHEADER.REFERER, conf.httpHeaders)
  883. conf.httpHeaders.append((HTTPHEADER.REFERER, conf.url))
  884. if not conf.host and intersect(HOST_ALIASES, conf.testParameter, True):
  885. debugMsg = "setting the HTTP Host header to the target url"
  886. logger.debug(debugMsg)
  887. conf.httpHeaders = filter(lambda (key, value): key != HTTPHEADER.HOST, conf.httpHeaders)
  888. conf.httpHeaders.append((HTTPHEADER.HOST, getHostHeader(conf.url)))
  889. if originalUrl != conf.url:
  890. kb.originalUrls[conf.url] = originalUrl
  891. def expandAsteriskForColumns(expression):
  892. """
  893. If the user provided an asterisk rather than the column(s)
  894. name, sqlmap will retrieve the columns itself and reprocess
  895. the SQL query string (expression)
  896. """
  897. asterisk = re.search("^SELECT\s+\*\s+FROM\s+([\w\.\_]+)\s*", expression, re.I)
  898. if asterisk:
  899. infoMsg = "you did not provide the fields in your query. "
  900. infoMsg += "sqlmap will retrieve the column names itself"
  901. logger.info(infoMsg)
  902. dbTbl = asterisk.group(1)
  903. if dbTbl and ".." in dbTbl:
  904. dbTbl = dbTbl.replace('..', '.dbo.')
  905. if dbTbl and "." in dbTbl:
  906. conf.db, conf.tbl = dbTbl.split(".", 1)
  907. else:
  908. conf.tbl = dbTbl
  909. columnsDict = conf.dbmsHandler.getColumns(onlyColNames=True)
  910. if columnsDict and conf.db in columnsDict and conf.tbl in columnsDict[conf.db]:
  911. columns = columnsDict[conf.db][conf.tbl].keys()
  912. columns.sort()
  913. columnsStr = ", ".join(column for column in columns)
  914. expression = expression.replace("*", columnsStr, 1)
  915. infoMsg = "the query with column names is: "
  916. infoMsg += "%s" % expression
  917. logger.info(infoMsg)
  918. return expression
  919. def getLimitRange(count, dump=False, plusOne=False):
  920. """
  921. Returns range of values used in limit/offset constructs
  922. """
  923. retVal = None
  924. count = int(count)
  925. limitStart, limitStop = 1, count
  926. if dump:
  927. if isinstance(conf.limitStop, int) and conf.limitStop > 0 and conf.limitStop < limitStop:
  928. limitStop = conf.limitStop
  929. if isinstance(conf.limitStart, int) and conf.limitStart > 0 and conf.limitStart <= limitStop:
  930. limitStart = conf.limitStart
  931. retVal = xrange(limitStart, limitStop + 1) if plusOne else xrange(limitStart - 1, limitStop)
  932. return retVal
  933. def parseUnionPage(page):
  934. """
  935. Returns resulting items from UNION query inside provided page content
  936. """
  937. if page is None:
  938. return None
  939. if page.startswith(kb.chars.start) and page.endswith(kb.chars.stop):
  940. if len(page) > LARGE_OUTPUT_THRESHOLD:
  941. warnMsg = "large output detected. This might take a while"
  942. logger.warn(warnMsg)
  943. data = BigArray()
  944. keys = set()
  945. for match in re.finditer("%s(.*?)%s" % (kb.chars.start, kb.chars.stop), page, re.DOTALL | re.IGNORECASE):
  946. entry = match.group(1)
  947. if kb.chars.start in entry:
  948. entry = entry.split(kb.chars.start)[-1]
  949. if kb.unionDuplicates:
  950. key = entry.lower()
  951. if key not in keys:
  952. keys.add(key)
  953. else:
  954. continue
  955. entry = entry.split(kb.chars.delimiter)
  956. if conf.hexConvert:
  957. entry = applyFunctionRecursively(entry, decodeHexValue)
  958. if kb.safeCharEncode:
  959. entry = applyFunctionRecursively(entry, safecharencode)
  960. data.append(entry[0] if len(entry) == 1 else entry)
  961. else:
  962. data = page
  963. if len(data) == 1 and isinstance(data[0], basestring):
  964. data = data[0]
  965. return data
  966. def parseFilePaths(page):
  967. """
  968. Detects (possible) absolute system paths inside the provided page content
  969. """
  970. if page:
  971. for regex in (r" in <b>(?P<result>.*?)</b> on line", r"(?:>|\s)(?P<result>[A-Za-z]:[\\/][\w.\\/]*)", r"(?:>|\s)(?P<result>/\w[/\w.]+)"):
  972. for match in re.finditer(regex, page):
  973. absFilePath = match.group("result").strip()
  974. page = page.replace(absFilePath, "")
  975. if isWindowsDriveLetterPath(absFilePath):
  976. absFilePath = posixToNtSlashes(absFilePath)
  977. if absFilePath not in kb.absFilePaths:
  978. kb.absFilePaths.add(absFilePath)
  979. def getLocalIP():
  980. retVal = None
  981. try:
  982. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  983. s.connect((conf.hostname, conf.port))
  984. retVal, _ = s.getsockname()
  985. s.close()
  986. except:
  987. debugMsg = "there was an error in opening socket "
  988. debugMsg += "connection toward '%s'" % conf.hostname
  989. logger.debug(debugMsg)
  990. return retVal
  991. def getRemoteIP():
  992. return socket.gethostbyname(conf.hostname)
  993. def getFileType(filePath):
  994. try:
  995. _ = magic.from_file(filePath)
  996. except:
  997. return "unknown"
  998. return "text" if "ASCII" in _ or "text" in _ else "binary"
  999. def getCharset(charsetType=None):
  1000. asciiTbl = []
  1001. if charsetType is None:
  1002. asciiTbl.extend(xrange(0, 128))
  1003. # 0 or 1
  1004. elif charsetType == CHARSET_TYPE.BINARY:
  1005. asciiTbl.extend([0, 1])
  1006. asciiTbl.extend(xrange(47, 50))
  1007. # Digits
  1008. elif charsetType == CHARSET_TYPE.DIGITS:
  1009. asciiTbl.extend([0, 1])
  1010. asciiTbl.extend(xrange(47, 58))
  1011. # Hexadecimal
  1012. elif charsetType == CHARSET_TYPE.HEXADECIMAL:
  1013. asciiTbl.extend([0, 1])
  1014. asciiTbl.extend(xrange(47, 58))
  1015. asciiTbl.extend(xrange(64, 71))
  1016. asciiTbl.extend([87, 88]) # X
  1017. asciiTbl.extend(xrange(96, 103))
  1018. asciiTbl.extend([119, 120]) # x
  1019. # Characters
  1020. elif charsetType == CHARSET_TYPE.ALPHA:
  1021. asciiTbl.extend([0, 1])
  1022. asciiTbl.extend(xrange(64, 91))
  1023. asciiTbl.extend(xrange(96, 123))
  1024. # Characters and digits
  1025. elif charsetType == CHARSET_TYPE.ALPHANUM:
  1026. asciiTbl.extend([0, 1])
  1027. asciiTbl.extend(xrange(47, 58))
  1028. asciiTbl.extend(xrange(64, 91))
  1029. asciiTbl.extend(xrange(96, 123))
  1030. return asciiTbl
  1031. def searchEnvPath(filename):
  1032. retVal = None
  1033. path = os.environ.get("PATH", "")
  1034. paths = path.split(";") if IS_WIN else path.split(":")
  1035. for _ in paths:
  1036. _ = _.replace(";", "")
  1037. retVal = os.path.exists(os.path.normpath(os.path.join(_, filename)))
  1038. if retVal:
  1039. break
  1040. return retVal
  1041. def directoryPath(filepath):
  1042. """
  1043. Returns directory path for a given filepath
  1044. """
  1045. retVal = filepath
  1046. if filepath:
  1047. retVal = ntpath.dirname(filepath) if isWindowsDriveLetterPath(filepath) else posixpath.dirname(filepath)
  1048. return retVal
  1049. def normalizePath(filepath):
  1050. """
  1051. Returns normalized string representation of a given filepath
  1052. """
  1053. retVal = filepath
  1054. if filepath:
  1055. retVal = ntpath.normpath(filepath) if isWindowsDriveLetterPath(filepath) else posixpath.normpath(filepath)
  1056. return retVal
  1057. def safeStringFormat(format_, params):
  1058. """
  1059. Avoids problems with inappropriate string format strings
  1060. """
  1061. retVal = format_.replace("%d", "%s")
  1062. if isinstance(params, basestring):
  1063. retVal = retVal.replace("%s", params)
  1064. else:
  1065. count, index = 0, 0
  1066. while index != -1:
  1067. index = retVal.find("%s")
  1068. if index != -1:
  1069. if count < len(params):
  1070. retVal = retVal[:index] + getUnicode(params[count]) + retVal[index + 2:]
  1071. else:
  1072. raise SqlmapNoneDataException, "wrong number of parameters during string formatting"
  1073. count += 1
  1074. return retVal
  1075. def getFilteredPageContent(page, onlyText=True):
  1076. """
  1077. Returns filtered page content without script, style and/or comments
  1078. or all HTML tags
  1079. """
  1080. retVal = page
  1081. # only if the page's charset has been successfully identified
  1082. if isinstance(page, unicode):
  1083. retVal = re.sub(r"(?si)<script.+?</script>|<!--.+?-->|<style.+?</style>%s" % (r"|<[^>]+>|\t|\n|\r" if onlyText else ""), " ", page)
  1084. while retVal.find(" ") != -1:
  1085. retVal = retVal.replace(" ", " ")
  1086. retVal = htmlunescape(retVal)
  1087. return retVal
  1088. def getPageWordSet(page):
  1089. """
  1090. Returns word set used in page content
  1091. """
  1092. retVal = set()
  1093. # only if the page's charset has been successfully identified
  1094. if isinstance(page, unicode):
  1095. _ = getFilteredPageContent(page)
  1096. retVal = set(re.findall(r"\w+", _))
  1097. return retVal
  1098. def showStaticWords(firstPage, secondPage):
  1099. infoMsg = "finding static words in longest matching part of dynamic page content"
  1100. logger.info(infoMsg)
  1101. firstPage = getFilteredPageContent(firstPage)
  1102. secondPage = getFilteredPageContent(secondPage)
  1103. infoMsg = "static words: "
  1104. if firstPage and secondPage:
  1105. match = SequenceMatcher(None, firstPage, secondPage).find_longest_match(0, len(firstPage), 0, len(secondPage))
  1106. commonText = firstPage[match[0]:match[0] + match[2]]
  1107. commonWords = getPageWordSet(commonText)
  1108. else:
  1109. commonWords = None
  1110. if commonWords:
  1111. commonWords = list(commonWords)
  1112. commonWords.sort(lambda a, b: cmp(a.lower(), b.lower()))
  1113. for word in commonWords:
  1114. if len(word) > 2:
  1115. infoMsg += "'%s', " % word
  1116. infoMsg = infoMsg.rstrip(", ")
  1117. else:
  1118. infoMsg += "None"
  1119. logger.info(infoMsg)
  1120. def isWindowsPath(filepath):
  1121. """
  1122. Returns True if given filepath is in Windows format
  1123. """
  1124. return re.search("\A[\w]\:\\\\", filepath) is not None
  1125. def isWindowsDriveLetterPath(filepath):
  1126. """
  1127. Returns True if given filepath starts with a Windows drive letter
  1128. """
  1129. return re.search("\A[\w]\:", filepath) is not None
  1130. def posixToNtSlashes(filepath):
  1131. """
  1132. Replaces all occurances of Posix slashes (/) in provided
  1133. filepath with NT ones (/)
  1134. >>> posixToNtSlashes('C:/Windows')
  1135. 'C:\\\\Windows'
  1136. """
  1137. return filepath.replace('/', '\\')
  1138. def ntToPosixSlashes(filepath):
  1139. """
  1140. Replaces all occurances of NT slashes (\) in provided
  1141. filepath with Posix ones (/)
  1142. >>> ntToPosixSlashes('C:\\Windows')
  1143. 'C:/Windows'
  1144. """
  1145. return filepath.replace('\\', '/')
  1146. def isBase64EncodedString(subject):
  1147. """
  1148. Checks if the provided string is Base64 encoded
  1149. >>> isBase64EncodedString('dGVzdA==')
  1150. True
  1151. >>> isBase64EncodedString('123456')
  1152. False
  1153. """
  1154. return re.match(r"\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\Z", subject) is not None
  1155. def isHexEncodedString(subject):
  1156. """
  1157. Checks if the provided string is hex encoded
  1158. >>> isHexEncodedString('DEADBEEF')
  1159. True
  1160. >>> isHexEncodedString('test')
  1161. False
  1162. """
  1163. return re.match(r"\A[0-9a-fA-Fx]+\Z", subject) is not None
  1164. def getConsoleWidth(default=80):
  1165. """
  1166. Returns console width
  1167. """
  1168. width = None
  1169. if os.getenv("COLUMNS", "").isdigit():
  1170. width = int(os.getenv("COLUMNS"))
  1171. else:
  1172. output=execute('stty size', shell=True, stdout=PIPE, stderr=PIPE).stdout.read()
  1173. items = output.split()
  1174. if len(items) == 2 and items[1].isdigit():
  1175. width = int(items[1])
  1176. if width is None:
  1177. try:
  1178. import curses
  1179. stdscr = curses.initscr()
  1180. _, width = stdscr.getmaxyx()
  1181. curses.endwin()
  1182. except:
  1183. pass
  1184. return width or default
  1185. def clearConsoleLine(forceOutput=False):
  1186. """
  1187. Clears current console line
  1188. """
  1189. if getattr(LOGGER_HANDLER, "is_tty", False):
  1190. dataToStdout("\r%s\r" % (" " * (getConsoleWidth() - 1)), forceOutput)
  1191. kb.prependFlag = False
  1192. kb.stickyLevel = None
  1193. def parseXmlFile(xmlFile, handler):
  1194. """
  1195. Parses XML file by a given handler
  1196. """
  1197. with contextlib.closing(StringIO(readCachedFileContent(xmlFile))) as stream:
  1198. parse(stream, handler)
  1199. def getSQLSnippet(dbms, sfile, **variables):
  1200. """
  1201. Returns content of SQL snippet located inside 'procs/' directory
  1202. """
  1203. if sfile.endswith('.sql') and os.path.exists(sfile):
  1204. filename = sfile
  1205. elif not sfile.endswith('.sql') and os.path.exists("%s.sql" % sfile):
  1206. filename = "%s.sql" % sfile
  1207. else:
  1208. filename = os.path.join(paths.SQLMAP_PROCS_PATH, DBMS_DIRECTORY_DICT[dbms], sfile if sfile.endswith('.sql') else "%s.sql" % sfile)
  1209. checkFile(filename)
  1210. retVal = readCachedFileContent(filename)
  1211. retVal = re.sub(r"#.+", "", retVal)
  1212. retVal = re.sub(r"(?s);\s+", "; ", retVal).strip("\r\n")
  1213. for _ in variables.keys():
  1214. retVal = re.sub(r"%%%s%%" % _, variables[_], retVal)
  1215. for _ in re.findall(r"%RANDSTR\d+%", retVal, re.I):
  1216. retVal = retVal.replace(_, randomStr())
  1217. for _ in re.findall(r"%RANDINT\d+%", retVal, re.I):
  1218. retVal = retVal.replace(_, randomInt())
  1219. variables = re.findall(r"%(\w+)%", retVal, re.I)
  1220. if variables:
  1221. errMsg = "unresolved variable%s '%s' in SQL file '%s'" % ("s" if len(variables) > 1 else "", ", ".join(variables), sfile)
  1222. logger.error(errMsg)
  1223. msg = "do you want to provide the substitution values? [y/N] "
  1224. choice = readInput(msg, default="N")
  1225. if choice and choice[0].lower() == "y":
  1226. for var in variables:
  1227. msg = "insert value for variable '%s': " % var
  1228. val = readInput(msg)
  1229. retVal = retVal.replace(r"%%%s%%" % var, val)
  1230. return retVal
  1231. def readCachedFileContent(filename, mode='rb'):
  1232. """
  1233. Cached reading of file content (avoiding multiple same file reading)
  1234. """
  1235. if filename not in kb.cache.content:
  1236. with kb.locks.cache:
  1237. if filename not in kb.cache.content:
  1238. checkFile(filename)
  1239. with codecs.open(filename, mode, UNICODE_ENCODING) as f:
  1240. kb.cache.content[filename] = f.read()
  1241. return kb.cache.content[filename]
  1242. def readXmlFile(xmlFile):
  1243. """
  1244. Reads XML file content and returns its DOM representation
  1245. """
  1246. checkFile(xmlFile)
  1247. retVal = minidom.parse(xmlFile).documentElement
  1248. return retVal
  1249. def stdev(values):
  1250. """
  1251. Computes standard deviation of a list of numbers.
  1252. Reference: http://www.goldb.org/corestats.html
  1253. """
  1254. if not values or len(values) < 2:
  1255. return None
  1256. key = (values[0], values[-1], len(values))
  1257. if key in kb.cache.stdev:
  1258. retVal = kb.cache.stdev[key]
  1259. else:
  1260. avg = average(values)
  1261. _ = reduce(lambda x, y: x + pow((y or 0) - avg, 2), values, 0.0)
  1262. retVal = sqrt(_ / (len(values) - 1))
  1263. kb.cache.stdev[key] = retVal
  1264. return retVal
  1265. def average(values):
  1266. """
  1267. Computes the arithmetic mean of a list of numbers.
  1268. """
  1269. return (sum(values) / len(values)) if values else None
  1270. def calculateDeltaSeconds(start):
  1271. """
  1272. Returns elapsed time from start till now
  1273. """
  1274. return time.time() - start
  1275. def initCommonOutputs():
  1276. """
  1277. Initializes dictionary containing common output values used by "good samaritan" feature
  1278. """
  1279. kb.commonOutputs = {}
  1280. key = None
  1281. with codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING) as f:
  1282. for line in f.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used
  1283. if line.find('#') != -1:
  1284. line = line[:line.find('#')]
  1285. line = line.strip()
  1286. if len(line) > 1:
  1287. if line.startswith('[') and line.endswith(']'):
  1288. key = line[1:-1]
  1289. elif key:
  1290. if key not in kb.commonOutputs:
  1291. kb.commonOutputs[key] = set()
  1292. if line not in kb.commonOutputs[key]:
  1293. kb.commonOutputs[key].add(line)
  1294. def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, unique=False):
  1295. """
  1296. Returns newline delimited items contained inside file
  1297. """
  1298. retVal = list() if not unique else OrderedDict()
  1299. checkFile(filename)
  1300. with codecs.open(filename, 'r', UNICODE_ENCODING) if unicode_ else open(filename, 'r') as f:
  1301. for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used
  1302. if commentPrefix:
  1303. if line.find(commentPrefix) != -1:
  1304. line = line[:line.find(commentPrefix)]
  1305. line = line.strip()
  1306. if not unicode_:
  1307. try:
  1308. line = str.encode(line)
  1309. except UnicodeDecodeError:
  1310. continue
  1311. if line:
  1312. if lowercase:
  1313. line = line.lower()
  1314. if unique and line in retVal:
  1315. continue
  1316. if unique:
  1317. retVal[line] = True
  1318. else:
  1319. retVal.append(line)
  1320. return retVal if not unique else retVal.keys()
  1321. def goGoodSamaritan(prevValue, originalCharset):
  1322. """
  1323. Function for retrieving parameters needed for common prediction (good
  1324. samaritan) feature.
  1325. prevValue: retrieved query output so far (e.g. 'i').
  1326. Returns commonValue if there is a complete single match (in kb.partRun
  1327. of txt/common-outputs.txt under kb.partRun) regarding parameter
  1328. prevValue. If there is no single value match, but multiple, commonCharset is
  1329. returned containing more probable characters (retrieved from matched
  1330. values in txt/common-outputs.txt) together with the rest of charset as
  1331. otherCharset.
  1332. """
  1333. if kb.commonOutputs is None:
  1334. initCommonOutputs()
  1335. predictionSet = set()
  1336. commonValue = None
  1337. commonPattern = None
  1338. countCommonValue = 0
  1339. # If the header (e.g. Databases) we are looking for has common
  1340. # outputs defined
  1341. if kb.partRun in kb.commonOutputs:
  1342. commonPartOutputs = kb.commonOutputs[kb.partRun]
  1343. commonPattern = commonFinderOnly(prevValue, commonPartOutputs)
  1344. # If the longest common prefix is the same as previous value then
  1345. # do not consider it
  1346. if commonPattern and commonPattern == prevValue:
  1347. commonPattern = None
  1348. # For each common output
  1349. for item in commonPartOutputs:
  1350. # Check if the common output (item) starts with prevValue
  1351. # where prevValue is the enumerated character(s) so far
  1352. if item.startswith(prevValue):
  1353. commonValue = item
  1354. countCommonValue += 1
  1355. if len(item) > len(prevValue):
  1356. char = item[len(prevValue)]
  1357. predictionSet.add(char)
  1358. # Reset single value if there is more than one possible common
  1359. # output
  1360. if countCommonValue > 1:
  1361. commonValue = None
  1362. commonCharset = []
  1363. otherCharset = []
  1364. # Split the original charset into common chars (commonCharset)
  1365. # and other chars (otherCharset)
  1366. for ordChar in originalCharset:
  1367. if chr(ordChar) not in predictionSet:
  1368. otherCharset.append(ordChar)
  1369. else:
  1370. commonCharset.append(ordChar)
  1371. commonCharset.sort()
  1372. return commonValue, commonPattern, commonCharset, originalCharset
  1373. else:
  1374. return None, None, None, originalCharset
  1375. def getPartRun():
  1376. """
  1377. Goes through call stack and finds constructs matching conf.dbmsHandler.*.
  1378. Returns it or its alias used in txt/common-outputs.txt
  1379. """
  1380. retVal = None
  1381. commonPartsDict = optDict["Enumeration"]
  1382. try:
  1383. stack = [item[4][0] if isinstance(item[4], list) else '' for item in inspect.stack()]
  1384. # Goes backwards through the stack to find the conf.dbmsHandler method
  1385. # calling this function
  1386. for i in xrange(0, len(stack) - 1):
  1387. for regex in (r"self\.(get[^(]+)\(\)", r"conf\.dbmsHandler\.([^(]+)\(\)"):
  1388. match = re.search(regex, stack[i])
  1389. if match:
  1390. # This is the calling conf.dbmsHandler or self method
  1391. # (e.g. 'getDbms')
  1392. retVal = match.groups()[0]
  1393. break
  1394. if retVal is not None:
  1395. break
  1396. # Reference: http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-06/2267.html
  1397. except TypeError:
  1398. pass
  1399. # Return the INI tag to consider for common outputs (e.g. 'Databases')
  1400. return commonPartsDict[retVal][1] if isinstance(commonPartsDict.get(retVal), tuple) else retVal
  1401. def getUnicode(value, encoding=None, system=False, noneToNull=False):
  1402. """
  1403. Return the unicode representation of the supplied value:
  1404. >>> getUnicode(u'test')
  1405. u'test'
  1406. >>> getUnicode('test')
  1407. u'test'
  1408. >>> getUnicode(1)
  1409. u'1'
  1410. """
  1411. if noneToNull and value is None:
  1412. return NULL
  1413. if isListLike(value):
  1414. value = list(getUnicode(_, encoding, system, noneToNull) for _ in value)
  1415. return value
  1416. if not system:
  1417. if isinstance(value, unicode):
  1418. return value
  1419. elif isinstance(value, basestring):
  1420. while True:
  1421. try:
  1422. return unicode(value, encoding or kb.pageEncoding or UNICODE_ENCODING)
  1423. except UnicodeDecodeError, ex:
  1424. value = value[:ex.start] + "".join("\\x%02x" % ord(_) for _ in value[ex.start:ex.end]) + value[ex.end:]
  1425. else:
  1426. return unicode(value) # encoding ignored for non-basestring instances
  1427. else:
  1428. try:
  1429. return getUnicode(value, sys.getfilesystemencoding() or sys.stdin.encoding)
  1430. except:
  1431. return getUnicode(value, UNICODE_ENCODING)
  1432. def longestCommonPrefix(*sequences):
  1433. """
  1434. Returns longest common prefix occuring in given sequences
  1435. Reference: http://boredzo.org/blog/archives/2007-01-06/longest-common-prefix-in-python-2
  1436. """
  1437. if len(sequences) == 1:
  1438. return sequences[0]
  1439. sequences = [pair[1] for pair in sorted((len(fi), fi) for fi in sequences)]
  1440. if not sequences:
  1441. return None
  1442. for i, comparison_ch in enumerate(sequences[0]):
  1443. for fi in sequences[1:]:
  1444. ch = fi[i]
  1445. if ch != comparison_ch:
  1446. return fi[:i]
  1447. return sequences[0]
  1448. def commonFinderOnly(initial, sequence):
  1449. return longestCommonPrefix(*filter(lambda x: x.startswith(initial), sequence))
  1450. def pushValue(value):
  1451. """
  1452. Push value to the stack (thread dependent)
  1453. """
  1454. getCurrentThreadData().valueStack.append(copy.deepcopy(value))
  1455. def popValue():
  1456. """
  1457. Pop value from the stack (thread dependent)
  1458. """
  1459. return getCurrentThreadData().valueStack.pop()
  1460. def wasLastRequestDBMSError():
  1461. """
  1462. Returns True if the last web request resulted in a (recognized) DBMS error page
  1463. """
  1464. threadData = getCurrentThreadData()
  1465. return threadData.lastErrorPage and threadData.lastErrorPage[0] == threadData.lastRequestUID
  1466. def wasLastRequestHTTPError():
  1467. """
  1468. Returns True if the last web request resulted in an errornous HTTP code (like 500)
  1469. """
  1470. threadData = getCurrentThreadData()
  1471. return threadData.lastHTTPError and threadData.lastHTTPError[0] == threadData.lastRequestUID
  1472. def wasLastRequestDelayed():
  1473. """
  1474. Returns True if the last web request resulted in a time-delay
  1475. """
  1476. # 99.9999999997440% of all non time-based SQL injection affected
  1477. # response times should be inside +-7*stdev([normal response times])
  1478. # Math reference: http://www.answers.com/topic/standard-deviation
  1479. deviation = stdev(kb.responseTimes)
  1480. threadData = getCurrentThreadData()
  1481. if deviation and not conf.direct:
  1482. if len(kb.responseTimes) < MIN_TIME_RESPONSES:
  1483. warnMsg = "time-based standard deviation method used on a model "
  1484. warnMsg += "with less than %d response times" % MIN_TIME_RESPONSES
  1485. logger.warn(warnMsg)
  1486. lowerStdLimit = average(kb.responseTimes) + TIME_STDEV_COEFF * deviation
  1487. retVal = (threadData.lastQueryDuration >= lowerStdLimit)
  1488. if not kb.testMode and retVal:
  1489. if kb.adjustTimeDelay is None:
  1490. msg = "do you want sqlmap to try to optimize value(s) "
  1491. msg += "for DBMS delay responses (option '--time-sec')? [Y/n] "
  1492. choice = readInput(msg, default='Y')
  1493. kb.adjustTimeDelay = ADJUST_TIME_DELAY.DISABLE if choice.upper() == 'N' else ADJUST_TIME_DELAY.YES
  1494. if kb.adjustTimeDelay is ADJUST_TIME_DELAY.YES:
  1495. adjustTimeDelay(threadData.lastQueryDuration, lowerStdLimit)
  1496. return retVal
  1497. else:
  1498. return (threadData.lastQueryDuration - conf.timeSec) >= 0
  1499. def adjustTimeDelay(lastQueryDuration, lowerStdLimit):
  1500. """
  1501. Provides tip for adjusting time delay in time-based data retrieval
  1502. """
  1503. candidate = 1 + int(round(lowerStdLimit))
  1504. if candidate:
  1505. kb.delayCandidates = [candidate] + kb.delayCandidates[:-1]
  1506. if all((x == candidate for x in kb.delayCandidates)) and candidate < conf.timeSec:
  1507. conf.timeSec = candidate
  1508. infoMsg = "adjusting time delay to "
  1509. infoMsg += "%d second%s due to good response times" % (conf.timeSec, 's' if conf.timeSec > 1 else '')
  1510. logger.info(infoMsg)
  1511. def getLastRequestHTTPError():
  1512. """
  1513. Returns last HTTP error code
  1514. """
  1515. threadData = getCurrentThreadData()
  1516. return threadData.lastHTTPError[1] if threadData.lastHTTPError else None
  1517. def extractErrorMessage(page):
  1518. """
  1519. Returns reported error message from page if it founds one
  1520. """
  1521. retVal = None
  1522. if isinstance(page, basestring):
  1523. for regex in ERROR_PARSING_REGEXES:
  1524. match = re.search(regex, page, re.DOTALL | re.IGNORECASE)
  1525. if match:
  1526. retVal = htmlunescape(match.group("result")).replace("<br>", "\n").strip()
  1527. break
  1528. return retVal
  1529. def findMultipartPostBoundary(post):
  1530. """
  1531. Finds value for a boundary parameter in given multipart POST body
  1532. """
  1533. retVal = None
  1534. done = set()
  1535. candidates = []
  1536. for match in re.finditer(r"(?m)^--(.+?)(--)?$", post or ""):
  1537. _ = match.group(1)
  1538. if _ in done:
  1539. continue
  1540. else:
  1541. candidates.append((post.count(_), _))
  1542. done.add(_)
  1543. if candidates:
  1544. candidates.sort(key=lambda _: _[0], reverse=True)
  1545. retVal = candidates[0][1]
  1546. return retVal
  1547. def urldecode(value, encoding=None, unsafe="%%&=;+%s" % CUSTOM_INJECTION_MARK_CHAR, convall=False):
  1548. result = value
  1549. if value:
  1550. try:
  1551. # for cases like T%C3%BCrk%C3%A7e
  1552. value = str(value)
  1553. except ValueError:
  1554. pass
  1555. finally:
  1556. if convall:
  1557. result = urllib.unquote_plus(value)
  1558. else:
  1559. def _(match):
  1560. charset = reduce(lambda x, y: x.replace(y, ""), unsafe, string.printable)
  1561. char = chr(ord(match.group(1).decode("hex")))
  1562. return char if char in charset else match.group(0)
  1563. result = re.sub("%([0-9a-fA-F]{2})", _, value)
  1564. result = result.replace("+", " ") # plus sign has a special meaning in url encoded data (hence the usage of urllib.unquote_plus in convall case)
  1565. if isinstance(result, str):
  1566. result = unicode(result, encoding or UNICODE_ENCODING, "replace")
  1567. return result
  1568. def urlencode(value, safe="%&=", convall=False, limit=False):
  1569. if conf.direct:
  1570. return value
  1571. count = 0
  1572. result = None if value is None else ""
  1573. if value:
  1574. if convall or safe is None:
  1575. safe = ""
  1576. # corner case when character % really needs to be
  1577. # encoded (when not representing url encoded char)
  1578. # except in cases when tampering scripts are used
  1579. if all(map(lambda x: '%' in x, [safe, value])) and not kb.tamperFunctions:
  1580. value = re.sub("%(?![0-9a-fA-F]{2})", "%25", value)
  1581. while True:
  1582. result = urllib.quote(utf8encode(value), safe)
  1583. if limit and len(result) > URLENCODE_CHAR_LIMIT:
  1584. if count >= len(URLENCODE_FAILSAFE_CHARS):
  1585. break
  1586. while count < len(URLENCODE_FAILSAFE_CHARS):
  1587. safe += URLENCODE_FAILSAFE_CHARS[count]
  1588. count += 1
  1589. if safe[-1] in value:
  1590. break
  1591. else:
  1592. break
  1593. return result
  1594. def runningAsAdmin():
  1595. """
  1596. Returns True if the current process is run under admin privileges
  1597. """
  1598. isAdmin = None
  1599. if PLATFORM in ("posix", "mac"):
  1600. _ = os.geteuid()
  1601. isAdmin = isinstance(_, (int, float, long)) and _ == 0
  1602. elif IS_WIN:
  1603. _ = ctypes.windll.shell32.IsUserAnAdmin()
  1604. isAdmin = isinstance(_, (int, float, long)) and _ == 1
  1605. else:
  1606. errMsg = "sqlmap is not able to check if you are running it "
  1607. errMsg += "as an administrator account on this platform. "
  1608. errMsg += "sqlmap will assume that you are an administrator "
  1609. errMsg += "which is mandatory for the requested takeover attack "
  1610. errMsg += "to work properly"
  1611. logger.error(errMsg)
  1612. isAdmin = True
  1613. return isAdmin
  1614. def logHTTPTraffic(requestLogMsg, responseLogMsg):
  1615. """
  1616. Logs HTTP traffic to the output file
  1617. """
  1618. if not conf.trafficFile:
  1619. return
  1620. with kb.locks.log:
  1621. dataToTrafficFile("%s%s" % (requestLogMsg, os.linesep))
  1622. dataToTrafficFile("%s%s" % (responseLogMsg, os.linesep))
  1623. dataToTrafficFile("%s%s%s%s" % (os.linesep, 76 * '#', os.linesep, os.linesep))
  1624. def getPageTemplate(payload, place): # Cross-linked function
  1625. pass
  1626. def getPublicTypeMembers(type_, onlyValues=False):
  1627. """
  1628. Useful for getting members from types (e.g. in enums)
  1629. """
  1630. for name, value in inspect.getmembers(type_):
  1631. if not name.startswith('__'):
  1632. if not onlyValues:
  1633. yield (name, value)
  1634. else:
  1635. yield value
  1636. def enumValueToNameLookup(type_, value_):
  1637. """
  1638. Returns name of a enum member with a given value
  1639. """
  1640. retVal = None
  1641. for name, value in getPublicTypeMembers(type_):
  1642. if value == value_:
  1643. retVal = name
  1644. break
  1645. return retVal
  1646. def extractRegexResult(regex, content, flags=0):
  1647. """
  1648. Returns 'result' group value from a possible match with regex on a given
  1649. content
  1650. """
  1651. retVal = None
  1652. if regex and content and '?P<result>' in regex:
  1653. match = re.search(regex, content, flags)
  1654. if match:
  1655. retVal = match.group("result")
  1656. return retVal
  1657. def extractTextTagContent(page):
  1658. """
  1659. Returns list containing content from "textual" tags
  1660. """
  1661. page = re.sub(r"(?si)[^\s>]*%s[^<]*" % REFLECTED_VALUE_MARKER, "", page or "")
  1662. return filter(None, (_.group('result').strip() for _ in re.finditer(TEXT_TAG_REGEX, page)))
  1663. def trimAlphaNum(value):
  1664. """
  1665. Trims alpha numeric characters from start and ending of a given value
  1666. """
  1667. while value and value[-1].isalnum():
  1668. value = value[:-1]
  1669. while value and value[0].isalnum():
  1670. value = value[1:]
  1671. return value
  1672. def isNumPosStrValue(value):
  1673. """
  1674. Returns True if value is a string (or integer) with a positive integer representation
  1675. """
  1676. return (value and isinstance(value, basestring) and value.isdigit() and value != "0") or (isinstance(value, int) and value != 0)
  1677. @cachedmethod
  1678. def aliasToDbmsEnum(dbms):
  1679. """
  1680. Returns major DBMS name from a given alias
  1681. """
  1682. retVal = None
  1683. if dbms:
  1684. for key, item in DBMS_DICT.items():
  1685. if dbms.lower() in item[0] or dbms.lower() == key.lower():
  1686. retVal = key
  1687. break
  1688. return retVal
  1689. def findDynamicContent(firstPage, secondPage):
  1690. """
  1691. This function checks if the provided pages have dynamic content. If they
  1692. are dynamic, proper markings will be made
  1693. """
  1694. infoMsg = "searching for dynamic content"
  1695. logger.info(infoMsg)
  1696. blocks = SequenceMatcher(None, firstPage, secondPage).get_matching_blocks()
  1697. kb.dynamicMarkings = []
  1698. # Removing too small matching blocks
  1699. for block in blocks[:]:
  1700. (_, _, length) = block
  1701. if length <= DYNAMICITY_MARK_LENGTH:
  1702. blocks.remove(block)
  1703. # Making of dynamic markings based on prefix/suffix principle
  1704. if len(blocks) > 0:
  1705. blocks.insert(0, None)
  1706. blocks.append(None)
  1707. for i in xrange(len(blocks) - 1):
  1708. prefix = firstPage[blocks[i][0]:blocks[i][0] + blocks[i][2]] if blocks[i] else None
  1709. suffix = firstPage[blocks[i + 1][0]:blocks[i + 1][0] + blocks[i + 1][2]] if blocks[i + 1] else None
  1710. if prefix is None and blocks[i + 1][0] == 0:
  1711. continue
  1712. if suffix is None and (blocks[i][0] + blocks[i][2] >= len(firstPage)):
  1713. continue
  1714. prefix = trimAlphaNum(prefix)
  1715. suffix = trimAlphaNum(suffix)
  1716. kb.dynamicMarkings.append((re.escape(prefix[-DYNAMICITY_MARK_LENGTH / 2:]) if prefix else None, re.escape(suffix[:DYNAMICITY_MARK_LENGTH / 2]) if suffix else None))
  1717. if len(kb.dynamicMarkings) > 0:
  1718. infoMsg = "dynamic content marked for removal (%d region%s)" % (len(kb.dynamicMarkings), 's' if len(kb.dynamicMarkings) > 1 else '')
  1719. logger.info(infoMsg)
  1720. def removeDynamicContent(page):
  1721. """
  1722. Removing dynamic content from supplied page basing removal on
  1723. precalculated dynamic markings
  1724. """
  1725. if page:
  1726. for item in kb.dynamicMarkings:
  1727. prefix, suffix = item
  1728. if prefix is None and suffix is None:
  1729. continue
  1730. elif prefix is None:
  1731. page = re.sub(r'(?s)^.+%s' % suffix, suffix, page)
  1732. elif suffix is None:
  1733. page = re.sub(r'(?s)%s.+$' % prefix, prefix, page)
  1734. else:
  1735. page = re.sub(r'(?s)%s.+%s' % (prefix, suffix), '%s%s' % (prefix, suffix), page)
  1736. return page
  1737. def filterStringValue(value, regex, replacement=""):
  1738. """
  1739. Returns string value consisting only of chars satisfying supplied
  1740. regular expression (note: it has to be in form [...])
  1741. """
  1742. retVal = value
  1743. if value:
  1744. retVal = re.sub(regex.replace("[", "[^") if "[^" not in regex else regex.replace("[^", "["), replacement, value)
  1745. return retVal
  1746. def filterControlChars(value):
  1747. """
  1748. Returns string value with control chars being supstituted with ' '
  1749. """
  1750. return filterStringValue(value, PRINTABLE_CHAR_REGEX, ' ')
  1751. def isDBMSVersionAtLeast(version):
  1752. """
  1753. Checks if the recognized DBMS version is at least the version
  1754. specified
  1755. """
  1756. retVal = None
  1757. if Backend.getVersion() and Backend.getVersion() != UNKNOWN_DBMS_VERSION:
  1758. value = Backend.getVersion().replace(" ", "").rstrip('.')
  1759. while True:
  1760. index = value.find('.', value.find('.') + 1)
  1761. if index > -1:
  1762. value = value[0:index] + value[index + 1:]
  1763. else:
  1764. break
  1765. value = filterStringValue(value, '[0-9.><=]')
  1766. if isinstance(value, basestring):
  1767. if value.startswith(">="):
  1768. value = float(value.replace(">=", ""))
  1769. elif value.startswith(">"):
  1770. value = float(value.replace(">", "")) + 0.01
  1771. elif value.startswith("<="):
  1772. value = float(value.replace("<=", ""))
  1773. elif value.startswith(">"):
  1774. value = float(value.replace("<", "")) - 0.01
  1775. retVal = getUnicode(value) >= getUnicode(version)
  1776. return retVal
  1777. def parseSqliteTableSchema(value):
  1778. """
  1779. Parses table column names and types from specified SQLite table schema
  1780. """
  1781. if value:
  1782. table = {}
  1783. columns = {}
  1784. for match in re.finditer(r"(\w+)\s+(TEXT|NUMERIC|INTEGER|REAL|NONE)", value):
  1785. columns[match.group(1)] = match.group(2)
  1786. table[conf.tbl] = columns
  1787. kb.data.cachedColumns[conf.db] = table
  1788. def getTechniqueData(technique=None):
  1789. """
  1790. Returns injection data for technique specified
  1791. """
  1792. return kb.injection.data.get(technique)
  1793. def isTechniqueAvailable(technique):
  1794. """
  1795. Returns True if there is injection data which sqlmap could use for
  1796. technique specified
  1797. """
  1798. if conf.tech and isinstance(conf.tech, list) and technique not in conf.tech:
  1799. return False
  1800. else:
  1801. return getTechniqueData(technique) is not None
  1802. def isInferenceAvailable():
  1803. """
  1804. Returns True whether techniques using inference technique are available
  1805. """
  1806. return any(isTechniqueAvailable(_) for _ in (PAYLOAD.TECHNIQUE.BOOLEAN, PAYLOAD.TECHNIQUE.STACKED, PAYLOAD.TECHNIQUE.TIME))
  1807. def setOptimize():
  1808. """
  1809. Sets options turned on by switch '-o'
  1810. """
  1811. #conf.predictOutput = True
  1812. conf.keepAlive = True
  1813. conf.threads = 3 if conf.threads < 3 else conf.threads
  1814. conf.nullConnection = not any([conf.data, conf.textOnly, conf.titles, conf.string, conf.notString, conf.regexp, conf.tor])
  1815. if not conf.nullConnection:
  1816. debugMsg = "turning off --null-connection switch used indirectly by switch -o"
  1817. logger.debug(debugMsg)
  1818. def initTechnique(technique=None):
  1819. """
  1820. Prepares data for technique specified
  1821. """
  1822. try:
  1823. data = getTechniqueData(technique)
  1824. resetCounter(technique)
  1825. if data:
  1826. kb.pageTemplate, kb.errorIsNone = getPageTemplate(data.templatePayload, kb.injection.place)
  1827. kb.matchRatio = data.matchRatio
  1828. kb.negativeLogic = (technique == PAYLOAD.TECHNIQUE.BOOLEAN) and (data.where == PAYLOAD.WHERE.NEGATIVE)
  1829. # Restoring stored conf options
  1830. for key, value in kb.injection.conf.items():
  1831. if value and (not hasattr(conf, key) or (hasattr(conf, key) and not getattr(conf, key))):
  1832. setattr(conf, key, value)
  1833. debugMsg = "resuming configuration option '%s' (%s)" % (key, value)
  1834. logger.debug(debugMsg)
  1835. if value and key == "optimize":
  1836. setOptimize()
  1837. else:
  1838. warnMsg = "there is no injection data available for technique "
  1839. warnMsg += "'%s'" % enumValueToNameLookup(PAYLOAD.TECHNIQUE, technique)
  1840. logger.warn(warnMsg)
  1841. except SqlmapDataException:
  1842. errMsg = "missing data in old session file(s). "
  1843. errMsg += "Please use '--flush-session' to deal "
  1844. errMsg += "with this error"
  1845. raise SqlmapNoneDataException, errMsg
  1846. def arrayizeValue(value):
  1847. """
  1848. Makes a list out of value if it is not already a list or tuple itself
  1849. """
  1850. if not isListLike(value):
  1851. value = [value]
  1852. return value
  1853. def unArrayizeValue(value):
  1854. """
  1855. Makes a value out of iterable if it is a list or tuple itself
  1856. """
  1857. if isListLike(value):
  1858. value = value[0] if len(value) > 0 else None
  1859. return value
  1860. def flattenValue(value):
  1861. """
  1862. Returns an iterator representing flat representation of a given value
  1863. """
  1864. for i in iter(value):
  1865. if isListLike(i):
  1866. for j in flattenValue(i):
  1867. yield j
  1868. else:
  1869. yield i
  1870. def isListLike(value):
  1871. """
  1872. Returns True if the given value is a list-like instance
  1873. """
  1874. return isinstance(value, (list, tuple, set, BigArray))
  1875. def getSortedInjectionTests():
  1876. """
  1877. Returns prioritized test list by eventually detected DBMS from error
  1878. messages
  1879. """
  1880. retVal = copy.deepcopy(conf.tests)
  1881. def priorityFunction(test):
  1882. retVal = SORT_ORDER.FIRST
  1883. if test.stype == PAYLOAD.TECHNIQUE.UNION:
  1884. retVal = SORT_ORDER.LAST
  1885. elif 'details' in test and 'dbms' in test.details:
  1886. if test.details.dbms in Backend.getErrorParsedDBMSes():
  1887. retVal = SORT_ORDER.SECOND
  1888. else:
  1889. retVal = SORT_ORDER.THIRD
  1890. return retVal
  1891. if Backend.getErrorParsedDBMSes():
  1892. retVal = sorted(retVal, key=priorityFunction)
  1893. return retVal
  1894. def filterListValue(value, regex):
  1895. """
  1896. Returns list with items that have parts satisfying given regular
  1897. expression
  1898. """
  1899. if isinstance(value, list) and regex:
  1900. retVal = filter(lambda _: re.search(regex, _, re.I), value)
  1901. else:
  1902. retVal = value
  1903. return retVal
  1904. def showHttpErrorCodes():
  1905. """
  1906. Shows all HTTP error codes raised till now
  1907. """
  1908. if kb.httpErrorCodes:
  1909. warnMsg = "HTTP error codes detected during run:\n"
  1910. warnMsg += ", ".join("%d (%s) - %d times" % (code, httplib.responses[code] \
  1911. if code in httplib.responses else '?', count) \
  1912. for code, count in kb.httpErrorCodes.items())
  1913. logger.warn(warnMsg)
  1914. def getComparePageRatio(firstPage, secondPage, filtered=False):
  1915. """
  1916. Returns comparison ratio between two given pages
  1917. """
  1918. if filtered:
  1919. (firstPage, secondPage) = map(getFilteredPageContent, (firstPage, secondPage))
  1920. seqMatcher = getCurrentThreadData().seqMatcher
  1921. seqMatcher.set_seq1(firstPage)
  1922. seqMatcher.set_seq2(secondPage)
  1923. return seqMatcher.quick_ratio()
  1924. def openFile(filename, mode='r'):
  1925. """
  1926. Returns file handle of a given filename
  1927. """
  1928. try:
  1929. return codecs.open(filename, mode, UNICODE_ENCODING, "replace")
  1930. except IOError:
  1931. errMsg = "there has been a file opening error for filename '%s'. " % filename
  1932. errMsg += "Please check %s permissions on a file " % ("write" if \
  1933. mode and ('w' in mode or 'a' in mode or '+' in mode) else "read")
  1934. errMsg += "and that it's not locked by another process."
  1935. raise SqlmapFilePathException, errMsg
  1936. def decodeIntToUnicode(value):
  1937. """
  1938. Decodes inferenced integer value to an unicode character
  1939. """
  1940. retVal = value
  1941. if isinstance(value, int):
  1942. try:
  1943. # http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_ord
  1944. if Backend.getIdentifiedDbms() in (DBMS.MYSQL,):
  1945. retVal = getUnicode(hexdecode(hex(value)))
  1946. elif value > 255:
  1947. retVal = unichr(value)
  1948. else:
  1949. retVal = getUnicode(chr(value))
  1950. except:
  1951. retVal = INFERENCE_UNKNOWN_CHAR
  1952. return retVal
  1953. def unhandledExceptionMessage():
  1954. """
  1955. Returns detailed message about occured unhandled exception
  1956. """
  1957. errMsg = "unhandled exception in %s, retry your " % VERSION_STRING
  1958. errMsg += "run with the latest development version from the GitHub "
  1959. errMsg += "repository. If the exception persists, please send by e-mail "
  1960. errMsg += "to '%s' or open a new issue at '%s' with the following text " % (ML, ISSUES_PAGE)
  1961. errMsg += "and any information required to reproduce the bug. The "
  1962. errMsg += "developers will try to reproduce the bug, fix it accordingly "
  1963. errMsg += "and get back to you.\n"
  1964. errMsg += "sqlmap version: %s%s\n" % (VERSION, "-%s" % REVISION if REVISION else "")
  1965. errMsg += "Python version: %s\n" % PYVERSION
  1966. errMsg += "Operating system: %s\n" % PLATFORM
  1967. errMsg += "Command line: %s\n" % " ".join(sys.argv)
  1968. errMsg += "Technique: %s\n" % (enumValueToNameLookup(PAYLOAD.TECHNIQUE, kb.technique) if kb.get("technique") else None)
  1969. errMsg += "Back-end DBMS: %s" % ("%s (fingerprinted)" % Backend.getDbms() if Backend.getDbms() is not None else "%s (identified)" % Backend.getIdentifiedDbms())
  1970. return maskSensitiveData(errMsg)
  1971. def maskSensitiveData(msg):
  1972. """
  1973. Masks sensitive data in the supplied message
  1974. """
  1975. retVal = msg
  1976. for item in filter(None, map(lambda x: conf.get(x), ("hostname", "googleDork", "aCred", "pCred", "tbl", "db", "col", "user", "cookie", "proxy"))):
  1977. regex = SENSITIVE_DATA_REGEX % re.sub("(\W)", r"\\\1", item)
  1978. while extractRegexResult(regex, retVal):
  1979. value = extractRegexResult(regex, retVal)
  1980. retVal = retVal.replace(value, '*' * len(value))
  1981. return retVal
  1982. def listToStrValue(value):
  1983. """
  1984. Flattens list to a string value
  1985. >>> listToStrValue([1,2,3])
  1986. '1, 2, 3'
  1987. """
  1988. if isinstance(value, (set, tuple)):
  1989. value = list(value)
  1990. if isinstance(value, list):
  1991. retVal = value.__str__().lstrip('[').rstrip(']')
  1992. else:
  1993. retVal = value
  1994. return retVal
  1995. def getExceptionFrameLocals():
  1996. """
  1997. Returns dictionary with local variable content from frame
  1998. where exception has been raised
  1999. """
  2000. retVal = {}
  2001. if sys.exc_info():
  2002. trace = sys.exc_info()[2]
  2003. while trace.tb_next:
  2004. trace = trace.tb_next
  2005. retVal = trace.tb_frame.f_locals
  2006. return retVal
  2007. def intersect(valueA, valueB, lowerCase=False):
  2008. """
  2009. Returns intersection of the array-ized values
  2010. """
  2011. retVal = None
  2012. if valueA and valueB:
  2013. valueA = arrayizeValue(valueA)
  2014. valueB = arrayizeValue(valueB)
  2015. if lowerCase:
  2016. valueA = [val.lower() if isinstance(val, basestring) else val for val in valueA]
  2017. valueB = [val.lower() if isinstance(val, basestring) else val for val in valueB]
  2018. retVal = [val for val in valueA if val in valueB]
  2019. return retVal
  2020. def cpuThrottle(value):
  2021. """
  2022. Does a CPU throttling for lesser CPU consumption
  2023. """
  2024. delay = 0.00001 * (value ** 2)
  2025. time.sleep(delay)
  2026. def removeReflectiveValues(content, payload, suppressWarning=False):
  2027. """
  2028. Neutralizes reflective values in a given content based on a payload
  2029. (e.g. ..search.php?q=1 AND 1=2 --> "...searching for <b>1%20AND%201%3D2</b>..." --> "...searching for <b>__REFLECTED_VALUE__</b>...")
  2030. """
  2031. retVal = content
  2032. if all([content, payload]) and isinstance(content, unicode) and kb.reflectiveMechanism:
  2033. def _(value):
  2034. while 2 * REFLECTED_REPLACEMENT_REGEX in value:
  2035. value = value.replace(2 * REFLECTED_REPLACEMENT_REGEX, REFLECTED_REPLACEMENT_REGEX)
  2036. return value
  2037. payload = getUnicode(urldecode(payload.replace(PAYLOAD_DELIMITER, ''), convall=True))
  2038. regex = _(filterStringValue(payload, r"[A-Za-z0-9]", REFLECTED_REPLACEMENT_REGEX.encode("string-escape")))
  2039. if regex != payload:
  2040. if all(part.lower() in content.lower() for part in filter(None, regex.split(REFLECTED_REPLACEMENT_REGEX))[1:]): # fast optimization check
  2041. parts = regex.split(REFLECTED_REPLACEMENT_REGEX)
  2042. retVal = content.replace(payload, REFLECTED_VALUE_MARKER) # dummy approach
  2043. if len(parts) > REFLECTED_MAX_REGEX_PARTS: # preventing CPU hogs
  2044. regex = _("%s%s%s" % (REFLECTED_REPLACEMENT_REGEX.join(parts[:REFLECTED_MAX_REGEX_PARTS / 2]), REFLECTED_REPLACEMENT_REGEX, REFLECTED_REPLACEMENT_REGEX.join(parts[-REFLECTED_MAX_REGEX_PARTS / 2:])))
  2045. parts = filter(None, regex.split(REFLECTED_REPLACEMENT_REGEX))
  2046. if regex.startswith(REFLECTED_REPLACEMENT_REGEX):
  2047. regex = r"%s%s" % (REFLECTED_BORDER_REGEX, regex[len(REFLECTED_REPLACEMENT_REGEX):])
  2048. else:
  2049. regex = r"\b%s" % regex
  2050. if regex.endswith(REFLECTED_REPLACEMENT_REGEX):
  2051. regex = r"%s%s" % (regex[:-len(REFLECTED_REPLACEMENT_REGEX)], REFLECTED_BORDER_REGEX)
  2052. else:
  2053. regex = r"%s\b" % regex
  2054. retVal = re.sub(r"(?i)%s" % regex, REFLECTED_VALUE_MARKER, retVal)
  2055. if len(parts) > 2:
  2056. regex = REFLECTED_REPLACEMENT_REGEX.join(parts[1:])
  2057. retVal = re.sub(r"(?i)\b%s\b" % regex, REFLECTED_VALUE_MARKER, retVal)
  2058. if retVal != content:
  2059. kb.reflectiveCounters[REFLECTIVE_COUNTER.HIT] += 1
  2060. if not suppressWarning:
  2061. warnMsg = "reflective value(s) found and filtering out"
  2062. singleTimeWarnMessage(warnMsg)
  2063. if re.search(r"FRAME[^>]+src=[^>]*%s" % REFLECTED_VALUE_MARKER, retVal, re.I):
  2064. warnMsg = "frames detected containing attacked parameter values. Please be sure to "
  2065. warnMsg += "test those separately in case that attack on this page fails"
  2066. singleTimeWarnMessage(warnMsg)
  2067. elif not kb.testMode and not kb.reflectiveCounters[REFLECTIVE_COUNTER.HIT]:
  2068. kb.reflectiveCounters[REFLECTIVE_COUNTER.MISS] += 1
  2069. if kb.reflectiveCounters[REFLECTIVE_COUNTER.MISS] > REFLECTIVE_MISS_THRESHOLD:
  2070. kb.reflectiveMechanism = False
  2071. if not suppressWarning:
  2072. debugMsg = "turning off reflection removal mechanism (for optimization purposes)"
  2073. logger.debug(debugMsg)
  2074. return retVal
  2075. def normalizeUnicode(value):
  2076. """
  2077. Does an ASCII normalization of unicode strings
  2078. Reference: http://www.peterbe.com/plog/unicode-to-ascii
  2079. """
  2080. return unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') if isinstance(value, unicode) else value
  2081. def safeSQLIdentificatorNaming(name, isTable=False):
  2082. """
  2083. Returns a safe representation of SQL identificator name (internal data format)
  2084. Reference: http://stackoverflow.com/questions/954884/what-special-characters-are-allowed-in-t-sql-column-retVal
  2085. """
  2086. retVal = name
  2087. if isinstance(name, basestring):
  2088. retVal = getUnicode(name)
  2089. _ = isTable and Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE)
  2090. if _:
  2091. retVal = re.sub(r"(?i)\A%s\." % DEFAULT_MSSQL_SCHEMA, "", retVal)
  2092. if not re.match(r"\A[A-Za-z0-9_@%s\$]+\Z" % ("." if _ else ""), retVal): # MsSQL is the only DBMS where we automatically prepend schema to table name (dot is normal)
  2093. if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.ACCESS):
  2094. retVal = "`%s`" % retVal.strip("`")
  2095. elif Backend.getIdentifiedDbms() in (DBMS.ORACLE, DBMS.PGSQL, DBMS.DB2):
  2096. retVal = "\"%s\"" % retVal.strip("\"")
  2097. elif Backend.getIdentifiedDbms() in (DBMS.MSSQL,):
  2098. retVal = "[%s]" % retVal.strip("[]")
  2099. if _ and DEFAULT_MSSQL_SCHEMA not in retVal and '.' not in re.sub(r"\[[^]]+\]", "", retVal):
  2100. retVal = "%s.%s" % (DEFAULT_MSSQL_SCHEMA, retVal)
  2101. return retVal
  2102. def unsafeSQLIdentificatorNaming(name):
  2103. """
  2104. Extracts identificator's name from its safe SQL representation
  2105. """
  2106. retVal = name
  2107. if isinstance(name, basestring):
  2108. if Backend.getIdentifiedDbms() in (DBMS.MYSQL, DBMS.ACCESS):
  2109. retVal = name.replace("`", "")
  2110. elif Backend.getIdentifiedDbms() in (DBMS.ORACLE, DBMS.PGSQL, DBMS.DB2):
  2111. retVal = name.replace("\"", "")
  2112. elif Backend.getIdentifiedDbms() in (DBMS.MSSQL,):
  2113. retVal = name.replace("[", "").replace("]", "")
  2114. if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.SYBASE):
  2115. prefix = "%s." % DEFAULT_MSSQL_SCHEMA
  2116. if retVal.startswith(prefix):
  2117. retVal = retVal[len(prefix):]
  2118. return retVal
  2119. def isBinaryData(value):
  2120. """
  2121. Tests given value for binary content
  2122. """
  2123. retVal = False
  2124. if isinstance(value, basestring):
  2125. retVal = reduce(lambda x, y: x or not (y in string.printable or ord(y) > 255), value, False)
  2126. return retVal
  2127. def isNoneValue(value):
  2128. """
  2129. Returns whether the value is unusable (None or '')
  2130. """
  2131. if isinstance(value, basestring):
  2132. return value in ("None", "")
  2133. elif isListLike(value):
  2134. return all(isNoneValue(_) for _ in value)
  2135. elif isinstance(value, dict):
  2136. return not any(value)
  2137. else:
  2138. return value is None
  2139. def isNullValue(value):
  2140. """
  2141. Returns whether the value contains explicit 'NULL' value
  2142. """
  2143. return isinstance(value, basestring) and value.upper() == NULL
  2144. def expandMnemonics(mnemonics, parser, args):
  2145. """
  2146. Expands mnemonic options
  2147. """
  2148. class MnemonicNode(object):
  2149. def __init__(self):
  2150. self.next = {}
  2151. self.current = []
  2152. head = MnemonicNode()
  2153. pointer = None
  2154. for group in parser.option_groups:
  2155. for option in group.option_list:
  2156. for opt in option._long_opts + option._short_opts:
  2157. pointer = head
  2158. for char in opt:
  2159. if char == "-":
  2160. continue
  2161. elif char not in pointer.next:
  2162. pointer.next[char] = MnemonicNode()
  2163. pointer = pointer.next[char]
  2164. pointer.current.append(option)
  2165. for mnemonic in mnemonics.split(','):
  2166. found = None
  2167. name = mnemonic.split('=')[0].replace("-", "").strip()
  2168. value = mnemonic.split('=')[1] if len(mnemonic.split('=')) > 1 else None
  2169. pointer = head
  2170. for char in name:
  2171. if char in pointer.next:
  2172. pointer = pointer.next[char]
  2173. else:
  2174. pointer = None
  2175. break
  2176. if pointer in (None, head):
  2177. errMsg = "mnemonic '%s' can't be resolved to any parameter name" % name
  2178. raise SqlmapSyntaxException, errMsg
  2179. elif len(pointer.current) > 1:
  2180. options = {}
  2181. for option in pointer.current:
  2182. for opt in option._long_opts + option._short_opts:
  2183. opt = opt.strip('-')
  2184. if opt.startswith(name):
  2185. options[opt] = option
  2186. if name in options:
  2187. found = name
  2188. debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
  2189. logger.debug(debugMsg)
  2190. else:
  2191. found = sorted(options.keys(), key=lambda x: len(x))[0]
  2192. warnMsg = "detected ambiguity (mnemonic '%s' can be resolved to: %s). " % (name, ", ".join("'%s'" % key for key in options.keys()))
  2193. warnMsg += "Resolved to shortest of those ('%s')" % found
  2194. logger.warn(warnMsg)
  2195. found = options[found]
  2196. else:
  2197. found = pointer.current[0]
  2198. debugMsg = "mnemonic '%s' resolved to %s). " % (name, found)
  2199. logger.debug(debugMsg)
  2200. if found:
  2201. try:
  2202. value = found.convert_value(found, value)
  2203. except OptionValueError:
  2204. value = None
  2205. if value is not None:
  2206. setattr(args, found.dest, value)
  2207. elif not found.type: # boolean
  2208. setattr(args, found.dest, True)
  2209. else:
  2210. errMsg = "mnemonic '%s' requires value of type '%s'" % (name, found.type)
  2211. raise SqlmapSyntaxException, errMsg
  2212. def safeCSValue(value):
  2213. """
  2214. Returns value safe for CSV dumping
  2215. Reference: http://tools.ietf.org/html/rfc4180
  2216. """
  2217. retVal = value
  2218. if retVal and isinstance(retVal, basestring):
  2219. if not (retVal[0] == retVal[-1] == '"'):
  2220. if any(_ in retVal for _ in (conf.csvDel, '"', '\n')):
  2221. retVal = '"%s"' % retVal.replace('"', '""')
  2222. return retVal
  2223. def filterPairValues(values):
  2224. """
  2225. Returns only list-like values with length 2
  2226. """
  2227. retVal = []
  2228. if not isNoneValue(values) and hasattr(values, '__iter__'):
  2229. retVal = filter(lambda x: isinstance(x, (tuple, list, set)) and len(x) == 2, values)
  2230. return retVal
  2231. def randomizeParameterValue(value):
  2232. """
  2233. Randomize a parameter value based on occurances of alphanumeric characters
  2234. """
  2235. retVal = value
  2236. for match in re.finditer('[A-Z]+', value):
  2237. retVal = retVal.replace(match.group(), randomStr(len(match.group())).upper())
  2238. for match in re.finditer('[a-z]+', value):
  2239. retVal = retVal.replace(match.group(), randomStr(len(match.group())).lower())
  2240. for match in re.finditer('[0-9]+', value):
  2241. retVal = retVal.replace(match.group(), str(randomInt(len(match.group()))))
  2242. return retVal
  2243. def asciifyUrl(url, forceQuote=False):
  2244. """
  2245. Attempts to make a unicode url usuable with ``urllib/urllib2``.
  2246. More specifically, it attempts to convert the unicode object ``url``,
  2247. which is meant to represent a IRI, to an unicode object that,
  2248. containing only ASCII characters, is a valid URI. This involves:
  2249. * IDNA/Puny-encoding the domain name.
  2250. * UTF8-quoting the path and querystring parts.
  2251. See also RFC 3987.
  2252. Reference: http://blog.elsdoerfer.name/2008/12/12/opening-iris-in-python/
  2253. """
  2254. parts = urlparse.urlsplit(url)
  2255. if not parts.scheme or not parts.netloc:
  2256. # apparently not an url
  2257. return url
  2258. if all(char in string.printable for char in url):
  2259. return url
  2260. # idna-encode domain
  2261. hostname = parts.hostname.encode("idna")
  2262. # UTF8-quote the other parts. We check each part individually if
  2263. # if needs to be quoted - that should catch some additional user
  2264. # errors, say for example an umlaut in the username even though
  2265. # the path *is* already quoted.
  2266. def quote(s, safe):
  2267. s = s or ''
  2268. # Triggers on non-ascii characters - another option would be:
  2269. # urllib.quote(s.replace('%', '')) != s.replace('%', '')
  2270. # which would trigger on all %-characters, e.g. "&".
  2271. if s.encode("ascii", "replace") != s or forceQuote:
  2272. return urllib.quote(s.encode(UNICODE_ENCODING), safe=safe)
  2273. return s
  2274. username = quote(parts.username, '')
  2275. password = quote(parts.password, safe='')
  2276. path = quote(parts.path, safe='/')
  2277. query = quote(parts.query, safe="&=")
  2278. # put everything back together
  2279. netloc = hostname
  2280. if username or password:
  2281. netloc = '@' + netloc
  2282. if password:
  2283. netloc = ':' + password + netloc
  2284. netloc = username + netloc
  2285. if parts.port:
  2286. netloc += ':' + str(parts.port)
  2287. return urlparse.urlunsplit([parts.scheme, netloc, path, query, parts.fragment])
  2288. def isAdminFromPrivileges(privileges):
  2289. """
  2290. Inspects privileges to see if those are comming from an admin user
  2291. """
  2292. # In PostgreSQL the usesuper privilege means that the
  2293. # user is DBA
  2294. retVal = (Backend.isDbms(DBMS.PGSQL) and "super" in privileges)
  2295. # In Oracle the DBA privilege means that the
  2296. # user is DBA
  2297. retVal |= (Backend.isDbms(DBMS.ORACLE) and "DBA" in privileges)
  2298. # In MySQL >= 5.0 the SUPER privilege means
  2299. # that the user is DBA
  2300. retVal |= (Backend.isDbms(DBMS.MYSQL) and kb.data.has_information_schema and "SUPER" in privileges)
  2301. # In MySQL < 5.0 the super_priv privilege means
  2302. # that the user is DBA
  2303. retVal |= (Backend.isDbms(DBMS.MYSQL) and not kb.data.has_information_schema and "super_priv" in privileges)
  2304. # In Firebird there is no specific privilege that means
  2305. # that the user is DBA
  2306. # TODO: confirm
  2307. retVal |= (Backend.isDbms(DBMS.FIREBIRD) and "SELECT" in privileges and "INSERT" in privileges and "UPDATE" in privileges and "DELETE" in privileges and "REFERENCES" in privileges and "EXECUTE" in privileges)
  2308. return retVal
  2309. def findPageForms(content, url, raise_=False, addToTargets=False):
  2310. """
  2311. Parses given page content for possible forms
  2312. """
  2313. class _(StringIO):
  2314. def __init__(self, content, url):
  2315. StringIO.__init__(self, unicodeencode(content, kb.pageEncoding) if isinstance(content, unicode) else content)
  2316. self._url = url
  2317. def geturl(self):
  2318. return self._url
  2319. if not content:
  2320. errMsg = "can't parse forms as the page content appears to be blank"
  2321. if raise_:
  2322. raise SqlmapGenericException, errMsg
  2323. else:
  2324. logger.debug(errMsg)
  2325. forms = None
  2326. retVal = set()
  2327. response = _(content, url)
  2328. try:
  2329. forms = ParseResponse(response, backwards_compat=False)
  2330. except ParseError:
  2331. warnMsg = "badly formed HTML at the given url ('%s'). Going to filter it" % url
  2332. logger.warning(warnMsg)
  2333. response.seek(0)
  2334. filtered = _("".join(re.findall(FORM_SEARCH_REGEX, response.read())), response.geturl())
  2335. try:
  2336. forms = ParseResponse(filtered, backwards_compat=False)
  2337. except ParseError:
  2338. errMsg = "no success"
  2339. if raise_:
  2340. raise SqlmapGenericException, errMsg
  2341. else:
  2342. logger.debug(errMsg)
  2343. if forms:
  2344. for form in forms:
  2345. try:
  2346. for control in form.controls:
  2347. if hasattr(control, "items"):
  2348. # if control has selectable items select first non-disabled
  2349. for item in control.items:
  2350. if not item.disabled:
  2351. if not item.selected:
  2352. item.selected = True
  2353. break
  2354. request = form.click()
  2355. except (ValueError, TypeError), ex:
  2356. errMsg = "there has been a problem while "
  2357. errMsg += "processing page forms ('%s')" % ex
  2358. if raise_:
  2359. raise SqlmapGenericException, errMsg
  2360. else:
  2361. logger.debug(errMsg)
  2362. else:
  2363. url = urldecode(request.get_full_url(), kb.pageEncoding)
  2364. method = request.get_method()
  2365. data = request.get_data() if request.has_data() else None
  2366. data = urldecode(data, kb.pageEncoding) if data and urlencode(DEFAULT_GET_POST_DELIMITER, None) not in data else data
  2367. if not data and method and method.upper() == HTTPMETHOD.POST:
  2368. debugMsg = "invalid POST form with blank data detected"
  2369. logger.debug(debugMsg)
  2370. continue
  2371. target = (url, method, data, conf.cookie)
  2372. retVal.add(target)
  2373. else:
  2374. errMsg = "there were no forms found at the given target url"
  2375. if raise_:
  2376. raise SqlmapGenericException, errMsg
  2377. else:
  2378. logger.debug(errMsg)
  2379. if addToTargets and retVal:
  2380. for target in retVal:
  2381. url = target[0]
  2382. # flag to know if we are dealing with the same target host
  2383. _ = reduce(lambda x, y: x == y, map(lambda x: urlparse.urlparse(x).netloc.split(':')[0], (response.geturl(), url)))
  2384. if conf.scope:
  2385. if not re.search(conf.scope, url, re.I):
  2386. continue
  2387. elif not _:
  2388. continue
  2389. kb.targets.add(target)
  2390. return retVal
  2391. def getHostHeader(url):
  2392. """
  2393. Returns proper Host header value for a given target URL
  2394. """
  2395. retVal = url
  2396. if url:
  2397. retVal = urlparse.urlparse(url).netloc
  2398. if re.search("http(s)?://\[.+\]", url, re.I):
  2399. retVal = extractRegexResult("http(s)?://\[(?P<result>.+)\]", url)
  2400. elif any(retVal.endswith(':%d' % _) for _ in (80, 443)):
  2401. retVal = retVal.split(':')[0]
  2402. return retVal
  2403. def checkDeprecatedOptions(args):
  2404. """
  2405. Checks for deprecated options
  2406. """
  2407. for _ in args:
  2408. if _ in DEPRECATED_OPTIONS:
  2409. errMsg = "switch/option '%s' is deprecated" % _
  2410. if _ in DEPRECATED_HINTS:
  2411. errMsg += " (hint: %s)" % DEPRECATED_HINTS[_]
  2412. raise SqlmapSyntaxException, errMsg
  2413. def evaluateCode(code, variables=None):
  2414. """
  2415. Executes given python code given in a string form
  2416. """
  2417. try:
  2418. exec(code, variables)
  2419. except KeyboardInterrupt:
  2420. raise
  2421. except Exception, ex:
  2422. errMsg = "an error occured while evaluating provided code ('%s'). " % ex
  2423. raise SqlmapGenericException, errMsg
  2424. def serializeObject(object_):
  2425. """
  2426. Serializes given object
  2427. """
  2428. return base64pickle(object_)
  2429. def unserializeObject(value):
  2430. """
  2431. Unserializes object from given serialized form
  2432. """
  2433. return base64unpickle(value) if value else None
  2434. def resetCounter(technique):
  2435. """
  2436. Resets query counter for a given technique
  2437. """
  2438. kb.counters[technique] = 0
  2439. def incrementCounter(technique):
  2440. """
  2441. Increments query counter for a given technique
  2442. """
  2443. kb.counters[technique] = getCounter(technique) + 1
  2444. def getCounter(technique):
  2445. """
  2446. Returns query counter for a given technique
  2447. """
  2448. return kb.counters.get(technique, 0)
  2449. def applyFunctionRecursively(value, function):
  2450. """
  2451. Applies function recursively through list-like structures
  2452. """
  2453. if isListLike(value):
  2454. retVal = [applyFunctionRecursively(_, function) for _ in value]
  2455. else:
  2456. retVal = function(value)
  2457. return retVal
  2458. def decodeHexValue(value):
  2459. """
  2460. Returns value decoded from DBMS specific hexadecimal representation
  2461. """
  2462. retVal = value
  2463. def _(value):
  2464. if value and isinstance(value, basestring) and len(value) % 2 == 0:
  2465. value = hexdecode(value)
  2466. if Backend.isDbms(DBMS.MSSQL):
  2467. try:
  2468. value = value.decode("utf-16-le")
  2469. except UnicodeDecodeError:
  2470. pass
  2471. if not isinstance(value, unicode):
  2472. value = value.decode("utf8", "replace")
  2473. return value
  2474. try:
  2475. retVal = applyFunctionRecursively(value, _)
  2476. except Exception:
  2477. singleTimeWarnMessage("there was a problem decoding value '%s' from expected hexadecimal form" % value)
  2478. return retVal
  2479. def extractExpectedValue(value, expected):
  2480. """
  2481. Extracts and returns expected value by a given type
  2482. """
  2483. if expected:
  2484. value = unArrayizeValue(value)
  2485. if isNoneValue(value):
  2486. value = None
  2487. elif expected == EXPECTED.BOOL:
  2488. if isinstance(value, int):
  2489. value = bool(value)
  2490. elif isinstance(value, basestring):
  2491. value = value.strip().lower()
  2492. if value in ("true", "false"):
  2493. value = value == "true"
  2494. elif value in ("1", "-1"):
  2495. value = True
  2496. elif value == "0":
  2497. value = False
  2498. else:
  2499. value = None
  2500. elif expected == EXPECTED.INT:
  2501. if isinstance(value, basestring):
  2502. value = int(value) if value.isdigit() else None
  2503. return value
  2504. def hashDBWrite(key, value, serialize=False):
  2505. """
  2506. Helper function for writing session data to HashDB
  2507. """
  2508. _ = "%s%s%s" % (conf.url or "%s%s" % (conf.hostname, conf.port), key, HASHDB_MILESTONE_VALUE)
  2509. conf.hashDB.write(_, value, serialize)
  2510. def hashDBRetrieve(key, unserialize=False, checkConf=False):
  2511. """
  2512. Helper function for restoring session data from HashDB
  2513. """
  2514. _ = "%s%s%s" % (conf.url or "%s%s" % (conf.hostname, conf.port), key, HASHDB_MILESTONE_VALUE)
  2515. _ = conf.hashDB.retrieve(_, unserialize) if kb.resumeValues and not (checkConf and any([conf.flushSession, conf.freshQueries])) else None
  2516. if not kb.inferenceMode and not kb.fileReadMode and _ and PARTIAL_VALUE_MARKER in _:
  2517. _ = None
  2518. return _
  2519. def resetCookieJar(cookieJar):
  2520. """
  2521. Cleans cookies from a given cookie jar
  2522. """
  2523. if not conf.loadCookies:
  2524. cookieJar.clear()
  2525. else:
  2526. try:
  2527. cookieJar.load(conf.loadCookies)
  2528. cookieJar.clear_expired_cookies()
  2529. except cookielib.LoadError, msg:
  2530. errMsg = "there was a problem loading "
  2531. errMsg += "cookies file ('%s')" % msg
  2532. raise SqlmapGenericException, errMsg
  2533. def prioritySortColumns(columns):
  2534. """
  2535. Sorts given column names by length in ascending order while those containing
  2536. string 'id' go first
  2537. """
  2538. _ = lambda x: x and "id" in x.lower()
  2539. return sorted(sorted(columns, key=len), lambda x, y: -1 if _(x) and not _(y) else 1 if not _(x) and _(y) else 0)
  2540. def getRequestHeader(request, name):
  2541. """
  2542. Solving an issue with an urllib2 Request header case sensitivity
  2543. Reference: http://bugs.python.org/issue2275
  2544. """
  2545. retVal = None
  2546. if request and name:
  2547. retVal = max(request.get_header(_) if name.upper() == _.upper() else None for _ in request.headers.keys())
  2548. return retVal
  2549. def isNumber(value):
  2550. """
  2551. Returns True if the given value is a number-like object
  2552. """
  2553. try:
  2554. float(value)
  2555. except:
  2556. return False
  2557. else:
  2558. return True