Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/core/shell.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import atexit
9
import os
10
11
from lib.core import readlineng as readline
12
from lib.core.common import getSafeExString
13
from lib.core.data import logger
14
from lib.core.data import paths
15
from lib.core.enums import AUTOCOMPLETE_TYPE
16
from lib.core.enums import OS
17
from lib.core.settings import IS_WIN
18
from lib.core.settings import MAX_HISTORY_LENGTH
19
20
try:
21
import rlcompleter
22
23
class CompleterNG(rlcompleter.Completer):
24
def global_matches(self, text):
25
"""
26
Compute matches when text is a simple name.
27
Return a list of all names currently defined in self.namespace
28
that match.
29
"""
30
31
matches = []
32
n = len(text)
33
34
for ns in (self.namespace,):
35
for word in ns:
36
if word[:n] == text:
37
matches.append(word)
38
39
return matches
40
except:
41
readline._readline = None
42
43
def readlineAvailable():
44
"""
45
Check if the readline is available. By default
46
it is not in Python default installation on Windows
47
"""
48
49
return readline._readline is not None
50
51
def clearHistory():
52
if not readlineAvailable():
53
return
54
55
readline.clear_history()
56
57
def saveHistory(completion=None):
58
try:
59
if not readlineAvailable():
60
return
61
62
if completion == AUTOCOMPLETE_TYPE.SQL:
63
historyPath = paths.SQL_SHELL_HISTORY
64
elif completion == AUTOCOMPLETE_TYPE.OS:
65
historyPath = paths.OS_SHELL_HISTORY
66
elif completion == AUTOCOMPLETE_TYPE.API:
67
historyPath = paths.API_SHELL_HISTORY
68
else:
69
historyPath = paths.SQLMAP_SHELL_HISTORY
70
71
try:
72
with open(historyPath, "w+"):
73
pass
74
except:
75
pass
76
77
readline.set_history_length(MAX_HISTORY_LENGTH)
78
try:
79
readline.write_history_file(historyPath)
80
except IOError as ex:
81
warnMsg = "there was a problem writing the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
82
logger.warning(warnMsg)
83
except KeyboardInterrupt:
84
pass
85
86
def loadHistory(completion=None):
87
if not readlineAvailable():
88
return
89
90
clearHistory()
91
92
if completion == AUTOCOMPLETE_TYPE.SQL:
93
historyPath = paths.SQL_SHELL_HISTORY
94
elif completion == AUTOCOMPLETE_TYPE.OS:
95
historyPath = paths.OS_SHELL_HISTORY
96
elif completion == AUTOCOMPLETE_TYPE.API:
97
historyPath = paths.API_SHELL_HISTORY
98
else:
99
historyPath = paths.SQLMAP_SHELL_HISTORY
100
101
if os.path.exists(historyPath):
102
try:
103
readline.read_history_file(historyPath)
104
except IOError as ex:
105
warnMsg = "there was a problem loading the history file '%s' (%s)" % (historyPath, getSafeExString(ex))
106
logger.warning(warnMsg)
107
except UnicodeError:
108
if IS_WIN:
109
warnMsg = "there was a problem loading the history file '%s'. " % historyPath
110
warnMsg += "More info can be found at 'https://github.com/pyreadline/pyreadline/issues/30'"
111
logger.warning(warnMsg)
112
113
def autoCompletion(completion=None, os=None, commands=None):
114
if not readlineAvailable():
115
return
116
117
if completion == AUTOCOMPLETE_TYPE.OS:
118
if os == OS.WINDOWS:
119
# Reference: http://en.wikipedia.org/wiki/List_of_DOS_commands
120
completer = CompleterNG({
121
"attrib": None, "copy": None, "del": None,
122
"dir": None, "echo": None, "fc": None,
123
"label": None, "md": None, "mem": None,
124
"move": None, "net": None, "netstat -na": None,
125
"tree": None, "truename": None, "type": None,
126
"ver": None, "vol": None, "xcopy": None,
127
})
128
129
else:
130
# Reference: http://en.wikipedia.org/wiki/List_of_Unix_commands
131
completer = CompleterNG({
132
"cat": None, "chmod": None, "chown": None,
133
"cp": None, "cut": None, "date": None, "df": None,
134
"diff": None, "du": None, "echo": None, "env": None,
135
"file": None, "find": None, "free": None, "grep": None,
136
"id": None, "ifconfig": None, "ls": None, "mkdir": None,
137
"mv": None, "netstat": None, "pwd": None, "rm": None,
138
"uname": None, "whoami": None,
139
})
140
141
readline.set_completer(completer.complete)
142
readline.parse_and_bind("tab: complete")
143
144
elif commands:
145
completer = CompleterNG(dict(((_, None) for _ in commands)))
146
readline.set_completer_delims(' ')
147
readline.set_completer(completer.complete)
148
readline.parse_and_bind("tab: complete")
149
150
loadHistory(completion)
151
atexit.register(saveHistory, completion)
152
153