2012-12-14 06:52:31 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/)
|
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
import argparse
|
2012-12-14 06:52:31 +04:00
|
|
|
import os
|
2012-12-14 16:01:13 +04:00
|
|
|
import sys
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
import json
|
|
|
|
|
2012-12-14 16:04:44 +04:00
|
|
|
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), "..", ".."))
|
|
|
|
|
|
|
|
from extra.bottle.bottle import abort
|
|
|
|
from extra.bottle.bottle import debug
|
|
|
|
from extra.bottle.bottle import error
|
|
|
|
from extra.bottle.bottle import get
|
2012-12-14 16:15:04 +04:00
|
|
|
from extra.bottle.bottle import hook
|
2012-12-14 16:04:44 +04:00
|
|
|
from extra.bottle.bottle import post
|
|
|
|
from extra.bottle.bottle import request
|
|
|
|
from extra.bottle.bottle import response
|
|
|
|
from extra.bottle.bottle import run
|
|
|
|
from extra.bottle.bottle import static_file
|
|
|
|
from extra.bottle.bottle import template
|
|
|
|
from lib.controller.controller import start
|
|
|
|
from lib.core.convert import hexencode
|
2012-12-14 16:15:04 +04:00
|
|
|
from lib.core.data import paths
|
2012-12-14 16:04:44 +04:00
|
|
|
from lib.core.datatype import AttribDict
|
|
|
|
from lib.core.data import cmdLineOptions
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.exception import SqlmapMissingDependence
|
|
|
|
from lib.core.option import init
|
|
|
|
from lib.core.settings import UNICODE_ENCODING
|
|
|
|
from lib.core.settings import RESTAPI_SERVER_PORT
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 16:15:04 +04:00
|
|
|
|
2012-12-14 06:52:31 +04:00
|
|
|
# local global variables
|
|
|
|
session_ids = []
|
|
|
|
admin_id = ""
|
|
|
|
|
|
|
|
|
|
|
|
# Generic functions
|
|
|
|
def jsonize(data):
|
2012-12-14 16:01:13 +04:00
|
|
|
#return json.dumps(data, sort_keys=False, indent=4)
|
|
|
|
return json.dumps(data, sort_keys=False)
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
def is_admin(session_id):
|
|
|
|
global admin_id
|
2012-12-14 16:01:13 +04:00
|
|
|
#print "[INFO] Admin ID: %s" % admin_id
|
|
|
|
#print "[INFO] Session ID: %s" % session_id
|
2012-12-14 06:52:31 +04:00
|
|
|
if admin_id != session_id:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2012-12-14 16:15:04 +04:00
|
|
|
@hook('after_request')
|
|
|
|
def security_headers():
|
|
|
|
"""
|
|
|
|
Set some headers across all HTTP responses
|
|
|
|
"""
|
|
|
|
response.headers["Server"] = "Server"
|
|
|
|
response.headers["X-Frame-Options"] = "sameorigin"
|
|
|
|
response.headers["X-XSS-Protection"] = "1; mode=block"
|
|
|
|
|
|
|
|
|
2012-12-14 06:52:31 +04:00
|
|
|
# HTTP Status Code functions
|
|
|
|
@error(401) # Access Denied
|
|
|
|
def error401(error):
|
|
|
|
return "Access denied"
|
|
|
|
|
|
|
|
|
|
|
|
@error(404) # Not Found
|
|
|
|
def error404(error):
|
|
|
|
return "Nothing here"
|
|
|
|
|
|
|
|
|
|
|
|
@error(405) # Method Not Allowed (e.g. when requesting a POST method via GET)
|
|
|
|
def error405(error):
|
|
|
|
return "Method not allowed"
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
|
2012-12-14 06:54:16 +04:00
|
|
|
@error(500) # Internal Server Error
|
2012-12-14 06:52:31 +04:00
|
|
|
def error500(error):
|
|
|
|
return "Internal server error"
|
|
|
|
|
|
|
|
|
|
|
|
################################
|
|
|
|
# Session management functions #
|
|
|
|
################################
|
|
|
|
|
|
|
|
# Users' methods
|
|
|
|
@get("/session/new")
|
|
|
|
def session_new():
|
|
|
|
"""
|
|
|
|
Create new session token
|
|
|
|
"""
|
|
|
|
global session_ids
|
|
|
|
session_id = hexencode(os.urandom(32))
|
|
|
|
session_ids.append(session_id)
|
2012-12-14 16:01:13 +04:00
|
|
|
response.content_type = "application/json; charset=UTF-8"
|
2012-12-14 06:52:31 +04:00
|
|
|
return jsonize({"sessionid": session_id})
|
|
|
|
|
|
|
|
|
|
|
|
@post("/session/destroy")
|
|
|
|
def session_destroy():
|
|
|
|
"""
|
|
|
|
Destroy own session token
|
|
|
|
"""
|
2012-12-14 16:01:13 +04:00
|
|
|
session_id = request.json.get("sessionid", "")
|
2012-12-14 06:52:31 +04:00
|
|
|
if session_id in session_ids:
|
|
|
|
session_ids.remove(session_id)
|
2012-12-14 16:15:04 +04:00
|
|
|
return jsonize({"success": True})
|
2012-12-14 06:52:31 +04:00
|
|
|
else:
|
|
|
|
abort(500)
|
|
|
|
|
|
|
|
# Admin's methods
|
|
|
|
@post("/session/list")
|
|
|
|
def session_list():
|
|
|
|
"""
|
|
|
|
List all active sessions
|
|
|
|
"""
|
2012-12-14 16:01:13 +04:00
|
|
|
if is_admin(request.json.get("sessionid", "")):
|
|
|
|
response.content_type = "application/json; charset=UTF-8"
|
2012-12-14 06:52:31 +04:00
|
|
|
return jsonize({"sessions": session_ids})
|
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
@post("/session/flush")
|
2012-12-14 06:52:31 +04:00
|
|
|
def session_flush():
|
|
|
|
"""
|
|
|
|
Flush session spool (destroy all sessions)
|
|
|
|
"""
|
|
|
|
global session_ids
|
2012-12-14 16:01:13 +04:00
|
|
|
if is_admin(request.json.get("sessionid", "")):
|
2012-12-14 06:52:31 +04:00
|
|
|
session_ids = []
|
2012-12-14 16:15:04 +04:00
|
|
|
return jsonize({"success": True})
|
2012-12-14 06:52:31 +04:00
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
@post("/download/<target>/<filename:path>")
|
|
|
|
def download(target, filename):
|
|
|
|
"""
|
|
|
|
Download a certain file from the file system
|
|
|
|
"""
|
|
|
|
path = os.path.join(paths.SQLMAP_OUTPUT_PATH, target)
|
|
|
|
if os.path.exists(path):
|
|
|
|
return static_file(filename, root=path)
|
|
|
|
else:
|
|
|
|
abort(500)
|
|
|
|
|
|
|
|
|
2012-12-14 06:52:31 +04:00
|
|
|
def restAPIrun(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
|
|
|
|
"""
|
|
|
|
Initiate REST-JSON API
|
|
|
|
"""
|
|
|
|
global admin_id
|
|
|
|
admin_id = hexencode(os.urandom(32))
|
|
|
|
options = AttribDict(cmdLineOptions)
|
|
|
|
logger.info("Running REST-JSON API server at '%s:%d'.." % (host, port))
|
|
|
|
logger.info("The admin session ID is: %s" % admin_id)
|
|
|
|
run(host=host, port=port)
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
def client(host, port):
|
|
|
|
addr = "http://%s:%d" % (host, port)
|
|
|
|
print "[INFO] Starting debug REST-JSON client to '%s'..." % addr
|
|
|
|
|
|
|
|
# TODO: write a simple client with urllib2, for now use curl from command line
|
|
|
|
print "[ERROR] Not yet implemented, use curl from command line instead for now, for example:"
|
|
|
|
print "\n\t$ curl --proxy http://127.0.0.1:8080 http://%s:%s/session/new" % (host, port)
|
|
|
|
print "\t$ curl --proxy http://127.0.0.1:8080 -H \"Content-Type: application/json\" -X POST -d '{\"sessionid\": \"<admin session id>\"}' http://%s:%d/session/list\n" % (host, port)
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-12-14 16:01:13 +04:00
|
|
|
"""
|
|
|
|
Standalone REST-JSON API wrapper function
|
|
|
|
"""
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-s", "--server", help="Act as a REST-JSON API server", default=RESTAPI_SERVER_PORT, action="store_true", required=False)
|
|
|
|
parser.add_argument("-c", "--client", help="Act as a REST-JSON API client", default=RESTAPI_SERVER_PORT, action="store_true", required=False)
|
|
|
|
parser.add_argument("-H", "--host", help="Host of the REST-JSON API server", default="0.0.0.0", action="store", required=False)
|
|
|
|
parser.add_argument("-p", "--port", help="Port of the the REST-JSON API server", default=RESTAPI_SERVER_PORT, action="store", required=False)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.server is True:
|
|
|
|
restAPIrun(args.host, args.port)
|
|
|
|
elif args.client is True:
|
|
|
|
client(args.host, args.port)
|