sqlmap/lib/core/shell.py

152 lines
4.8 KiB
Python
Raw Permalink Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2008-10-15 19:38:22 +04:00
"""
2022-01-03 13:30:34 +03:00
Copyright (c) 2006-2022 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2008-10-15 19:38:22 +04:00
"""
import atexit
import os
from lib.core import readlineng as readline
from lib.core.common import getSafeExString
2011-01-27 15:38:39 +03:00
from lib.core.data import logger
2008-10-15 19:38:22 +04:00
from lib.core.data import paths
2014-09-16 11:07:31 +04:00
from lib.core.enums import AUTOCOMPLETE_TYPE
from lib.core.enums import OS
2019-05-16 13:15:51 +03:00
from lib.core.settings import IS_WIN
2014-09-16 16:12:43 +04:00
from lib.core.settings import MAX_HISTORY_LENGTH
2015-11-11 17:55:28 +03:00
try:
import rlcompleter
class CompleterNG(rlcompleter.Completer):
def global_matches(self, text):
"""
Compute matches when text is a simple name.
Return a list of all names currently defined in self.namespace
that match.
"""
matches = []
n = len(text)
for ns in (self.namespace,):
for word in ns:
if word[:n] == text:
matches.append(word)
return matches
except:
readline._readline = None
2014-09-16 16:12:43 +04:00
def readlineAvailable():
"""
Check if the readline is available. By default
it is not in Python default installation on Windows
"""
return readline._readline is not None
def clearHistory():
if not readlineAvailable():
return
readline.clear_history()
2008-10-15 19:38:22 +04:00
2014-09-16 17:17:50 +04:00
def saveHistory(completion=None):
2014-09-16 16:12:43 +04:00
try:
2018-10-30 13:13:09 +03:00
if not readlineAvailable():
return
if completion == AUTOCOMPLETE_TYPE.SQL:
historyPath = paths.SQL_SHELL_HISTORY
elif completion == AUTOCOMPLETE_TYPE.OS:
historyPath = paths.OS_SHELL_HISTORY
elif completion == AUTOCOMPLETE_TYPE.API:
historyPath = paths.API_SHELL_HISTORY
else:
historyPath = paths.SQLMAP_SHELL_HISTORY
try:
with open(historyPath, "w+"):
pass
except:
2014-09-17 20:29:01 +04:00
pass
2014-09-16 16:12:43 +04:00
2018-10-30 13:13:09 +03:00
readline.set_history_length(MAX_HISTORY_LENGTH)
try:
readline.write_history_file(historyPath)
except IOError as ex:
warnMsg = "there was a problem writing the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
logger.warning(warnMsg)
2018-10-30 13:13:09 +03:00
except KeyboardInterrupt:
pass
2008-10-15 19:38:22 +04:00
2014-09-16 17:17:50 +04:00
def loadHistory(completion=None):
2014-09-16 16:12:43 +04:00
if not readlineAvailable():
return
2014-09-16 17:17:50 +04:00
clearHistory()
if completion == AUTOCOMPLETE_TYPE.SQL:
historyPath = paths.SQL_SHELL_HISTORY
elif completion == AUTOCOMPLETE_TYPE.OS:
historyPath = paths.OS_SHELL_HISTORY
2018-06-21 00:04:58 +03:00
elif completion == AUTOCOMPLETE_TYPE.API:
historyPath = paths.API_SHELL_HISTORY
2014-09-16 17:17:50 +04:00
else:
historyPath = paths.SQLMAP_SHELL_HISTORY
2008-10-15 19:38:22 +04:00
if os.path.exists(historyPath):
2011-01-27 15:38:39 +03:00
try:
readline.read_history_file(historyPath)
except IOError as ex:
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
logger.warning(warnMsg)
2019-05-16 13:15:51 +03:00
except UnicodeError:
if IS_WIN:
warnMsg = "there was a problem loading the history file '%s'. " % historyPath
warnMsg += "More info can be found at 'https://github.com/pyreadline/pyreadline/issues/30'"
logger.warning(warnMsg)
2008-10-15 19:38:22 +04:00
2014-09-16 16:12:43 +04:00
def autoCompletion(completion=None, os=None, commands=None):
if not readlineAvailable():
2008-10-15 19:38:22 +04:00
return
2014-09-16 11:07:31 +04:00
if completion == AUTOCOMPLETE_TYPE.OS:
2014-09-16 16:12:43 +04:00
if os == OS.WINDOWS:
# Reference: http://en.wikipedia.org/wiki/List_of_DOS_commands
completer = CompleterNG({
2020-01-03 16:03:01 +03:00
"attrib": None, "copy": None, "del": None,
"dir": None, "echo": None, "fc": None,
"label": None, "md": None, "mem": None,
"move": None, "net": None, "netstat -na": None,
2020-01-03 16:03:01 +03:00
"tree": None, "truename": None, "type": None,
"ver": None, "vol": None, "xcopy": None,
})
else:
# Reference: http://en.wikipedia.org/wiki/List_of_Unix_commands
completer = CompleterNG({
2020-01-03 16:03:01 +03:00
"cat": None, "chmod": None, "chown": None,
"cp": None, "cut": None, "date": None, "df": None,
"diff": None, "du": None, "echo": None, "env": None,
"file": None, "find": None, "free": None, "grep": None,
"id": None, "ifconfig": None, "ls": None, "mkdir": None,
"mv": None, "netstat": None, "pwd": None, "rm": None,
"uname": None, "whoami": None,
})
2008-10-15 19:38:22 +04:00
readline.set_completer(completer.complete)
readline.parse_and_bind("tab: complete")
2008-10-15 19:38:22 +04:00
2014-09-16 16:12:43 +04:00
elif commands:
completer = CompleterNG(dict(((_, None) for _ in commands)))
readline.set_completer_delims(' ')
readline.set_completer(completer.complete)
readline.parse_and_bind("tab: complete")
2014-09-16 17:17:50 +04:00
loadHistory(completion)
atexit.register(saveHistory, completion)