|
5 | 5 | See the file 'doc/COPYING' for copying permission |
6 | 6 | """ |
7 | 7 |
|
| 8 | +import bdb |
| 9 | +import os |
8 | 10 | import sys |
| 11 | +import time |
| 12 | +import traceback |
| 13 | +import warnings |
9 | 14 |
|
10 | | -PYVERSION = sys.version.split()[0] |
| 15 | +warnings.filterwarnings(action="ignore", message=".*was already imported", category=UserWarning) |
| 16 | +warnings.filterwarnings(action="ignore", category=DeprecationWarning) |
11 | 17 |
|
12 | | -if PYVERSION >= "3" or PYVERSION < "2.6": |
13 | | - exit("[CRITICAL] incompatible Python version detected ('%s'). For successfully running sqlmap you'll have to use version 2.6 or 2.7 (visit 'http://www.python.org/download/')" % PYVERSION) |
14 | | -elif __name__ == "__main__": |
15 | | - from _sqlmap import main |
16 | | - from lib.controller.controller import start # needed for proper working of --profile switch |
| 18 | +from lib.utils import versioncheck |
| 19 | +from lib.controller.controller import start |
| 20 | +from lib.core.common import banner |
| 21 | +from lib.core.common import dataToStdout |
| 22 | +from lib.core.common import getUnicode |
| 23 | +from lib.core.common import setPaths |
| 24 | +from lib.core.common import weAreFrozen |
| 25 | +from lib.core.data import cmdLineOptions |
| 26 | +from lib.core.data import conf |
| 27 | +from lib.core.data import kb |
| 28 | +from lib.core.data import logger |
| 29 | +from lib.core.data import paths |
| 30 | +from lib.core.common import unhandledExceptionMessage |
| 31 | +from lib.core.exception import SqlmapBaseException |
| 32 | +from lib.core.exception import SqlmapSilentQuitException |
| 33 | +from lib.core.exception import SqlmapUserQuitException |
| 34 | +from lib.core.option import initOptions |
| 35 | +from lib.core.option import init |
| 36 | +from lib.core.profiling import profile |
| 37 | +from lib.core.settings import LEGAL_DISCLAIMER |
| 38 | +from lib.core.testing import smokeTest |
| 39 | +from lib.core.testing import liveTest |
| 40 | +from lib.parse.cmdline import cmdLineParser |
| 41 | +from lib.utils.api import setRestAPILog |
| 42 | +from lib.utils.api import StdDbOut |
| 43 | + |
| 44 | +def modulePath(): |
| 45 | + """ |
| 46 | + This will get us the program's directory, even if we are frozen |
| 47 | + using py2exe |
| 48 | + """ |
| 49 | + |
| 50 | + return os.path.dirname(getUnicode(sys.executable if weAreFrozen() else __file__, sys.getfilesystemencoding())) |
| 51 | + |
| 52 | +def main(): |
| 53 | + """ |
| 54 | + Main function of sqlmap when running from command line. |
| 55 | + """ |
| 56 | + |
| 57 | + try: |
| 58 | + paths.SQLMAP_ROOT_PATH = modulePath() |
| 59 | + setPaths() |
| 60 | + |
| 61 | + # Store original command line options for possible later restoration |
| 62 | + cmdLineOptions.update(cmdLineParser().__dict__) |
| 63 | + initOptions(cmdLineOptions) |
| 64 | + |
| 65 | + if hasattr(conf, "api"): |
| 66 | + # Overwrite system standard output and standard error to write |
| 67 | + # to an IPC database |
| 68 | + sys.stdout = StdDbOut(conf.taskid, messagetype="stdout") |
| 69 | + sys.stderr = StdDbOut(conf.taskid, messagetype="stderr") |
| 70 | + setRestAPILog() |
| 71 | + |
| 72 | + banner() |
| 73 | + |
| 74 | + dataToStdout("[!] legal disclaimer: %s\n\n" % LEGAL_DISCLAIMER, forceOutput=True) |
| 75 | + dataToStdout("[*] starting at %s\n\n" % time.strftime("%X"), forceOutput=True) |
| 76 | + |
| 77 | + init() |
| 78 | + |
| 79 | + if conf.profile: |
| 80 | + profile() |
| 81 | + elif conf.smokeTest: |
| 82 | + smokeTest() |
| 83 | + elif conf.liveTest: |
| 84 | + liveTest() |
| 85 | + else: |
| 86 | + start() |
| 87 | + |
| 88 | + except SqlmapUserQuitException: |
| 89 | + errMsg = "user quit" |
| 90 | + logger.error(errMsg) |
| 91 | + |
| 92 | + except (SqlmapSilentQuitException, bdb.BdbQuit): |
| 93 | + pass |
| 94 | + |
| 95 | + except SqlmapBaseException as e: |
| 96 | + e = getUnicode(e) |
| 97 | + logger.critical(e) |
| 98 | + sys.exit(1) |
| 99 | + |
| 100 | + except KeyboardInterrupt: |
| 101 | + print |
| 102 | + errMsg = "user aborted" |
| 103 | + logger.error(errMsg) |
| 104 | + |
| 105 | + except EOFError: |
| 106 | + print |
| 107 | + errMsg = "exit" |
| 108 | + logger.error(errMsg) |
| 109 | + |
| 110 | + except SystemExit: |
| 111 | + pass |
| 112 | + |
| 113 | + except: |
| 114 | + print |
| 115 | + errMsg = unhandledExceptionMessage() |
| 116 | + logger.critical(errMsg) |
| 117 | + traceback.print_exc() |
| 118 | + |
| 119 | + finally: |
| 120 | + dataToStdout("\n[*] shutting down at %s\n\n" % time.strftime("%X"), forceOutput=True) |
| 121 | + |
| 122 | + kb.threadContinue = False |
| 123 | + kb.threadException = True |
| 124 | + |
| 125 | + if conf.get("hashDB"): |
| 126 | + try: |
| 127 | + conf.hashDB.flush(True) |
| 128 | + except KeyboardInterrupt: |
| 129 | + pass |
| 130 | + |
| 131 | + if hasattr(conf, "api"): |
| 132 | + try: |
| 133 | + conf.database_cursor.disconnect() |
| 134 | + except KeyboardInterrupt: |
| 135 | + pass |
| 136 | + |
| 137 | + # Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program |
| 138 | + if conf.get("threads", 0) > 1 or conf.get("dnsServer"): |
| 139 | + os._exit(0) |
| 140 | + |
| 141 | +if __name__ == "__main__": |
17 | 142 | main() |
0 commit comments