示例#1
0
 def read_config(self):
     response = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                  "/get/section/NmapProbes")
     ipc.assert_response_valid(response, dict)
     self.nmap_probes = response["text"].copy()
     for key, value in self.nmap_probes.items():
         self.nmap.add_file(value)
示例#2
0
    async def reload_all(self, _request):
        options = ipc.sync_http_raw("GET", SOCK_CONFIG, "/get/section/Options")
        ipc.assert_response_valid(options, dict)
        options = self.parse_option(options["text"])

        self.modules.reload_all("modules/", options)

        return sanic.response.json(RETURN_OK)
示例#3
0
    def refresh_config(self):
        response = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                     "/get/variable/DNSAPI/root")
        ipc.assert_response_valid(response, dict)
        assert "root" in response["text"]
        root = response["text"]["root"]
        roots = root.split(",")

        response = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                     "/get/section/ManageWebserver")
        ipc.assert_response_valid(response, dict)
        self.config = response["text"].copy()
        self.config["roots"] = roots
        assert isinstance(self.config, dict) and \
            "port" in self.config and \
            "bind" in self.config and \
            "redirect_initial" in self.config and \
            "redirect_attack" in self.config
示例#4
0
    def run(self):
        logger.info("Initializing module loader")
        self.socket = ipc.unix_socket(SOCKET_MODULES)

        # Get global options from config file
        options = ipc.sync_http_raw("GET", SOCK_CONFIG, "/get/section/Options")
        ipc.assert_response_valid(options, dict)
        options = self.parse_option(options["text"])

        # Get options for this class
        mod_options = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                        "/get/section/Modules")
        ipc.assert_response_valid(mod_options, dict)
        self.options = mod_options["text"].copy()

        # Some values must be present in options
        assert "safety" in self.options
        assert "intrusiveness" in self.options

        if "default_passes" in self.options:
            self.options["default_passes"] = misc.file2json(
                self.options["default_passes"])
            logger.info("Loaded default passes")

        # Try and convert string to enums
        try:
            self.options["safety"] = Safety[self.options["safety"]]
            self.options["intrusiveness"] = Intrusiveness[
                self.options["intrusiveness"]]
        except:
            logger.critical(
                "Safety or intrusiveness values could not be converted to enum"
            )
            return

        self.modules.parse_mod_dir("modules/", options)

        self.app = Sanic("ModuleLoader")
        self.add_routes()
        logger.info("Done initializing module loader")
        self.app.run(sock=self.socket, access_log=False)
示例#5
0
    def run(self, sock, port):
        logger.info("Starting web server on port {}".format(port))
        self.port = port

        response = ipc.sync_http_raw("GET", SOCK_CONFIG, "/get/section/Scan")
        ipc.assert_response_valid(response, dict)
        self.config = response["text"]
        self.set_default_options()

        # Get all static files to serve
        response = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                     "/get/section/Webserver")
        ipc.assert_response_valid(response, dict)
        assert "static" in response["text"]
        self.config["files2serve"] = []
        for entry in response["text"]["static"].split(";"):
            try:
                webname, path = entry.split(":")
                self.config["files2serve"].append((webname, path))
            except:
                logger.critical(
                    "Entry: '{}' in config file is invalid".format(entry))
                sys.exit(1)

        self.client_managed = misc.string2bool(response["text"].get(
            "client_managed", "False"))
        assert isinstance(self.client_managed, bool)

        assert "interface" in response["text"]
        self.config["interface"] = response["text"]["interface"]

        self.config["debug_mode"] = misc.string2bool(response["text"].get(
            "debug_mode", "False"))
        assert isinstance(self.config["debug_mode"], bool)

        if "redirect_index" in response["text"]:
            self.config["redirect_index"] = response["text"].get(
                "redirect_index")

        response = ipc.sync_http_raw("GET", SOCK_CONFIG,
                                     "/get/variable/DNSAPI/root")
        ipc.assert_response_valid(response, dict)
        assert "root" in response["text"]
        self.root = response["text"]["root"]

        self.app = Sanic("Webserver")
        self.cors = CORS(self.app,
                         automatic_options=True,
                         resources={r"/*": {
                             "origins": "*"
                         }})

        if self.add_routes() is not True:
            logger.fatal("Unable to add routes")
            return False

        # Add some middleware to the webserver

        @self.app.middleware('request')
        async def all_requests(request):
            logger.debug("Web request {} -> {}".format(request.ip,
                                                       request.path))

        @self.app.middleware('request')
        async def check_host(request):
            host = request.host.split(":")[0]
            if host != self.root and host.endswith(".{}".format(
                    self.root)) is False:
                logger.warning(
                    "Tried to access with invalid hostname {} from {}".format(
                        request.host, request.ip))
                return sanic.response.text("Invalid request", status=500)

        # Catch some exceptions to avoid cluttering the log file
        @self.app.exception(sanic.exceptions.NotFound)
        def custom_404(request, exception):
            logger.error("Resource not found {} from client {}".format(
                request.path, request.ip))
            return sanic.response.text("Requested URL {} not found".format(
                request.path),
                                       status=404)

        logger.info("Started web server on port {}".format(port))
        self.app.run(sock=sock, access_log=False)