Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/sqlmapapi.py
2983 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 sys
9
10
sys.dont_write_bytecode = True
11
12
__import__("lib.utils.versioncheck") # this has to be the first non-standard import
13
14
import logging
15
import os
16
import warnings
17
18
warnings.filterwarnings(action="ignore", category=UserWarning)
19
warnings.filterwarnings(action="ignore", category=DeprecationWarning)
20
21
try:
22
from optparse import OptionGroup
23
from optparse import OptionParser as ArgumentParser
24
25
ArgumentParser.add_argument = ArgumentParser.add_option
26
27
def _add_argument(self, *args, **kwargs):
28
return self.add_option(*args, **kwargs)
29
30
OptionGroup.add_argument = _add_argument
31
32
except ImportError:
33
from argparse import ArgumentParser
34
35
finally:
36
def get_actions(instance):
37
for attr in ("option_list", "_group_actions", "_actions"):
38
if hasattr(instance, attr):
39
return getattr(instance, attr)
40
41
def get_groups(parser):
42
return getattr(parser, "option_groups", None) or getattr(parser, "_action_groups")
43
44
def get_all_options(parser):
45
retVal = set()
46
47
for option in get_actions(parser):
48
if hasattr(option, "option_strings"):
49
retVal.update(option.option_strings)
50
else:
51
retVal.update(option._long_opts)
52
retVal.update(option._short_opts)
53
54
for group in get_groups(parser):
55
for option in get_actions(group):
56
if hasattr(option, "option_strings"):
57
retVal.update(option.option_strings)
58
else:
59
retVal.update(option._long_opts)
60
retVal.update(option._short_opts)
61
62
return retVal
63
64
from lib.core.common import getUnicode
65
from lib.core.common import setPaths
66
from lib.core.data import logger
67
from lib.core.patch import dirtyPatches
68
from lib.core.patch import resolveCrossReferences
69
from lib.core.settings import RESTAPI_DEFAULT_ADAPTER
70
from lib.core.settings import RESTAPI_DEFAULT_ADDRESS
71
from lib.core.settings import RESTAPI_DEFAULT_PORT
72
from lib.core.settings import UNICODE_ENCODING
73
from lib.utils.api import client
74
from lib.utils.api import server
75
76
try:
77
from sqlmap import modulePath
78
except ImportError:
79
def modulePath():
80
return getUnicode(os.path.dirname(os.path.realpath(__file__)), encoding=sys.getfilesystemencoding() or UNICODE_ENCODING)
81
82
def main():
83
"""
84
REST-JSON API main function
85
"""
86
87
dirtyPatches()
88
resolveCrossReferences()
89
90
# Set default logging level to debug
91
logger.setLevel(logging.DEBUG)
92
93
# Initialize paths
94
setPaths(modulePath())
95
96
# Parse command line options
97
apiparser = ArgumentParser()
98
apiparser.add_argument("-s", "--server", help="Run as a REST-JSON API server", action="store_true")
99
apiparser.add_argument("-c", "--client", help="Run as a REST-JSON API client", action="store_true")
100
apiparser.add_argument("-H", "--host", help="Host of the REST-JSON API server (default \"%s\")" % RESTAPI_DEFAULT_ADDRESS, default=RESTAPI_DEFAULT_ADDRESS)
101
apiparser.add_argument("-p", "--port", help="Port of the REST-JSON API server (default %d)" % RESTAPI_DEFAULT_PORT, default=RESTAPI_DEFAULT_PORT, type=int)
102
apiparser.add_argument("--adapter", help="Server (bottle) adapter to use (default \"%s\")" % RESTAPI_DEFAULT_ADAPTER, default=RESTAPI_DEFAULT_ADAPTER)
103
apiparser.add_argument("--database", help="Set IPC database filepath (optional)")
104
apiparser.add_argument("--username", help="Basic authentication username (optional)")
105
apiparser.add_argument("--password", help="Basic authentication password (optional)")
106
(args, _) = apiparser.parse_known_args() if hasattr(apiparser, "parse_known_args") else apiparser.parse_args()
107
108
# Start the client or the server
109
if args.server:
110
server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password, database=args.database)
111
elif args.client:
112
client(args.host, args.port, username=args.username, password=args.password)
113
else:
114
apiparser.print_help()
115
116
if __name__ == "__main__":
117
main()
118
119