2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
"""
|
2014-01-13 21:24:49 +04:00
|
|
|
Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import atexit
|
|
|
|
import os
|
|
|
|
import rlcompleter
|
|
|
|
|
|
|
|
from lib.core import readlineng as readline
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
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
|
2011-04-23 20:25:09 +04:00
|
|
|
from lib.core.enums import OS
|
2014-09-16 16:12:43 +04:00
|
|
|
from lib.core.settings import MAX_HISTORY_LENGTH
|
|
|
|
|
|
|
|
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
|
|
|
if not readlineAvailable():
|
|
|
|
return
|
|
|
|
|
2014-09-16 17:17:50 +04:00
|
|
|
if completion == AUTOCOMPLETE_TYPE.SQL:
|
|
|
|
historyPath = paths.SQL_SHELL_HISTORY
|
|
|
|
elif completion == AUTOCOMPLETE_TYPE.OS:
|
|
|
|
historyPath = paths.OS_SHELL_HISTORY
|
|
|
|
else:
|
|
|
|
historyPath = paths.SQLMAP_SHELL_HISTORY
|
|
|
|
|
2014-09-16 16:12:43 +04:00
|
|
|
try:
|
2014-09-17 12:28:18 +04:00
|
|
|
with open(historyPath, "w+") as f:
|
2014-09-17 20:29:01 +04:00
|
|
|
pass
|
2014-09-16 16:12:43 +04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
readline.set_history_length(MAX_HISTORY_LENGTH)
|
2014-09-17 20:25:24 +04:00
|
|
|
try:
|
|
|
|
readline.write_history_file(historyPath)
|
|
|
|
except IOError, msg:
|
|
|
|
warnMsg = "there was a problem writing the history file '%s' (%s)" % (historyPath, msg)
|
|
|
|
logger.warn(warnMsg)
|
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
|
|
|
|
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, msg:
|
2011-01-30 19:34:13 +03:00
|
|
|
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, msg)
|
2011-01-27 15:38:39 +03:00
|
|
|
logger.warn(warnMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2013-01-10 14:54:07 +04:00
|
|
|
for ns in (self.namespace,):
|
2009-04-28 03:05:11 +04:00
|
|
|
for word in ns:
|
2008-10-15 19:38:22 +04:00
|
|
|
if word[:n] == text:
|
|
|
|
matches.append(word)
|
|
|
|
|
|
|
|
return matches
|
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:
|
2009-04-22 15:48:07 +04:00
|
|
|
# Reference: http://en.wikipedia.org/wiki/List_of_DOS_commands
|
|
|
|
completer = CompleterNG({
|
|
|
|
"copy": None, "del": None, "dir": None,
|
|
|
|
"echo": None, "md": None, "mem": None,
|
|
|
|
"move": None, "net": None, "netstat -na": None,
|
|
|
|
"ver": None, "xcopy": None, "whoami": None,
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Reference: http://en.wikipedia.org/wiki/List_of_Unix_commands
|
|
|
|
completer = CompleterNG({
|
2012-02-22 14:40:11 +04:00
|
|
|
"cp": None, "rm": None, "ls": None,
|
2009-04-22 15:48:07 +04:00
|
|
|
"echo": None, "mkdir": None, "free": None,
|
|
|
|
"mv": None, "ifconfig": None, "netstat -natu": None,
|
|
|
|
"pwd": None, "uname": None, "id": None,
|
|
|
|
})
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2011-06-24 17:05:13 +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)
|