Refactoring KeyboardInterrupt mess

This commit is contained in:
Miroslav Stampar 2018-12-17 15:00:57 +01:00
parent c497aa98ed
commit 28e7c8f378
3 changed files with 136 additions and 154 deletions

View File

@ -19,7 +19,7 @@ from lib.core.enums import DBMS_DIRECTORY_NAME
from lib.core.enums import OS from lib.core.enums import OS
# sqlmap version (<major>.<minor>.<month>.<monthly commit>) # sqlmap version (<major>.<minor>.<month>.<monthly commit>)
VERSION = "1.2.12.21" VERSION = "1.2.12.22"
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable" TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34} TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE) VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

284
sqlmap.py
View File

@ -172,10 +172,7 @@ def main():
except SqlmapUserQuitException: except SqlmapUserQuitException:
errMsg = "user quit" errMsg = "user quit"
try: logger.error(errMsg)
logger.error(errMsg)
except KeyboardInterrupt:
pass
except (SqlmapSilentQuitException, bdb.BdbQuit): except (SqlmapSilentQuitException, bdb.BdbQuit):
pass pass
@ -185,10 +182,8 @@ def main():
except SqlmapBaseException as ex: except SqlmapBaseException as ex:
errMsg = getSafeExString(ex) errMsg = getSafeExString(ex)
try: logger.critical(errMsg)
logger.critical(errMsg)
except KeyboardInterrupt:
pass
raise SystemExit raise SystemExit
except KeyboardInterrupt: except KeyboardInterrupt:
@ -196,12 +191,9 @@ def main():
except EOFError: except EOFError:
print print
errMsg = "exit"
try: errMsg = "exit"
logger.error(errMsg) logger.error(errMsg)
except KeyboardInterrupt:
pass
except SystemExit: except SystemExit:
pass pass
@ -212,140 +204,136 @@ def main():
excMsg = traceback.format_exc() excMsg = traceback.format_exc()
valid = checkIntegrity() valid = checkIntegrity()
try: if valid is False:
if valid is False: errMsg = "code integrity check failed (turning off automatic issue creation). "
errMsg = "code integrity check failed (turning off automatic issue creation). " errMsg += "You should retrieve the latest development version from official GitHub "
errMsg += "You should retrieve the latest development version from official GitHub " errMsg += "repository at '%s'" % GIT_PAGE
errMsg += "repository at '%s'" % GIT_PAGE logger.critical(errMsg)
logger.critical(errMsg) print
print dataToStdout(excMsg)
dataToStdout(excMsg) raise SystemExit
raise SystemExit
elif any(_ in excMsg for _ in ("tamper/", "waf/")): elif any(_ in excMsg for _ in ("tamper/", "waf/")):
logger.critical(errMsg) logger.critical(errMsg)
print print
dataToStdout(excMsg) dataToStdout(excMsg)
raise SystemExit raise SystemExit
elif any(_ in excMsg for _ in ("ImportError", "Can't find file for module")): elif any(_ in excMsg for _ in ("ImportError", "Can't find file for module")):
errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip() errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip()
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "MemoryError" in excMsg: elif "MemoryError" in excMsg:
errMsg = "memory exhaustion detected" errMsg = "memory exhaustion detected"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif any(_ in excMsg for _ in ("No space left", "Disk quota exceeded")): elif any(_ in excMsg for _ in ("No space left", "Disk quota exceeded")):
errMsg = "no space left on output device" errMsg = "no space left on output device"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif all(_ in excMsg for _ in ("No such file", "_'", "self.get_prog_name()")): elif all(_ in excMsg for _ in ("No such file", "_'", "self.get_prog_name()")):
errMsg = "corrupted installation detected ('%s'). " % excMsg.strip().split('\n')[-1] errMsg = "corrupted installation detected ('%s'). " % excMsg.strip().split('\n')[-1]
errMsg += "You should retrieve the latest development version from official GitHub " errMsg += "You should retrieve the latest development version from official GitHub "
errMsg += "repository at '%s'" % GIT_PAGE errMsg += "repository at '%s'" % GIT_PAGE
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "Read-only file system" in excMsg: elif "Read-only file system" in excMsg:
errMsg = "output device is mounted as read-only" errMsg = "output device is mounted as read-only"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "OperationalError: disk I/O error" in excMsg: elif "OperationalError: disk I/O error" in excMsg:
errMsg = "I/O error on output device" errMsg = "I/O error on output device"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "Violation of BIDI" in excMsg: elif "Violation of BIDI" in excMsg:
errMsg = "invalid URL (violation of Bidi IDNA rule - RFC 5893)" errMsg = "invalid URL (violation of Bidi IDNA rule - RFC 5893)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "_mkstemp_inner" in excMsg: elif "_mkstemp_inner" in excMsg:
errMsg = "there has been a problem while accessing temporary files" errMsg = "there has been a problem while accessing temporary files"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif all(_ in excMsg for _ in ("twophase", "sqlalchemy")): elif all(_ in excMsg for _ in ("twophase", "sqlalchemy")):
errMsg = "please update the 'sqlalchemy' package (>= 1.1.11) " errMsg = "please update the 'sqlalchemy' package (>= 1.1.11) "
errMsg += "(Reference: https://qiita.com/tkprof/items/7d7b2d00df9c5f16fffe)" errMsg += "(Reference: https://qiita.com/tkprof/items/7d7b2d00df9c5f16fffe)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif all(_ in excMsg for _ in ("scramble_caching_sha2", "TypeError")): elif all(_ in excMsg for _ in ("scramble_caching_sha2", "TypeError")):
errMsg = "please downgrade the 'PyMySQL' package (=< 0.8.1) " errMsg = "please downgrade the 'PyMySQL' package (=< 0.8.1) "
errMsg += "(Reference: https://github.com/PyMySQL/PyMySQL/issues/700)" errMsg += "(Reference: https://github.com/PyMySQL/PyMySQL/issues/700)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "must be pinned buffer, not bytearray" in excMsg: elif "must be pinned buffer, not bytearray" in excMsg:
errMsg = "error occurred at Python interpreter which " errMsg = "error occurred at Python interpreter which "
errMsg += "is fixed in 2.7.x. Please update accordingly " errMsg += "is fixed in 2.7.x. Please update accordingly "
errMsg += "(Reference: https://bugs.python.org/issue8104)" errMsg += "(Reference: https://bugs.python.org/issue8104)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "can't start new thread" in excMsg: elif "can't start new thread" in excMsg:
errMsg = "there has been a problem while creating new thread instance. " errMsg = "there has been a problem while creating new thread instance. "
errMsg += "Please make sure that you are not running too many processes" errMsg += "Please make sure that you are not running too many processes"
if not IS_WIN: if not IS_WIN:
errMsg += " (or increase the 'ulimit -u' value)" errMsg += " (or increase the 'ulimit -u' value)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "'DictObject' object has no attribute '" in excMsg and all(_ in errMsg for _ in ("(fingerprinted)", "(identified)")): elif "'DictObject' object has no attribute '" in excMsg and all(_ in errMsg for _ in ("(fingerprinted)", "(identified)")):
errMsg = "there has been a problem in enumeration. " errMsg = "there has been a problem in enumeration. "
errMsg += "Because of a considerable chance of false-positive case " errMsg += "Because of a considerable chance of false-positive case "
errMsg += "you are advised to rerun with switch '--flush-session'" errMsg += "you are advised to rerun with switch '--flush-session'"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif all(_ in excMsg for _ in ("pymysql", "configparser")): elif all(_ in excMsg for _ in ("pymysql", "configparser")):
errMsg = "wrong initialization of pymsql detected (using Python3 dependencies)" errMsg = "wrong initialization of pymsql detected (using Python3 dependencies)"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif "bad marshal data (unknown type code)" in excMsg: elif "bad marshal data (unknown type code)" in excMsg:
match = re.search(r"\s*(.+)\s+ValueError", 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 = "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" errMsg += ". Please delete .pyc files on your system to fix the problem"
logger.critical(errMsg) logger.critical(errMsg)
raise SystemExit raise SystemExit
elif kb.get("dumpKeyboardInterrupt"): elif kb.get("dumpKeyboardInterrupt"):
raise SystemExit raise SystemExit
elif any(_ in excMsg for _ in ("Broken pipe",)): elif any(_ in excMsg for _ in ("Broken pipe",)):
raise SystemExit raise SystemExit
for match in re.finditer(r'File "(.+?)", line', excMsg): for match in re.finditer(r'File "(.+?)", line', excMsg):
file_ = match.group(1) file_ = match.group(1)
file_ = os.path.relpath(file_, os.path.dirname(__file__)) file_ = os.path.relpath(file_, os.path.dirname(__file__))
file_ = file_.replace("\\", '/') file_ = file_.replace("\\", '/')
if "../" in file_: if "../" in file_:
file_ = re.sub(r"(\.\./)+", '/', 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: else:
logger.critical(errMsg) file_ = file_.lstrip('/')
kb.stickyLevel = logging.CRITICAL file_ = re.sub(r"/{2,}", '/', file_)
dataToStdout(excMsg) excMsg = excMsg.replace(match.group(1), file_)
createGithubIssue(errMsg, excMsg)
except KeyboardInterrupt: errMsg = maskSensitiveData(errMsg)
pass excMsg = maskSensitiveData(excMsg)
if conf.get("api") or not valid:
logger.critical("%s\n%s" % (errMsg, excMsg))
else:
logger.critical(errMsg)
kb.stickyLevel = logging.CRITICAL
dataToStdout(excMsg)
createGithubIssue(errMsg, excMsg)
finally: finally:
kb.threadContinue = False kb.threadContinue = False
@ -366,45 +354,39 @@ def main():
shutil.rmtree(kb.tempDir, ignore_errors=True) shutil.rmtree(kb.tempDir, ignore_errors=True)
if conf.get("hashDB"): if conf.get("hashDB"):
try: conf.hashDB.flush(True)
conf.hashDB.flush(True)
except KeyboardInterrupt:
pass
if conf.get("harFile"): if conf.get("harFile"):
with openFile(conf.harFile, "w+b") as f: with openFile(conf.harFile, "w+b") as f:
json.dump(conf.httpCollector.obtain(), fp=f, indent=4, separators=(',', ': ')) json.dump(conf.httpCollector.obtain(), fp=f, indent=4, separators=(',', ': '))
if conf.get("api"): if conf.get("api"):
try: conf.databaseCursor.disconnect()
conf.databaseCursor.disconnect()
except KeyboardInterrupt:
pass
if conf.get("dumper"): if conf.get("dumper"):
conf.dumper.flush() conf.dumper.flush()
# short delay for thread finalization # short delay for thread finalization
try: _ = time.time()
_ = time.time() while threading.activeCount() > 1 and (time.time() - _) > THREAD_FINALIZATION_TIMEOUT:
while threading.activeCount() > 1 and (time.time() - _) > THREAD_FINALIZATION_TIMEOUT: time.sleep(0.01)
time.sleep(0.01)
if cmdLineOptions.get("sqlmapShell"): if cmdLineOptions.get("sqlmapShell"):
cmdLineOptions.clear() cmdLineOptions.clear()
conf.clear() conf.clear()
kb.clear() kb.clear()
conf.disableBanner = True conf.disableBanner = True
main() main()
except KeyboardInterrupt:
pass
finally:
# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
if threading.activeCount() > 1:
os._exit(0)
if __name__ == "__main__": if __name__ == "__main__":
main() try:
main()
except KeyboardInterrupt:
pass
finally:
# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
if threading.activeCount() > 1:
os._exit(0)
else: else:
# cancelling postponed imports (because of Travis CI checks) # cancelling postponed imports (because of Travis CI checks)
from lib.controller.controller import start from lib.controller.controller import start

View File

@ -49,7 +49,7 @@ c8c386d644d57c659d74542f5f57f632 lib/core/patch.py
0c3eef46bdbf87e29a3f95f90240d192 lib/core/replication.py 0c3eef46bdbf87e29a3f95f90240d192 lib/core/replication.py
a7db43859b61569b601b97f187dd31c5 lib/core/revision.py a7db43859b61569b601b97f187dd31c5 lib/core/revision.py
fcb74fcc9577523524659ec49e2e964b lib/core/session.py fcb74fcc9577523524659ec49e2e964b lib/core/session.py
8e45f357b6d73d128267f3b66fe5e081 lib/core/settings.py 7d446786d9c7f49c591f382079d39787 lib/core/settings.py
a971ce157d04de96ba6e710d3d38a9a8 lib/core/shell.py a971ce157d04de96ba6e710d3d38a9a8 lib/core/shell.py
a7edc9250d13af36ac0108f259859c19 lib/core/subprocessng.py a7edc9250d13af36ac0108f259859c19 lib/core/subprocessng.py
1581be48127a3a7a9fd703359b6e7567 lib/core/target.py 1581be48127a3a7a9fd703359b6e7567 lib/core/target.py
@ -234,7 +234,7 @@ ec2ba8c757ac96425dcd2b97970edd3a shell/stagers/stager.asp_
0c48ddb1feb7e38a951ef05a0d48e032 shell/stagers/stager.jsp_ 0c48ddb1feb7e38a951ef05a0d48e032 shell/stagers/stager.jsp_
2f9e459a4cf6a58680978cdce5ff7971 shell/stagers/stager.php_ 2f9e459a4cf6a58680978cdce5ff7971 shell/stagers/stager.php_
cd90da0474d7b1a67d7b40d208493375 sqlmapapi.py cd90da0474d7b1a67d7b40d208493375 sqlmapapi.py
55ba3999ab8819e0d34ca075d46fa9dd sqlmap.py 82f2326ec9cc8719859266f72c19f9b3 sqlmap.py
523dab9e1093eb59264c6beb366b255a tamper/0x2char.py 523dab9e1093eb59264c6beb366b255a tamper/0x2char.py
3a1697585ae4e7bf315e9dda97d6f321 tamper/apostrophemask.py 3a1697585ae4e7bf315e9dda97d6f321 tamper/apostrophemask.py
d7a119a74be9b385ee3884fb5e6af041 tamper/apostrophenullencode.py d7a119a74be9b385ee3884fb5e6af041 tamper/apostrophenullencode.py