Beispiel #1
0
def main_loop():
    setproctitle('webshelld')

    dispatcher = SimpleXMLRPCDispatcher()
    SOCKFILE = '/var/run/webshell.sock'
    if os.path.exists(SOCKFILE):
        os.unlink(SOCKFILE)
    server = socketserver.UnixStreamServer(SOCKFILE, XMLRPCHandler)
    os.chmod(SOCKFILE, 0o700)
    dispatcher.register_instance(
        Multiplex("/usr/local/bin/bash", "xterm-color"))
    server.dispatcher = dispatcher
    server.serve_forever()
Beispiel #2
0
def main_loop():
    setproctitle('webshelld')

    dispatcher = SimpleXMLRPCDispatcher()
    SOCKFILE = '/var/run/webshell.sock'
    if os.path.exists(SOCKFILE):
        os.unlink(SOCKFILE)
    server = socketserver.UnixStreamServer(SOCKFILE, XMLRPCHandler)
    os.chmod(SOCKFILE, 0o700)
    dispatcher.register_instance(
        Multiplex("/usr/local/bin/bash", "xterm-color"))
    server.dispatcher = dispatcher
    server.serve_forever()
Beispiel #3
0
def handle_request(app, request):
    '''Wrap an invocation of the XML-RPC dispatcher.
    '''
    # unicode strings will be encoded in utf-8 by xmlrpclib
    dispatcher = SimpleXMLRPCDispatcher()
    dispatcher.register_instance(Interface(app, request))

    # read in the XML-RPC request data, limiting to a sensible size
    if int(request.headers['Content-Length']) > 10 * 1024 * 1024:
        raise BadRequest('request data too large')
    xml_request = request.get_data(cache=False, as_text=True)

    # errors here are handled by _marshaled_dispatch
    response = dispatcher._marshaled_dispatch(xml_request)

    # legacy; remove non-printable ASCII control codes from the response
    # RJ: disabled this as it's a giant, unreliable hack that doesn't work and
    # I can't even remember why it's in here to start with
    # response = re.sub('([\x00-\x08]|[\x0b-\x0c]|[\x0e-\x1f])+', '', response)

    return Response(response, mimetype="text/xml; charset=utf-8")
Beispiel #4
0
def handler(app, request):
    '''Wrap an invocation of the XML-RPC dispatcher.
    '''
    # unicode strings will be encoded in utf-8 by xmlrpclib
    dispatcher = SimpleXMLRPCDispatcher()
    dispatcher.register_instance(Interface(app, request))

    # read in the XML-RPC request data, limiting to a sensible size
    if int(request.headers['Content-Length']) > 10 * 1024 * 1024:
        raise BadRequest('request data too large')
    xml_request = request.get_data(cache=False, as_text=True)

    # errors here are handled by _marshaled_dispatch
    response = dispatcher._marshaled_dispatch(xml_request)

    # legacy; remove non-printable ASCII control codes from the response
    # RJ: disabled this as it's a giant, unreliable hack that doesn't work and
    # I can't even remember why it's in here to start with
    # response = re.sub('([\x00-\x08]|[\x0b-\x0c]|[\x0e-\x1f])+', '', response)

    return Response(response, mimetype="text/xml; charset=utf-8")
class WSGIXMLRPCApplication(object):
    """Application to handle requests to the XMLRPC service"""
    def __init__(self, instance=None, methods=None):
        """Create windmill xmlrpc dispatcher"""
        if methods is None:
            methods = []
        try:
            self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True,
                                                     encoding=None)
        except TypeError:
            # python 2.4
            self.dispatcher = SimpleXMLRPCDispatcher()
        if instance is not None:
            self.dispatcher.register_instance(instance)
        for method in methods:
            self.dispatcher.register_function(method)
        self.dispatcher.register_introspection_functions()

    def register_instance(self, instance):
        return self.dispatcher.register_instance(instance)

    def register_function(self, function, name=None):
        return self.dispatcher.register_function(function, name)

    def handler(self, environ, start_response):
        """XMLRPC service for windmill browser core to communicate with"""

        if environ['REQUEST_METHOD'] == 'POST':
            return self.handle_POST(environ, start_response)
        else:
            start_response("400 Bad request", [('Content-Type', 'text/plain')])
            return ['']

    def handle_POST(self, environ, start_response):
        """Handles the HTTP POST request.

        Attempts to interpret all HTTP POST requests as XML-RPC calls,
        which are forwarded to the server's _dispatch method for handling.

        Most code taken from SimpleXMLRPCServer with modifications for wsgi and my custom dispatcher.
        """

        try:
            # Get arguments by reading body of request.
            # We read this in chunks to avoid straining
            # socket.read(); around the 10 or 15Mb mark, some platforms
            # begin to have problems (bug #792570).

            length = int(environ['CONTENT_LENGTH'])
            data = environ['wsgi.input'].read(length)
            print('-------wsgi----data')
            print(data)
            # In previous versions of SimpleXMLRPCServer, _dispatch
            # could be overridden in this class, instead of in
            # SimpleXMLRPCDispatcher. To maintain backwards compatibility,
            # check to see if a subclass implements _dispatch and
            # using that method if present.
            response = self.dispatcher._marshaled_dispatch(
                data, getattr(self.dispatcher, '_dispatch', None))
            response += b'\n'
        except Exception as e:  # This should only happen if the module is buggy
            # internal error, report as HTTP server error
            print(e)
            start_response("500 Server error",
                           [('Content-Type', 'text/plain')])
            return []
        else:
            # got a valid XML RPC response
            start_response("200 OK", [('Content-Type', 'text/xml'),
                                      (
                                          'Content-Length',
                                          str(len(response)),
                                      )])
            return [response]

    def __call__(self, environ, start_response):
        print(environ)
        return self.handler(environ, start_response)
Beispiel #6
0
class WSGIXMLRPCApplication(object):
    """
    基于WSGI的XMLRPC应用
    """
    def __init__(self, instance=None, methods=None, **kwargs):
        """
        创建xmlrpc dispatcher
        """
        if methods is None:
            methods = []
        self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True,
                                                 encoding=None)
        if instance is not None:
            self.dispatcher.register_instance(instance)
        for method in methods:
            self.dispatcher.register_function(method)
        self.dispatcher.register_introspection_functions()
        self.logger = kwargs.get('logger', logging.getLogger(__name__))

    def register_instance(self, instance):
        return self.dispatcher.register_instance(instance)

    def register_function(self, function, name=None):
        return self.dispatcher.register_function(function, name)

    def handler(self, environ, start_response):
        """
        处理HTTP访问
        """

        if environ['REQUEST_METHOD'] == 'POST':
            return self.handle_POST(environ, start_response)
        else:
            start_response("400 Bad request", [('Content-Type', 'text/plain')])
            return []

    def handle_POST(self, environ, start_response):
        """
        处理HTTP POST请求
        """

        try:
            length = int(environ['CONTENT_LENGTH'])
            data = environ['wsgi.input'].read(length)

            response = self.dispatcher._marshaled_dispatch(
                data, getattr(self.dispatcher, '_dispatch', None))
            response += b'\n'
        except Exception as e:
            self.logger.exception(e)
            start_response("500 Server error",
                           [('Content-Type', 'text/plain')])
            return []
        else:
            start_response("200 OK", [('Content-Type', 'text/xml'),
                                      (
                                          'Content-Length',
                                          str(len(response)),
                                      )])
            return [response]

    def __call__(self, environ, start_response):
        return self.handler(environ, start_response)