PageRenderTime 75ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/core/common.py

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