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-15 02:00:42 +04:00
|
|
|
import json
|
2012-12-14 17:35:11 +04:00
|
|
|
import optparse
|
2012-12-14 06:52:31 +04:00
|
|
|
import os
|
2012-12-15 02:00:42 +04:00
|
|
|
import shutil
|
2012-12-14 16:01:13 +04:00
|
|
|
import sys
|
2012-12-14 19:52:35 +04:00
|
|
|
import tempfile
|
2012-12-14 17:40:25 +04:00
|
|
|
import threading
|
2012-12-14 06:52:31 +04:00
|
|
|
|
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 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
|
2012-12-15 04:12:22 +04:00
|
|
|
from lib.core.log import LOGGER_OUTPUT
|
2012-12-14 16:04:44 +04:00
|
|
|
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 17:40:25 +04:00
|
|
|
# Local global variables
|
|
|
|
adminid = ""
|
2012-12-15 02:00:42 +04:00
|
|
|
tasks = AttribDict()
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
# Generic functions
|
|
|
|
def jsonize(data):
|
2012-12-15 02:00:42 +04:00
|
|
|
return json.dumps(data, sort_keys=False, indent=4)
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
def is_admin(taskid):
|
|
|
|
global adminid
|
|
|
|
if adminid != taskid:
|
2012-12-14 06:52:31 +04:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2012-12-14 21:21:19 +04:00
|
|
|
@hook("after_request")
|
2012-12-14 16:15:04 +04:00
|
|
|
def security_headers():
|
|
|
|
"""
|
|
|
|
Set some headers across all HTTP responses
|
|
|
|
"""
|
|
|
|
response.headers["Server"] = "Server"
|
2012-12-14 17:40:25 +04:00
|
|
|
response.headers["X-Content-Type-Options"] = "nosniff"
|
|
|
|
response.headers["X-Frame-Options"] = "DENY"
|
2012-12-14 16:15:04 +04:00
|
|
|
response.headers["X-XSS-Protection"] = "1; mode=block"
|
2012-12-14 17:40:25 +04:00
|
|
|
response.headers["Pragma"] = "no-cache"
|
|
|
|
response.headers["Cache-Control"] = "no-cache"
|
|
|
|
response.headers["Expires"] = "0"
|
|
|
|
response.content_type = "application/json; charset=UTF-8"
|
2012-12-14 16:15:04 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
##############################
|
|
|
|
# HTTP Status Code functions #
|
|
|
|
##############################
|
2012-12-14 16:15:04 +04:00
|
|
|
|
2012-12-14 06:52:31 +04:00
|
|
|
@error(401) # Access Denied
|
2012-12-14 17:40:25 +04:00
|
|
|
def error401(error=None):
|
2012-12-14 06:52:31 +04:00
|
|
|
return "Access denied"
|
|
|
|
|
|
|
|
@error(404) # Not Found
|
2012-12-14 17:40:25 +04:00
|
|
|
def error404(error=None):
|
2012-12-14 06:52:31 +04:00
|
|
|
return "Nothing here"
|
|
|
|
|
|
|
|
@error(405) # Method Not Allowed (e.g. when requesting a POST method via GET)
|
2012-12-14 17:40:25 +04:00
|
|
|
def error405(error=None):
|
2012-12-14 06:52:31 +04:00
|
|
|
return "Method not allowed"
|
|
|
|
|
2012-12-14 06:54:16 +04:00
|
|
|
@error(500) # Internal Server Error
|
2012-12-14 17:40:25 +04:00
|
|
|
def error500(error=None):
|
2012-12-14 06:52:31 +04:00
|
|
|
return "Internal server error"
|
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
#############################
|
|
|
|
# Task management functions #
|
|
|
|
#############################
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
# Users' methods
|
2012-12-14 17:40:25 +04:00
|
|
|
@get("/task/new")
|
|
|
|
def task_new():
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
Create new task ID
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
global tasks
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 18:51:01 +04:00
|
|
|
taskid = hexencode(os.urandom(16))
|
2012-12-15 04:12:22 +04:00
|
|
|
tasks[taskid] = AttribDict(cmdLineOptions)
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
return jsonize({"taskid": taskid})
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
@get("/task/<taskid>/destroy")
|
|
|
|
def task_destroy(taskid):
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
Destroy own task ID
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 22:13:21 +04:00
|
|
|
if taskid in tasks and not is_admin(taskid):
|
|
|
|
tasks.pop(taskid)
|
2012-12-14 16:15:04 +04:00
|
|
|
return jsonize({"success": True})
|
2012-12-14 06:52:31 +04:00
|
|
|
else:
|
2012-12-14 17:40:25 +04:00
|
|
|
abort(500, "Invalid task ID")
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
# Admin's methods
|
2012-12-14 17:40:25 +04:00
|
|
|
@get("/task/<taskid>/list")
|
|
|
|
def task_list(taskid):
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
List all active tasks
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
if is_admin(taskid):
|
|
|
|
return jsonize({"tasks": tasks})
|
2012-12-14 06:52:31 +04:00
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
@get("/task/<taskid>/flush")
|
|
|
|
def task_flush(taskid):
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-15 02:00:42 +04:00
|
|
|
Flush task spool (destroy all tasks except admin)
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-15 02:00:42 +04:00
|
|
|
global adminid
|
2012-12-14 17:40:25 +04:00
|
|
|
global tasks
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
if is_admin(taskid):
|
2012-12-15 02:00:42 +04:00
|
|
|
admin_task = tasks[adminid]
|
|
|
|
tasks = AttribDict()
|
|
|
|
tasks[adminid] = admin_task
|
|
|
|
|
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 17:40:25 +04:00
|
|
|
##################################
|
|
|
|
# sqlmap core interact functions #
|
|
|
|
##################################
|
2012-12-14 18:51:01 +04:00
|
|
|
|
2012-12-14 21:21:19 +04:00
|
|
|
# Admin's methods
|
2012-12-14 20:18:45 +04:00
|
|
|
@get("/status/<taskid>")
|
|
|
|
def status(taskid):
|
|
|
|
"""
|
|
|
|
Verify the status of the API as well as the core
|
|
|
|
"""
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 20:18:45 +04:00
|
|
|
if is_admin(taskid):
|
|
|
|
busy = kb.get("busyFlag")
|
|
|
|
tasks_num = len(tasks)
|
|
|
|
return jsonize({"busy": busy, "tasks": tasks_num})
|
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
@get("/cleanup/<taskid>")
|
|
|
|
def cleanup(taskid):
|
|
|
|
"""
|
|
|
|
Destroy all sessions except admin ID and all output directories
|
|
|
|
"""
|
|
|
|
global tasks
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 20:18:45 +04:00
|
|
|
if is_admin(taskid):
|
2012-12-15 04:12:22 +04:00
|
|
|
for task, options in tasks.items():
|
|
|
|
if "oDir" in options and options.oDir is not None:
|
|
|
|
shutil.rmtree(options.oDir)
|
2012-12-15 02:00:42 +04:00
|
|
|
|
|
|
|
admin_task = tasks[adminid]
|
|
|
|
tasks = AttribDict()
|
|
|
|
tasks[adminid] = admin_task
|
|
|
|
|
2012-12-14 20:18:45 +04:00
|
|
|
return jsonize({"success": True})
|
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
|
2012-12-14 21:21:19 +04:00
|
|
|
# Functions to handle options
|
2012-12-14 18:51:01 +04:00
|
|
|
@get("/option/<taskid>/list")
|
|
|
|
def option_list(taskid):
|
|
|
|
"""
|
|
|
|
List options for a certain task ID
|
|
|
|
"""
|
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
return jsonize(tasks[taskid])
|
2012-12-14 18:51:01 +04:00
|
|
|
|
|
|
|
@post("/option/<taskid>/get")
|
|
|
|
def option_get(taskid):
|
|
|
|
"""
|
|
|
|
Get the value of an option (command line switch) for a certain task ID
|
|
|
|
"""
|
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
|
|
|
option = request.json.get("option", "")
|
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
if option in tasks[taskid]:
|
|
|
|
return jsonize({option: tasks[taskid][option]})
|
2012-12-14 18:51:01 +04:00
|
|
|
else:
|
|
|
|
return jsonize({option: None})
|
|
|
|
|
|
|
|
@post("/option/<taskid>/set")
|
|
|
|
def option_set(taskid):
|
|
|
|
"""
|
|
|
|
Set an option (command line switch) for a certain task ID
|
|
|
|
"""
|
2012-12-15 02:00:42 +04:00
|
|
|
global tasks
|
|
|
|
|
2012-12-14 18:51:01 +04:00
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
|
|
|
for key, value in request.json.items():
|
2012-12-15 04:12:22 +04:00
|
|
|
tasks[taskid][key] = value
|
2012-12-14 18:51:01 +04:00
|
|
|
|
|
|
|
return jsonize({"success": True})
|
|
|
|
|
2012-12-14 21:21:19 +04:00
|
|
|
# Function to handle scans
|
2012-12-14 19:52:35 +04:00
|
|
|
@post("/scan/<taskid>/start")
|
2012-12-15 04:29:35 +04:00
|
|
|
def scan_start(taskid):
|
2012-12-14 17:40:25 +04:00
|
|
|
"""
|
2012-12-14 18:51:01 +04:00
|
|
|
Launch a scan
|
2012-12-14 17:40:25 +04:00
|
|
|
"""
|
2012-12-15 02:00:42 +04:00
|
|
|
global tasks
|
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 17:40:25 +04:00
|
|
|
# Initialize sqlmap engine's options with user's provided options
|
|
|
|
# within the JSON request
|
|
|
|
for key, value in request.json.items():
|
2012-12-15 04:12:22 +04:00
|
|
|
tasks[taskid][key] = value
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
# Overwrite output directory (oDir) value to a temporary directory
|
|
|
|
tasks[taskid].oDir = tempfile.mkdtemp(prefix="sqlmap-")
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
init(tasks[taskid], True)
|
2012-12-14 17:40:25 +04:00
|
|
|
|
|
|
|
# Launch sqlmap engine in a separate thread
|
|
|
|
thread = threading.Thread(target=start)
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
return jsonize({"success": True})
|
|
|
|
|
2012-12-14 18:51:01 +04:00
|
|
|
@get("/scan/<taskid>/output")
|
|
|
|
def scan_output(taskid):
|
|
|
|
"""
|
|
|
|
Read the standard output of sqlmap core execution
|
|
|
|
"""
|
2012-12-14 22:13:21 +04:00
|
|
|
global tasks
|
|
|
|
|
2012-12-14 18:51:01 +04:00
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
sys.stdout.seek(0)
|
|
|
|
output = sys.stdout.read()
|
|
|
|
sys.stdout.flush()
|
2012-12-14 19:52:35 +04:00
|
|
|
sys.stdout.truncate(0)
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
return jsonize({"output": output})
|
2012-12-14 18:51:01 +04:00
|
|
|
|
2012-12-15 02:00:42 +04:00
|
|
|
@get("/scan/<taskid>/delete")
|
|
|
|
def scan_delete(taskid):
|
|
|
|
"""
|
|
|
|
Delete a scan and corresponding temporary output directory
|
|
|
|
"""
|
|
|
|
global tasks
|
|
|
|
|
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
if "oDir" in tasks[taskid] and tasks[taskid].oDir is not None:
|
|
|
|
shutil.rmtree(tasks[taskid].oDir)
|
2012-12-15 02:00:42 +04:00
|
|
|
|
|
|
|
return jsonize({"success": True})
|
|
|
|
|
2012-12-14 21:21:19 +04:00
|
|
|
# Function to handle scans' logs
|
2012-12-15 04:12:22 +04:00
|
|
|
@get("/scan/<taskid>/log")
|
|
|
|
def scan_log(taskid):
|
2012-12-14 21:21:19 +04:00
|
|
|
"""
|
2012-12-17 15:28:03 +04:00
|
|
|
Retrieve the log messages
|
2012-12-14 21:21:19 +04:00
|
|
|
"""
|
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
LOGGER_OUTPUT.seek(0)
|
|
|
|
output = LOGGER_OUTPUT.read()
|
|
|
|
LOGGER_OUTPUT.flush()
|
|
|
|
LOGGER_OUTPUT.truncate(0)
|
|
|
|
|
|
|
|
return jsonize({"log": output})
|
2012-12-14 21:21:19 +04:00
|
|
|
|
|
|
|
# Function to handle files inside the output directory
|
2012-12-14 19:52:35 +04:00
|
|
|
@get("/download/<taskid>/<target>/<filename:path>")
|
2012-12-14 17:40:25 +04:00
|
|
|
def download(taskid, target, filename):
|
2012-12-14 16:01:13 +04:00
|
|
|
"""
|
|
|
|
Download a certain file from the file system
|
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
if taskid not in tasks:
|
|
|
|
abort(500, "Invalid task ID")
|
|
|
|
|
2012-12-14 20:18:45 +04:00
|
|
|
# Prevent file path traversal - the lame way
|
|
|
|
if target.startswith("."):
|
|
|
|
abort(500)
|
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
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-15 04:12:22 +04:00
|
|
|
def restAPISetup(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-15 04:12:22 +04:00
|
|
|
Setup REST-JSON API
|
2012-12-14 06:52:31 +04:00
|
|
|
"""
|
2012-12-14 17:40:25 +04:00
|
|
|
global adminid
|
|
|
|
global tasks
|
2012-12-15 02:00:42 +04:00
|
|
|
|
2012-12-14 18:51:01 +04:00
|
|
|
adminid = hexencode(os.urandom(16))
|
2012-12-15 04:12:22 +04:00
|
|
|
tasks[adminid] = AttribDict(cmdLineOptions)
|
|
|
|
|
|
|
|
logger.info("running REST-JSON API server at '%s:%d'.." % (host, port))
|
|
|
|
logger.info("the admin task ID is: %s" % adminid)
|
2012-12-14 18:51:01 +04:00
|
|
|
|
2012-12-15 04:12:22 +04:00
|
|
|
def restAPIRun(host="0.0.0.0", port=RESTAPI_SERVER_PORT):
|
|
|
|
"""
|
|
|
|
Run REST-JSON API
|
|
|
|
"""
|
2012-12-14 22:13:21 +04:00
|
|
|
run(host=host, port=port, quiet=False, debug=False)
|
2012-12-14 06:52:31 +04:00
|
|
|
|
2012-12-14 16:01:13 +04:00
|
|
|
def client(host, port):
|
2012-12-15 04:29:35 +04:00
|
|
|
"""
|
|
|
|
REST-JSON API client
|
|
|
|
"""
|
2012-12-14 16:01:13 +04:00
|
|
|
addr = "http://%s:%d" % (host, port)
|
2012-12-15 04:12:22 +04:00
|
|
|
print "[*] starting debug REST-JSON client to '%s'..." % addr
|
2012-12-14 16:01:13 +04:00
|
|
|
|
|
|
|
# TODO: write a simple client with urllib2, for now use curl from command line
|
2012-12-15 04:12:22 +04:00
|
|
|
print "[!] not yet implemented, use curl from command line instead for now, for example:"
|
2012-12-14 17:40:25 +04:00
|
|
|
print "\n\t$ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:%s/task/new" % port
|
2012-12-15 02:00:42 +04:00
|
|
|
print "\t$ curl --proxy http://127.0.0.1:8080 -H \"Content-Type: application/json\" -X POST -d '{\"url\": \"http://testphp.vulnweb.com/artists.php?artist=1\"}' http://127.0.0.1:%d/scan/<taskID>/start" % port
|
2012-12-15 04:29:35 +04:00
|
|
|
print "\t$ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:8775/scan/<taskID>/output"
|
|
|
|
print "\t$ curl --proxy http://127.0.0.1:8080 http://127.0.0.1:8775/scan/<taskID>/log\n"
|
2012-12-14 06:52:31 +04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-12-14 16:01:13 +04:00
|
|
|
"""
|
2012-12-15 04:29:35 +04:00
|
|
|
REST-JSON API wrapper function
|
2012-12-14 16:01:13 +04:00
|
|
|
"""
|
2012-12-14 17:35:11 +04:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("-s", "--server", help="Act as a REST-JSON API server", default=RESTAPI_SERVER_PORT, action="store_true")
|
|
|
|
parser.add_option("-c", "--client", help="Act as a REST-JSON API client", default=RESTAPI_SERVER_PORT, action="store_true")
|
|
|
|
parser.add_option("-H", "--host", help="Host of the REST-JSON API server", default="0.0.0.0", action="store")
|
|
|
|
parser.add_option("-p", "--port", help="Port of the the REST-JSON API server", default=RESTAPI_SERVER_PORT, action="store")
|
|
|
|
(args, _) = parser.parse_args()
|
2012-12-14 16:01:13 +04:00
|
|
|
|
|
|
|
if args.server is True:
|
|
|
|
restAPIrun(args.host, args.port)
|
|
|
|
elif args.client is True:
|
|
|
|
client(args.host, args.port)
|