Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mitmproxy, push own WebSocket message

I inspect a HTTPS WebSocket traffic with Mitmproxy. Currently I can read/edit WS messages with:

class Intercept:
    def websocket_message(self, flow):
        print(flow.messages[-1])

def start():
    return Intercept()

.. as attached script to Mitmproxy.

How do I push/inject my own message to the client? Not edit existing one, but add a new message.

like image 334
Alexey Avatar asked Mar 19 '26 08:03

Alexey


1 Answers

You can do this with inject.websocket:

from mitmproxy import ctx

class Intercept:
    def websocket_message(self, flow):
        print(flow.messages[-1])
        to_client = True
        ctx.master.commands.call("inject.websocket", flow, to_client, b"Hello World!", True)

def start():
    return Intercept()

There are more examples in the documentation.

like image 144
HellaMad Avatar answered Mar 20 '26 21:03

HellaMad