|
| 1 | +"""HTTP Payload Handler.""" |
| 2 | +import contextlib |
| 3 | +import json |
| 4 | +import sys |
| 5 | +from functools import partial |
| 6 | +from http.server import SimpleHTTPRequestHandler |
| 7 | +from json import JSONDecodeError |
| 8 | +from socketserver import TCPServer |
| 9 | +from typing import Any, Self |
| 10 | +from urllib.parse import parse_qsl, unquote |
| 11 | + |
| 12 | +import rich |
| 13 | +import websocket |
| 14 | + |
| 15 | +from sqlmap_websocket_proxy.console import error, status, timestamp |
| 16 | + |
| 17 | +# Don't proxy these headers |
| 18 | +SKIPPED_HEADERS = [ |
| 19 | + "connection", |
| 20 | + "accept-encoding", |
| 21 | + "accept", |
| 22 | +] |
| 23 | + |
| 24 | +class HTTPHandler(SimpleHTTPRequestHandler): |
| 25 | + """HTTP Endpoint that Proxies to the weboscket.""" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self: Self, |
| 29 | + url: str, |
| 30 | + data: str, |
| 31 | + *args: Any, # noqa: ANN401 |
| 32 | + **kwargs: dict[Any], |
| 33 | + ) -> None: |
| 34 | + """Init.""" |
| 35 | + self.url = url |
| 36 | + self.data = data |
| 37 | + |
| 38 | + self.json_encode = False |
| 39 | + with contextlib.suppress(JSONDecodeError): |
| 40 | + json.loads(data) |
| 41 | + self.json_encode = True |
| 42 | + status("Detected JSON input. Auto-escaping.") |
| 43 | + |
| 44 | + # Suppress default logging |
| 45 | + self.log_message = lambda *_args: None |
| 46 | + |
| 47 | + super().__init__(*args, **kwargs) |
| 48 | + |
| 49 | + def do_GET(self: Self) -> None: # noqa: N802 |
| 50 | + """Handle GET requests.""" |
| 51 | + self.send_response(200) |
| 52 | + self.end_headers() |
| 53 | + resp = send_inject( |
| 54 | + self.url, |
| 55 | + self.path, |
| 56 | + self.data, |
| 57 | + self.headers, |
| 58 | + self.json_encode, |
| 59 | + ) |
| 60 | + self.wfile.write(resp) |
| 61 | + |
| 62 | +def send_inject( |
| 63 | + url: str, |
| 64 | + path: str, |
| 65 | + data: str, |
| 66 | + headers: dict, |
| 67 | + json_encode: bool, # noqa: FBT001 |
| 68 | +) -> bytes: |
| 69 | + """Send sqlmap inject acrossthe websocket.""" |
| 70 | + params = [x for _, x in parse_qsl(path)] |
| 71 | + |
| 72 | + if json_encode: |
| 73 | + params = [unquote(x).replace('"',"'") for x in params] |
| 74 | + |
| 75 | + for x in params: |
| 76 | + data = data.replace("%param%", x, 1) |
| 77 | + |
| 78 | + try: |
| 79 | + ws = websocket.create_connection( |
| 80 | + url, |
| 81 | + header=[ |
| 82 | + f"{k}: {v}" |
| 83 | + for k, v in headers.items() |
| 84 | + if k.lower() not in SKIPPED_HEADERS |
| 85 | + ], |
| 86 | + ) |
| 87 | + except Exception as e: # noqa: BLE001 |
| 88 | + error(f"Websocket Connection Failed: {e}") |
| 89 | + |
| 90 | + try: |
| 91 | + ws.send(data) |
| 92 | + rich.print(f"[{timestamp()}] Proxied: {data}") |
| 93 | + data = ws.recv() |
| 94 | + return data.encode("utf-8") if data else b"" |
| 95 | + except Exception as err: # noqa: BLE001 |
| 96 | + rich.print(f"[yellow]Request Failed: {err!s}[/yellow]") |
| 97 | + finally: |
| 98 | + ws.close() |
| 99 | + |
| 100 | + |
| 101 | +def run_server(port: int, url: str, data: str) -> None: |
| 102 | + """Run the Proxy Server.""" |
| 103 | + try: |
| 104 | + handler = partial(HTTPHandler, url, data) |
| 105 | + with TCPServer(("", port), handler) as httpd: |
| 106 | + status("Server Started (Ctrl+c to stop)\n") |
| 107 | + httpd.serve_forever() |
| 108 | + except KeyboardInterrupt: |
| 109 | + status("Quitting...") |
| 110 | + sys.exit(0) |
| 111 | + except Exception as err: # noqa: BLE001 |
| 112 | + error(f"Exception: {err}") |
0 commit comments