Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e31e87

Browse files
committed
added initial support (hidden from -hh and not yet usable) for REST-JSON API
1 parent 46885d4 commit 6e31e87

4 files changed

Lines changed: 36 additions & 2 deletions

File tree

_sqlmap.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,28 @@
3232
from lib.core.data import paths
3333
from lib.core.common import unhandledExceptionMessage
3434
from lib.core.exception import exceptionsTuple
35+
from lib.core.exception import SqlmapMissingDependence
3536
from lib.core.exception import SqlmapSilentQuitException
3637
from lib.core.exception import SqlmapUserQuitException
3738
from lib.core.log import FORMATTER
3839
from lib.core.log import LOGGER_HANDLER
3940
from lib.core.option import init
4041
from lib.core.profiling import profile
4142
from lib.core.settings import LEGAL_DISCLAIMER
43+
from lib.core.settings import RESTAPI_SERVER_PORT
4244
from lib.core.settings import XMLRPC_SERVER_PORT
4345
from lib.core.testing import smokeTest
4446
from lib.core.testing import liveTest
4547
from lib.parse.cmdline import cmdLineParser
4648
from lib.utils.xmlrpc import XMLRPCServer
4749

50+
try:
51+
from lib.utils.restapi import restAPIrun
52+
except SqlmapMissingDependence, e:
53+
e = getUnicode(e)
54+
logger.critical(e)
55+
sys.exit(1)
56+
4857
def modulePath():
4958
"""
5059
This will get us the program's directory, even if we are frozen
@@ -53,6 +62,18 @@ def modulePath():
5362

5463
return os.path.dirname(getUnicode(sys.executable if weAreFrozen() else __file__, sys.getfilesystemencoding()))
5564

65+
def restApiServe():
66+
logger.setLevel(logging.INFO)
67+
cmdLineOptions.batch = True
68+
cmdLineOptions.disableColoring = True
69+
restAPIrun(port=cmdLineOptions.restApiPort or RESTAPI_SERVER_PORT)
70+
def emit(self, record):
71+
message = stdoutencode(FORMATTER.format(record))
72+
sys.stdout.write("%s\n" % message.strip('\r'))
73+
LOGGER_HANDLER.emit = types.MethodType(emit, LOGGER_HANDLER, type(LOGGER_HANDLER))
74+
sys.stdout = StringIO.StringIO()
75+
sys.stderr = StringIO.StringIO()
76+
5677
def xmlRpcServe():
5778
logger.setLevel(logging.INFO)
5879
cmdLineOptions.batch = True
@@ -82,7 +103,9 @@ def main():
82103
# Store original command line options for possible later restoration
83104
cmdLineOptions.update(cmdLineParser().__dict__)
84105

85-
if cmdLineOptions.xmlRpc:
106+
if cmdLineOptions.restApi:
107+
restApiServe()
108+
elif cmdLineOptions.xmlRpc:
86109
xmlRpcServe()
87110
else:
88111
init(cmdLineOptions)
@@ -106,6 +129,7 @@ def main():
106129
except exceptionsTuple, e:
107130
e = getUnicode(e)
108131
logger.critical(e)
132+
sys.exit(1)
109133

110134
except KeyboardInterrupt:
111135
print

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@
485485
# Default TCP port used for XML-RPC server instance
486486
XMLRPC_SERVER_PORT = 8776
487487

488+
# Default TCP port used for REST API server instance
489+
RESTAPI_SERVER_PORT = 8775
490+
488491
# Regular expression for SOAP-like POST data
489492
SOAP_RECOGNITION_REGEX = r"(?s)\A(<\?xml[^>]+>)?\s*<([^> ]+)( [^>]+)?>.+</\2.*>\s*\Z"
490493

lib/parse/cmdline.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ def cmdLineParser():
682682
parser.add_option("--run-case", dest="runCase", type="int",
683683
help=SUPPRESS_HELP)
684684

685+
parser.add_option("--restapi", dest="restApi", action="store_true",
686+
help=SUPPRESS_HELP)
687+
688+
parser.add_option("--restApi-port", dest="restApiPort", type="int",
689+
help=SUPPRESS_HELP)
690+
685691
parser.add_option("--xmlrpc", dest="xmlRpc", action="store_true",
686692
help=SUPPRESS_HELP)
687693

@@ -761,7 +767,7 @@ def _(self, *args):
761767

762768
if not any((args.direct, args.url, args.logFile, args.bulkFile, args.googleDork, args.configFile, \
763769
args.requestFile, args.updateAll, args.smokeTest, args.liveTest, args.wizard, args.dependencies, \
764-
args.xmlRpc, args.purgeOutput)):
770+
args.restApi, args.xmlRpc, args.purgeOutput)):
765771
errMsg = "missing a mandatory option (-d, -u, -l, -m, -r, -g, -c, --wizard, --update, --purge-output or --dependencies), "
766772
errMsg += "use -h for basic or -hh for advanced help"
767773
parser.error(errMsg)

lib/utils/xmlrpc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def read_output(self):
6666
return retval
6767

6868
def run(self):
69+
print "CALLING RUN"
6970
if not self.is_busy():
7071
init(self.options, True)
7172
thread = threading.Thread(target=start)

0 commit comments

Comments
 (0)