#!/usr/bin/env python"""Copyright (c) 2006-2022 sqlmap developers (https://sqlmap.org/)See the file 'LICENSE' for copying permission"""from __future__ import print_functiontry:import syssys.dont_write_bytecode = Truetry:__import__("lib.utils.versioncheck") # this has to be the first non-standard importexcept ImportError:sys.exit("[!] wrong installation detected (missing modules). Visit 'https://github.com/sqlmapproject/sqlmap/#installation' for further details")import bdbimport globimport inspectimport jsonimport loggingimport osimport reimport shutilimport sysimport tempfileimport threadingimport timeimport tracebackimport warningswarnings.filterwarnings(action="ignore", message="Python 2 is no longer supported")warnings.filterwarnings(action="ignore", message=".*was already imported", category=UserWarning)warnings.filterwarnings(action="ignore", message=".*using a very old release", category=UserWarning)warnings.filterwarnings(action="ignore", message=".*default buffer size will be used", category=RuntimeWarning)warnings.filterwarnings(action="ignore", category=UserWarning, module="psycopg2")if "--deprecations" not in sys.argv:warnings.filterwarnings(action="ignore", category=DeprecationWarning)from lib.core.data import loggerfrom lib.core.common import bannerfrom lib.core.common import checkIntegrityfrom lib.core.common import checkPipedInputfrom lib.core.common import createGithubIssuefrom lib.core.common import dataToStdoutfrom lib.core.common import filterNonefrom lib.core.common import getDaysFromLastUpdatefrom lib.core.common import getFileItemsfrom lib.core.common import getSafeExStringfrom lib.core.common import maskSensitiveDatafrom lib.core.common import openFilefrom lib.core.common import setPathsfrom lib.core.common import weAreFrozenfrom lib.core.convert import getUnicodefrom lib.core.common import MKSTEMP_PREFIXfrom lib.core.common import setColorfrom lib.core.common import unhandledExceptionMessagefrom lib.core.data import cmdLineOptionsfrom lib.core.data import conffrom lib.core.data import kbfrom lib.core.datatype import OrderedSetfrom lib.core.compat import LooseVersionfrom lib.core.compat import xrangefrom lib.core.exception import SqlmapBaseExceptionfrom lib.core.exception import SqlmapShellQuitExceptionfrom lib.core.exception import SqlmapSilentQuitExceptionfrom lib.core.exception import SqlmapUserQuitExceptionfrom lib.core.option import initfrom lib.core.option import initOptionsfrom lib.core.patch import dirtyPatchesfrom lib.core.patch import resolveCrossReferencesfrom lib.core.settings import GIT_PAGEfrom lib.core.settings import IS_WINfrom lib.core.settings import LAST_UPDATE_NAGGING_DAYSfrom lib.core.settings import LEGAL_DISCLAIMERfrom lib.core.settings import THREAD_FINALIZATION_TIMEOUTfrom lib.core.settings import UNICODE_ENCODINGfrom lib.core.settings import VERSIONfrom lib.parse.cmdline import cmdLineParserfrom lib.utils.crawler import crawlexcept KeyboardInterrupt:errMsg = "user aborted"if "logger" in globals():logger.critical(errMsg)raise SystemExitelse:import timesys.exit("\r[%s] [CRITICAL] %s" % (time.strftime("%X"), errMsg))def modulePath():"""This will get us the program's directory, even if we are frozenusing py2exe"""try:_ = sys.executable if weAreFrozen() else __file__except NameError:_ = inspect.getsourcefile(modulePath)return getUnicode(os.path.dirname(os.path.realpath(_)), encoding=sys.getfilesystemencoding() or UNICODE_ENCODING)def checkEnvironment():try:os.path.isdir(modulePath())except UnicodeEncodeError:errMsg = "your system does not properly handle non-ASCII paths. "errMsg += "Please move the sqlmap's directory to the other location"logger.critical(errMsg)raise SystemExitif LooseVersion(VERSION) < LooseVersion("1.0"):errMsg = "your runtime environment (e.g. PYTHONPATH) is "errMsg += "broken. Please make sure that you are not running "errMsg += "newer versions of sqlmap with runtime scripts for older "errMsg += "versions"logger.critical(errMsg)raise SystemExit# Patch for pip (import) environmentif "sqlmap.sqlmap" in sys.modules:for _ in ("cmdLineOptions", "conf", "kb"):globals()[_] = getattr(sys.modules["lib.core.data"], _)for _ in ("SqlmapBaseException", "SqlmapShellQuitException", "SqlmapSilentQuitException", "SqlmapUserQuitException"):globals()[_] = getattr(sys.modules["lib.core.exception"], _)def main():"""Main function of sqlmap when running from command line."""try:dirtyPatches()resolveCrossReferences()checkEnvironment()setPaths(modulePath())banner()# Store original command line options for possible later restorationargs = cmdLineParser()cmdLineOptions.update(args.__dict__ if hasattr(args, "__dict__") else args)initOptions(cmdLineOptions)if checkPipedInput():conf.batch = Trueif conf.get("api"):# heavy importsfrom lib.utils.api import StdDbOutfrom lib.utils.api import setRestAPILog# Overwrite system standard output and standard error to write# to an IPC databasesys.stdout = StdDbOut(conf.taskid, messagetype="stdout")sys.stderr = StdDbOut(conf.taskid, messagetype="stderr")setRestAPILog()conf.showTime = TruedataToStdout("[!] legal disclaimer: %s\n\n" % LEGAL_DISCLAIMER, forceOutput=True)dataToStdout("[*] starting @ %s\n\n" % time.strftime("%X /%Y-%m-%d/"), forceOutput=True)init()if not conf.updateAll:# Postponed imports (faster start)if conf.smokeTest:from lib.core.testing import smokeTestos._exitcode = 1 - (smokeTest() or 0)elif conf.vulnTest:from lib.core.testing import vulnTestos._exitcode = 1 - (vulnTest() or 0)else:from lib.controller.controller import startif conf.profile:from lib.core.profiling import profileglobals()["start"] = startprofile()else:try:if conf.crawlDepth and conf.bulkFile:targets = getFileItems(conf.bulkFile)for i in xrange(len(targets)):target = Nonetry:kb.targets = OrderedSet()target = targets[i]if not re.search(r"(?i)\Ahttp[s]*://", target):target = "http://%s" % targetinfoMsg = "starting crawler for target URL '%s' (%d/%d)" % (target, i + 1, len(targets))logger.info(infoMsg)crawl(target)except Exception as ex:if target and not isinstance(ex, SqlmapUserQuitException):errMsg = "problem occurred while crawling '%s' ('%s')" % (target, getSafeExString(ex))logger.error(errMsg)else:raiseelse:if kb.targets:start()else:start()except Exception as ex:os._exitcode = 1if "can't start new thread" in getSafeExString(ex):errMsg = "unable to start new threads. Please check OS (u)limits"logger.critical(errMsg)raise SystemExitelse:raiseexcept SqlmapUserQuitException:if not conf.batch:errMsg = "user quit"logger.error(errMsg)except (SqlmapSilentQuitException, bdb.BdbQuit):passexcept SqlmapShellQuitException:cmdLineOptions.sqlmapShell = Falseexcept SqlmapBaseException as ex:errMsg = getSafeExString(ex)logger.critical(errMsg)os._exitcode = 1raise SystemExitexcept KeyboardInterrupt:print()except EOFError:print()errMsg = "exit"logger.error(errMsg)except SystemExit as ex:os._exitcode = ex.code or 0except:print()errMsg = unhandledExceptionMessage()excMsg = traceback.format_exc()valid = checkIntegrity()os._exitcode = 255if any(_ in excMsg for _ in ("MemoryError", "Cannot allocate memory")):errMsg = "memory exhaustion detected"logger.critical(errMsg)raise SystemExitelif any(_ in excMsg for _ in ("No space left", "Disk quota exceeded", "Disk full while accessing")):errMsg = "no space left on output device"logger.critical(errMsg)raise SystemExitelif any(_ in excMsg for _ in ("The paging file is too small",)):errMsg = "no space left for paging file"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("Access is denied", "subprocess", "metasploit")):errMsg = "permission error occurred while running Metasploit"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("Permission denied", "metasploit")):errMsg = "permission error occurred while using Metasploit"logger.critical(errMsg)raise SystemExitelif "Read-only file system" in excMsg:errMsg = "output device is mounted as read-only"logger.critical(errMsg)raise SystemExitelif "Insufficient system resources" in excMsg:errMsg = "resource exhaustion detected"logger.critical(errMsg)raise SystemExitelif "OperationalError: disk I/O error" in excMsg:errMsg = "I/O error on output device"logger.critical(errMsg)raise SystemExitelif "Violation of BIDI" in excMsg:errMsg = "invalid URL (violation of Bidi IDNA rule - RFC 5893)"logger.critical(errMsg)raise SystemExitelif "Invalid IPv6 URL" in excMsg:errMsg = "invalid URL ('%s')" % excMsg.strip().split('\n')[-1]logger.critical(errMsg)raise SystemExitelif "_mkstemp_inner" in excMsg:errMsg = "there has been a problem while accessing temporary files"logger.critical(errMsg)raise SystemExitelif any(_ in excMsg for _ in ("tempfile.mkdtemp", "tempfile.mkstemp", "tempfile.py")):errMsg = "unable to write to the temporary directory '%s'. " % tempfile.gettempdir()errMsg += "Please make sure that your disk is not full and "errMsg += "that you have sufficient write permissions to "errMsg += "create temporary files and/or directories"logger.critical(errMsg)raise SystemExitelif "Permission denied: '" in excMsg:match = re.search(r"Permission denied: '([^']*)", excMsg)errMsg = "permission error occurred while accessing file '%s'" % match.group(1)logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("twophase", "sqlalchemy")):errMsg = "please update the 'sqlalchemy' package (>= 1.1.11) "errMsg += "(Reference: 'https://qiita.com/tkprof/items/7d7b2d00df9c5f16fffe')"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("scramble_caching_sha2", "TypeError")):errMsg = "please downgrade the 'PyMySQL' package (=< 0.8.1) "errMsg += "(Reference: 'https://github.com/PyMySQL/PyMySQL/issues/700')"logger.critical(errMsg)raise SystemExitelif "must be pinned buffer, not bytearray" in excMsg:errMsg = "error occurred at Python interpreter which "errMsg += "is fixed in 2.7. Please update accordingly "errMsg += "(Reference: 'https://bugs.python.org/issue8104')"logger.critical(errMsg)raise SystemExitelif "hash_randomization" in excMsg:errMsg = "error occurred at Python interpreter which "errMsg += "is fixed in 2.7.3. Please update accordingly "errMsg += "(Reference: 'https://docs.python.org/2/library/sys.html')"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("Resource temporarily unavailable", "os.fork()", "dictionaryAttack")):errMsg = "there has been a problem while running the multiprocessing hash cracking. "errMsg += "Please rerun with option '--threads=1'"logger.critical(errMsg)raise SystemExitelif "can't start new thread" in excMsg:errMsg = "there has been a problem while creating new thread instance. "errMsg += "Please make sure that you are not running too many processes"if not IS_WIN:errMsg += " (or increase the 'ulimit -u' value)"logger.critical(errMsg)raise SystemExitelif "can't allocate read lock" in excMsg:errMsg = "there has been a problem in regular socket operation "errMsg += "('%s')" % excMsg.strip().split('\n')[-1]logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("pymysql", "configparser")):errMsg = "wrong initialization of pymsql detected (using Python3 dependencies)"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("ntlm", "socket.error, err", "SyntaxError")):errMsg = "wrong initialization of python-ntlm detected (using Python2 syntax)"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("drda", "to_bytes")):errMsg = "wrong initialization of drda detected (using Python3 syntax)"logger.critical(errMsg)raise SystemExitelif "'WebSocket' object has no attribute 'status'" in excMsg:errMsg = "wrong websocket library detected"errMsg += " (Reference: 'https://github.com/sqlmapproject/sqlmap/issues/4572#issuecomment-775041086')"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("window = tkinter.Tk()",)):errMsg = "there has been a problem in initialization of GUI interface "errMsg += "('%s')" % excMsg.strip().split('\n')[-1]logger.critical(errMsg)raise SystemExitelif any(_ in excMsg for _ in ("unable to access item 'liveTest'",)):errMsg = "detected usage of files from different versions of sqlmap"logger.critical(errMsg)raise SystemExitelif kb.get("dumpKeyboardInterrupt"):raise SystemExitelif any(_ in excMsg for _ in ("Broken pipe",)):raise SystemExitelif valid is False:errMsg = "code integrity check failed (turning off automatic issue creation). "errMsg += "You should retrieve the latest development version from official GitHub "errMsg += "repository at '%s'" % GIT_PAGElogger.critical(errMsg)print()dataToStdout(excMsg)raise SystemExitelif any(_ in excMsg for _ in ("tamper/", "waf/")):logger.critical(errMsg)print()dataToStdout(excMsg)raise SystemExitelif any(_ in excMsg for _ in ("ImportError", "ModuleNotFoundError", "Can't find file for module", "SAXReaderNotAvailable", "source code string cannot contain null bytes", "No module named", "tp_name field")):errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip()logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("SyntaxError: Non-ASCII character", ".py on line", "but no encoding declared")):errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip()logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("No such file", "_'")):errMsg = "corrupted installation detected ('%s'). " % excMsg.strip().split('\n')[-1]errMsg += "You should retrieve the latest development version from official GitHub "errMsg += "repository at '%s'" % GIT_PAGElogger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("No such file", "sqlmap.conf", "Test")):errMsg = "you are trying to run (hidden) development tests inside the production environment"logger.critical(errMsg)raise SystemExitelif all(_ in excMsg for _ in ("HTTPNtlmAuthHandler", "'str' object has no attribute 'decode'")):errMsg = "package 'python-ntlm' has a known compatibility issue with the "errMsg += "Python 3 (Reference: 'https://github.com/mullender/python-ntlm/pull/61')"logger.critical(errMsg)raise SystemExitelif "'DictObject' object has no attribute '" in excMsg and all(_ in errMsg for _ in ("(fingerprinted)", "(identified)")):errMsg = "there has been a problem in enumeration. "errMsg += "Because of a considerable chance of false-positive case "errMsg += "you are advised to rerun with switch '--flush-session'"logger.critical(errMsg)raise SystemExitelif "AttributeError: 'module' object has no attribute 'F_GETFD'" in excMsg:errMsg = "invalid runtime (\"%s\") " % excMsg.split("Error: ")[-1].strip()errMsg += "(Reference: 'https://stackoverflow.com/a/38841364' & 'https://bugs.python.org/issue24944#msg249231')"logger.critical(errMsg)raise SystemExitelif "bad marshal data (unknown type code)" in excMsg:match = re.search(r"\s*(.+)\s+ValueError", excMsg)errMsg = "one of your .pyc files are corrupted%s" % (" ('%s')" % match.group(1) if match else "")errMsg += ". Please delete .pyc files on your system to fix the problem"logger.critical(errMsg)raise SystemExitfor match in re.finditer(r'File "(.+?)", line', excMsg):file_ = match.group(1)try:file_ = os.path.relpath(file_, os.path.dirname(__file__))except ValueError:passfile_ = file_.replace("\\", '/')if "../" in file_:file_ = re.sub(r"(\.\./)+", '/', file_)else:file_ = file_.lstrip('/')file_ = re.sub(r"/{2,}", '/', file_)excMsg = excMsg.replace(match.group(1), file_)errMsg = maskSensitiveData(errMsg)excMsg = maskSensitiveData(excMsg)if conf.get("api") or not valid:logger.critical("%s\n%s" % (errMsg, excMsg))else:logger.critical(errMsg)dataToStdout("%s\n" % setColor(excMsg.strip(), level=logging.CRITICAL))createGithubIssue(errMsg, excMsg)finally:kb.threadContinue = Falseif getDaysFromLastUpdate() > LAST_UPDATE_NAGGING_DAYS:warnMsg = "your sqlmap version is outdated"logger.warn(warnMsg)if conf.get("showTime"):dataToStdout("\n[*] ending @ %s\n\n" % time.strftime("%X /%Y-%m-%d/"), forceOutput=True)kb.threadException = Trueif kb.get("tempDir"):for prefix in (MKSTEMP_PREFIX.IPC, MKSTEMP_PREFIX.TESTING, MKSTEMP_PREFIX.COOKIE_JAR, MKSTEMP_PREFIX.BIG_ARRAY):for filepath in glob.glob(os.path.join(kb.tempDir, "%s*" % prefix)):try:os.remove(filepath)except OSError:passif not filterNone(filepath for filepath in glob.glob(os.path.join(kb.tempDir, '*')) if not any(filepath.endswith(_) for _ in (".lock", ".exe", ".so", '_'))): # ignore junk filestry:shutil.rmtree(kb.tempDir, ignore_errors=True)except OSError:passif conf.get("hashDB"):conf.hashDB.flush(True)conf.hashDB.close() # NOTE: because of PyPyif conf.get("harFile"):try:with openFile(conf.harFile, "w+b") as f:json.dump(conf.httpCollector.obtain(), fp=f, indent=4, separators=(',', ': '))except SqlmapBaseException as ex:errMsg = getSafeExString(ex)logger.critical(errMsg)if conf.get("api"):conf.databaseCursor.disconnect()if conf.get("dumper"):conf.dumper.flush()# short delay for thread finalization_ = time.time()while threading.active_count() > 1 and (time.time() - _) > THREAD_FINALIZATION_TIMEOUT:time.sleep(0.01)if cmdLineOptions.get("sqlmapShell"):cmdLineOptions.clear()conf.clear()kb.clear()conf.disableBanner = Truemain()if __name__ == "__main__":try:main()except KeyboardInterrupt:passexcept SystemExit:raiseexcept:traceback.print_exc()finally:# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-programif threading.active_count() > 1:os._exit(getattr(os, "_exitcode", 0))else:sys.exit(getattr(os, "_exitcode", 0))else:# cancelling postponed imports (because of Travis CI checks)__import__("lib.controller.controller")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。