From 2f9ee862ab56bc0b6b08e9fcb34686f16be0c635 Mon Sep 17 00:00:00 2001 From: cc Date: Fri, 18 Oct 2024 21:37:27 +0200 Subject: [PATCH 1/2] tracer: Randomize web UI port --- frida_tools/tracer.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/frida_tools/tracer.py b/frida_tools/tracer.py index b5ded91..dd5044b 100644 --- a/frida_tools/tracer.py +++ b/frida_tools/tracer.py @@ -5,7 +5,6 @@ import binascii import codecs import email.utils -import errno import gzip import http import mimetypes @@ -43,7 +42,7 @@ class TracerApplication(ConsoleApplication, UI): def __init__(self) -> None: super().__init__(await_ctrl_c) self._handlers = OrderedDict() - self._ui_port = 1337 + self._ui_port = 0 self._ui_zip = ZipFile(Path(__file__).parent / "tracer_ui.zip", "r") self._ui_socket_handlers: Set[UISocketHandler] = set() self._ui_worker = None @@ -306,21 +305,13 @@ def _run_ui_server(self): async def _handle_ui_requests(self): self._asyncio_loop = asyncio.get_running_loop() - while True: - try: - async with websockets.asyncio.server.serve( - self._handle_websocket_connection, - "localhost", - self._ui_port, - process_request=self._handle_asset_request, - ): - await asyncio.get_running_loop().create_future() - return - except OSError as e: - if e.errno == errno.EADDRINUSE: - self._ui_port += 1 - else: - raise + async with websockets.asyncio.server.serve( + self._handle_websocket_connection, + "localhost", + process_request=self._handle_asset_request, + ) as server: + self._ui_port = server.sockets[0].getsockname()[1] + await asyncio.get_running_loop().create_future() async def _handle_websocket_connection(self, websocket: websockets.asyncio.server.ServerConnection): if self._tracer is None: From 5316b6f2e56c3b2df7d3001c35d578fbdaf8d6d8 Mon Sep 17 00:00:00 2001 From: cc Date: Fri, 18 Oct 2024 23:48:19 +0200 Subject: [PATCH 2/2] tracer: Validate Origin on WebSocket connection To prevent abuse from untrusted web contents. --- frida_tools/tracer.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/frida_tools/tracer.py b/frida_tools/tracer.py index dd5044b..423dccd 100644 --- a/frida_tools/tracer.py +++ b/frida_tools/tracer.py @@ -340,6 +340,16 @@ def _handle_asset_request( self, connection: websockets.asyncio.server.ServerConnection, request: websockets.asyncio.server.Request ): if request.headers.get("Connection") == "Upgrade": + origin = request.headers.get("Origin") + if origin != f"http://localhost:{self._ui_port}": + self._print( + Fore.RED + + Style.BRIGHT + + "Warning" + + Style.RESET_ALL + + f": Cross-origin request from {origin} denied" + ) + return connection.respond(http.HTTPStatus.FORBIDDEN, "Cross-origin request denied\n") return raw_path = request.path.split("?", maxsplit=1)[0]